vp9_ratectrl.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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_mem/vpx_mem.h"
  17. #include "vpx_ports/mem.h"
  18. #include "vp9/common/vp9_alloccommon.h"
  19. #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
  20. #include "vp9/common/vp9_common.h"
  21. #include "vp9/common/vp9_entropymode.h"
  22. #include "vp9/common/vp9_quant_common.h"
  23. #include "vp9/common/vp9_seg_common.h"
  24. #include "vp9/common/vp9_systemdependent.h"
  25. #include "vp9/encoder/vp9_encodemv.h"
  26. #include "vp9/encoder/vp9_ratectrl.h"
  27. // Max rate target for 1080P and below encodes under normal circumstances
  28. // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
  29. #define MAX_MB_RATE 250
  30. #define MAXRATE_1080P 2025000
  31. #define DEFAULT_KF_BOOST 2000
  32. #define DEFAULT_GF_BOOST 2000
  33. #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
  34. #define MIN_BPB_FACTOR 0.005
  35. #define MAX_BPB_FACTOR 50
  36. #define FRAME_OVERHEAD_BITS 200
  37. #if CONFIG_VP9_HIGHBITDEPTH
  38. #define ASSIGN_MINQ_TABLE(bit_depth, name) \
  39. do { \
  40. switch (bit_depth) { \
  41. case VPX_BITS_8: \
  42. name = name##_8; \
  43. break; \
  44. case VPX_BITS_10: \
  45. name = name##_10; \
  46. break; \
  47. case VPX_BITS_12: \
  48. name = name##_12; \
  49. break; \
  50. default: \
  51. assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10" \
  52. " or VPX_BITS_12"); \
  53. name = NULL; \
  54. } \
  55. } while (0)
  56. #else
  57. #define ASSIGN_MINQ_TABLE(bit_depth, name) \
  58. do { \
  59. (void) bit_depth; \
  60. name = name##_8; \
  61. } while (0)
  62. #endif
  63. // Tables relating active max Q to active min Q
  64. static int kf_low_motion_minq_8[QINDEX_RANGE];
  65. static int kf_high_motion_minq_8[QINDEX_RANGE];
  66. static int arfgf_low_motion_minq_8[QINDEX_RANGE];
  67. static int arfgf_high_motion_minq_8[QINDEX_RANGE];
  68. static int inter_minq_8[QINDEX_RANGE];
  69. static int rtc_minq_8[QINDEX_RANGE];
  70. #if CONFIG_VP9_HIGHBITDEPTH
  71. static int kf_low_motion_minq_10[QINDEX_RANGE];
  72. static int kf_high_motion_minq_10[QINDEX_RANGE];
  73. static int arfgf_low_motion_minq_10[QINDEX_RANGE];
  74. static int arfgf_high_motion_minq_10[QINDEX_RANGE];
  75. static int inter_minq_10[QINDEX_RANGE];
  76. static int rtc_minq_10[QINDEX_RANGE];
  77. static int kf_low_motion_minq_12[QINDEX_RANGE];
  78. static int kf_high_motion_minq_12[QINDEX_RANGE];
  79. static int arfgf_low_motion_minq_12[QINDEX_RANGE];
  80. static int arfgf_high_motion_minq_12[QINDEX_RANGE];
  81. static int inter_minq_12[QINDEX_RANGE];
  82. static int rtc_minq_12[QINDEX_RANGE];
  83. #endif
  84. static int gf_high = 2000;
  85. static int gf_low = 400;
  86. static int kf_high = 5000;
  87. static int kf_low = 400;
  88. // Functions to compute the active minq lookup table entries based on a
  89. // formulaic approach to facilitate easier adjustment of the Q tables.
  90. // The formulae were derived from computing a 3rd order polynomial best
  91. // fit to the original data (after plotting real maxq vs minq (not q index))
  92. static int get_minq_index(double maxq, double x3, double x2, double x1,
  93. vpx_bit_depth_t bit_depth) {
  94. int i;
  95. const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq,
  96. maxq);
  97. // Special case handling to deal with the step from q2.0
  98. // down to lossless mode represented by q 1.0.
  99. if (minqtarget <= 2.0)
  100. return 0;
  101. for (i = 0; i < QINDEX_RANGE; i++) {
  102. if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth))
  103. return i;
  104. }
  105. return QINDEX_RANGE - 1;
  106. }
  107. static void init_minq_luts(int *kf_low_m, int *kf_high_m,
  108. int *arfgf_low, int *arfgf_high,
  109. int *inter, int *rtc, vpx_bit_depth_t bit_depth) {
  110. int i;
  111. for (i = 0; i < QINDEX_RANGE; i++) {
  112. const double maxq = vp9_convert_qindex_to_q(i, bit_depth);
  113. kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth);
  114. kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
  115. arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth);
  116. arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth);
  117. inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth);
  118. rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);
  119. }
  120. }
  121. void vp9_rc_init_minq_luts(void) {
  122. init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8,
  123. arfgf_low_motion_minq_8, arfgf_high_motion_minq_8,
  124. inter_minq_8, rtc_minq_8, VPX_BITS_8);
  125. #if CONFIG_VP9_HIGHBITDEPTH
  126. init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10,
  127. arfgf_low_motion_minq_10, arfgf_high_motion_minq_10,
  128. inter_minq_10, rtc_minq_10, VPX_BITS_10);
  129. init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12,
  130. arfgf_low_motion_minq_12, arfgf_high_motion_minq_12,
  131. inter_minq_12, rtc_minq_12, VPX_BITS_12);
  132. #endif
  133. }
  134. // These functions use formulaic calculations to make playing with the
  135. // quantizer tables easier. If necessary they can be replaced by lookup
  136. // tables if and when things settle down in the experimental bitstream
  137. double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth) {
  138. // Convert the index to a real Q value (scaled down to match old Q values)
  139. #if CONFIG_VP9_HIGHBITDEPTH
  140. switch (bit_depth) {
  141. case VPX_BITS_8:
  142. return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
  143. case VPX_BITS_10:
  144. return vp9_ac_quant(qindex, 0, bit_depth) / 16.0;
  145. case VPX_BITS_12:
  146. return vp9_ac_quant(qindex, 0, bit_depth) / 64.0;
  147. default:
  148. assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
  149. return -1.0;
  150. }
  151. #else
  152. return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;
  153. #endif
  154. }
  155. int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
  156. double correction_factor,
  157. vpx_bit_depth_t bit_depth) {
  158. const double q = vp9_convert_qindex_to_q(qindex, bit_depth);
  159. int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000;
  160. assert(correction_factor <= MAX_BPB_FACTOR &&
  161. correction_factor >= MIN_BPB_FACTOR);
  162. // q based adjustment to baseline enumerator
  163. enumerator += (int)(enumerator * q) >> 12;
  164. return (int)(enumerator * correction_factor / q);
  165. }
  166. int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs,
  167. double correction_factor,
  168. vpx_bit_depth_t bit_depth) {
  169. const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor,
  170. bit_depth));
  171. return MAX(FRAME_OVERHEAD_BITS,
  172. (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
  173. }
  174. int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
  175. const RATE_CONTROL *rc = &cpi->rc;
  176. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  177. const int min_frame_target = MAX(rc->min_frame_bandwidth,
  178. rc->avg_frame_bandwidth >> 5);
  179. if (target < min_frame_target)
  180. target = min_frame_target;
  181. if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
  182. // If there is an active ARF at this location use the minimum
  183. // bits on this frame even if it is a constructed arf.
  184. // The active maximum quantizer insures that an appropriate
  185. // number of bits will be spent if needed for constructed ARFs.
  186. target = min_frame_target;
  187. }
  188. // Clip the frame target to the maximum allowed value.
  189. if (target > rc->max_frame_bandwidth)
  190. target = rc->max_frame_bandwidth;
  191. if (oxcf->rc_max_inter_bitrate_pct) {
  192. const int max_rate = rc->avg_frame_bandwidth *
  193. oxcf->rc_max_inter_bitrate_pct / 100;
  194. target = MIN(target, max_rate);
  195. }
  196. return target;
  197. }
  198. int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
  199. const RATE_CONTROL *rc = &cpi->rc;
  200. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  201. if (oxcf->rc_max_intra_bitrate_pct) {
  202. const int max_rate = rc->avg_frame_bandwidth *
  203. oxcf->rc_max_intra_bitrate_pct / 100;
  204. target = MIN(target, max_rate);
  205. }
  206. if (target > rc->max_frame_bandwidth)
  207. target = rc->max_frame_bandwidth;
  208. return target;
  209. }
  210. // Update the buffer level for higher temporal layers, given the encoded current
  211. // temporal layer.
  212. static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) {
  213. int i = 0;
  214. int current_temporal_layer = svc->temporal_layer_id;
  215. for (i = current_temporal_layer + 1;
  216. i < svc->number_temporal_layers; ++i) {
  217. const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i,
  218. svc->number_temporal_layers);
  219. LAYER_CONTEXT *lc = &svc->layer_context[layer];
  220. RATE_CONTROL *lrc = &lc->rc;
  221. int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate -
  222. encoded_frame_size);
  223. lrc->bits_off_target += bits_off_for_this_layer;
  224. // Clip buffer level to maximum buffer size for the layer.
  225. lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size);
  226. lrc->buffer_level = lrc->bits_off_target;
  227. }
  228. }
  229. // Update the buffer level: leaky bucket model.
  230. static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
  231. const VP9_COMMON *const cm = &cpi->common;
  232. RATE_CONTROL *const rc = &cpi->rc;
  233. // Non-viewable frames are a special case and are treated as pure overhead.
  234. if (!cm->show_frame) {
  235. rc->bits_off_target -= encoded_frame_size;
  236. } else {
  237. rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size;
  238. }
  239. // Clip the buffer level to the maximum specified buffer size.
  240. rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
  241. rc->buffer_level = rc->bits_off_target;
  242. if (is_one_pass_cbr_svc(cpi)) {
  243. update_layer_buffer_level(&cpi->svc, encoded_frame_size);
  244. }
  245. }
  246. void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
  247. int i;
  248. if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
  249. rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
  250. rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
  251. } else {
  252. rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q +
  253. oxcf->best_allowed_q) / 2;
  254. rc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q +
  255. oxcf->best_allowed_q) / 2;
  256. }
  257. rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
  258. rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
  259. rc->buffer_level = rc->starting_buffer_level;
  260. rc->bits_off_target = rc->starting_buffer_level;
  261. rc->rolling_target_bits = rc->avg_frame_bandwidth;
  262. rc->rolling_actual_bits = rc->avg_frame_bandwidth;
  263. rc->long_rolling_target_bits = rc->avg_frame_bandwidth;
  264. rc->long_rolling_actual_bits = rc->avg_frame_bandwidth;
  265. rc->total_actual_bits = 0;
  266. rc->total_target_bits = 0;
  267. rc->total_target_vs_actual = 0;
  268. rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
  269. rc->frames_since_key = 8; // Sensible default for first frame.
  270. rc->this_key_frame_forced = 0;
  271. rc->next_key_frame_forced = 0;
  272. rc->source_alt_ref_pending = 0;
  273. rc->source_alt_ref_active = 0;
  274. rc->frames_till_gf_update_due = 0;
  275. rc->ni_av_qi = oxcf->worst_allowed_q;
  276. rc->ni_tot_qi = 0;
  277. rc->ni_frames = 0;
  278. rc->tot_q = 0.0;
  279. rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth);
  280. for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
  281. rc->rate_correction_factors[i] = 1.0;
  282. }
  283. }
  284. int vp9_rc_drop_frame(VP9_COMP *cpi) {
  285. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  286. RATE_CONTROL *const rc = &cpi->rc;
  287. if (!oxcf->drop_frames_water_mark) {
  288. return 0;
  289. } else {
  290. if (rc->buffer_level < 0) {
  291. // Always drop if buffer is below 0.
  292. return 1;
  293. } else {
  294. // If buffer is below drop_mark, for now just drop every other frame
  295. // (starting with the next frame) until it increases back over drop_mark.
  296. int drop_mark = (int)(oxcf->drop_frames_water_mark *
  297. rc->optimal_buffer_level / 100);
  298. if ((rc->buffer_level > drop_mark) &&
  299. (rc->decimation_factor > 0)) {
  300. --rc->decimation_factor;
  301. } else if (rc->buffer_level <= drop_mark &&
  302. rc->decimation_factor == 0) {
  303. rc->decimation_factor = 1;
  304. }
  305. if (rc->decimation_factor > 0) {
  306. if (rc->decimation_count > 0) {
  307. --rc->decimation_count;
  308. return 1;
  309. } else {
  310. rc->decimation_count = rc->decimation_factor;
  311. return 0;
  312. }
  313. } else {
  314. rc->decimation_count = 0;
  315. return 0;
  316. }
  317. }
  318. }
  319. }
  320. static double get_rate_correction_factor(const VP9_COMP *cpi) {
  321. const RATE_CONTROL *const rc = &cpi->rc;
  322. double rcf;
  323. if (cpi->common.frame_type == KEY_FRAME) {
  324. rcf = rc->rate_correction_factors[KF_STD];
  325. } else if (cpi->oxcf.pass == 2) {
  326. RATE_FACTOR_LEVEL rf_lvl =
  327. cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
  328. rcf = rc->rate_correction_factors[rf_lvl];
  329. } else {
  330. if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
  331. !rc->is_src_frame_alt_ref && !cpi->use_svc &&
  332. (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
  333. rcf = rc->rate_correction_factors[GF_ARF_STD];
  334. else
  335. rcf = rc->rate_correction_factors[INTER_NORMAL];
  336. }
  337. rcf *= rcf_mult[rc->frame_size_selector];
  338. return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
  339. }
  340. static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
  341. RATE_CONTROL *const rc = &cpi->rc;
  342. // Normalize RCF to account for the size-dependent scaling factor.
  343. factor /= rcf_mult[cpi->rc.frame_size_selector];
  344. factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR);
  345. if (cpi->common.frame_type == KEY_FRAME) {
  346. rc->rate_correction_factors[KF_STD] = factor;
  347. } else if (cpi->oxcf.pass == 2) {
  348. RATE_FACTOR_LEVEL rf_lvl =
  349. cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
  350. rc->rate_correction_factors[rf_lvl] = factor;
  351. } else {
  352. if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
  353. !rc->is_src_frame_alt_ref && !cpi->use_svc &&
  354. (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20))
  355. rc->rate_correction_factors[GF_ARF_STD] = factor;
  356. else
  357. rc->rate_correction_factors[INTER_NORMAL] = factor;
  358. }
  359. }
  360. void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi) {
  361. const VP9_COMMON *const cm = &cpi->common;
  362. int correction_factor = 100;
  363. double rate_correction_factor = get_rate_correction_factor(cpi);
  364. double adjustment_limit;
  365. int projected_size_based_on_q = 0;
  366. // Do not update the rate factors for arf overlay frames.
  367. if (cpi->rc.is_src_frame_alt_ref)
  368. return;
  369. // Clear down mmx registers to allow floating point in what follows
  370. vp9_clear_system_state();
  371. // Work out how big we would have expected the frame to be at this Q given
  372. // the current correction factor.
  373. // Stay in double to avoid int overflow when values are large
  374. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) {
  375. projected_size_based_on_q =
  376. vp9_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor);
  377. } else {
  378. projected_size_based_on_q = vp9_estimate_bits_at_q(cpi->common.frame_type,
  379. cm->base_qindex,
  380. cm->MBs,
  381. rate_correction_factor,
  382. cm->bit_depth);
  383. }
  384. // Work out a size correction factor.
  385. if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
  386. correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) /
  387. projected_size_based_on_q);
  388. // More heavily damped adjustment used if we have been oscillating either side
  389. // of target.
  390. adjustment_limit = 0.25 +
  391. 0.5 * MIN(1, fabs(log10(0.01 * correction_factor)));
  392. cpi->rc.q_2_frame = cpi->rc.q_1_frame;
  393. cpi->rc.q_1_frame = cm->base_qindex;
  394. cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
  395. if (correction_factor > 110)
  396. cpi->rc.rc_1_frame = -1;
  397. else if (correction_factor < 90)
  398. cpi->rc.rc_1_frame = 1;
  399. else
  400. cpi->rc.rc_1_frame = 0;
  401. if (correction_factor > 102) {
  402. // We are not already at the worst allowable quality
  403. correction_factor = (int)(100 + ((correction_factor - 100) *
  404. adjustment_limit));
  405. rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
  406. // Keep rate_correction_factor within limits
  407. if (rate_correction_factor > MAX_BPB_FACTOR)
  408. rate_correction_factor = MAX_BPB_FACTOR;
  409. } else if (correction_factor < 99) {
  410. // We are not already at the best allowable quality
  411. correction_factor = (int)(100 - ((100 - correction_factor) *
  412. adjustment_limit));
  413. rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
  414. // Keep rate_correction_factor within limits
  415. if (rate_correction_factor < MIN_BPB_FACTOR)
  416. rate_correction_factor = MIN_BPB_FACTOR;
  417. }
  418. set_rate_correction_factor(cpi, rate_correction_factor);
  419. }
  420. int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
  421. int active_best_quality, int active_worst_quality) {
  422. const VP9_COMMON *const cm = &cpi->common;
  423. int q = active_worst_quality;
  424. int last_error = INT_MAX;
  425. int i, target_bits_per_mb, bits_per_mb_at_this_q;
  426. const double correction_factor = get_rate_correction_factor(cpi);
  427. // Calculate required scaling factor based on target frame size and size of
  428. // frame produced using previous Q.
  429. target_bits_per_mb =
  430. ((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs;
  431. i = active_best_quality;
  432. do {
  433. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
  434. cm->seg.enabled &&
  435. cpi->svc.temporal_layer_id == 0 &&
  436. cpi->svc.spatial_layer_id == 0) {
  437. bits_per_mb_at_this_q =
  438. (int)vp9_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor);
  439. } else {
  440. bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cm->frame_type, i,
  441. correction_factor,
  442. cm->bit_depth);
  443. }
  444. if (bits_per_mb_at_this_q <= target_bits_per_mb) {
  445. if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error)
  446. q = i;
  447. else
  448. q = i - 1;
  449. break;
  450. } else {
  451. last_error = bits_per_mb_at_this_q - target_bits_per_mb;
  452. }
  453. } while (++i <= active_worst_quality);
  454. // In CBR mode, this makes sure q is between oscillating Qs to prevent
  455. // resonance.
  456. if (cpi->oxcf.rc_mode == VPX_CBR &&
  457. (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
  458. cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
  459. q = clamp(q, MIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
  460. MAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
  461. }
  462. return q;
  463. }
  464. static int get_active_quality(int q, int gfu_boost, int low, int high,
  465. int *low_motion_minq, int *high_motion_minq) {
  466. if (gfu_boost > high) {
  467. return low_motion_minq[q];
  468. } else if (gfu_boost < low) {
  469. return high_motion_minq[q];
  470. } else {
  471. const int gap = high - low;
  472. const int offset = high - gfu_boost;
  473. const int qdiff = high_motion_minq[q] - low_motion_minq[q];
  474. const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
  475. return low_motion_minq[q] + adjustment;
  476. }
  477. }
  478. static int get_kf_active_quality(const RATE_CONTROL *const rc, int q,
  479. vpx_bit_depth_t bit_depth) {
  480. int *kf_low_motion_minq;
  481. int *kf_high_motion_minq;
  482. ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq);
  483. ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq);
  484. return get_active_quality(q, rc->kf_boost, kf_low, kf_high,
  485. kf_low_motion_minq, kf_high_motion_minq);
  486. }
  487. static int get_gf_active_quality(const RATE_CONTROL *const rc, int q,
  488. vpx_bit_depth_t bit_depth) {
  489. int *arfgf_low_motion_minq;
  490. int *arfgf_high_motion_minq;
  491. ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq);
  492. ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq);
  493. return get_active_quality(q, rc->gfu_boost, gf_low, gf_high,
  494. arfgf_low_motion_minq, arfgf_high_motion_minq);
  495. }
  496. static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
  497. const RATE_CONTROL *const rc = &cpi->rc;
  498. const unsigned int curr_frame = cpi->common.current_video_frame;
  499. int active_worst_quality;
  500. if (cpi->common.frame_type == KEY_FRAME) {
  501. active_worst_quality = curr_frame == 0 ? rc->worst_quality
  502. : rc->last_q[KEY_FRAME] * 2;
  503. } else {
  504. if (!rc->is_src_frame_alt_ref &&
  505. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  506. active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4
  507. : rc->last_q[INTER_FRAME];
  508. } else {
  509. active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2
  510. : rc->last_q[INTER_FRAME] * 2;
  511. }
  512. }
  513. return MIN(active_worst_quality, rc->worst_quality);
  514. }
  515. // Adjust active_worst_quality level based on buffer level.
  516. static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
  517. // Adjust active_worst_quality: If buffer is above the optimal/target level,
  518. // bring active_worst_quality down depending on fullness of buffer.
  519. // If buffer is below the optimal level, let the active_worst_quality go from
  520. // ambient Q (at buffer = optimal level) to worst_quality level
  521. // (at buffer = critical level).
  522. const VP9_COMMON *const cm = &cpi->common;
  523. const RATE_CONTROL *rc = &cpi->rc;
  524. // Buffer level below which we push active_worst to worst_quality.
  525. int64_t critical_level = rc->optimal_buffer_level >> 3;
  526. int64_t buff_lvl_step = 0;
  527. int adjustment = 0;
  528. int active_worst_quality;
  529. int ambient_qp;
  530. if (cm->frame_type == KEY_FRAME)
  531. return rc->worst_quality;
  532. // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
  533. // for the first few frames following key frame. These are both initialized
  534. // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
  535. // So for first few frames following key, the qp of that key frame is weighted
  536. // into the active_worst_quality setting.
  537. ambient_qp = (cm->current_video_frame < 5) ?
  538. MIN(rc->avg_frame_qindex[INTER_FRAME], rc->avg_frame_qindex[KEY_FRAME]) :
  539. rc->avg_frame_qindex[INTER_FRAME];
  540. active_worst_quality = MIN(rc->worst_quality,
  541. ambient_qp * 5 / 4);
  542. if (rc->buffer_level > rc->optimal_buffer_level) {
  543. // Adjust down.
  544. // Maximum limit for down adjustment, ~30%.
  545. int max_adjustment_down = active_worst_quality / 3;
  546. if (max_adjustment_down) {
  547. buff_lvl_step = ((rc->maximum_buffer_size -
  548. rc->optimal_buffer_level) / max_adjustment_down);
  549. if (buff_lvl_step)
  550. adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
  551. buff_lvl_step);
  552. active_worst_quality -= adjustment;
  553. }
  554. } else if (rc->buffer_level > critical_level) {
  555. // Adjust up from ambient Q.
  556. if (critical_level) {
  557. buff_lvl_step = (rc->optimal_buffer_level - critical_level);
  558. if (buff_lvl_step) {
  559. adjustment = (int)((rc->worst_quality - ambient_qp) *
  560. (rc->optimal_buffer_level - rc->buffer_level) /
  561. buff_lvl_step);
  562. }
  563. active_worst_quality = ambient_qp + adjustment;
  564. }
  565. } else {
  566. // Set to worst_quality if buffer is below critical level.
  567. active_worst_quality = rc->worst_quality;
  568. }
  569. return active_worst_quality;
  570. }
  571. static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
  572. int *bottom_index,
  573. int *top_index) {
  574. const VP9_COMMON *const cm = &cpi->common;
  575. const RATE_CONTROL *const rc = &cpi->rc;
  576. int active_best_quality;
  577. int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
  578. int q;
  579. int *rtc_minq;
  580. ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq);
  581. if (frame_is_intra_only(cm)) {
  582. active_best_quality = rc->best_quality;
  583. // Handle the special case for key frames forced when we have reached
  584. // the maximum key frame interval. Here force the Q to a range
  585. // based on the ambient Q to reduce the risk of popping.
  586. if (rc->this_key_frame_forced) {
  587. int qindex = rc->last_boosted_qindex;
  588. double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  589. int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
  590. (last_boosted_q * 0.75),
  591. cm->bit_depth);
  592. active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
  593. } else if (cm->current_video_frame > 0) {
  594. // not first frame of one pass and kf_boost is set
  595. double q_adj_factor = 1.0;
  596. double q_val;
  597. active_best_quality =
  598. get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME],
  599. cm->bit_depth);
  600. // Allow somewhat lower kf minq with small image formats.
  601. if ((cm->width * cm->height) <= (352 * 288)) {
  602. q_adj_factor -= 0.25;
  603. }
  604. // Convert the adjustment factor to a qindex delta
  605. // on active_best_quality.
  606. q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
  607. active_best_quality += vp9_compute_qdelta(rc, q_val,
  608. q_val * q_adj_factor,
  609. cm->bit_depth);
  610. }
  611. } else if (!rc->is_src_frame_alt_ref &&
  612. !cpi->use_svc &&
  613. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  614. // Use the lower of active_worst_quality and recent
  615. // average Q as basis for GF/ARF best Q limit unless last frame was
  616. // a key frame.
  617. if (rc->frames_since_key > 1 &&
  618. rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
  619. q = rc->avg_frame_qindex[INTER_FRAME];
  620. } else {
  621. q = active_worst_quality;
  622. }
  623. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  624. } else {
  625. // Use the lower of active_worst_quality and recent/average Q.
  626. if (cm->current_video_frame > 1) {
  627. if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
  628. active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]];
  629. else
  630. active_best_quality = rtc_minq[active_worst_quality];
  631. } else {
  632. if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
  633. active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]];
  634. else
  635. active_best_quality = rtc_minq[active_worst_quality];
  636. }
  637. }
  638. // Clip the active best and worst quality values to limits
  639. active_best_quality = clamp(active_best_quality,
  640. rc->best_quality, rc->worst_quality);
  641. active_worst_quality = clamp(active_worst_quality,
  642. active_best_quality, rc->worst_quality);
  643. *top_index = active_worst_quality;
  644. *bottom_index = active_best_quality;
  645. #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
  646. // Limit Q range for the adaptive loop.
  647. if (cm->frame_type == KEY_FRAME &&
  648. !rc->this_key_frame_forced &&
  649. !(cm->current_video_frame == 0)) {
  650. int qdelta = 0;
  651. vp9_clear_system_state();
  652. qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
  653. active_worst_quality, 2.0,
  654. cm->bit_depth);
  655. *top_index = active_worst_quality + qdelta;
  656. *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
  657. }
  658. #endif
  659. // Special case code to try and match quality with forced key frames
  660. if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
  661. q = rc->last_boosted_qindex;
  662. } else {
  663. q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
  664. active_best_quality, active_worst_quality);
  665. if (q > *top_index) {
  666. // Special case when we are targeting the max allowed rate
  667. if (rc->this_frame_target >= rc->max_frame_bandwidth)
  668. *top_index = q;
  669. else
  670. q = *top_index;
  671. }
  672. }
  673. assert(*top_index <= rc->worst_quality &&
  674. *top_index >= rc->best_quality);
  675. assert(*bottom_index <= rc->worst_quality &&
  676. *bottom_index >= rc->best_quality);
  677. assert(q <= rc->worst_quality && q >= rc->best_quality);
  678. return q;
  679. }
  680. static int get_active_cq_level(const RATE_CONTROL *rc,
  681. const VP9EncoderConfig *const oxcf) {
  682. static const double cq_adjust_threshold = 0.1;
  683. int active_cq_level = oxcf->cq_level;
  684. if (oxcf->rc_mode == VPX_CQ &&
  685. rc->total_target_bits > 0) {
  686. const double x = (double)rc->total_actual_bits / rc->total_target_bits;
  687. if (x < cq_adjust_threshold) {
  688. active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold);
  689. }
  690. }
  691. return active_cq_level;
  692. }
  693. static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
  694. int *bottom_index,
  695. int *top_index) {
  696. const VP9_COMMON *const cm = &cpi->common;
  697. const RATE_CONTROL *const rc = &cpi->rc;
  698. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  699. const int cq_level = get_active_cq_level(rc, oxcf);
  700. int active_best_quality;
  701. int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
  702. int q;
  703. int *inter_minq;
  704. ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
  705. if (frame_is_intra_only(cm)) {
  706. // Handle the special case for key frames forced when we have reached
  707. // the maximum key frame interval. Here force the Q to a range
  708. // based on the ambient Q to reduce the risk of popping.
  709. if (rc->this_key_frame_forced) {
  710. int qindex = rc->last_boosted_qindex;
  711. double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  712. int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
  713. last_boosted_q * 0.75,
  714. cm->bit_depth);
  715. active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
  716. } else {
  717. // not first frame of one pass and kf_boost is set
  718. double q_adj_factor = 1.0;
  719. double q_val;
  720. active_best_quality =
  721. get_kf_active_quality(rc, rc->avg_frame_qindex[KEY_FRAME],
  722. cm->bit_depth);
  723. // Allow somewhat lower kf minq with small image formats.
  724. if ((cm->width * cm->height) <= (352 * 288)) {
  725. q_adj_factor -= 0.25;
  726. }
  727. // Convert the adjustment factor to a qindex delta
  728. // on active_best_quality.
  729. q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
  730. active_best_quality += vp9_compute_qdelta(rc, q_val,
  731. q_val * q_adj_factor,
  732. cm->bit_depth);
  733. }
  734. } else if (!rc->is_src_frame_alt_ref &&
  735. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  736. // Use the lower of active_worst_quality and recent
  737. // average Q as basis for GF/ARF best Q limit unless last frame was
  738. // a key frame.
  739. if (rc->frames_since_key > 1 &&
  740. rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
  741. q = rc->avg_frame_qindex[INTER_FRAME];
  742. } else {
  743. q = rc->avg_frame_qindex[KEY_FRAME];
  744. }
  745. // For constrained quality dont allow Q less than the cq level
  746. if (oxcf->rc_mode == VPX_CQ) {
  747. if (q < cq_level)
  748. q = cq_level;
  749. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  750. // Constrained quality use slightly lower active best.
  751. active_best_quality = active_best_quality * 15 / 16;
  752. } else if (oxcf->rc_mode == VPX_Q) {
  753. if (!cpi->refresh_alt_ref_frame) {
  754. active_best_quality = cq_level;
  755. } else {
  756. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  757. }
  758. } else {
  759. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  760. }
  761. } else {
  762. if (oxcf->rc_mode == VPX_Q) {
  763. active_best_quality = cq_level;
  764. } else {
  765. // Use the lower of active_worst_quality and recent/average Q.
  766. if (cm->current_video_frame > 1)
  767. active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
  768. else
  769. active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
  770. // For the constrained quality mode we don't want
  771. // q to fall below the cq level.
  772. if ((oxcf->rc_mode == VPX_CQ) &&
  773. (active_best_quality < cq_level)) {
  774. active_best_quality = cq_level;
  775. }
  776. }
  777. }
  778. // Clip the active best and worst quality values to limits
  779. active_best_quality = clamp(active_best_quality,
  780. rc->best_quality, rc->worst_quality);
  781. active_worst_quality = clamp(active_worst_quality,
  782. active_best_quality, rc->worst_quality);
  783. *top_index = active_worst_quality;
  784. *bottom_index = active_best_quality;
  785. #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
  786. {
  787. int qdelta = 0;
  788. vp9_clear_system_state();
  789. // Limit Q range for the adaptive loop.
  790. if (cm->frame_type == KEY_FRAME &&
  791. !rc->this_key_frame_forced &&
  792. !(cm->current_video_frame == 0)) {
  793. qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
  794. active_worst_quality, 2.0,
  795. cm->bit_depth);
  796. } else if (!rc->is_src_frame_alt_ref &&
  797. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  798. qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
  799. active_worst_quality, 1.75,
  800. cm->bit_depth);
  801. }
  802. *top_index = active_worst_quality + qdelta;
  803. *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
  804. }
  805. #endif
  806. if (oxcf->rc_mode == VPX_Q) {
  807. q = active_best_quality;
  808. // Special case code to try and match quality with forced key frames
  809. } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
  810. q = rc->last_boosted_qindex;
  811. } else {
  812. q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
  813. active_best_quality, active_worst_quality);
  814. if (q > *top_index) {
  815. // Special case when we are targeting the max allowed rate
  816. if (rc->this_frame_target >= rc->max_frame_bandwidth)
  817. *top_index = q;
  818. else
  819. q = *top_index;
  820. }
  821. }
  822. assert(*top_index <= rc->worst_quality &&
  823. *top_index >= rc->best_quality);
  824. assert(*bottom_index <= rc->worst_quality &&
  825. *bottom_index >= rc->best_quality);
  826. assert(q <= rc->worst_quality && q >= rc->best_quality);
  827. return q;
  828. }
  829. int vp9_frame_type_qdelta(const VP9_COMP *cpi, int rf_level, int q) {
  830. static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
  831. 1.00, // INTER_NORMAL
  832. 1.00, // INTER_HIGH
  833. 1.50, // GF_ARF_LOW
  834. 1.75, // GF_ARF_STD
  835. 2.00, // KF_STD
  836. };
  837. static const FRAME_TYPE frame_type[RATE_FACTOR_LEVELS] =
  838. {INTER_FRAME, INTER_FRAME, INTER_FRAME, INTER_FRAME, KEY_FRAME};
  839. const VP9_COMMON *const cm = &cpi->common;
  840. int qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, frame_type[rf_level],
  841. q, rate_factor_deltas[rf_level],
  842. cm->bit_depth);
  843. return qdelta;
  844. }
  845. #define STATIC_MOTION_THRESH 95
  846. static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi,
  847. int *bottom_index,
  848. int *top_index) {
  849. const VP9_COMMON *const cm = &cpi->common;
  850. const RATE_CONTROL *const rc = &cpi->rc;
  851. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  852. const GF_GROUP *gf_group = &cpi->twopass.gf_group;
  853. const int cq_level = get_active_cq_level(rc, oxcf);
  854. int active_best_quality;
  855. int active_worst_quality = cpi->twopass.active_worst_quality;
  856. int q;
  857. int *inter_minq;
  858. ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq);
  859. if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) {
  860. // Handle the special case for key frames forced when we have reached
  861. // the maximum key frame interval. Here force the Q to a range
  862. // based on the ambient Q to reduce the risk of popping.
  863. if (rc->this_key_frame_forced) {
  864. double last_boosted_q;
  865. int delta_qindex;
  866. int qindex;
  867. if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
  868. qindex = MIN(rc->last_kf_qindex, rc->last_boosted_qindex);
  869. active_best_quality = qindex;
  870. last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  871. delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
  872. last_boosted_q * 1.25,
  873. cm->bit_depth);
  874. active_worst_quality = MIN(qindex + delta_qindex, active_worst_quality);
  875. } else {
  876. qindex = rc->last_boosted_qindex;
  877. last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  878. delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
  879. last_boosted_q * 0.75,
  880. cm->bit_depth);
  881. active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
  882. }
  883. } else {
  884. // Not forced keyframe.
  885. double q_adj_factor = 1.0;
  886. double q_val;
  887. // Baseline value derived from cpi->active_worst_quality and kf boost.
  888. active_best_quality = get_kf_active_quality(rc, active_worst_quality,
  889. cm->bit_depth);
  890. // Allow somewhat lower kf minq with small image formats.
  891. if ((cm->width * cm->height) <= (352 * 288)) {
  892. q_adj_factor -= 0.25;
  893. }
  894. // Make a further adjustment based on the kf zero motion measure.
  895. q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
  896. // Convert the adjustment factor to a qindex delta
  897. // on active_best_quality.
  898. q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth);
  899. active_best_quality += vp9_compute_qdelta(rc, q_val,
  900. q_val * q_adj_factor,
  901. cm->bit_depth);
  902. }
  903. } else if (!rc->is_src_frame_alt_ref &&
  904. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
  905. // Use the lower of active_worst_quality and recent
  906. // average Q as basis for GF/ARF best Q limit unless last frame was
  907. // a key frame.
  908. if (rc->frames_since_key > 1 &&
  909. rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
  910. q = rc->avg_frame_qindex[INTER_FRAME];
  911. } else {
  912. q = active_worst_quality;
  913. }
  914. // For constrained quality dont allow Q less than the cq level
  915. if (oxcf->rc_mode == VPX_CQ) {
  916. if (q < cq_level)
  917. q = cq_level;
  918. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  919. // Constrained quality use slightly lower active best.
  920. active_best_quality = active_best_quality * 15 / 16;
  921. } else if (oxcf->rc_mode == VPX_Q) {
  922. if (!cpi->refresh_alt_ref_frame) {
  923. active_best_quality = cq_level;
  924. } else {
  925. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  926. // Modify best quality for second level arfs. For mode VPX_Q this
  927. // becomes the baseline frame q.
  928. if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
  929. active_best_quality = (active_best_quality + cq_level + 1) / 2;
  930. }
  931. } else {
  932. active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
  933. }
  934. } else {
  935. if (oxcf->rc_mode == VPX_Q) {
  936. active_best_quality = cq_level;
  937. } else {
  938. active_best_quality = inter_minq[active_worst_quality];
  939. // For the constrained quality mode we don't want
  940. // q to fall below the cq level.
  941. if ((oxcf->rc_mode == VPX_CQ) &&
  942. (active_best_quality < cq_level)) {
  943. active_best_quality = cq_level;
  944. }
  945. }
  946. }
  947. // Extension to max or min Q if undershoot or overshoot is outside
  948. // the permitted range.
  949. if ((cpi->oxcf.rc_mode != VPX_Q) &&
  950. (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) {
  951. if (frame_is_intra_only(cm) ||
  952. (!rc->is_src_frame_alt_ref &&
  953. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
  954. active_best_quality -=
  955. (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast);
  956. active_worst_quality += (cpi->twopass.extend_maxq / 2);
  957. } else {
  958. active_best_quality -=
  959. (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2;
  960. active_worst_quality += cpi->twopass.extend_maxq;
  961. }
  962. }
  963. #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
  964. vp9_clear_system_state();
  965. // Static forced key frames Q restrictions dealt with elsewhere.
  966. if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) ||
  967. !rc->this_key_frame_forced ||
  968. (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) {
  969. int qdelta = vp9_frame_type_qdelta(cpi, gf_group->rf_level[gf_group->index],
  970. active_worst_quality);
  971. active_worst_quality = MAX(active_worst_quality + qdelta,
  972. active_best_quality);
  973. }
  974. #endif
  975. // Modify active_best_quality for downscaled normal frames.
  976. if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) {
  977. int qdelta = vp9_compute_qdelta_by_rate(rc, cm->frame_type,
  978. active_best_quality, 2.0,
  979. cm->bit_depth);
  980. active_best_quality = MAX(active_best_quality + qdelta, rc->best_quality);
  981. }
  982. active_best_quality = clamp(active_best_quality,
  983. rc->best_quality, rc->worst_quality);
  984. active_worst_quality = clamp(active_worst_quality,
  985. active_best_quality, rc->worst_quality);
  986. if (oxcf->rc_mode == VPX_Q) {
  987. q = active_best_quality;
  988. // Special case code to try and match quality with forced key frames.
  989. } else if ((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) &&
  990. rc->this_key_frame_forced) {
  991. // If static since last kf use better of last boosted and last kf q.
  992. if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) {
  993. q = MIN(rc->last_kf_qindex, rc->last_boosted_qindex);
  994. } else {
  995. q = rc->last_boosted_qindex;
  996. }
  997. } else {
  998. q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
  999. active_best_quality, active_worst_quality);
  1000. if (q > active_worst_quality) {
  1001. // Special case when we are targeting the max allowed rate.
  1002. if (rc->this_frame_target >= rc->max_frame_bandwidth)
  1003. active_worst_quality = q;
  1004. else
  1005. q = active_worst_quality;
  1006. }
  1007. }
  1008. clamp(q, active_best_quality, active_worst_quality);
  1009. *top_index = active_worst_quality;
  1010. *bottom_index = active_best_quality;
  1011. assert(*top_index <= rc->worst_quality &&
  1012. *top_index >= rc->best_quality);
  1013. assert(*bottom_index <= rc->worst_quality &&
  1014. *bottom_index >= rc->best_quality);
  1015. assert(q <= rc->worst_quality && q >= rc->best_quality);
  1016. return q;
  1017. }
  1018. int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi,
  1019. int *bottom_index, int *top_index) {
  1020. int q;
  1021. if (cpi->oxcf.pass == 0) {
  1022. if (cpi->oxcf.rc_mode == VPX_CBR)
  1023. q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
  1024. else
  1025. q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
  1026. } else {
  1027. q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index);
  1028. }
  1029. if (cpi->sf.use_nonrd_pick_mode) {
  1030. if (cpi->sf.force_frame_boost == 1)
  1031. q -= cpi->sf.max_delta_qindex;
  1032. if (q < *bottom_index)
  1033. *bottom_index = q;
  1034. else if (q > *top_index)
  1035. *top_index = q;
  1036. }
  1037. return q;
  1038. }
  1039. void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi,
  1040. int frame_target,
  1041. int *frame_under_shoot_limit,
  1042. int *frame_over_shoot_limit) {
  1043. if (cpi->oxcf.rc_mode == VPX_Q) {
  1044. *frame_under_shoot_limit = 0;
  1045. *frame_over_shoot_limit = INT_MAX;
  1046. } else {
  1047. // For very small rate targets where the fractional adjustment
  1048. // may be tiny make sure there is at least a minimum range.
  1049. const int tolerance = (cpi->sf.recode_tolerance * frame_target) / 100;
  1050. *frame_under_shoot_limit = MAX(frame_target - tolerance - 200, 0);
  1051. *frame_over_shoot_limit = MIN(frame_target + tolerance + 200,
  1052. cpi->rc.max_frame_bandwidth);
  1053. }
  1054. }
  1055. void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) {
  1056. const VP9_COMMON *const cm = &cpi->common;
  1057. RATE_CONTROL *const rc = &cpi->rc;
  1058. rc->this_frame_target = target;
  1059. // Modify frame size target when down-scaling.
  1060. if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
  1061. rc->frame_size_selector != UNSCALED)
  1062. rc->this_frame_target = (int)(rc->this_frame_target
  1063. * rate_thresh_mult[rc->frame_size_selector]);
  1064. // Target rate per SB64 (including partial SB64s.
  1065. rc->sb64_target_rate = ((int64_t)rc->this_frame_target * 64 * 64) /
  1066. (cm->width * cm->height);
  1067. }
  1068. static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
  1069. // this frame refreshes means next frames don't unless specified by user
  1070. RATE_CONTROL *const rc = &cpi->rc;
  1071. rc->frames_since_golden = 0;
  1072. // Mark the alt ref as done (setting to 0 means no further alt refs pending).
  1073. rc->source_alt_ref_pending = 0;
  1074. // Set the alternate reference frame active flag
  1075. rc->source_alt_ref_active = 1;
  1076. }
  1077. static void update_golden_frame_stats(VP9_COMP *cpi) {
  1078. RATE_CONTROL *const rc = &cpi->rc;
  1079. // Update the Golden frame usage counts.
  1080. if (cpi->refresh_golden_frame) {
  1081. // this frame refreshes means next frames don't unless specified by user
  1082. rc->frames_since_golden = 0;
  1083. // If we are not using alt ref in the up and coming group clear the arf
  1084. // active flag.
  1085. if (!rc->source_alt_ref_pending) {
  1086. rc->source_alt_ref_active = 0;
  1087. }
  1088. // Decrement count down till next gf
  1089. if (rc->frames_till_gf_update_due > 0)
  1090. rc->frames_till_gf_update_due--;
  1091. } else if (!cpi->refresh_alt_ref_frame) {
  1092. // Decrement count down till next gf
  1093. if (rc->frames_till_gf_update_due > 0)
  1094. rc->frames_till_gf_update_due--;
  1095. rc->frames_since_golden++;
  1096. }
  1097. }
  1098. void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) {
  1099. const VP9_COMMON *const cm = &cpi->common;
  1100. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1101. RATE_CONTROL *const rc = &cpi->rc;
  1102. const int qindex = cm->base_qindex;
  1103. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
  1104. vp9_cyclic_refresh_postencode(cpi);
  1105. }
  1106. // Update rate control heuristics
  1107. rc->projected_frame_size = (int)(bytes_used << 3);
  1108. // Post encode loop adjustment of Q prediction.
  1109. vp9_rc_update_rate_correction_factors(cpi);
  1110. // Keep a record of last Q and ambient average Q.
  1111. if (cm->frame_type == KEY_FRAME) {
  1112. rc->last_q[KEY_FRAME] = qindex;
  1113. rc->avg_frame_qindex[KEY_FRAME] =
  1114. ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
  1115. } else {
  1116. if (rc->is_src_frame_alt_ref ||
  1117. !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) ||
  1118. (cpi->use_svc && oxcf->rc_mode == VPX_CBR)) {
  1119. rc->last_q[INTER_FRAME] = qindex;
  1120. rc->avg_frame_qindex[INTER_FRAME] =
  1121. ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2);
  1122. rc->ni_frames++;
  1123. rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth);
  1124. rc->avg_q = rc->tot_q / rc->ni_frames;
  1125. // Calculate the average Q for normal inter frames (not key or GFU
  1126. // frames).
  1127. rc->ni_tot_qi += qindex;
  1128. rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames;
  1129. }
  1130. }
  1131. // Keep record of last boosted (KF/KF/ARF) Q value.
  1132. // If the current frame is coded at a lower Q then we also update it.
  1133. // If all mbs in this group are skipped only update if the Q value is
  1134. // better than that already stored.
  1135. // This is used to help set quality in forced key frames to reduce popping
  1136. if ((qindex < rc->last_boosted_qindex) ||
  1137. (cm->frame_type == KEY_FRAME) ||
  1138. (!rc->constrained_gf_group &&
  1139. (cpi->refresh_alt_ref_frame ||
  1140. (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) {
  1141. rc->last_boosted_qindex = qindex;
  1142. }
  1143. if (cm->frame_type == KEY_FRAME)
  1144. rc->last_kf_qindex = qindex;
  1145. update_buffer_level(cpi, rc->projected_frame_size);
  1146. // Rolling monitors of whether we are over or underspending used to help
  1147. // regulate min and Max Q in two pass.
  1148. if (cm->frame_type != KEY_FRAME) {
  1149. rc->rolling_target_bits = ROUND_POWER_OF_TWO(
  1150. rc->rolling_target_bits * 3 + rc->this_frame_target, 2);
  1151. rc->rolling_actual_bits = ROUND_POWER_OF_TWO(
  1152. rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2);
  1153. rc->long_rolling_target_bits = ROUND_POWER_OF_TWO(
  1154. rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5);
  1155. rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO(
  1156. rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5);
  1157. }
  1158. // Actual bits spent
  1159. rc->total_actual_bits += rc->projected_frame_size;
  1160. rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0;
  1161. rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits;
  1162. if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame &&
  1163. (cm->frame_type != KEY_FRAME))
  1164. // Update the alternate reference frame stats as appropriate.
  1165. update_alt_ref_frame_stats(cpi);
  1166. else
  1167. // Update the Golden frame stats as appropriate.
  1168. update_golden_frame_stats(cpi);
  1169. if (cm->frame_type == KEY_FRAME)
  1170. rc->frames_since_key = 0;
  1171. if (cm->show_frame) {
  1172. rc->frames_since_key++;
  1173. rc->frames_to_key--;
  1174. }
  1175. // Trigger the resizing of the next frame if it is scaled.
  1176. cpi->resize_pending =
  1177. rc->next_frame_size_selector != rc->frame_size_selector;
  1178. rc->frame_size_selector = rc->next_frame_size_selector;
  1179. }
  1180. void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) {
  1181. // Update buffer level with zero size, update frame counters, and return.
  1182. update_buffer_level(cpi, 0);
  1183. cpi->rc.frames_since_key++;
  1184. cpi->rc.frames_to_key--;
  1185. cpi->rc.rc_2_frame = 0;
  1186. cpi->rc.rc_1_frame = 0;
  1187. }
  1188. // Use this macro to turn on/off use of alt-refs in one-pass mode.
  1189. #define USE_ALTREF_FOR_ONE_PASS 1
  1190. static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
  1191. static const int af_ratio = 10;
  1192. const RATE_CONTROL *const rc = &cpi->rc;
  1193. int target;
  1194. #if USE_ALTREF_FOR_ONE_PASS
  1195. target = (!rc->is_src_frame_alt_ref &&
  1196. (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) ?
  1197. (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
  1198. (rc->baseline_gf_interval + af_ratio - 1) :
  1199. (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
  1200. (rc->baseline_gf_interval + af_ratio - 1);
  1201. #else
  1202. target = rc->avg_frame_bandwidth;
  1203. #endif
  1204. return vp9_rc_clamp_pframe_target_size(cpi, target);
  1205. }
  1206. static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
  1207. static const int kf_ratio = 25;
  1208. const RATE_CONTROL *rc = &cpi->rc;
  1209. const int target = rc->avg_frame_bandwidth * kf_ratio;
  1210. return vp9_rc_clamp_iframe_target_size(cpi, target);
  1211. }
  1212. void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
  1213. VP9_COMMON *const cm = &cpi->common;
  1214. RATE_CONTROL *const rc = &cpi->rc;
  1215. int target;
  1216. // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
  1217. if (!cpi->refresh_alt_ref_frame &&
  1218. (cm->current_video_frame == 0 ||
  1219. (cpi->frame_flags & FRAMEFLAGS_KEY) ||
  1220. rc->frames_to_key == 0 ||
  1221. (cpi->oxcf.auto_key && 0))) {
  1222. cm->frame_type = KEY_FRAME;
  1223. rc->this_key_frame_forced = cm->current_video_frame != 0 &&
  1224. rc->frames_to_key == 0;
  1225. rc->frames_to_key = cpi->oxcf.key_freq;
  1226. rc->kf_boost = DEFAULT_KF_BOOST;
  1227. rc->source_alt_ref_active = 0;
  1228. } else {
  1229. cm->frame_type = INTER_FRAME;
  1230. }
  1231. if (rc->frames_till_gf_update_due == 0) {
  1232. rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
  1233. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  1234. // NOTE: frames_till_gf_update_due must be <= frames_to_key.
  1235. if (rc->frames_till_gf_update_due > rc->frames_to_key) {
  1236. rc->frames_till_gf_update_due = rc->frames_to_key;
  1237. rc->constrained_gf_group = 1;
  1238. } else {
  1239. rc->constrained_gf_group = 0;
  1240. }
  1241. cpi->refresh_golden_frame = 1;
  1242. rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS;
  1243. rc->gfu_boost = DEFAULT_GF_BOOST;
  1244. }
  1245. if (cm->frame_type == KEY_FRAME)
  1246. target = calc_iframe_target_size_one_pass_vbr(cpi);
  1247. else
  1248. target = calc_pframe_target_size_one_pass_vbr(cpi);
  1249. vp9_rc_set_frame_target(cpi, target);
  1250. }
  1251. static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
  1252. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  1253. const RATE_CONTROL *rc = &cpi->rc;
  1254. const SVC *const svc = &cpi->svc;
  1255. const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
  1256. const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
  1257. int min_frame_target = MAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
  1258. int target;
  1259. if (oxcf->gf_cbr_boost_pct) {
  1260. const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
  1261. target = cpi->refresh_golden_frame ?
  1262. (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio_pct) /
  1263. (rc->baseline_gf_interval * 100 + af_ratio_pct - 100) :
  1264. (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
  1265. (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
  1266. } else {
  1267. target = rc->avg_frame_bandwidth;
  1268. }
  1269. if (is_one_pass_cbr_svc(cpi)) {
  1270. // Note that for layers, avg_frame_bandwidth is the cumulative
  1271. // per-frame-bandwidth. For the target size of this frame, use the
  1272. // layer average frame size (i.e., non-cumulative per-frame-bw).
  1273. int layer =
  1274. LAYER_IDS_TO_IDX(svc->spatial_layer_id,
  1275. svc->temporal_layer_id, svc->number_temporal_layers);
  1276. const LAYER_CONTEXT *lc = &svc->layer_context[layer];
  1277. target = lc->avg_frame_size;
  1278. min_frame_target = MAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
  1279. }
  1280. if (diff > 0) {
  1281. // Lower the target bandwidth for this frame.
  1282. const int pct_low = (int)MIN(diff / one_pct_bits, oxcf->under_shoot_pct);
  1283. target -= (target * pct_low) / 200;
  1284. } else if (diff < 0) {
  1285. // Increase the target bandwidth for this frame.
  1286. const int pct_high = (int)MIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
  1287. target += (target * pct_high) / 200;
  1288. }
  1289. if (oxcf->rc_max_inter_bitrate_pct) {
  1290. const int max_rate = rc->avg_frame_bandwidth *
  1291. oxcf->rc_max_inter_bitrate_pct / 100;
  1292. target = MIN(target, max_rate);
  1293. }
  1294. return MAX(min_frame_target, target);
  1295. }
  1296. static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
  1297. const RATE_CONTROL *rc = &cpi->rc;
  1298. const VP9EncoderConfig *oxcf = &cpi->oxcf;
  1299. const SVC *const svc = &cpi->svc;
  1300. int target;
  1301. if (cpi->common.current_video_frame == 0) {
  1302. target = ((rc->starting_buffer_level / 2) > INT_MAX)
  1303. ? INT_MAX : (int)(rc->starting_buffer_level / 2);
  1304. } else {
  1305. int kf_boost = 32;
  1306. double framerate = cpi->framerate;
  1307. if (svc->number_temporal_layers > 1 &&
  1308. oxcf->rc_mode == VPX_CBR) {
  1309. // Use the layer framerate for temporal layers CBR mode.
  1310. const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id,
  1311. svc->temporal_layer_id, svc->number_temporal_layers);
  1312. const LAYER_CONTEXT *lc = &svc->layer_context[layer];
  1313. framerate = lc->framerate;
  1314. }
  1315. kf_boost = MAX(kf_boost, (int)(2 * framerate - 16));
  1316. if (rc->frames_since_key < framerate / 2) {
  1317. kf_boost = (int)(kf_boost * rc->frames_since_key /
  1318. (framerate / 2));
  1319. }
  1320. target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
  1321. }
  1322. return vp9_rc_clamp_iframe_target_size(cpi, target);
  1323. }
  1324. // Reset information needed to set proper reference frames and buffer updates
  1325. // for temporal layering. This is called when a key frame is encoded.
  1326. static void reset_temporal_layer_to_zero(VP9_COMP *cpi) {
  1327. int sl;
  1328. LAYER_CONTEXT *lc = NULL;
  1329. cpi->svc.temporal_layer_id = 0;
  1330. for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
  1331. lc = &cpi->svc.layer_context[sl * cpi->svc.number_temporal_layers];
  1332. lc->current_video_frame_in_layer = 0;
  1333. lc->frames_from_key_frame = 0;
  1334. }
  1335. }
  1336. void vp9_rc_get_svc_params(VP9_COMP *cpi) {
  1337. VP9_COMMON *const cm = &cpi->common;
  1338. RATE_CONTROL *const rc = &cpi->rc;
  1339. int target = rc->avg_frame_bandwidth;
  1340. const int layer = LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id,
  1341. cpi->svc.temporal_layer_id, cpi->svc.number_temporal_layers);
  1342. if ((cm->current_video_frame == 0) ||
  1343. (cpi->frame_flags & FRAMEFLAGS_KEY) ||
  1344. (cpi->oxcf.auto_key && (rc->frames_since_key %
  1345. cpi->oxcf.key_freq == 0))) {
  1346. cm->frame_type = KEY_FRAME;
  1347. rc->source_alt_ref_active = 0;
  1348. if (is_two_pass_svc(cpi)) {
  1349. cpi->svc.layer_context[layer].is_key_frame = 1;
  1350. cpi->ref_frame_flags &=
  1351. (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
  1352. } else if (is_one_pass_cbr_svc(cpi)) {
  1353. cpi->svc.layer_context[layer].is_key_frame = 1;
  1354. reset_temporal_layer_to_zero(cpi);
  1355. cpi->ref_frame_flags &=
  1356. (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
  1357. // Assumption here is that LAST_FRAME is being updated for a keyframe.
  1358. // Thus no change in update flags.
  1359. target = calc_iframe_target_size_one_pass_cbr(cpi);
  1360. }
  1361. } else {
  1362. cm->frame_type = INTER_FRAME;
  1363. if (is_two_pass_svc(cpi)) {
  1364. LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
  1365. if (cpi->svc.spatial_layer_id == 0) {
  1366. lc->is_key_frame = 0;
  1367. } else {
  1368. lc->is_key_frame =
  1369. cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame;
  1370. if (lc->is_key_frame)
  1371. cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
  1372. }
  1373. cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
  1374. } else if (is_one_pass_cbr_svc(cpi)) {
  1375. LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
  1376. if (cpi->svc.spatial_layer_id == 0) {
  1377. lc->is_key_frame = 0;
  1378. } else {
  1379. lc->is_key_frame =
  1380. cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame;
  1381. }
  1382. target = calc_pframe_target_size_one_pass_cbr(cpi);
  1383. }
  1384. }
  1385. // Any update/change of global cyclic refresh parameters (amount/delta-qp)
  1386. // should be done here, before the frame qp is selected.
  1387. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
  1388. vp9_cyclic_refresh_update_parameters(cpi);
  1389. vp9_rc_set_frame_target(cpi, target);
  1390. rc->frames_till_gf_update_due = INT_MAX;
  1391. rc->baseline_gf_interval = INT_MAX;
  1392. }
  1393. void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
  1394. VP9_COMMON *const cm = &cpi->common;
  1395. RATE_CONTROL *const rc = &cpi->rc;
  1396. int target;
  1397. // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
  1398. if ((cm->current_video_frame == 0 ||
  1399. (cpi->frame_flags & FRAMEFLAGS_KEY) ||
  1400. rc->frames_to_key == 0 ||
  1401. (cpi->oxcf.auto_key && 0))) {
  1402. cm->frame_type = KEY_FRAME;
  1403. rc->this_key_frame_forced = cm->current_video_frame != 0 &&
  1404. rc->frames_to_key == 0;
  1405. rc->frames_to_key = cpi->oxcf.key_freq;
  1406. rc->kf_boost = DEFAULT_KF_BOOST;
  1407. rc->source_alt_ref_active = 0;
  1408. } else {
  1409. cm->frame_type = INTER_FRAME;
  1410. }
  1411. if (rc->frames_till_gf_update_due == 0) {
  1412. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
  1413. vp9_cyclic_refresh_set_golden_update(cpi);
  1414. else
  1415. rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
  1416. rc->frames_till_gf_update_due = rc->baseline_gf_interval;
  1417. // NOTE: frames_till_gf_update_due must be <= frames_to_key.
  1418. if (rc->frames_till_gf_update_due > rc->frames_to_key)
  1419. rc->frames_till_gf_update_due = rc->frames_to_key;
  1420. cpi->refresh_golden_frame = 1;
  1421. rc->gfu_boost = DEFAULT_GF_BOOST;
  1422. }
  1423. // Any update/change of global cyclic refresh parameters (amount/delta-qp)
  1424. // should be done here, before the frame qp is selected.
  1425. if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
  1426. vp9_cyclic_refresh_update_parameters(cpi);
  1427. if (cm->frame_type == KEY_FRAME)
  1428. target = calc_iframe_target_size_one_pass_cbr(cpi);
  1429. else
  1430. target = calc_pframe_target_size_one_pass_cbr(cpi);
  1431. vp9_rc_set_frame_target(cpi, target);
  1432. }
  1433. int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
  1434. vpx_bit_depth_t bit_depth) {
  1435. int start_index = rc->worst_quality;
  1436. int target_index = rc->worst_quality;
  1437. int i;
  1438. // Convert the average q value to an index.
  1439. for (i = rc->best_quality; i < rc->worst_quality; ++i) {
  1440. start_index = i;
  1441. if (vp9_convert_qindex_to_q(i, bit_depth) >= qstart)
  1442. break;
  1443. }
  1444. // Convert the q target to an index
  1445. for (i = rc->best_quality; i < rc->worst_quality; ++i) {
  1446. target_index = i;
  1447. if (vp9_convert_qindex_to_q(i, bit_depth) >= qtarget)
  1448. break;
  1449. }
  1450. return target_index - start_index;
  1451. }
  1452. int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
  1453. int qindex, double rate_target_ratio,
  1454. vpx_bit_depth_t bit_depth) {
  1455. int target_index = rc->worst_quality;
  1456. int i;
  1457. // Look up the current projected bits per block for the base index
  1458. const int base_bits_per_mb = vp9_rc_bits_per_mb(frame_type, qindex, 1.0,
  1459. bit_depth);
  1460. // Find the target bits per mb based on the base value and given ratio.
  1461. const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb);
  1462. // Convert the q target to an index
  1463. for (i = rc->best_quality; i < rc->worst_quality; ++i) {
  1464. if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <=
  1465. target_bits_per_mb) {
  1466. target_index = i;
  1467. break;
  1468. }
  1469. }
  1470. return target_index - qindex;
  1471. }
  1472. #define MIN_GF_INTERVAL 4
  1473. #define MAX_GF_INTERVAL 16
  1474. void vp9_rc_set_gf_interval_range(const VP9_COMP *const cpi,
  1475. RATE_CONTROL *const rc) {
  1476. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1477. // Set a minimum interval.
  1478. rc->min_gf_interval =
  1479. MIN(MAX_GF_INTERVAL, MAX(MIN_GF_INTERVAL, (int)(cpi->framerate * 0.125)));
  1480. // Set Maximum gf/arf interval.
  1481. rc->max_gf_interval =
  1482. MIN(MAX_GF_INTERVAL, (int)(cpi->framerate * 0.75));
  1483. // Round up to next even number if odd.
  1484. rc->max_gf_interval += (rc->max_gf_interval & 0x01);
  1485. // Extended interval for genuinely static scenes
  1486. rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2;
  1487. if (is_altref_enabled(cpi)) {
  1488. if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
  1489. rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
  1490. }
  1491. if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
  1492. rc->max_gf_interval = rc->static_scene_max_gf_interval;
  1493. // Clamp min to max
  1494. rc->min_gf_interval = MIN(rc->min_gf_interval, rc->max_gf_interval);
  1495. }
  1496. void vp9_rc_update_framerate(VP9_COMP *cpi) {
  1497. const VP9_COMMON *const cm = &cpi->common;
  1498. const VP9EncoderConfig *const oxcf = &cpi->oxcf;
  1499. RATE_CONTROL *const rc = &cpi->rc;
  1500. int vbr_max_bits;
  1501. rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate);
  1502. rc->min_frame_bandwidth = (int)(rc->avg_frame_bandwidth *
  1503. oxcf->two_pass_vbrmin_section / 100);
  1504. rc->min_frame_bandwidth = MAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
  1505. // A maximum bitrate for a frame is defined.
  1506. // The baseline for this aligns with HW implementations that
  1507. // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
  1508. // per 16x16 MB (averaged over a frame). However this limit is extended if
  1509. // a very high rate is given on the command line or the the rate cannnot
  1510. // be acheived because of a user specificed max q (e.g. when the user
  1511. // specifies lossless encode.
  1512. vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth *
  1513. oxcf->two_pass_vbrmax_section) / 100);
  1514. rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P),
  1515. vbr_max_bits);
  1516. vp9_rc_set_gf_interval_range(cpi, rc);
  1517. }
  1518. #define VBR_PCT_ADJUSTMENT_LIMIT 50
  1519. // For VBR...adjustment to the frame target based on error from previous frames
  1520. static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) {
  1521. RATE_CONTROL *const rc = &cpi->rc;
  1522. int64_t vbr_bits_off_target = rc->vbr_bits_off_target;
  1523. int max_delta;
  1524. double position_factor = 1.0;
  1525. // How far through the clip are we.
  1526. // This number is used to damp the per frame rate correction.
  1527. // Range 0 - 1.0
  1528. if (cpi->twopass.total_stats.count) {
  1529. position_factor = sqrt((double)cpi->common.current_video_frame /
  1530. cpi->twopass.total_stats.count);
  1531. }
  1532. max_delta = (int)(position_factor *
  1533. ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
  1534. // vbr_bits_off_target > 0 means we have extra bits to spend
  1535. if (vbr_bits_off_target > 0) {
  1536. *this_frame_target +=
  1537. (vbr_bits_off_target > max_delta) ? max_delta
  1538. : (int)vbr_bits_off_target;
  1539. } else {
  1540. *this_frame_target -=
  1541. (vbr_bits_off_target < -max_delta) ? max_delta
  1542. : (int)-vbr_bits_off_target;
  1543. }
  1544. // Fast redistribution of bits arising from massive local undershoot.
  1545. // Dont do it for kf,arf,gf or overlay frames.
  1546. if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref &&
  1547. rc->vbr_bits_off_target_fast) {
  1548. int one_frame_bits = MAX(rc->avg_frame_bandwidth, *this_frame_target);
  1549. int fast_extra_bits;
  1550. fast_extra_bits =
  1551. (int)MIN(rc->vbr_bits_off_target_fast, one_frame_bits);
  1552. fast_extra_bits = (int)MIN(fast_extra_bits,
  1553. MAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8));
  1554. *this_frame_target += (int)fast_extra_bits;
  1555. rc->vbr_bits_off_target_fast -= fast_extra_bits;
  1556. }
  1557. }
  1558. void vp9_set_target_rate(VP9_COMP *cpi) {
  1559. RATE_CONTROL *const rc = &cpi->rc;
  1560. int target_rate = rc->base_frame_target;
  1561. // Correction to rate target based on prior over or under shoot.
  1562. if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ)
  1563. vbr_rate_correction(cpi, &target_rate);
  1564. vp9_rc_set_frame_target(cpi, target_rate);
  1565. }