vp9_ratectrl.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457
  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 target for 1080P and below encodes under normal circumstances
  30. // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
  31. #define MAX_MB_RATE 250
  32. #define MAXRATE_1080P 2025000
  33. #define DEFAULT_KF_BOOST 2000
  34. #define DEFAULT_GF_BOOST 2000
  35. #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
  36. #define MIN_BPB_FACTOR 0.005
  37. #define MAX_BPB_FACTOR 50
  38. #define FRAME_OVERHEAD_BITS 200
  39. #if CONFIG_VP9_HIGHBITDEPTH
  40. #define ASSIGN_MINQ_TABLE(bit_depth, name) \
  41. do { \
  42. switch (bit_depth) { \
  43. case VPX_BITS_8: name = name##_8; break; \
  44. case VPX_BITS_10: name = name##_10; break; \
  45. case VPX_BITS_12: name = name##_12; break; \
  46. default: \
  47. assert(0 && \
  48. "bit_depth should be VPX_BITS_8, VPX_BITS_10" \
  49. " or VPX_BITS_12"); \
  50. name = NULL; \
  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 = 5000;
  90. static int kf_low = 400;
  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.55, 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. case VPX_BITS_12: return vp9_ac_quant(qindex, 0, bit_depth) / 64.0;
  150. default:
  151. assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
  152. return -1.0;
  153. }
  154. #else
  155. return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
  156. #endif
  157. }
  158. int vp9_convert_q_to_qindex(double q_val, vpx_bit_depth_t bit_depth) {
  159. int i;
  160. for (i = 0; i < QINDEX_RANGE; ++i)
  161. if (vp9_convert_qindex_to_q(i, bit_depth) >= q_val) break;
  162. if (i == QINDEX_RANGE) i--;
  163. return i;
  164. }
  165. int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
  166. double correction_factor, vpx_bit_depth_t bit_depth) {
  167. const double q = vp9_convert_qindex_to_q(qindex, bit_depth);
  168. int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000;
  169. assert(correction_factor <= MAX_BPB_FACTOR &&
  170. correction_factor >= MIN_BPB_FACTOR);
  171. // q based adjustment to baseline enumerator
  172. enumerator += (int)(enumerator * q) >> 12;
  173. return (int)(enumerator * correction_factor / q);
  174. }
  175. int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
  176. double correction_factor,
  177. vpx_bit_depth_t bit_depth) {
  178. const int bpm =
  179. (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth));
  180. return VPXMAX(FRAME_OVERHEAD_BITS,
  181. (int)(((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS));
  182. }
  183. int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
  184. const RATE_CONTROL *rc = &cpi->rc;
  185. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  186. const int min_frame_target =
  187. VPXMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
  188. if (target < min_frame_target) target = min_frame_target;
  189. if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
  190. // If there is an active ARF at this location use the minimum
  191. // bits on this frame even if it is a constructed arf.
  192. // The active maximum quantizer insures that an appropriate
  193. // number of bits will be spent if needed for constructed ARFs.
  194. target = min_frame_target;
  195. }
  196. // Clip the frame target to the maximum allowed value.
  197. if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
  198. if (oxcf->rc_max_inter_bitrate_pct) {
  199. const int max_rate =
  200. rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
  201. target = VPXMIN(target, max_rate);
  202. }
  203. return target;
  204. }
  205. int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
  206. const RATE_CONTROL *rc = &cpi->rc;
  207. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  208. if (oxcf->rc_max_intra_bitrate_pct) {
  209. const int max_rate =
  210. rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100;
  211. target = VPXMIN(target, max_rate);
  212. }
  213. if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth;
  214. return target;
  215. }
  216. // Update the buffer level for higher temporal layers, given the encoded current
  217. // temporal layer.
  218. static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) {
  219. int i = 0;
  220. int current_temporal_layer = svc->temporal_layer_id;
  221. for (i = current_temporal_layer + 1; i < svc->number_temporal_layers; ++i) {
  222. const int layer =
  223. LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers);
  224. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  225. RATE_CONTROL *lrc = &lc->rc;
  226. int bits_off_for_this_layer =
  227. (int)(lc->target_bandwidth / lc->framerate - encoded_frame_size);
  228. lrc->bits_off_target += bits_off_for_this_layer;
  229. // Clip buffer level to maximum buffer size for the layer.
  230. lrc->bits_off_target =
  231. VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
  232. lrc->buffer_level = lrc->bits_off_target;
  233. }
  234. }
  235. // Update the buffer level: leaky bucket model.
  236. static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
  237. const VP9_COMMON *const cm = &cpi->common;
  238. RATE_CONTROL *const rc = &cpi->rc;
  239. // Non-viewable frames are a special case and are treated as pure overhead.
  240. if (!cm->show_frame) {
  241. rc->bits_off_target -= encoded_frame_size;
  242. } else {
  243. rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
  244. }
  245. // Clip the buffer level to the maximum specified buffer size.
  246. rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
  247. // For screen-content mode, and if frame-dropper is off, don't let buffer
  248. // level go below threshold, given here as -rc->maximum_ buffer_size.
  249. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
  250. cpi->oxcf.drop_frames_water_mark == 0)
  251. rc->bits_off_target = VPXMAX(rc->bits_off_target, -rc->maximum_buffer_size);
  252. rc->buffer_level = rc->bits_off_target;
  253. if (is_one_pass_cbr_svc(cpi)) {
  254. update_layer_buffer_level(&cpi->svc, encoded_frame_size);
  255. }
  256. }
  257. int vp9_rc_get_default_min_gf_interval(int width, int height,
  258. double framerate) {
  259. // Assume we do not need any constraint lower than 4K 20 fps
  260. static const double factor_safe = 3840 * 2160 * 20.0;
  261. const double factor = width * height * framerate;
  262. const int default_interval =
  263. clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL);
  264. if (factor <= factor_safe)
  265. return default_interval;
  266. else
  267. return VPXMAX(default_interval,
  268. (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5));
  269. // Note this logic makes:
  270. // 4K24: 5
  271. // 4K30: 6
  272. // 4K60: 12
  273. }
  274. int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) {
  275. int interval = VPXMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75));
  276. interval += (interval & 0x01); // Round to even value
  277. return VPXMAX(interval, min_gf_interval);
  278. }
  279. void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
  280. int i;
  281. if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
  282. rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
  283. rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
  284. } else {
  285. rc->avg_frame_qindex[KEY_FRAME] =
  286. (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
  287. rc->avg_frame_qindex[INTER_FRAME] =
  288. (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
  289. }
  290. rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
  291. rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
  292. rc->buffer_level = rc->starting_buffer_level;
  293. rc->bits_off_target = rc->starting_buffer_level;
  294. rc->rolling_target_bits = rc->avg_frame_bandwidth;
  295. rc->rolling_actual_bits = rc->avg_frame_bandwidth;
  296. rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
  297. rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
  298. rc->total_actual_bits = 0;
  299. rc->total_target_bits = 0;
  300. rc->total_target_vs_actual = 0;
  301. rc->avg_frame_low_motion = 0;
  302. rc->count_last_scene_change = 0;
  303. rc->af_ratio_onepass_vbr = 10;
  304. rc->prev_avg_source_sad_lag = 0;
  305. rc->high_source_sad = 0;
  306. rc->reset_high_source_sad = 0;
  307. rc->high_source_sad_lagindex = -1;
  308. rc->alt_ref_gf_group = 0;
  309. rc->fac_active_worst_inter = 150;
  310. rc->fac_active_worst_gf = 100;
  311. rc->force_qpmin = 0;
  312. for (i = 0; i < MAX_LAG_BUFFERS; ++i) rc->avg_source_sad[i] = 0;
  313. rc->frames_since_key = 8; // Sensible default for first frame.
  314. rc->this_key_frame_forced = 0;
  315. rc->next_key_frame_forced = 0;
  316. rc->source_alt_ref_pending = 0;
  317. rc->source_alt_ref_active = 0;
  318. rc->frames_till_gf_update_due = 0;
  319. rc->ni_av_qi = oxcf->worst_allowed_q;
  320. rc->ni_tot_qi = 0;
  321. rc->ni_frames = 0;
  322. rc->tot_q = 0.0;
  323. rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
  324. for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
  325. rc->rate_correction_factors[i] = 1.0;
  326. }
  327. rc->min_gf_interval = oxcf->min_gf_interval;
  328. rc->max_gf_interval = oxcf->max_gf_interval;
  329. if (rc->min_gf_interval == 0)
  330. rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
  331. oxcf->width, oxcf->height, oxcf->init_framerate);
  332. if (rc->max_gf_interval == 0)
  333. rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
  334. oxcf->init_framerate, rc->min_gf_interval);
  335. rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
  336. }
  337. int vp9_rc_drop_frame(VP9_COMP *cpi) {
  338. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  339. RATE_CONTROL *const rc = &cpi->rc;
  340. if (!oxcf->drop_frames_water_mark ||
  341. (is_one_pass_cbr_svc(cpi) &&
  342. cpi->svc.spatial_layer_id > cpi->svc.first_spatial_layer_to_encode)) {
  343. return 0;
  344. } else {
  345. if (rc->buffer_level < 0) {
  346. // Always drop if buffer is below 0.
  347. return 1;
  348. } else {
  349. // If buffer is below drop_mark, for now just drop every other frame
  350. // (starting with the next frame) until it increases back over drop_mark.
  351. int drop_mark =
  352. (int)(oxcf->drop_frames_water_mark * rc->optimal_buffer_level / 100);
  353. if ((rc->buffer_level > drop_mark) && (rc->decimation_factor > 0)) {
  354. --rc->decimation_factor;
  355. } else if (rc->buffer_level <= drop_mark && rc->decimation_factor == 0) {
  356. rc->decimation_factor = 1;
  357. }
  358. if (rc->decimation_factor > 0) {
  359. if (rc->decimation_count > 0) {
  360. --rc->decimation_count;
  361. return 1;
  362. } else {
  363. rc->decimation_count = rc->decimation_factor;
  364. return 0;
  365. }
  366. } else {
  367. rc->decimation_count = 0;
  368. return 0;
  369. }
  370. }
  371. }
  372. }
  373. static double get_rate_correction_factor(const VP9_COMP *cpi) {
  374. const RATE_CONTROL *const rc = &cpi->rc;
  375. double rcf;
  376. if (cpi->common.frame_type == KEY_FRAME) {
  377. rcf = rc->rate_correction_factors[KF_STD];
  378. } else if (cpi->oxcf.pass == 2) {
  379. RATE_FACTOR_LEVEL rf_lvl =
  380. cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
  381. rcf = rc->rate_correction_factors[rf_lvl];
  382. } else {
  383. if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
  384. !rc->is_src_frame_alt_ref && !cpi->use_svc &&
  385. (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100))
  386. rcf = rc->rate_correction_factors[GF_ARF_STD];
  387. else
  388. rcf = rc->rate_correction_factors[INTER_NORMAL];
  389. }
  390. rcf *= rcf_mult[rc->frame_size_selector];
  391. return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
  392. }
  393. static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
  394. RATE_CONTROL *const rc = &cpi->rc;
  395. // Normalize RCF to account for the size-dependent scaling factor.
  396. factor /= rcf_mult[cpi->rc.frame_size_selector];
  397. factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
  398. if (cpi->common.frame_type == KEY_FRAME) {
  399. rc->rate_correction_factors[KF_STD] = factor;
  400. } else if (cpi->oxcf.pass == 2) {
  401. RATE_FACTOR_LEVEL rf_lvl =
  402. cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
  403. rc->rate_correction_factors[rf_lvl] = factor;
  404. } else {
  405. if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
  406. !rc->is_src_frame_alt_ref && !cpi->use_svc &&
  407. (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100))
  408. rc->rate_correction_factors[GF_ARF_STD] = factor;
  409. else
  410. rc->rate_correction_factors[INTER_NORMAL] = factor;
  411. }
  412. }
  413. void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi) {
  414. const VP9_COMMON *const cm = &cpi->common;
  415. int correction_factor = 100;
  416. double rate_correction_factor = get_rate_correction_factor(cpi);
  417. double adjustment_limit;
  418. int projected_size_based_on_q = 0;
  419. // Do not update the rate factors for arf overlay frames.
  420. if (cpi->rc.is_src_frame_alt_ref) return;
  421. // Clear down mmx registers to allow floating point in what follows
  422. vpx_clear_system_state();
  423. // Work out how big we would have expected the frame to be at this Q given
  424. // the current correction factor.
  425. // Stay in double to avoid int overflow when values are large
  426. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
  427. projected_size_based_on_q =
  428. vp9_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
  429. } else {
  430. projected_size_based_on_q =
  431. vp9_estimate_bits_at_q(cpi->common.frame_type, cm->base_qindex, cm->MBs,
  432. rate_correction_factor, cm->bit_depth);
  433. }
  434. // Work out a size correction factor.
  435. if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
  436. correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
  437. projected_size_based_on_q);
  438. // More heavily damped adjustment used if we have been oscillating either side
  439. // of target.
  440. adjustment_limit =
  441. 0.25 + 0.5 * VPXMIN(1, fabs(log10(0.01 * correction_factor)));
  442. cpi->rc.q_2_frame = cpi->rc.q_1_frame;
  443. cpi->rc.q_1_frame = cm->base_qindex;
  444. cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
  445. if (correction_factor > 110)
  446. cpi->rc.rc_1_frame = -1;
  447. else if (correction_factor < 90)
  448. cpi->rc.rc_1_frame = 1;
  449. else
  450. cpi->rc.rc_1_frame = 0;
  451. // Turn off oscilation detection in the case of massive overshoot.
  452. if (cpi->rc.rc_1_frame == -1 && cpi->rc.rc_2_frame == 1 &&
  453. correction_factor > 1000) {
  454. cpi->rc.rc_2_frame = 0;
  455. }
  456. if (correction_factor > 102) {
  457. // We are not already at the worst allowable quality
  458. correction_factor =
  459. (int)(100 + ((correction_factor - 100) * adjustment_limit));
  460. rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
  461. // Keep rate_correction_factor within limits
  462. if (rate_correction_factor > MAX_BPB_FACTOR)
  463. rate_correction_factor = MAX_BPB_FACTOR;
  464. } else if (correction_factor < 99) {
  465. // We are not already at the best allowable quality
  466. correction_factor =
  467. (int)(100 - ((100 - correction_factor) * adjustment_limit));
  468. rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
  469. // Keep rate_correction_factor within limits
  470. if (rate_correction_factor < MIN_BPB_FACTOR)
  471. rate_correction_factor = MIN_BPB_FACTOR;
  472. }
  473. set_rate_correction_factor(cpi, rate_correction_factor);
  474. }
  475. int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
  476. int active_best_quality, int active_worst_quality) {
  477. const VP9_COMMON *const cm = &cpi->common;
  478. CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  479. int q = active_worst_quality;
  480. int last_error = INT_MAX;
  481. int i, target_bits_per_mb, bits_per_mb_at_this_q;
  482. const double correction_factor = get_rate_correction_factor(cpi);
  483. // Calculate required scaling factor based on target frame size and size of
  484. // frame produced using previous Q.
  485. target_bits_per_mb =
  486. (int)(((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs);
  487. i = active_best_quality;
  488. do {
  489. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
  490. cr->apply_cyclic_refresh &&
  491. (!cpi->oxcf.gf_cbr_boost_pct || !cpi->refresh_golden_frame)) {
  492. bits_per_mb_at_this_q =
  493. (int)vp9_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor);
  494. } else {
  495. bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(
  496. cm->frame_type, i, correction_factor, cm->bit_depth);
  497. }
  498. if (bits_per_mb_at_this_q <= target_bits_per_mb) {
  499. if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
  500. q = i;
  501. else
  502. q = i - 1;
  503. break;
  504. } else {
  505. last_error = bits_per_mb_at_this_q - target_bits_per_mb;
  506. }
  507. } while (++i <= active_worst_quality);
  508. // In CBR mode, this makes sure q is between oscillating Qs to prevent
  509. // resonance.
  510. if (cpi->oxcf.rc_mode == VPX_CBR && !cpi->rc.reset_high_source_sad &&
  511. (!cpi->oxcf.gf_cbr_boost_pct ||
  512. !(cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame)) &&
  513. (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
  514. cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
  515. q = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
  516. VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
  517. }
  518. if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf &&
  519. cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_VBR &&
  520. cpi->oxcf.lag_in_frames > 0 && cpi->rc.is_src_frame_alt_ref &&
  521. !cpi->rc.alt_ref_gf_group) {
  522. q = VPXMIN(q, (q + cpi->rc.last_boosted_qindex) >> 1);
  523. }
  524. return q;
  525. }
  526. static int get_active_quality(int q, int gfu_boost, int low, int high,
  527. int *low_motion_minq, int *high_motion_minq) {
  528. if (gfu_boost > high) {
  529. return low_motion_minq[q];
  530. } else if (gfu_boost < low) {
  531. return high_motion_minq[q];
  532. } else {
  533. const int gap = high - low;
  534. const int offset = high - gfu_boost;
  535. const int qdiff = high_motion_minq[q] - low_motion_minq[q];
  536. const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
  537. return low_motion_minq[q] + adjustment;
  538. }
  539. }
  540. static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
  541. vpx_bit_depth_t bit_depth) {
  542. int *kf_low_motion_minq;
  543. int *kf_high_motion_minq;
  544. ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
  545. ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
  546. return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
  547. kf_low_motion_minq, kf_high_motion_minq);
  548. }
  549. static int get_gf_active_quality(const RATE_CONTROL *const rc, int q,
  550. vpx_bit_depth_t bit_depth) {
  551. int *arfgf_low_motion_minq;
  552. int *arfgf_high_motion_minq;
  553. ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
  554. ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
  555. return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
  556. arfgf_low_motion_minq, arfgf_high_motion_minq);
  557. }
  558. static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
  559. const RATE_CONTROL *const rc = &cpi->rc;
  560. const unsigned int curr_frame = cpi->common.current_video_frame;
  561. int active_worst_quality;
  562. if (cpi->common.frame_type == KEY_FRAME) {
  563. active_worst_quality =
  564. curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] << 1;
  565. } else {
  566. if (!rc->is_src_frame_alt_ref &&
  567. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  568. active_worst_quality =
  569. curr_frame == 1
  570. ? rc->last_q[KEY_FRAME] * 5 >> 2
  571. : rc->last_q[INTER_FRAME] * rc->fac_active_worst_gf / 100;
  572. } else {
  573. active_worst_quality = curr_frame == 1
  574. ? rc->last_q[KEY_FRAME] << 1
  575. : rc->avg_frame_qindex[INTER_FRAME] *
  576. rc->fac_active_worst_inter / 100;
  577. }
  578. }
  579. return VPXMIN(active_worst_quality, rc->worst_quality);
  580. }
  581. // Adjust active_worst_quality level based on buffer level.
  582. static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
  583. // Adjust active_worst_quality: If buffer is above the optimal/target level,
  584. // bring active_worst_quality down depending on fullness of buffer.
  585. // If buffer is below the optimal level, let the active_worst_quality go from
  586. // ambient Q (at buffer = optimal level) to worst_quality level
  587. // (at buffer = critical level).
  588. const VP9_COMMON *const cm = &cpi->common;
  589. const RATE_CONTROL *rc = &cpi->rc;
  590. // Buffer level below which we push active_worst to worst_quality.
  591. int64_t critical_level = rc->optimal_buffer_level >> 3;
  592. int64_t buff_lvl_step = 0;
  593. int adjustment = 0;
  594. int active_worst_quality;
  595. int ambient_qp;
  596. unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers;
  597. if (cm->frame_type == KEY_FRAME || rc->reset_high_source_sad)
  598. return rc->worst_quality;
  599. // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
  600. // for the first few frames following key frame. These are both initialized
  601. // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
  602. // So for first few frames following key, the qp of that key frame is weighted
  603. // into the active_worst_quality setting.
  604. ambient_qp = (cm->current_video_frame < num_frames_weight_key)
  605. ? VPXMIN(rc->avg_frame_qindex[INTER_FRAME],
  606. rc->avg_frame_qindex[KEY_FRAME])
  607. : rc->avg_frame_qindex[INTER_FRAME];
  608. // For SVC if the current base spatial layer was key frame, use the QP from
  609. // that base layer for ambient_qp.
  610. if (cpi->use_svc && cpi->svc.spatial_layer_id > 0) {
  611. int layer = LAYER_IDS_TO_IDX(0, cpi->svc.temporal_layer_id,
  612. cpi->svc.number_temporal_layers);
  613. const LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
  614. if (lc->is_key_frame) {
  615. const RATE_CONTROL *lrc = &lc->rc;
  616. ambient_qp = VPXMIN(ambient_qp, lrc->last_q[KEY_FRAME]);
  617. }
  618. }
  619. active_worst_quality = VPXMIN(rc->worst_quality, ambient_qp * 5 >> 2);
  620. if (rc->buffer_level > rc->optimal_buffer_level) {
  621. // Adjust down.
  622. // Maximum limit for down adjustment, ~30%.
  623. int max_adjustment_down = active_worst_quality / 3;
  624. if (max_adjustment_down) {
  625. buff_lvl_step = ((rc->maximum_buffer_size - rc->optimal_buffer_level) /
  626. max_adjustment_down);
  627. if (buff_lvl_step)
  628. adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
  629. buff_lvl_step);
  630. active_worst_quality -= adjustment;
  631. }
  632. } else if (rc->buffer_level > critical_level) {
  633. // Adjust up from ambient Q.
  634. if (critical_level) {
  635. buff_lvl_step = (rc->optimal_buffer_level - critical_level);
  636. if (buff_lvl_step) {
  637. adjustment = (int)((rc->worst_quality - ambient_qp) *
  638. (rc->optimal_buffer_level - rc->buffer_level) /
  639. buff_lvl_step);
  640. }
  641. active_worst_quality = ambient_qp + adjustment;
  642. }
  643. } else {
  644. // Set to worst_quality if buffer is below critical level.
  645. active_worst_quality = rc->worst_quality;
  646. }
  647. return active_worst_quality;
  648. }
  649. static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
  650. int *bottom_index,
  651. int *top_index) {
  652. const VP9_COMMON *const cm = &cpi->common;
  653. const RATE_CONTROL *const rc = &cpi->rc;
  654. int active_best_quality;
  655. int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
  656. int q;
  657. int *rtc_minq;
  658. ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
  659. if (frame_is_intra_only(cm)) {
  660. active_best_quality = rc->best_quality;
  661. // Handle the special case for key frames forced when we have reached
  662. // the maximum key frame interval. Here force the Q to a range
  663. // based on the ambient Q to reduce the risk of popping.
  664. if (rc->this_key_frame_forced) {
  665. int qindex = rc->last_boosted_qindex;
  666. double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  667. int delta_qindex = vp9_compute_qdelta(
  668. rc, last_boosted_q, (last_boosted_q * 0.75), cm->bit_depth);
  669. active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
  670. } else if (cm->current_video_frame > 0) {
  671. // not first frame of one pass and kf_boost is set
  672. double q_adj_factor = 1.0;
  673. double q_val;
  674. active_best_quality = get_kf_active_quality(
  675. rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
  676. // Allow somewhat lower kf minq with small image formats.
  677. if ((cm->width * cm->height) <= (352 * 288)) {
  678. q_adj_factor -= 0.25;
  679. }
  680. // Convert the adjustment factor to a qindex delta
  681. // on active_best_quality.
  682. q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
  683. active_best_quality +=
  684. vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
  685. }
  686. } else if (!rc->is_src_frame_alt_ref && !cpi->use_svc &&
  687. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  688. // Use the lower of active_worst_quality and recent
  689. // average Q as basis for GF/ARF best Q limit unless last frame was
  690. // a key frame.
  691. if (rc->frames_since_key > 1 &&
  692. rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
  693. q = rc->avg_frame_qindex[INTER_FRAME];
  694. } else {
  695. q = active_worst_quality;
  696. }
  697. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  698. } else {
  699. // Use the lower of active_worst_quality and recent/average Q.
  700. if (cm->current_video_frame > 1) {
  701. if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
  702. active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
  703. else
  704. active_best_quality = rtc_minq[active_worst_quality];
  705. } else {
  706. if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
  707. active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
  708. else
  709. active_best_quality = rtc_minq[active_worst_quality];
  710. }
  711. }
  712. // Clip the active best and worst quality values to limits
  713. active_best_quality =
  714. clamp(active_best_quality, rc->best_quality, rc->worst_quality);
  715. active_worst_quality =
  716. clamp(active_worst_quality, active_best_quality, rc->worst_quality);
  717. *top_index = active_worst_quality;
  718. *bottom_index = active_best_quality;
  719. #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
  720. // Limit Q range for the adaptive loop.
  721. if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
  722. !(cm->current_video_frame == 0)) {
  723. int qdelta = 0;
  724. vpx_clear_system_state();
  725. qdelta = vp9_compute_qdelta_by_rate(
  726. &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
  727. *top_index = active_worst_quality + qdelta;
  728. *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
  729. }
  730. #endif
  731. // Special case code to try and match quality with forced key frames
  732. if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
  733. q = rc->last_boosted_qindex;
  734. } else {
  735. q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
  736. active_worst_quality);
  737. if (q > *top_index) {
  738. // Special case when we are targeting the max allowed rate
  739. if (rc->this_frame_target >= rc->max_frame_bandwidth)
  740. *top_index = q;
  741. else
  742. q = *top_index;
  743. }
  744. }
  745. assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
  746. assert(*bottom_index <= rc->worst_quality &&
  747. *bottom_index >= rc->best_quality);
  748. assert(q <= rc->worst_quality && q >= rc->best_quality);
  749. return q;
  750. }
  751. static int get_active_cq_level_one_pass(const RATE_CONTROL *rc,
  752. const VP9EncoderConfig *const oxcf) {
  753. static const double cq_adjust_threshold = 0.1;
  754. int active_cq_level = oxcf->cq_level;
  755. if (oxcf->rc_mode == VPX_CQ && rc->total_target_bits > 0) {
  756. const double x = (double)rc->total_actual_bits / rc->total_target_bits;
  757. if (x < cq_adjust_threshold) {
  758. active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
  759. }
  760. }
  761. return active_cq_level;
  762. }
  763. #define SMOOTH_PCT_MIN 0.1
  764. #define SMOOTH_PCT_DIV 0.05
  765. static int get_active_cq_level_two_pass(const TWO_PASS *twopass,
  766. const RATE_CONTROL *rc,
  767. const VP9EncoderConfig *const oxcf) {
  768. static const double cq_adjust_threshold = 0.1;
  769. int active_cq_level = oxcf->cq_level;
  770. if (oxcf->rc_mode == VPX_CQ) {
  771. if (twopass->mb_smooth_pct > SMOOTH_PCT_MIN) {
  772. active_cq_level -=
  773. (int)((twopass->mb_smooth_pct - SMOOTH_PCT_MIN) / SMOOTH_PCT_DIV);
  774. active_cq_level = VPXMAX(active_cq_level, 0);
  775. }
  776. if (rc->total_target_bits > 0) {
  777. const double x = (double)rc->total_actual_bits / rc->total_target_bits;
  778. if (x < cq_adjust_threshold) {
  779. active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
  780. }
  781. }
  782. }
  783. return active_cq_level;
  784. }
  785. static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
  786. int *bottom_index,
  787. int *top_index) {
  788. const VP9_COMMON *const cm = &cpi->common;
  789. const RATE_CONTROL *const rc = &cpi->rc;
  790. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  791. const int cq_level = get_active_cq_level_one_pass(rc, oxcf);
  792. int active_best_quality;
  793. int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
  794. int q;
  795. int *inter_minq;
  796. ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
  797. if (frame_is_intra_only(cm)) {
  798. if (oxcf->rc_mode == VPX_Q) {
  799. int qindex = cq_level;
  800. double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  801. int delta_qindex = vp9_compute_qdelta(rc, q, q * 0.25, cm->bit_depth);
  802. active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
  803. } else if (rc->this_key_frame_forced) {
  804. // Handle the special case for key frames forced when we have reached
  805. // the maximum key frame interval. Here force the Q to a range
  806. // based on the ambient Q to reduce the risk of popping.
  807. int qindex = rc->last_boosted_qindex;
  808. double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  809. int delta_qindex = vp9_compute_qdelta(
  810. rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth);
  811. active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
  812. } else {
  813. // not first frame of one pass and kf_boost is set
  814. double q_adj_factor = 1.0;
  815. double q_val;
  816. active_best_quality = get_kf_active_quality(
  817. rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth);
  818. // Allow somewhat lower kf minq with small image formats.
  819. if ((cm->width * cm->height) <= (352 * 288)) {
  820. q_adj_factor -= 0.25;
  821. }
  822. // Convert the adjustment factor to a qindex delta
  823. // on active_best_quality.
  824. q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
  825. active_best_quality +=
  826. vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
  827. }
  828. } else if (!rc->is_src_frame_alt_ref &&
  829. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  830. // Use the lower of active_worst_quality and recent
  831. // average Q as basis for GF/ARF best Q limit unless last frame was
  832. // a key frame.
  833. if (rc->frames_since_key > 1) {
  834. if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
  835. q = rc->avg_frame_qindex[INTER_FRAME];
  836. } else {
  837. q = active_worst_quality;
  838. }
  839. } else {
  840. q = rc->avg_frame_qindex[KEY_FRAME];
  841. }
  842. // For constrained quality dont allow Q less than the cq level
  843. if (oxcf->rc_mode == VPX_CQ) {
  844. if (q < cq_level) q = cq_level;
  845. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  846. // Constrained quality use slightly lower active best.
  847. active_best_quality = active_best_quality * 15 / 16;
  848. } else if (oxcf->rc_mode == VPX_Q) {
  849. int qindex = cq_level;
  850. double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  851. int delta_qindex;
  852. if (cpi->refresh_alt_ref_frame)
  853. delta_qindex = vp9_compute_qdelta(rc, q, q * 0.40, cm->bit_depth);
  854. else
  855. delta_qindex = vp9_compute_qdelta(rc, q, q * 0.50, cm->bit_depth);
  856. active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
  857. } else {
  858. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  859. }
  860. } else {
  861. if (oxcf->rc_mode == VPX_Q) {
  862. int qindex = cq_level;
  863. double q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  864. double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0,
  865. 0.70, 1.0, 0.85, 1.0 };
  866. int delta_qindex = vp9_compute_qdelta(
  867. rc, q, q * delta_rate[cm->current_video_frame % FIXED_GF_INTERVAL],
  868. cm->bit_depth);
  869. active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
  870. } else {
  871. // Use the min of the average Q and active_worst_quality as basis for
  872. // active_best.
  873. if (cm->current_video_frame > 1) {
  874. q = VPXMIN(rc->avg_frame_qindex[INTER_FRAME], active_worst_quality);
  875. active_best_quality = inter_minq[q];
  876. } else {
  877. active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
  878. }
  879. // For the constrained quality mode we don't want
  880. // q to fall below the cq level.
  881. if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) {
  882. active_best_quality = cq_level;
  883. }
  884. }
  885. }
  886. // Clip the active best and worst quality values to limits
  887. active_best_quality =
  888. clamp(active_best_quality, rc->best_quality, rc->worst_quality);
  889. active_worst_quality =
  890. clamp(active_worst_quality, active_best_quality, rc->worst_quality);
  891. *top_index = active_worst_quality;
  892. *bottom_index = active_best_quality;
  893. #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
  894. {
  895. int qdelta = 0;
  896. vpx_clear_system_state();
  897. // Limit Q range for the adaptive loop.
  898. if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced &&
  899. !(cm->current_video_frame == 0)) {
  900. qdelta = vp9_compute_qdelta_by_rate(
  901. &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth);
  902. } else if (!rc->is_src_frame_alt_ref &&
  903. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  904. qdelta = vp9_compute_qdelta_by_rate(
  905. &cpi->rc, cm->frame_type, active_worst_quality, 1.75, cm->bit_depth);
  906. }
  907. *top_index = active_worst_quality + qdelta;
  908. *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
  909. }
  910. #endif
  911. if (oxcf->rc_mode == VPX_Q) {
  912. q = active_best_quality;
  913. // Special case code to try and match quality with forced key frames
  914. } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
  915. q = rc->last_boosted_qindex;
  916. } else {
  917. q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
  918. active_worst_quality);
  919. if (q > *top_index) {
  920. // Special case when we are targeting the max allowed rate
  921. if (rc->this_frame_target >= rc->max_frame_bandwidth)
  922. *top_index = q;
  923. else
  924. q = *top_index;
  925. }
  926. }
  927. assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
  928. assert(*bottom_index <= rc->worst_quality &&
  929. *bottom_index >= rc->best_quality);
  930. assert(q <= rc->worst_quality && q >= rc->best_quality);
  931. return q;
  932. }
  933. int vp9_frame_type_qdelta(const VP9_COMP *cpi, int rf_level, int q) {
  934. static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
  935. 1.00, // INTER_NORMAL
  936. 1.00, // INTER_HIGH
  937. 1.50, // GF_ARF_LOW
  938. 1.75, // GF_ARF_STD
  939. 2.00, // KF_STD
  940. };
  941. static const FRAME_TYPE frame_type[RATE_FACTOR_LEVELS] = {
  942. INTER_FRAME, INTER_FRAME, INTER_FRAME, INTER_FRAME, KEY_FRAME
  943. };
  944. const VP9_COMMON *const cm = &cpi->common;
  945. int qdelta =
  946. vp9_compute_qdelta_by_rate(&cpi->rc, frame_type[rf_level], q,
  947. rate_factor_deltas[rf_level], cm->bit_depth);
  948. return qdelta;
  949. }
  950. #define STATIC_MOTION_THRESH 95
  951. static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, int *bottom_index,
  952. int *top_index) {
  953. const VP9_COMMON *const cm = &cpi->common;
  954. const RATE_CONTROL *const rc = &cpi->rc;
  955. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  956. const GF_GROUP *gf_group = &cpi->twopass.gf_group;
  957. const int cq_level = get_active_cq_level_two_pass(&cpi->twopass, rc, oxcf);
  958. int active_best_quality;
  959. int active_worst_quality = cpi->twopass.active_worst_quality;
  960. int q;
  961. int *inter_minq;
  962. ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
  963. if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) {
  964. // Handle the special case for key frames forced when we have reached
  965. // the maximum key frame interval. Here force the Q to a range
  966. // based on the ambient Q to reduce the risk of popping.
  967. if (rc->this_key_frame_forced) {
  968. double last_boosted_q;
  969. int delta_qindex;
  970. int qindex;
  971. if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
  972. qindex = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
  973. active_best_quality = qindex;
  974. last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  975. delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
  976. last_boosted_q * 1.25, cm->bit_depth);
  977. active_worst_quality =
  978. VPXMIN(qindex + delta_qindex, active_worst_quality);
  979. } else {
  980. qindex = rc->last_boosted_qindex;
  981. last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  982. delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
  983. last_boosted_q * 0.75, cm->bit_depth);
  984. active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality);
  985. }
  986. } else {
  987. // Not forced keyframe.
  988. double q_adj_factor = 1.0;
  989. double q_val;
  990. // Baseline value derived from cpi->active_worst_quality and kf boost.
  991. active_best_quality =
  992. get_kf_active_quality(rc, active_worst_quality, cm->bit_depth);
  993. // Allow somewhat lower kf minq with small image formats.
  994. if ((cm->width * cm->height) <= (352 * 288)) {
  995. q_adj_factor -= 0.25;
  996. }
  997. // Make a further adjustment based on the kf zero motion measure.
  998. q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
  999. // Convert the adjustment factor to a qindex delta
  1000. // on active_best_quality.
  1001. q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
  1002. active_best_quality +=
  1003. vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth);
  1004. }
  1005. } else if (!rc->is_src_frame_alt_ref &&
  1006. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  1007. // Use the lower of active_worst_quality and recent
  1008. // average Q as basis for GF/ARF best Q limit unless last frame was
  1009. // a key frame.
  1010. if (rc->frames_since_key > 1 &&
  1011. rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
  1012. q = rc->avg_frame_qindex[INTER_FRAME];
  1013. } else {
  1014. q = active_worst_quality;
  1015. }
  1016. // For constrained quality dont allow Q less than the cq level
  1017. if (oxcf->rc_mode == VPX_CQ) {
  1018. if (q < cq_level) q = cq_level;
  1019. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  1020. // Constrained quality use slightly lower active best.
  1021. active_best_quality = active_best_quality * 15 / 16;
  1022. } else if (oxcf->rc_mode == VPX_Q) {
  1023. if (!cpi->refresh_alt_ref_frame) {
  1024. active_best_quality = cq_level;
  1025. } else {
  1026. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  1027. // Modify best quality for second level arfs. For mode VPX_Q this
  1028. // becomes the baseline frame q.
  1029. if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
  1030. active_best_quality = (active_best_quality + cq_level + 1) / 2;
  1031. }
  1032. } else {
  1033. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  1034. }
  1035. } else {
  1036. if (oxcf->rc_mode == VPX_Q) {
  1037. active_best_quality = cq_level;
  1038. } else {
  1039. active_best_quality = inter_minq[active_worst_quality];
  1040. // For the constrained quality mode we don't want
  1041. // q to fall below the cq level.
  1042. if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) {
  1043. active_best_quality = cq_level;
  1044. }
  1045. }
  1046. }
  1047. // Extension to max or min Q if undershoot or overshoot is outside
  1048. // the permitted range.
  1049. if (cpi->oxcf.rc_mode != VPX_Q) {
  1050. if (frame_is_intra_only(cm) ||
  1051. (!rc->is_src_frame_alt_ref &&
  1052. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
  1053. active_best_quality -=
  1054. (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
  1055. active_worst_quality += (cpi->twopass.extend_maxq / 2);
  1056. } else {
  1057. active_best_quality -=
  1058. (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
  1059. active_worst_quality += cpi->twopass.extend_maxq;
  1060. }
  1061. }
  1062. #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
  1063. vpx_clear_system_state();
  1064. // Static forced key frames Q restrictions dealt with elsewhere.
  1065. if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) ||
  1066. !rc->this_key_frame_forced ||
  1067. (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
  1068. int qdelta = vp9_frame_type_qdelta(cpi, gf_group->rf_level[gf_group->index],
  1069. active_worst_quality);
  1070. active_worst_quality =
  1071. VPXMAX(active_worst_quality + qdelta, active_best_quality);
  1072. }
  1073. #endif
  1074. // Modify active_best_quality for downscaled normal frames.
  1075. if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) {
  1076. int qdelta = vp9_compute_qdelta_by_rate(
  1077. rc, cm->frame_type, active_best_quality, 2.0, cm->bit_depth);
  1078. active_best_quality =
  1079. VPXMAX(active_best_quality + qdelta, rc->best_quality);
  1080. }
  1081. active_best_quality =
  1082. clamp(active_best_quality, rc->best_quality, rc->worst_quality);
  1083. active_worst_quality =
  1084. clamp(active_worst_quality, active_best_quality, rc->worst_quality);
  1085. if (oxcf->rc_mode == VPX_Q) {
  1086. q = active_best_quality;
  1087. // Special case code to try and match quality with forced key frames.
  1088. } else if ((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) &&
  1089. rc->this_key_frame_forced) {
  1090. // If static since last kf use better of last boosted and last kf q.
  1091. if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
  1092. q = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex);
  1093. } else {
  1094. q = rc->last_boosted_qindex;
  1095. }
  1096. } else {
  1097. q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality,
  1098. active_worst_quality);
  1099. if (q > active_worst_quality) {
  1100. // Special case when we are targeting the max allowed rate.
  1101. if (rc->this_frame_target >= rc->max_frame_bandwidth)
  1102. active_worst_quality = q;
  1103. else
  1104. q = active_worst_quality;
  1105. }
  1106. }
  1107. clamp(q, active_best_quality, active_worst_quality);
  1108. *top_index = active_worst_quality;
  1109. *bottom_index = active_best_quality;
  1110. assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality);
  1111. assert(*bottom_index <= rc->worst_quality &&
  1112. *bottom_index >= rc->best_quality);
  1113. assert(q <= rc->worst_quality && q >= rc->best_quality);
  1114. return q;
  1115. }
  1116. int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, int *bottom_index,
  1117. int *top_index) {
  1118. int q;
  1119. if (cpi->oxcf.pass == 0) {
  1120. if (cpi->oxcf.rc_mode == VPX_CBR)
  1121. q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
  1122. else
  1123. q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
  1124. } else {
  1125. q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index);
  1126. }
  1127. if (cpi->sf.use_nonrd_pick_mode) {
  1128. if (cpi->sf.force_frame_boost == 1) q -= cpi->sf.max_delta_qindex;
  1129. if (q < *bottom_index)
  1130. *bottom_index = q;
  1131. else if (q > *top_index)
  1132. *top_index = q;
  1133. }
  1134. return q;
  1135. }
  1136. void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi, int frame_target,
  1137. int *frame_under_shoot_limit,
  1138. int *frame_over_shoot_limit) {
  1139. if (cpi->oxcf.rc_mode == VPX_Q) {
  1140. *frame_under_shoot_limit = 0;
  1141. *frame_over_shoot_limit = INT_MAX;
  1142. } else {
  1143. // For very small rate targets where the fractional adjustment
  1144. // may be tiny make sure there is at least a minimum range.
  1145. const int tol_low = (cpi->sf.recode_tolerance_low * frame_target) / 100;
  1146. const int tol_high = (cpi->sf.recode_tolerance_high * frame_target) / 100;
  1147. *frame_under_shoot_limit = VPXMAX(frame_target - tol_low - 100, 0);
  1148. *frame_over_shoot_limit =
  1149. VPXMIN(frame_target + tol_high + 100, cpi->rc.max_frame_bandwidth);
  1150. }
  1151. }
  1152. void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) {
  1153. const VP9_COMMON *const cm = &cpi->common;
  1154. RATE_CONTROL *const rc = &cpi->rc;
  1155. rc->this_frame_target = target;
  1156. // Modify frame size target when down-scaling.
  1157. if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
  1158. rc->frame_size_selector != UNSCALED)
  1159. rc->this_frame_target = (int)(rc->this_frame_target *
  1160. rate_thresh_mult[rc->frame_size_selector]);
  1161. // Target rate per SB64 (including partial SB64s.
  1162. rc->sb64_target_rate = (int)(((int64_t)rc->this_frame_target * 64 * 64) /
  1163. (cm->width * cm->height));
  1164. }
  1165. static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
  1166. // this frame refreshes means next frames don't unless specified by user
  1167. RATE_CONTROL *const rc = &cpi->rc;
  1168. rc->frames_since_golden = 0;
  1169. // Mark the alt ref as done (setting to 0 means no further alt refs pending).
  1170. rc->source_alt_ref_pending = 0;
  1171. // Set the alternate reference frame active flag
  1172. rc->source_alt_ref_active = 1;
  1173. }
  1174. static void update_golden_frame_stats(VP9_COMP *cpi) {
  1175. RATE_CONTROL *const rc = &cpi->rc;
  1176. // Update the Golden frame usage counts.
  1177. if (cpi->refresh_golden_frame) {
  1178. // this frame refreshes means next frames don't unless specified by user
  1179. rc->frames_since_golden = 0;
  1180. // If we are not using alt ref in the up and coming group clear the arf
  1181. // active flag. In multi arf group case, if the index is not 0 then
  1182. // we are overlaying a mid group arf so should not reset the flag.
  1183. if (cpi->oxcf.pass == 2) {
  1184. if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0))
  1185. rc->source_alt_ref_active = 0;
  1186. } else if (!rc->source_alt_ref_pending) {
  1187. rc->source_alt_ref_active = 0;
  1188. }
  1189. // Decrement count down till next gf
  1190. if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
  1191. } else if (!cpi->refresh_alt_ref_frame) {
  1192. // Decrement count down till next gf
  1193. if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--;
  1194. rc->frames_since_golden++;
  1195. }
  1196. }
  1197. static void compute_frame_low_motion(VP9_COMP *const cpi) {
  1198. VP9_COMMON *const cm = &cpi->common;
  1199. int mi_row, mi_col;
  1200. MODE_INFO **mi = cm->mi_grid_visible;
  1201. RATE_CONTROL *const rc = &cpi->rc;
  1202. const int rows = cm->mi_rows, cols = cm->mi_cols;
  1203. int cnt_zeromv = 0;
  1204. for (mi_row = 0; mi_row < rows; mi_row++) {
  1205. for (mi_col = 0; mi_col < cols; mi_col++) {
  1206. if (abs(mi[0]->mv[0].as_mv.row) < 16 && abs(mi[0]->mv[0].as_mv.col) < 16)
  1207. cnt_zeromv++;
  1208. mi++;
  1209. }
  1210. mi += 8;
  1211. }
  1212. cnt_zeromv = 100 * cnt_zeromv / (rows * cols);
  1213. rc->avg_frame_low_motion = (3 * rc->avg_frame_low_motion + cnt_zeromv) >> 2;
  1214. }
  1215. void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) {
  1216. const VP9_COMMON *const cm = &cpi->common;
  1217. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1218. RATE_CONTROL *const rc = &cpi->rc;
  1219. const int qindex = cm->base_qindex;
  1220. // Update rate control heuristics
  1221. rc->projected_frame_size = (int)(bytes_used << 3);
  1222. // Post encode loop adjustment of Q prediction.
  1223. vp9_rc_update_rate_correction_factors(cpi);
  1224. // Keep a record of last Q and ambient average Q.
  1225. if (cm->frame_type == KEY_FRAME) {
  1226. rc->last_q[KEY_FRAME] = qindex;
  1227. rc->avg_frame_qindex[KEY_FRAME] =
  1228. ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
  1229. if (cpi->use_svc) {
  1230. int i = 0;
  1231. SVC *svc = &cpi->svc;
  1232. for (i = 0; i < svc->number_temporal_layers; ++i) {
  1233. const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
  1234. svc->number_temporal_layers);
  1235. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  1236. RATE_CONTROL *lrc = &lc->rc;
  1237. lrc->last_q[KEY_FRAME] = rc->last_q[KEY_FRAME];
  1238. lrc->avg_frame_qindex[KEY_FRAME] = rc->avg_frame_qindex[KEY_FRAME];
  1239. }
  1240. }
  1241. } else {
  1242. if ((cpi->use_svc && oxcf->rc_mode == VPX_CBR) ||
  1243. (!rc->is_src_frame_alt_ref &&
  1244. !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
  1245. rc->last_q[INTER_FRAME] = qindex;
  1246. rc->avg_frame_qindex[INTER_FRAME] =
  1247. ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
  1248. rc->ni_frames++;
  1249. rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  1250. rc->avg_q = rc->tot_q / rc->ni_frames;
  1251. // Calculate the average Q for normal inter frames (not key or GFU
  1252. // frames).
  1253. rc->ni_tot_qi += qindex;
  1254. rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
  1255. }
  1256. }
  1257. // Keep record of last boosted (KF/KF/ARF) Q value.
  1258. // If the current frame is coded at a lower Q then we also update it.
  1259. // If all mbs in this group are skipped only update if the Q value is
  1260. // better than that already stored.
  1261. // This is used to help set quality in forced key frames to reduce popping
  1262. if ((qindex < rc->last_boosted_qindex) || (cm->frame_type == KEY_FRAME) ||
  1263. (!rc->constrained_gf_group &&
  1264. (cpi->refresh_alt_ref_frame ||
  1265. (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
  1266. rc->last_boosted_qindex = qindex;
  1267. }
  1268. if (cm->frame_type == KEY_FRAME) rc->last_kf_qindex = qindex;
  1269. update_buffer_level(cpi, rc->projected_frame_size);
  1270. // Rolling monitors of whether we are over or underspending used to help
  1271. // regulate min and Max Q in two pass.
  1272. if (cm->frame_type != KEY_FRAME) {
  1273. rc->rolling_target_bits = ROUND_POWER_OF_TWO(
  1274. rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
  1275. rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
  1276. rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
  1277. rc->long_rolling_target_bits = ROUND_POWER_OF_TWO(
  1278. rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
  1279. rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
  1280. rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
  1281. }
  1282. // Actual bits spent
  1283. rc->total_actual_bits += rc->projected_frame_size;
  1284. rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
  1285. rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
  1286. if (!cpi->use_svc || is_two_pass_svc(cpi)) {
  1287. if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
  1288. (cm->frame_type != KEY_FRAME))
  1289. // Update the alternate reference frame stats as appropriate.
  1290. update_alt_ref_frame_stats(cpi);
  1291. else
  1292. // Update the Golden frame stats as appropriate.
  1293. update_golden_frame_stats(cpi);
  1294. }
  1295. if (cm->frame_type == KEY_FRAME) rc->frames_since_key = 0;
  1296. if (cm->show_frame) {
  1297. rc->frames_since_key++;
  1298. rc->frames_to_key--;
  1299. }
  1300. // Trigger the resizing of the next frame if it is scaled.
  1301. if (oxcf->pass != 0) {
  1302. cpi->resize_pending =
  1303. rc->next_frame_size_selector != rc->frame_size_selector;
  1304. rc->frame_size_selector = rc->next_frame_size_selector;
  1305. }
  1306. if (oxcf->pass == 0) {
  1307. if (cm->frame_type != KEY_FRAME) compute_frame_low_motion(cpi);
  1308. }
  1309. if (cm->frame_type != KEY_FRAME) rc->reset_high_source_sad = 0;
  1310. }
  1311. void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) {
  1312. // Update buffer level with zero size, update frame counters, and return.
  1313. update_buffer_level(cpi, 0);
  1314. cpi->rc.frames_since_key++;
  1315. cpi->rc.frames_to_key--;
  1316. cpi->rc.rc_2_frame = 0;
  1317. cpi->rc.rc_1_frame = 0;
  1318. }
  1319. static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
  1320. const RATE_CONTROL *const rc = &cpi->rc;
  1321. const int af_ratio = rc->af_ratio_onepass_vbr;
  1322. int target =
  1323. (!rc->is_src_frame_alt_ref &&
  1324. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))
  1325. ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
  1326. (rc->baseline_gf_interval + af_ratio - 1)
  1327. : (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
  1328. (rc->baseline_gf_interval + af_ratio - 1);
  1329. return vp9_rc_clamp_pframe_target_size(cpi, target);
  1330. }
  1331. static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
  1332. static const int kf_ratio = 25;
  1333. const RATE_CONTROL *rc = &cpi->rc;
  1334. const int target = rc->avg_frame_bandwidth * kf_ratio;
  1335. return vp9_rc_clamp_iframe_target_size(cpi, target);
  1336. }
  1337. static void adjust_gfint_frame_constraint(VP9_COMP *cpi, int frame_constraint) {
  1338. RATE_CONTROL *const rc = &cpi->rc;
  1339. rc->constrained_gf_group = 0;
  1340. // Reset gf interval to make more equal spacing for frame_constraint.
  1341. if ((frame_constraint <= 7 * rc->baseline_gf_interval >> 2) &&
  1342. (frame_constraint > rc->baseline_gf_interval)) {
  1343. rc->baseline_gf_interval = frame_constraint >> 1;
  1344. if (rc->baseline_gf_interval < 5)
  1345. rc->baseline_gf_interval = frame_constraint;
  1346. rc->constrained_gf_group = 1;
  1347. } else {
  1348. // Reset to keep gf_interval <= frame_constraint.
  1349. if (rc->baseline_gf_interval > frame_constraint) {
  1350. rc->baseline_gf_interval = frame_constraint;
  1351. rc->constrained_gf_group = 1;
  1352. }
  1353. }
  1354. }
  1355. void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
  1356. VP9_COMMON *const cm = &cpi->common;
  1357. RATE_CONTROL *const rc = &cpi->rc;
  1358. int target;
  1359. // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
  1360. if (!cpi->refresh_alt_ref_frame &&
  1361. (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
  1362. rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) {
  1363. cm->frame_type = KEY_FRAME;
  1364. rc->this_key_frame_forced =
  1365. cm->current_video_frame != 0 && rc->frames_to_key == 0;
  1366. rc->frames_to_key = cpi->oxcf.key_freq;
  1367. rc->kf_boost = DEFAULT_KF_BOOST;
  1368. rc->source_alt_ref_active = 0;
  1369. } else {
  1370. cm->frame_type = INTER_FRAME;
  1371. }
  1372. if (rc->frames_till_gf_update_due == 0) {
  1373. double rate_err = 1.0;
  1374. rc->gfu_boost = DEFAULT_GF_BOOST;
  1375. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0) {
  1376. vp9_cyclic_refresh_set_golden_update(cpi);
  1377. } else {
  1378. rc->baseline_gf_interval = VPXMIN(
  1379. 20, VPXMAX(10, (rc->min_gf_interval + rc->max_gf_interval) / 2));
  1380. }
  1381. rc->af_ratio_onepass_vbr = 10;
  1382. if (rc->rolling_target_bits > 0)
  1383. rate_err =
  1384. (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
  1385. if (cm->current_video_frame > 30) {
  1386. if (rc->avg_frame_qindex[INTER_FRAME] > (7 * rc->worst_quality) >> 3 &&
  1387. rate_err > 3.5) {
  1388. rc->baseline_gf_interval =
  1389. VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1);
  1390. } else if (rc->avg_frame_low_motion < 20) {
  1391. // Decrease gf interval for high motion case.
  1392. rc->baseline_gf_interval = VPXMAX(6, rc->baseline_gf_interval >> 1);
  1393. }
  1394. // Adjust boost and af_ratio based on avg_frame_low_motion, which varies
  1395. // between 0 and 100 (stationary, 100% zero/small motion).
  1396. rc->gfu_boost =
  1397. VPXMAX(500,
  1398. DEFAULT_GF_BOOST * (rc->avg_frame_low_motion << 1) /
  1399. (rc->avg_frame_low_motion + 100));
  1400. rc->af_ratio_onepass_vbr = VPXMIN(15, VPXMAX(5, 3 * rc->gfu_boost / 400));
  1401. }
  1402. adjust_gfint_frame_constraint(cpi, rc->frames_to_key);
  1403. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  1404. cpi->refresh_golden_frame = 1;
  1405. rc->source_alt_ref_pending = 0;
  1406. rc->alt_ref_gf_group = 0;
  1407. if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) {
  1408. rc->source_alt_ref_pending = 1;
  1409. rc->alt_ref_gf_group = 1;
  1410. }
  1411. }
  1412. if (cm->frame_type == KEY_FRAME)
  1413. target = calc_iframe_target_size_one_pass_vbr(cpi);
  1414. else
  1415. target = calc_pframe_target_size_one_pass_vbr(cpi);
  1416. vp9_rc_set_frame_target(cpi, target);
  1417. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0)
  1418. vp9_cyclic_refresh_update_parameters(cpi);
  1419. }
  1420. static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
  1421. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  1422. const RATE_CONTROL *rc = &cpi->rc;
  1423. const SVC *const svc = &cpi->svc;
  1424. const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
  1425. const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
  1426. int min_frame_target =
  1427. VPXMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
  1428. int target;
  1429. if (oxcf->gf_cbr_boost_pct) {
  1430. const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
  1431. target = cpi->refresh_golden_frame
  1432. ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval *
  1433. af_ratio_pct) /
  1434. (rc->baseline_gf_interval * 100 + af_ratio_pct - 100)
  1435. : (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
  1436. (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
  1437. } else {
  1438. target = rc->avg_frame_bandwidth;
  1439. }
  1440. if (is_one_pass_cbr_svc(cpi)) {
  1441. // Note that for layers, avg_frame_bandwidth is the cumulative
  1442. // per-frame-bandwidth. For the target size of this frame, use the
  1443. // layer average frame size (i.e., non-cumulative per-frame-bw).
  1444. int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
  1445. svc->number_temporal_layers);
  1446. const LAYER_CONTEXT *lc = &svc->layer_context[layer];
  1447. target = lc->avg_frame_size;
  1448. min_frame_target = VPXMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
  1449. }
  1450. if (diff > 0) {
  1451. // Lower the target bandwidth for this frame.
  1452. const int pct_low = (int)VPXMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
  1453. target -= (target * pct_low) / 200;
  1454. } else if (diff < 0) {
  1455. // Increase the target bandwidth for this frame.
  1456. const int pct_high =
  1457. (int)VPXMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
  1458. target += (target * pct_high) / 200;
  1459. }
  1460. if (oxcf->rc_max_inter_bitrate_pct) {
  1461. const int max_rate =
  1462. rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
  1463. target = VPXMIN(target, max_rate);
  1464. }
  1465. return VPXMAX(min_frame_target, target);
  1466. }
  1467. static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
  1468. const RATE_CONTROL *rc = &cpi->rc;
  1469. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  1470. const SVC *const svc = &cpi->svc;
  1471. int target;
  1472. if (cpi->common.current_video_frame == 0) {
  1473. target = ((rc->starting_buffer_level / 2) > INT_MAX)
  1474. ? INT_MAX
  1475. : (int)(rc->starting_buffer_level / 2);
  1476. } else {
  1477. int kf_boost = 32;
  1478. double framerate = cpi->framerate;
  1479. if (svc->number_temporal_layers > 1 && oxcf->rc_mode == VPX_CBR) {
  1480. // Use the layer framerate for temporal layers CBR mode.
  1481. const int layer =
  1482. LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
  1483. svc->number_temporal_layers);
  1484. const LAYER_CONTEXT *lc = &svc->layer_context[layer];
  1485. framerate = lc->framerate;
  1486. }
  1487. kf_boost = VPXMAX(kf_boost, (int)(2 * framerate - 16));
  1488. if (rc->frames_since_key < framerate / 2) {
  1489. kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2));
  1490. }
  1491. target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
  1492. }
  1493. return vp9_rc_clamp_iframe_target_size(cpi, target);
  1494. }
  1495. void vp9_rc_get_svc_params(VP9_COMP *cpi) {
  1496. VP9_COMMON *const cm = &cpi->common;
  1497. RATE_CONTROL *const rc = &cpi->rc;
  1498. int target = rc->avg_frame_bandwidth;
  1499. int layer =
  1500. LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, cpi->svc.temporal_layer_id,
  1501. cpi->svc.number_temporal_layers);
  1502. // Periodic key frames is based on the super-frame counter
  1503. // (svc.current_superframe), also only base spatial layer is key frame.
  1504. if ((cm->current_video_frame == 0) || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
  1505. (cpi->oxcf.auto_key &&
  1506. (cpi->svc.current_superframe % cpi->oxcf.key_freq == 0) &&
  1507. cpi->svc.spatial_layer_id == 0)) {
  1508. cm->frame_type = KEY_FRAME;
  1509. rc->source_alt_ref_active = 0;
  1510. if (is_two_pass_svc(cpi)) {
  1511. cpi->svc.layer_context[layer].is_key_frame = 1;
  1512. cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
  1513. } else if (is_one_pass_cbr_svc(cpi)) {
  1514. if (cm->current_video_frame > 0) vp9_svc_reset_key_frame(cpi);
  1515. layer = LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id,
  1516. cpi->svc.temporal_layer_id,
  1517. cpi->svc.number_temporal_layers);
  1518. cpi->svc.layer_context[layer].is_key_frame = 1;
  1519. cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
  1520. // Assumption here is that LAST_FRAME is being updated for a keyframe.
  1521. // Thus no change in update flags.
  1522. target = calc_iframe_target_size_one_pass_cbr(cpi);
  1523. }
  1524. } else {
  1525. cm->frame_type = INTER_FRAME;
  1526. if (is_two_pass_svc(cpi)) {
  1527. LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
  1528. if (cpi->svc.spatial_layer_id == 0) {
  1529. lc->is_key_frame = 0;
  1530. } else {
  1531. lc->is_key_frame =
  1532. cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame;
  1533. if (lc->is_key_frame) cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
  1534. }
  1535. cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
  1536. } else if (is_one_pass_cbr_svc(cpi)) {
  1537. LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
  1538. if (cpi->svc.spatial_layer_id == cpi->svc.first_spatial_layer_to_encode) {
  1539. lc->is_key_frame = 0;
  1540. } else {
  1541. lc->is_key_frame =
  1542. cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame;
  1543. }
  1544. target = calc_pframe_target_size_one_pass_cbr(cpi);
  1545. }
  1546. }
  1547. // Any update/change of global cyclic refresh parameters (amount/delta-qp)
  1548. // should be done here, before the frame qp is selected.
  1549. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
  1550. vp9_cyclic_refresh_update_parameters(cpi);
  1551. vp9_rc_set_frame_target(cpi, target);
  1552. rc->frames_till_gf_update_due = INT_MAX;
  1553. rc->baseline_gf_interval = INT_MAX;
  1554. }
  1555. void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
  1556. VP9_COMMON *const cm = &cpi->common;
  1557. RATE_CONTROL *const rc = &cpi->rc;
  1558. int target;
  1559. // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
  1560. if ((cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) ||
  1561. rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) {
  1562. cm->frame_type = KEY_FRAME;
  1563. rc->this_key_frame_forced =
  1564. cm->current_video_frame != 0 && rc->frames_to_key == 0;
  1565. rc->frames_to_key = cpi->oxcf.key_freq;
  1566. rc->kf_boost = DEFAULT_KF_BOOST;
  1567. rc->source_alt_ref_active = 0;
  1568. } else {
  1569. cm->frame_type = INTER_FRAME;
  1570. }
  1571. if (rc->frames_till_gf_update_due == 0) {
  1572. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
  1573. vp9_cyclic_refresh_set_golden_update(cpi);
  1574. else
  1575. rc->baseline_gf_interval =
  1576. (rc->min_gf_interval + rc->max_gf_interval) / 2;
  1577. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  1578. // NOTE: frames_till_gf_update_due must be <= frames_to_key.
  1579. if (rc->frames_till_gf_update_due > rc->frames_to_key)
  1580. rc->frames_till_gf_update_due = rc->frames_to_key;
  1581. cpi->refresh_golden_frame = 1;
  1582. rc->gfu_boost = DEFAULT_GF_BOOST;
  1583. }
  1584. // Any update/change of global cyclic refresh parameters (amount/delta-qp)
  1585. // should be done here, before the frame qp is selected.
  1586. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
  1587. vp9_cyclic_refresh_update_parameters(cpi);
  1588. if (cm->frame_type == KEY_FRAME)
  1589. target = calc_iframe_target_size_one_pass_cbr(cpi);
  1590. else
  1591. target = calc_pframe_target_size_one_pass_cbr(cpi);
  1592. vp9_rc_set_frame_target(cpi, target);
  1593. if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC)
  1594. cpi->resize_pending = vp9_resize_one_pass_cbr(cpi);
  1595. else
  1596. cpi->resize_pending = 0;
  1597. }
  1598. int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
  1599. vpx_bit_depth_t bit_depth) {
  1600. int start_index = rc->worst_quality;
  1601. int target_index = rc->worst_quality;
  1602. int i;
  1603. // Convert the average q value to an index.
  1604. for (i = rc->best_quality; i < rc->worst_quality; ++i) {
  1605. start_index = i;
  1606. if (vp9_convert_qindex_to_q(i, bit_depth) >= qstart) break;
  1607. }
  1608. // Convert the q target to an index
  1609. for (i = rc->best_quality; i < rc->worst_quality; ++i) {
  1610. target_index = i;
  1611. if (vp9_convert_qindex_to_q(i, bit_depth) >= qtarget) break;
  1612. }
  1613. return target_index - start_index;
  1614. }
  1615. int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
  1616. int qindex, double rate_target_ratio,
  1617. vpx_bit_depth_t bit_depth) {
  1618. int target_index = rc->worst_quality;
  1619. int i;
  1620. // Look up the current projected bits per block for the base index
  1621. const int base_bits_per_mb =
  1622. vp9_rc_bits_per_mb(frame_type, qindex, 1.0, bit_depth);
  1623. // Find the target bits per mb based on the base value and given ratio.
  1624. const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
  1625. // Convert the q target to an index
  1626. for (i = rc->best_quality; i < rc->worst_quality; ++i) {
  1627. if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <=
  1628. target_bits_per_mb) {
  1629. target_index = i;
  1630. break;
  1631. }
  1632. }
  1633. return target_index - qindex;
  1634. }
  1635. void vp9_rc_set_gf_interval_range(const VP9_COMP *const cpi,
  1636. RATE_CONTROL *const rc) {
  1637. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1638. // Special case code for 1 pass fixed Q mode tests
  1639. if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
  1640. rc->max_gf_interval = FIXED_GF_INTERVAL;
  1641. rc->min_gf_interval = FIXED_GF_INTERVAL;
  1642. rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL;
  1643. } else {
  1644. // Set Maximum gf/arf interval
  1645. rc->max_gf_interval = oxcf->max_gf_interval;
  1646. rc->min_gf_interval = oxcf->min_gf_interval;
  1647. if (rc->min_gf_interval == 0)
  1648. rc->min_gf_interval = vp9_rc_get_default_min_gf_interval(
  1649. oxcf->width, oxcf->height, cpi->framerate);
  1650. if (rc->max_gf_interval == 0)
  1651. rc->max_gf_interval = vp9_rc_get_default_max_gf_interval(
  1652. cpi->framerate, rc->min_gf_interval);
  1653. // Extended interval for genuinely static scenes
  1654. rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2;
  1655. if (is_altref_enabled(cpi)) {
  1656. if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
  1657. rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
  1658. }
  1659. if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
  1660. rc->max_gf_interval = rc->static_scene_max_gf_interval;
  1661. // Clamp min to max
  1662. rc->min_gf_interval = VPXMIN(rc->min_gf_interval, rc->max_gf_interval);
  1663. if (oxcf->target_level == LEVEL_AUTO) {
  1664. const uint32_t pic_size = cpi->common.width * cpi->common.height;
  1665. int i;
  1666. for (i = LEVEL_1; i < LEVEL_MAX; ++i) {
  1667. if (vp9_level_defs[i].max_luma_picture_size > pic_size) {
  1668. if (rc->min_gf_interval <=
  1669. (int)vp9_level_defs[i].min_altref_distance) {
  1670. rc->min_gf_interval =
  1671. (int)vp9_level_defs[i].min_altref_distance + 1;
  1672. rc->max_gf_interval =
  1673. VPXMAX(rc->max_gf_interval, rc->min_gf_interval);
  1674. }
  1675. break;
  1676. }
  1677. }
  1678. }
  1679. }
  1680. }
  1681. void vp9_rc_update_framerate(VP9_COMP *cpi) {
  1682. const VP9_COMMON *const cm = &cpi->common;
  1683. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1684. RATE_CONTROL *const rc = &cpi->rc;
  1685. int vbr_max_bits;
  1686. rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate);
  1687. rc->min_frame_bandwidth =
  1688. (int)(rc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
  1689. rc->min_frame_bandwidth =
  1690. VPXMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
  1691. // A maximum bitrate for a frame is defined.
  1692. // The baseline for this aligns with HW implementations that
  1693. // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
  1694. // per 16x16 MB (averaged over a frame). However this limit is extended if
  1695. // a very high rate is given on the command line or the the rate cannnot
  1696. // be acheived because of a user specificed max q (e.g. when the user
  1697. // specifies lossless encode.
  1698. vbr_max_bits =
  1699. (int)(((int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section) /
  1700. 100);
  1701. rc->max_frame_bandwidth =
  1702. VPXMAX(VPXMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
  1703. vp9_rc_set_gf_interval_range(cpi, rc);
  1704. }
  1705. #define VBR_PCT_ADJUSTMENT_LIMIT 50
  1706. // For VBR...adjustment to the frame target based on error from previous frames
  1707. static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) {
  1708. RATE_CONTROL *const rc = &cpi->rc;
  1709. int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
  1710. int max_delta;
  1711. int frame_window = VPXMIN(16, ((int)cpi->twopass.total_stats.count -
  1712. cpi->common.current_video_frame));
  1713. // Calcluate the adjustment to rate for this frame.
  1714. if (frame_window > 0) {
  1715. max_delta = (vbr_bits_off_target > 0)
  1716. ? (int)(vbr_bits_off_target / frame_window)
  1717. : (int)(-vbr_bits_off_target / frame_window);
  1718. max_delta = VPXMIN(max_delta,
  1719. ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
  1720. // vbr_bits_off_target > 0 means we have extra bits to spend
  1721. if (vbr_bits_off_target > 0) {
  1722. *this_frame_target += (vbr_bits_off_target > max_delta)
  1723. ? max_delta
  1724. : (int)vbr_bits_off_target;
  1725. } else {
  1726. *this_frame_target -= (vbr_bits_off_target < -max_delta)
  1727. ? max_delta
  1728. : (int)-vbr_bits_off_target;
  1729. }
  1730. }
  1731. // Fast redistribution of bits arising from massive local undershoot.
  1732. // Dont do it for kf,arf,gf or overlay frames.
  1733. if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
  1734. rc->vbr_bits_off_target_fast) {
  1735. int one_frame_bits = VPXMAX(rc->avg_frame_bandwidth, *this_frame_target);
  1736. int fast_extra_bits;
  1737. fast_extra_bits = (int)VPXMIN(rc->vbr_bits_off_target_fast, one_frame_bits);
  1738. fast_extra_bits = (int)VPXMIN(
  1739. fast_extra_bits,
  1740. VPXMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
  1741. *this_frame_target += (int)fast_extra_bits;
  1742. rc->vbr_bits_off_target_fast -= fast_extra_bits;
  1743. }
  1744. }
  1745. void vp9_set_target_rate(VP9_COMP *cpi) {
  1746. RATE_CONTROL *const rc = &cpi->rc;
  1747. int target_rate = rc->base_frame_target;
  1748. if (cpi->common.frame_type == KEY_FRAME)
  1749. target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
  1750. else
  1751. target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
  1752. // Correction to rate target based on prior over or under shoot.
  1753. if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ)
  1754. vbr_rate_correction(cpi, &target_rate);
  1755. vp9_rc_set_frame_target(cpi, target_rate);
  1756. }
  1757. // Check if we should resize, based on average QP from past x frames.
  1758. // Only allow for resize at most one scale down for now, scaling factor is 2.
  1759. int vp9_resize_one_pass_cbr(VP9_COMP *cpi) {
  1760. const VP9_COMMON *const cm = &cpi->common;
  1761. RATE_CONTROL *const rc = &cpi->rc;
  1762. RESIZE_ACTION resize_action = NO_RESIZE;
  1763. int avg_qp_thr1 = 70;
  1764. int avg_qp_thr2 = 50;
  1765. int min_width = 180;
  1766. int min_height = 180;
  1767. int down_size_on = 1;
  1768. cpi->resize_scale_num = 1;
  1769. cpi->resize_scale_den = 1;
  1770. // Don't resize on key frame; reset the counters on key frame.
  1771. if (cm->frame_type == KEY_FRAME) {
  1772. cpi->resize_avg_qp = 0;
  1773. cpi->resize_count = 0;
  1774. return 0;
  1775. }
  1776. // Check current frame reslution to avoid generating frames smaller than
  1777. // the minimum resolution.
  1778. if (ONEHALFONLY_RESIZE) {
  1779. if ((cm->width >> 1) < min_width || (cm->height >> 1) < min_height)
  1780. down_size_on = 0;
  1781. } else {
  1782. if (cpi->resize_state == ORIG &&
  1783. (cm->width * 3 / 4 < min_width || cm->height * 3 / 4 < min_height))
  1784. return 0;
  1785. else if (cpi->resize_state == THREE_QUARTER &&
  1786. ((cpi->oxcf.width >> 1) < min_width ||
  1787. (cpi->oxcf.height >> 1) < min_height))
  1788. down_size_on = 0;
  1789. }
  1790. #if CONFIG_VP9_TEMPORAL_DENOISING
  1791. // If denoiser is on, apply a smaller qp threshold.
  1792. if (cpi->oxcf.noise_sensitivity > 0) {
  1793. avg_qp_thr1 = 60;
  1794. avg_qp_thr2 = 40;
  1795. }
  1796. #endif
  1797. // Resize based on average buffer underflow and QP over some window.
  1798. // Ignore samples close to key frame, since QP is usually high after key.
  1799. if (cpi->rc.frames_since_key > 2 * cpi->framerate) {
  1800. const int window = (int)(4 * cpi->framerate);
  1801. cpi->resize_avg_qp += cm->base_qindex;
  1802. if (cpi->rc.buffer_level < (int)(30 * rc->optimal_buffer_level / 100))
  1803. ++cpi->resize_buffer_underflow;
  1804. ++cpi->resize_count;
  1805. // Check for resize action every "window" frames.
  1806. if (cpi->resize_count >= window) {
  1807. int avg_qp = cpi->resize_avg_qp / cpi->resize_count;
  1808. // Resize down if buffer level has underflowed sufficient amount in past
  1809. // window, and we are at original or 3/4 of original resolution.
  1810. // Resize back up if average QP is low, and we are currently in a resized
  1811. // down state, i.e. 1/2 or 3/4 of original resolution.
  1812. // Currently, use a flag to turn 3/4 resizing feature on/off.
  1813. if (cpi->resize_buffer_underflow > (cpi->resize_count >> 2)) {
  1814. if (cpi->resize_state == THREE_QUARTER && down_size_on) {
  1815. resize_action = DOWN_ONEHALF;
  1816. cpi->resize_state = ONE_HALF;
  1817. } else if (cpi->resize_state == ORIG) {
  1818. resize_action = ONEHALFONLY_RESIZE ? DOWN_ONEHALF : DOWN_THREEFOUR;
  1819. cpi->resize_state = ONEHALFONLY_RESIZE ? ONE_HALF : THREE_QUARTER;
  1820. }
  1821. } else if (cpi->resize_state != ORIG &&
  1822. avg_qp < avg_qp_thr1 * cpi->rc.worst_quality / 100) {
  1823. if (cpi->resize_state == THREE_QUARTER ||
  1824. avg_qp < avg_qp_thr2 * cpi->rc.worst_quality / 100 ||
  1825. ONEHALFONLY_RESIZE) {
  1826. resize_action = UP_ORIG;
  1827. cpi->resize_state = ORIG;
  1828. } else if (cpi->resize_state == ONE_HALF) {
  1829. resize_action = UP_THREEFOUR;
  1830. cpi->resize_state = THREE_QUARTER;
  1831. }
  1832. }
  1833. // Reset for next window measurement.
  1834. cpi->resize_avg_qp = 0;
  1835. cpi->resize_count = 0;
  1836. cpi->resize_buffer_underflow = 0;
  1837. }
  1838. }
  1839. // If decision is to resize, reset some quantities, and check is we should
  1840. // reduce rate correction factor,
  1841. if (resize_action != NO_RESIZE) {
  1842. int target_bits_per_frame;
  1843. int active_worst_quality;
  1844. int qindex;
  1845. int tot_scale_change;
  1846. if (resize_action == DOWN_THREEFOUR || resize_action == UP_THREEFOUR) {
  1847. cpi->resize_scale_num = 3;
  1848. cpi->resize_scale_den = 4;
  1849. } else if (resize_action == DOWN_ONEHALF) {
  1850. cpi->resize_scale_num = 1;
  1851. cpi->resize_scale_den = 2;
  1852. } else { // UP_ORIG or anything else
  1853. cpi->resize_scale_num = 1;
  1854. cpi->resize_scale_den = 1;
  1855. }
  1856. tot_scale_change = (cpi->resize_scale_den * cpi->resize_scale_den) /
  1857. (cpi->resize_scale_num * cpi->resize_scale_num);
  1858. // Reset buffer level to optimal, update target size.
  1859. rc->buffer_level = rc->optimal_buffer_level;
  1860. rc->bits_off_target = rc->optimal_buffer_level;
  1861. rc->this_frame_target = calc_pframe_target_size_one_pass_cbr(cpi);
  1862. // Get the projected qindex, based on the scaled target frame size (scaled
  1863. // so target_bits_per_mb in vp9_rc_regulate_q will be correct target).
  1864. target_bits_per_frame = (resize_action >= 0)
  1865. ? rc->this_frame_target * tot_scale_change
  1866. : rc->this_frame_target / tot_scale_change;
  1867. active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
  1868. qindex = vp9_rc_regulate_q(cpi, target_bits_per_frame, rc->best_quality,
  1869. active_worst_quality);
  1870. // If resize is down, check if projected q index is close to worst_quality,
  1871. // and if so, reduce the rate correction factor (since likely can afford
  1872. // lower q for resized frame).
  1873. if (resize_action > 0 && qindex > 90 * cpi->rc.worst_quality / 100) {
  1874. rc->rate_correction_factors[INTER_NORMAL] *= 0.85;
  1875. }
  1876. // If resize is back up, check if projected q index is too much above the
  1877. // current base_qindex, and if so, reduce the rate correction factor
  1878. // (since prefer to keep q for resized frame at least close to previous q).
  1879. if (resize_action < 0 && qindex > 130 * cm->base_qindex / 100) {
  1880. rc->rate_correction_factors[INTER_NORMAL] *= 0.9;
  1881. }
  1882. }
  1883. return resize_action;
  1884. }
  1885. static void adjust_gf_boost_lag_one_pass_vbr(VP9_COMP *cpi,
  1886. uint64_t avg_sad_current) {
  1887. VP9_COMMON *const cm = &cpi->common;
  1888. RATE_CONTROL *const rc = &cpi->rc;
  1889. int target;
  1890. int found = 0;
  1891. int found2 = 0;
  1892. int frame;
  1893. int i;
  1894. uint64_t avg_source_sad_lag = avg_sad_current;
  1895. int high_source_sad_lagindex = -1;
  1896. int steady_sad_lagindex = -1;
  1897. uint32_t sad_thresh1 = 60000;
  1898. uint32_t sad_thresh2 = 120000;
  1899. int low_content = 0;
  1900. int high_content = 0;
  1901. double rate_err = 1.0;
  1902. // Get measure of complexity over the future frames, and get the first
  1903. // future frame with high_source_sad/scene-change.
  1904. int tot_frames = (int)vp9_lookahead_depth(cpi->lookahead) - 1;
  1905. for (frame = tot_frames; frame >= 1; --frame) {
  1906. const int lagframe_idx = tot_frames - frame + 1;
  1907. uint64_t reference_sad = rc->avg_source_sad[0];
  1908. for (i = 1; i < lagframe_idx; ++i) {
  1909. if (rc->avg_source_sad[i] > 0)
  1910. reference_sad = (3 * reference_sad + rc->avg_source_sad[i]) >> 2;
  1911. }
  1912. // Detect up-coming scene change.
  1913. if (!found &&
  1914. (rc->avg_source_sad[lagframe_idx] >
  1915. VPXMAX(sad_thresh1, (unsigned int)(reference_sad << 1)) ||
  1916. rc->avg_source_sad[lagframe_idx] >
  1917. VPXMAX(3 * sad_thresh1 >> 2,
  1918. (unsigned int)(reference_sad << 2)))) {
  1919. high_source_sad_lagindex = lagframe_idx;
  1920. found = 1;
  1921. }
  1922. // Detect change from motion to steady.
  1923. if (!found2 && lagframe_idx > 1 && lagframe_idx < tot_frames &&
  1924. rc->avg_source_sad[lagframe_idx - 1] > (sad_thresh1 >> 2)) {
  1925. found2 = 1;
  1926. for (i = lagframe_idx; i < tot_frames; ++i) {
  1927. if (!(rc->avg_source_sad[i] > 0 &&
  1928. rc->avg_source_sad[i] < (sad_thresh1 >> 2) &&
  1929. rc->avg_source_sad[i] <
  1930. (rc->avg_source_sad[lagframe_idx - 1] >> 1))) {
  1931. found2 = 0;
  1932. i = tot_frames;
  1933. }
  1934. }
  1935. if (found2) steady_sad_lagindex = lagframe_idx;
  1936. }
  1937. avg_source_sad_lag += rc->avg_source_sad[lagframe_idx];
  1938. }
  1939. if (tot_frames > 0) avg_source_sad_lag = avg_source_sad_lag / tot_frames;
  1940. // Constrain distance between detected scene cuts.
  1941. if (high_source_sad_lagindex != -1 &&
  1942. high_source_sad_lagindex != rc->high_source_sad_lagindex - 1 &&
  1943. abs(high_source_sad_lagindex - rc->high_source_sad_lagindex) < 4)
  1944. rc->high_source_sad_lagindex = -1;
  1945. else
  1946. rc->high_source_sad_lagindex = high_source_sad_lagindex;
  1947. // Adjust some factors for the next GF group, ignore initial key frame,
  1948. // and only for lag_in_frames not too small.
  1949. if (cpi->refresh_golden_frame == 1 && cm->current_video_frame > 30 &&
  1950. cpi->oxcf.lag_in_frames > 8) {
  1951. int frame_constraint;
  1952. if (rc->rolling_target_bits > 0)
  1953. rate_err =
  1954. (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits;
  1955. high_content = high_source_sad_lagindex != -1 ||
  1956. avg_source_sad_lag > (rc->prev_avg_source_sad_lag << 1) ||
  1957. avg_source_sad_lag > sad_thresh2;
  1958. low_content = high_source_sad_lagindex == -1 &&
  1959. ((avg_source_sad_lag < (rc->prev_avg_source_sad_lag >> 1)) ||
  1960. (avg_source_sad_lag < sad_thresh1));
  1961. if (low_content) {
  1962. rc->gfu_boost = DEFAULT_GF_BOOST;
  1963. rc->baseline_gf_interval =
  1964. VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1);
  1965. } else if (high_content) {
  1966. rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
  1967. rc->baseline_gf_interval = (rate_err > 3.0)
  1968. ? VPXMAX(10, rc->baseline_gf_interval >> 1)
  1969. : VPXMAX(6, rc->baseline_gf_interval >> 1);
  1970. }
  1971. if (rc->baseline_gf_interval > cpi->oxcf.lag_in_frames - 1)
  1972. rc->baseline_gf_interval = cpi->oxcf.lag_in_frames - 1;
  1973. // Check for constraining gf_interval for up-coming scene/content changes,
  1974. // or for up-coming key frame, whichever is closer.
  1975. frame_constraint = rc->frames_to_key;
  1976. if (rc->high_source_sad_lagindex > 0 &&
  1977. frame_constraint > rc->high_source_sad_lagindex)
  1978. frame_constraint = rc->high_source_sad_lagindex;
  1979. if (steady_sad_lagindex > 3 && frame_constraint > steady_sad_lagindex)
  1980. frame_constraint = steady_sad_lagindex;
  1981. adjust_gfint_frame_constraint(cpi, frame_constraint);
  1982. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  1983. // Adjust factors for active_worst setting & af_ratio for next gf interval.
  1984. rc->fac_active_worst_inter = 150; // corresponds to 3/2 (= 150 /100).
  1985. rc->fac_active_worst_gf = 100;
  1986. if (rate_err < 2.0 && !high_content) {
  1987. rc->fac_active_worst_inter = 120;
  1988. rc->fac_active_worst_gf = 90;
  1989. } else if (rate_err > 8.0 && rc->avg_frame_qindex[INTER_FRAME] < 16) {
  1990. // Increase active_worst faster at low Q if rate fluctuation is high.
  1991. rc->fac_active_worst_inter = 200;
  1992. if (rc->avg_frame_qindex[INTER_FRAME] < 8)
  1993. rc->fac_active_worst_inter = 400;
  1994. }
  1995. if (low_content && rc->avg_frame_low_motion > 80) {
  1996. rc->af_ratio_onepass_vbr = 15;
  1997. } else if (high_content || rc->avg_frame_low_motion < 30) {
  1998. rc->af_ratio_onepass_vbr = 5;
  1999. rc->gfu_boost = DEFAULT_GF_BOOST >> 2;
  2000. }
  2001. if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) {
  2002. // Don't use alt-ref if there is a scene cut within the group,
  2003. // or content is not low.
  2004. if ((rc->high_source_sad_lagindex > 0 &&
  2005. rc->high_source_sad_lagindex <= rc->frames_till_gf_update_due) ||
  2006. (avg_source_sad_lag > 3 * sad_thresh1 >> 3)) {
  2007. rc->source_alt_ref_pending = 0;
  2008. rc->alt_ref_gf_group = 0;
  2009. } else {
  2010. rc->source_alt_ref_pending = 1;
  2011. rc->alt_ref_gf_group = 1;
  2012. // If alt-ref is used for this gf group, limit the interval.
  2013. if (rc->baseline_gf_interval > 12) {
  2014. rc->baseline_gf_interval = 12;
  2015. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  2016. }
  2017. }
  2018. }
  2019. target = calc_pframe_target_size_one_pass_vbr(cpi);
  2020. vp9_rc_set_frame_target(cpi, target);
  2021. }
  2022. rc->prev_avg_source_sad_lag = avg_source_sad_lag;
  2023. }
  2024. // Compute average source sad (temporal sad: between current source and
  2025. // previous source) over a subset of superblocks. Use this is detect big changes
  2026. // in content and allow rate control to react.
  2027. // This function also handles special case of lag_in_frames, to measure content
  2028. // level in #future frames set by the lag_in_frames.
  2029. void vp9_scene_detection_onepass(VP9_COMP *cpi) {
  2030. VP9_COMMON *const cm = &cpi->common;
  2031. RATE_CONTROL *const rc = &cpi->rc;
  2032. #if CONFIG_VP9_HIGHBITDEPTH
  2033. if (cm->use_highbitdepth) return;
  2034. #endif
  2035. rc->high_source_sad = 0;
  2036. if (cpi->Last_Source != NULL &&
  2037. cpi->Last_Source->y_width == cpi->Source->y_width &&
  2038. cpi->Last_Source->y_height == cpi->Source->y_height) {
  2039. YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS] = { NULL };
  2040. uint8_t *src_y = cpi->Source->y_buffer;
  2041. int src_ystride = cpi->Source->y_stride;
  2042. uint8_t *last_src_y = cpi->Last_Source->y_buffer;
  2043. int last_src_ystride = cpi->Last_Source->y_stride;
  2044. int start_frame = 0;
  2045. int frames_to_buffer = 1;
  2046. int frame = 0;
  2047. uint64_t avg_sad_current = 0;
  2048. uint32_t min_thresh = 4000;
  2049. float thresh = 8.0f;
  2050. if (cpi->oxcf.rc_mode == VPX_VBR) {
  2051. min_thresh = 60000;
  2052. thresh = 2.1f;
  2053. }
  2054. if (cpi->oxcf.lag_in_frames > 0) {
  2055. frames_to_buffer = (cm->current_video_frame == 1)
  2056. ? (int)vp9_lookahead_depth(cpi->lookahead) - 1
  2057. : 2;
  2058. start_frame = (int)vp9_lookahead_depth(cpi->lookahead) - 1;
  2059. for (frame = 0; frame < frames_to_buffer; ++frame) {
  2060. const int lagframe_idx = start_frame - frame;
  2061. if (lagframe_idx >= 0) {
  2062. struct lookahead_entry *buf =
  2063. vp9_lookahead_peek(cpi->lookahead, lagframe_idx);
  2064. frames[frame] = &buf->img;
  2065. }
  2066. }
  2067. // The avg_sad for this current frame is the value of frame#1
  2068. // (first future frame) from previous frame.
  2069. avg_sad_current = rc->avg_source_sad[1];
  2070. if (avg_sad_current >
  2071. VPXMAX(min_thresh,
  2072. (unsigned int)(rc->avg_source_sad[0] * thresh)) &&
  2073. cm->current_video_frame > (unsigned int)cpi->oxcf.lag_in_frames)
  2074. rc->high_source_sad = 1;
  2075. else
  2076. rc->high_source_sad = 0;
  2077. // Update recursive average for current frame.
  2078. if (avg_sad_current > 0)
  2079. rc->avg_source_sad[0] =
  2080. (3 * rc->avg_source_sad[0] + avg_sad_current) >> 2;
  2081. // Shift back data, starting at frame#1.
  2082. for (frame = 1; frame < cpi->oxcf.lag_in_frames - 1; ++frame)
  2083. rc->avg_source_sad[frame] = rc->avg_source_sad[frame + 1];
  2084. }
  2085. for (frame = 0; frame < frames_to_buffer; ++frame) {
  2086. if (cpi->oxcf.lag_in_frames == 0 ||
  2087. (frames[frame] != NULL && frames[frame + 1] != NULL &&
  2088. frames[frame]->y_width == frames[frame + 1]->y_width &&
  2089. frames[frame]->y_height == frames[frame + 1]->y_height)) {
  2090. int sbi_row, sbi_col;
  2091. const int lagframe_idx =
  2092. (cpi->oxcf.lag_in_frames == 0) ? 0 : start_frame - frame + 1;
  2093. const BLOCK_SIZE bsize = BLOCK_64X64;
  2094. // Loop over sub-sample of frame, compute average sad over 64x64 blocks.
  2095. uint64_t avg_sad = 0;
  2096. uint64_t tmp_sad = 0;
  2097. int num_samples = 0;
  2098. int sb_cols = (cm->mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
  2099. int sb_rows = (cm->mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE;
  2100. if (cpi->oxcf.lag_in_frames > 0) {
  2101. src_y = frames[frame]->y_buffer;
  2102. src_ystride = frames[frame]->y_stride;
  2103. last_src_y = frames[frame + 1]->y_buffer;
  2104. last_src_ystride = frames[frame + 1]->y_stride;
  2105. }
  2106. for (sbi_row = 0; sbi_row < sb_rows; ++sbi_row) {
  2107. for (sbi_col = 0; sbi_col < sb_cols; ++sbi_col) {
  2108. // Checker-board pattern, ignore boundary.
  2109. if (((sbi_row > 0 && sbi_col > 0) &&
  2110. (sbi_row < sb_rows - 1 && sbi_col < sb_cols - 1) &&
  2111. ((sbi_row % 2 == 0 && sbi_col % 2 == 0) ||
  2112. (sbi_row % 2 != 0 && sbi_col % 2 != 0)))) {
  2113. tmp_sad = cpi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y,
  2114. last_src_ystride);
  2115. avg_sad += tmp_sad;
  2116. num_samples++;
  2117. }
  2118. src_y += 64;
  2119. last_src_y += 64;
  2120. }
  2121. src_y += (src_ystride << 6) - (sb_cols << 6);
  2122. last_src_y += (last_src_ystride << 6) - (sb_cols << 6);
  2123. }
  2124. if (num_samples > 0) avg_sad = avg_sad / num_samples;
  2125. // Set high_source_sad flag if we detect very high increase in avg_sad
  2126. // between current and previous frame value(s). Use minimum threshold
  2127. // for cases where there is small change from content that is completely
  2128. // static.
  2129. if (lagframe_idx == 0) {
  2130. if (avg_sad >
  2131. VPXMAX(min_thresh,
  2132. (unsigned int)(rc->avg_source_sad[0] * thresh)) &&
  2133. rc->frames_since_key > 1)
  2134. rc->high_source_sad = 1;
  2135. else
  2136. rc->high_source_sad = 0;
  2137. if (avg_sad > 0 || cpi->oxcf.rc_mode == VPX_CBR)
  2138. rc->avg_source_sad[0] = (3 * rc->avg_source_sad[0] + avg_sad) >> 2;
  2139. } else {
  2140. rc->avg_source_sad[lagframe_idx] = avg_sad;
  2141. }
  2142. }
  2143. }
  2144. // For CBR non-screen content mode, check if we should reset the rate
  2145. // control. Reset is done if high_source_sad is detected and the rate
  2146. // control is at very low QP with rate correction factor at min level.
  2147. if (cpi->oxcf.rc_mode == VPX_CBR &&
  2148. cpi->oxcf.content != VP9E_CONTENT_SCREEN && !cpi->use_svc) {
  2149. if (rc->high_source_sad && rc->last_q[INTER_FRAME] == rc->best_quality &&
  2150. rc->avg_frame_qindex[INTER_FRAME] < (rc->best_quality << 1) &&
  2151. rc->rate_correction_factors[INTER_NORMAL] == MIN_BPB_FACTOR) {
  2152. rc->rate_correction_factors[INTER_NORMAL] = 0.5;
  2153. rc->avg_frame_qindex[INTER_FRAME] = rc->worst_quality;
  2154. rc->buffer_level = rc->optimal_buffer_level;
  2155. rc->bits_off_target = rc->optimal_buffer_level;
  2156. rc->reset_high_source_sad = 1;
  2157. }
  2158. if (cm->frame_type != KEY_FRAME && rc->reset_high_source_sad)
  2159. rc->this_frame_target = rc->avg_frame_bandwidth;
  2160. }
  2161. // For VBR, under scene change/high content change, force golden refresh.
  2162. if (cpi->oxcf.rc_mode == VPX_VBR && cm->frame_type != KEY_FRAME &&
  2163. rc->high_source_sad && rc->frames_to_key > 3 &&
  2164. rc->count_last_scene_change > 4 &&
  2165. cpi->ext_refresh_frame_flags_pending == 0) {
  2166. int target;
  2167. cpi->refresh_golden_frame = 1;
  2168. rc->source_alt_ref_pending = 0;
  2169. if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf)
  2170. rc->source_alt_ref_pending = 1;
  2171. rc->gfu_boost = DEFAULT_GF_BOOST >> 1;
  2172. rc->baseline_gf_interval =
  2173. VPXMIN(20, VPXMAX(10, rc->baseline_gf_interval));
  2174. adjust_gfint_frame_constraint(cpi, rc->frames_to_key);
  2175. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  2176. target = calc_pframe_target_size_one_pass_vbr(cpi);
  2177. vp9_rc_set_frame_target(cpi, target);
  2178. rc->count_last_scene_change = 0;
  2179. } else {
  2180. rc->count_last_scene_change++;
  2181. }
  2182. // If lag_in_frame is used, set the gf boost and interval.
  2183. if (cpi->oxcf.lag_in_frames > 0)
  2184. adjust_gf_boost_lag_one_pass_vbr(cpi, avg_sad_current);
  2185. }
  2186. }
  2187. // Test if encoded frame will significantly overshoot the target bitrate, and
  2188. // if so, set the QP, reset/adjust some rate control parameters, and return 1.
  2189. int vp9_encodedframe_overshoot(VP9_COMP *cpi, int frame_size, int *q) {
  2190. VP9_COMMON *const cm = &cpi->common;
  2191. RATE_CONTROL *const rc = &cpi->rc;
  2192. int thresh_qp = 3 * (rc->worst_quality >> 2);
  2193. int thresh_rate = rc->avg_frame_bandwidth * 10;
  2194. if (cm->base_qindex < thresh_qp && frame_size > thresh_rate) {
  2195. double rate_correction_factor =
  2196. cpi->rc.rate_correction_factors[INTER_NORMAL];
  2197. const int target_size = cpi->rc.avg_frame_bandwidth;
  2198. double new_correction_factor;
  2199. int target_bits_per_mb;
  2200. double q2;
  2201. int enumerator;
  2202. // Force a re-encode, and for now use max-QP.
  2203. *q = cpi->rc.worst_quality;
  2204. // Adjust avg_frame_qindex, buffer_level, and rate correction factors, as
  2205. // these parameters will affect QP selection for subsequent frames. If they
  2206. // have settled down to a very different (low QP) state, then not adjusting
  2207. // them may cause next frame to select low QP and overshoot again.
  2208. cpi->rc.avg_frame_qindex[INTER_FRAME] = *q;
  2209. rc->buffer_level = rc->optimal_buffer_level;
  2210. rc->bits_off_target = rc->optimal_buffer_level;
  2211. // Reset rate under/over-shoot flags.
  2212. cpi->rc.rc_1_frame = 0;
  2213. cpi->rc.rc_2_frame = 0;
  2214. // Adjust rate correction factor.
  2215. target_bits_per_mb =
  2216. (int)(((uint64_t)target_size << BPER_MB_NORMBITS) / cm->MBs);
  2217. // Rate correction factor based on target_bits_per_mb and qp (==max_QP).
  2218. // This comes from the inverse computation of vp9_rc_bits_per_mb().
  2219. q2 = vp9_convert_qindex_to_q(*q, cm->bit_depth);
  2220. enumerator = 1800000; // Factor for inter frame.
  2221. enumerator += (int)(enumerator * q2) >> 12;
  2222. new_correction_factor = (double)target_bits_per_mb * q2 / enumerator;
  2223. if (new_correction_factor > rate_correction_factor) {
  2224. rate_correction_factor =
  2225. VPXMIN(2.0 * rate_correction_factor, new_correction_factor);
  2226. if (rate_correction_factor > MAX_BPB_FACTOR)
  2227. rate_correction_factor = MAX_BPB_FACTOR;
  2228. cpi->rc.rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
  2229. }
  2230. // For temporal layers, reset the rate control parametes across all
  2231. // temporal layers.
  2232. if (cpi->use_svc) {
  2233. int i = 0;
  2234. SVC *svc = &cpi->svc;
  2235. for (i = 0; i < svc->number_temporal_layers; ++i) {
  2236. const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
  2237. svc->number_temporal_layers);
  2238. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  2239. RATE_CONTROL *lrc = &lc->rc;
  2240. lrc->avg_frame_qindex[INTER_FRAME] = *q;
  2241. lrc->buffer_level = rc->optimal_buffer_level;
  2242. lrc->bits_off_target = rc->optimal_buffer_level;
  2243. lrc->rc_1_frame = 0;
  2244. lrc->rc_2_frame = 0;
  2245. lrc->rate_correction_factors[INTER_NORMAL] = rate_correction_factor;
  2246. }
  2247. }
  2248. return 1;
  2249. } else {
  2250. return 0;
  2251. }
  2252. }