2
0

ratectrl.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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 <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <limits.h>
  14. #include <assert.h>
  15. #include "math.h"
  16. #include "vp8/common/common.h"
  17. #include "ratectrl.h"
  18. #include "vp8/common/entropymode.h"
  19. #include "vpx_mem/vpx_mem.h"
  20. #include "vp8/common/systemdependent.h"
  21. #include "encodemv.h"
  22. #include "vpx_dsp/vpx_dsp_common.h"
  23. #define MIN_BPB_FACTOR 0.01
  24. #define MAX_BPB_FACTOR 50
  25. extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
  26. #ifdef MODE_STATS
  27. extern int y_modes[5];
  28. extern int uv_modes[4];
  29. extern int b_modes[10];
  30. extern int inter_y_modes[10];
  31. extern int inter_uv_modes[4];
  32. extern int inter_b_modes[10];
  33. #endif
  34. /* Bits Per MB at different Q (Multiplied by 512) */
  35. #define BPER_MB_NORMBITS 9
  36. /* Work in progress recalibration of baseline rate tables based on
  37. * the assumption that bits per mb is inversely proportional to the
  38. * quantizer value.
  39. */
  40. const int vp8_bits_per_mb[2][QINDEX_RANGE] = {
  41. /* Intra case 450000/Qintra */
  42. {
  43. 1125000, 900000, 750000, 642857, 562500, 500000, 450000, 450000, 409090,
  44. 375000, 346153, 321428, 300000, 281250, 264705, 264705, 250000, 236842,
  45. 225000, 225000, 214285, 214285, 204545, 204545, 195652, 195652, 187500,
  46. 180000, 180000, 173076, 166666, 160714, 155172, 150000, 145161, 140625,
  47. 136363, 132352, 128571, 125000, 121621, 121621, 118421, 115384, 112500,
  48. 109756, 107142, 104651, 102272, 100000, 97826, 97826, 95744, 93750,
  49. 91836, 90000, 88235, 86538, 84905, 83333, 81818, 80357, 78947,
  50. 77586, 76271, 75000, 73770, 72580, 71428, 70312, 69230, 68181,
  51. 67164, 66176, 65217, 64285, 63380, 62500, 61643, 60810, 60000,
  52. 59210, 59210, 58441, 57692, 56962, 56250, 55555, 54878, 54216,
  53. 53571, 52941, 52325, 51724, 51136, 50561, 49450, 48387, 47368,
  54. 46875, 45918, 45000, 44554, 44117, 43269, 42452, 41666, 40909,
  55. 40178, 39473, 38793, 38135, 36885, 36290, 35714, 35156, 34615,
  56. 34090, 33582, 33088, 32608, 32142, 31468, 31034, 30405, 29801,
  57. 29220, 28662,
  58. },
  59. /* Inter case 285000/Qinter */
  60. {
  61. 712500, 570000, 475000, 407142, 356250, 316666, 285000, 259090, 237500,
  62. 219230, 203571, 190000, 178125, 167647, 158333, 150000, 142500, 135714,
  63. 129545, 123913, 118750, 114000, 109615, 105555, 101785, 98275, 95000,
  64. 91935, 89062, 86363, 83823, 81428, 79166, 77027, 75000, 73076,
  65. 71250, 69512, 67857, 66279, 64772, 63333, 61956, 60638, 59375,
  66. 58163, 57000, 55882, 54807, 53773, 52777, 51818, 50892, 50000,
  67. 49137, 47500, 45967, 44531, 43181, 41911, 40714, 39583, 38513,
  68. 37500, 36538, 35625, 34756, 33928, 33139, 32386, 31666, 30978,
  69. 30319, 29687, 29081, 28500, 27941, 27403, 26886, 26388, 25909,
  70. 25446, 25000, 24568, 23949, 23360, 22800, 22265, 21755, 21268,
  71. 20802, 20357, 19930, 19520, 19127, 18750, 18387, 18037, 17701,
  72. 17378, 17065, 16764, 16473, 16101, 15745, 15405, 15079, 14766,
  73. 14467, 14179, 13902, 13636, 13380, 13133, 12895, 12666, 12445,
  74. 12179, 11924, 11632, 11445, 11220, 11003, 10795, 10594, 10401,
  75. 10215, 10035,
  76. }
  77. };
  78. static const int kf_boost_qadjustment[QINDEX_RANGE] = {
  79. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
  80. 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,
  81. 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
  82. 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187,
  83. 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 200, 201,
  84. 201, 202, 203, 203, 203, 204, 204, 205, 205, 206, 206, 207, 207, 208, 208,
  85. 209, 209, 210, 210, 211, 211, 212, 212, 213, 213, 214, 214, 215, 215, 216,
  86. 216, 217, 217, 218, 218, 219, 219, 220, 220, 220, 220, 220, 220, 220, 220,
  87. 220, 220, 220, 220, 220, 220, 220, 220,
  88. };
  89. /* #define GFQ_ADJUSTMENT (Q+100) */
  90. #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
  91. const int vp8_gf_boost_qadjustment[QINDEX_RANGE] = {
  92. 80, 82, 84, 86, 88, 90, 92, 94, 96, 97, 98, 99, 100, 101, 102,
  93. 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
  94. 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132,
  95. 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
  96. 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162,
  97. 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
  98. 178, 179, 180, 181, 182, 183, 184, 184, 185, 185, 186, 186, 187, 187, 188,
  99. 188, 189, 189, 190, 190, 191, 191, 192, 192, 193, 193, 194, 194, 194, 194,
  100. 195, 195, 196, 196, 197, 197, 198, 198
  101. };
  102. /*
  103. const int vp8_gf_boost_qadjustment[QINDEX_RANGE] =
  104. {
  105. 100,101,102,103,104,105,105,106,
  106. 106,107,107,108,109,109,110,111,
  107. 112,113,114,115,116,117,118,119,
  108. 120,121,122,123,124,125,126,127,
  109. 128,129,130,131,132,133,134,135,
  110. 136,137,138,139,140,141,142,143,
  111. 144,145,146,147,148,149,150,151,
  112. 152,153,154,155,156,157,158,159,
  113. 160,161,162,163,164,165,166,167,
  114. 168,169,170,170,171,171,172,172,
  115. 173,173,173,174,174,174,175,175,
  116. 175,176,176,176,177,177,177,177,
  117. 178,178,179,179,180,180,181,181,
  118. 182,182,183,183,184,184,185,185,
  119. 186,186,187,187,188,188,189,189,
  120. 190,190,191,191,192,192,193,193,
  121. };
  122. */
  123. static const int kf_gf_boost_qlimits[QINDEX_RANGE] = {
  124. 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220,
  125. 225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295,
  126. 300, 305, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 410, 420, 430,
  127. 440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560, 570, 580,
  128. 590, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600,
  129. 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600,
  130. 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600,
  131. 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600, 600,
  132. 600, 600, 600, 600, 600, 600, 600, 600,
  133. };
  134. static const int gf_adjust_table[101] = {
  135. 100, 115, 130, 145, 160, 175, 190, 200, 210, 220, 230, 240, 260, 270, 280,
  136. 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 400, 400, 400,
  137. 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
  138. 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
  139. 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
  140. 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
  141. 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
  142. };
  143. static const int gf_intra_usage_adjustment[20] = {
  144. 125, 120, 115, 110, 105, 100, 95, 85, 80, 75,
  145. 70, 65, 60, 55, 50, 50, 50, 50, 50, 50,
  146. };
  147. static const int gf_interval_table[101] = {
  148. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  149. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8,
  150. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  151. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  152. 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  153. 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  154. };
  155. static const unsigned int prior_key_frame_weight[KEY_FRAME_CONTEXT] = { 1, 2, 3,
  156. 4, 5 };
  157. void vp8_save_coding_context(VP8_COMP *cpi) {
  158. CODING_CONTEXT *const cc = &cpi->coding_context;
  159. /* Stores a snapshot of key state variables which can subsequently be
  160. * restored with a call to vp8_restore_coding_context. These functions are
  161. * intended for use in a re-code loop in vp8_compress_frame where the
  162. * quantizer value is adjusted between loop iterations.
  163. */
  164. cc->frames_since_key = cpi->frames_since_key;
  165. cc->filter_level = cpi->common.filter_level;
  166. cc->frames_till_gf_update_due = cpi->frames_till_gf_update_due;
  167. cc->frames_since_golden = cpi->frames_since_golden;
  168. vp8_copy(cc->mvc, cpi->common.fc.mvc);
  169. vp8_copy(cc->mvcosts, cpi->rd_costs.mvcosts);
  170. vp8_copy(cc->ymode_prob, cpi->common.fc.ymode_prob);
  171. vp8_copy(cc->uv_mode_prob, cpi->common.fc.uv_mode_prob);
  172. vp8_copy(cc->ymode_count, cpi->mb.ymode_count);
  173. vp8_copy(cc->uv_mode_count, cpi->mb.uv_mode_count);
  174. /* Stats */
  175. #ifdef MODE_STATS
  176. vp8_copy(cc->y_modes, y_modes);
  177. vp8_copy(cc->uv_modes, uv_modes);
  178. vp8_copy(cc->b_modes, b_modes);
  179. vp8_copy(cc->inter_y_modes, inter_y_modes);
  180. vp8_copy(cc->inter_uv_modes, inter_uv_modes);
  181. vp8_copy(cc->inter_b_modes, inter_b_modes);
  182. #endif
  183. cc->this_frame_percent_intra = cpi->this_frame_percent_intra;
  184. }
  185. void vp8_restore_coding_context(VP8_COMP *cpi) {
  186. CODING_CONTEXT *const cc = &cpi->coding_context;
  187. /* Restore key state variables to the snapshot state stored in the
  188. * previous call to vp8_save_coding_context.
  189. */
  190. cpi->frames_since_key = cc->frames_since_key;
  191. cpi->common.filter_level = cc->filter_level;
  192. cpi->frames_till_gf_update_due = cc->frames_till_gf_update_due;
  193. cpi->frames_since_golden = cc->frames_since_golden;
  194. vp8_copy(cpi->common.fc.mvc, cc->mvc);
  195. vp8_copy(cpi->rd_costs.mvcosts, cc->mvcosts);
  196. vp8_copy(cpi->common.fc.ymode_prob, cc->ymode_prob);
  197. vp8_copy(cpi->common.fc.uv_mode_prob, cc->uv_mode_prob);
  198. vp8_copy(cpi->mb.ymode_count, cc->ymode_count);
  199. vp8_copy(cpi->mb.uv_mode_count, cc->uv_mode_count);
  200. /* Stats */
  201. #ifdef MODE_STATS
  202. vp8_copy(y_modes, cc->y_modes);
  203. vp8_copy(uv_modes, cc->uv_modes);
  204. vp8_copy(b_modes, cc->b_modes);
  205. vp8_copy(inter_y_modes, cc->inter_y_modes);
  206. vp8_copy(inter_uv_modes, cc->inter_uv_modes);
  207. vp8_copy(inter_b_modes, cc->inter_b_modes);
  208. #endif
  209. cpi->this_frame_percent_intra = cc->this_frame_percent_intra;
  210. }
  211. void vp8_setup_key_frame(VP8_COMP *cpi) {
  212. /* Setup for Key frame: */
  213. vp8_default_coef_probs(&cpi->common);
  214. memcpy(cpi->common.fc.mvc, vp8_default_mv_context,
  215. sizeof(vp8_default_mv_context));
  216. {
  217. int flag[2] = { 1, 1 };
  218. vp8_build_component_cost_table(
  219. cpi->mb.mvcost, (const MV_CONTEXT *)cpi->common.fc.mvc, flag);
  220. }
  221. /* Make sure we initialize separate contexts for altref,gold, and normal.
  222. * TODO shouldn't need 3 different copies of structure to do this!
  223. */
  224. memcpy(&cpi->lfc_a, &cpi->common.fc, sizeof(cpi->common.fc));
  225. memcpy(&cpi->lfc_g, &cpi->common.fc, sizeof(cpi->common.fc));
  226. memcpy(&cpi->lfc_n, &cpi->common.fc, sizeof(cpi->common.fc));
  227. cpi->common.filter_level = cpi->common.base_qindex * 3 / 8;
  228. /* Provisional interval before next GF */
  229. if (cpi->auto_gold) {
  230. cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
  231. } else {
  232. cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
  233. }
  234. cpi->common.refresh_golden_frame = 1;
  235. cpi->common.refresh_alt_ref_frame = 1;
  236. }
  237. static int estimate_bits_at_q(int frame_kind, int Q, int MBs,
  238. double correction_factor) {
  239. int Bpm = (int)(.5 + correction_factor * vp8_bits_per_mb[frame_kind][Q]);
  240. /* Attempt to retain reasonable accuracy without overflow. The cutoff is
  241. * chosen such that the maximum product of Bpm and MBs fits 31 bits. The
  242. * largest Bpm takes 20 bits.
  243. */
  244. if (MBs > (1 << 11)) {
  245. return (Bpm >> BPER_MB_NORMBITS) * MBs;
  246. } else {
  247. return (Bpm * MBs) >> BPER_MB_NORMBITS;
  248. }
  249. }
  250. static void calc_iframe_target_size(VP8_COMP *cpi) {
  251. /* boost defaults to half second */
  252. int kf_boost;
  253. uint64_t target;
  254. /* Clear down mmx registers to allow floating point in what follows */
  255. vp8_clear_system_state();
  256. if (cpi->oxcf.fixed_q >= 0) {
  257. int Q = cpi->oxcf.key_q;
  258. target = estimate_bits_at_q(INTRA_FRAME, Q, cpi->common.MBs,
  259. cpi->key_frame_rate_correction_factor);
  260. } else if (cpi->pass == 2) {
  261. /* New Two pass RC */
  262. target = cpi->per_frame_bandwidth;
  263. }
  264. /* First Frame is a special case */
  265. else if (cpi->common.current_video_frame == 0) {
  266. /* 1 Pass there is no information on which to base size so use
  267. * bandwidth per second * fraction of the initial buffer
  268. * level
  269. */
  270. target = cpi->oxcf.starting_buffer_level / 2;
  271. if (target > cpi->oxcf.target_bandwidth * 3 / 2) {
  272. target = cpi->oxcf.target_bandwidth * 3 / 2;
  273. }
  274. } else {
  275. /* if this keyframe was forced, use a more recent Q estimate */
  276. int Q = (cpi->common.frame_flags & FRAMEFLAGS_KEY) ? cpi->avg_frame_qindex
  277. : cpi->ni_av_qi;
  278. int initial_boost = 32; /* |3.0 * per_frame_bandwidth| */
  279. /* Boost depends somewhat on frame rate: only used for 1 layer case. */
  280. if (cpi->oxcf.number_of_layers == 1) {
  281. kf_boost = VPXMAX(initial_boost, (int)(2 * cpi->output_framerate - 16));
  282. } else {
  283. /* Initial factor: set target size to: |3.0 * per_frame_bandwidth|. */
  284. kf_boost = initial_boost;
  285. }
  286. /* adjustment up based on q: this factor ranges from ~1.2 to 2.2. */
  287. kf_boost = kf_boost * kf_boost_qadjustment[Q] / 100;
  288. /* frame separation adjustment ( down) */
  289. if (cpi->frames_since_key < cpi->output_framerate / 2) {
  290. kf_boost =
  291. (int)(kf_boost * cpi->frames_since_key / (cpi->output_framerate / 2));
  292. }
  293. /* Minimal target size is |2* per_frame_bandwidth|. */
  294. if (kf_boost < 16) kf_boost = 16;
  295. target = ((16 + kf_boost) * cpi->per_frame_bandwidth) >> 4;
  296. }
  297. if (cpi->oxcf.rc_max_intra_bitrate_pct) {
  298. unsigned int max_rate =
  299. cpi->per_frame_bandwidth * cpi->oxcf.rc_max_intra_bitrate_pct / 100;
  300. if (target > max_rate) target = max_rate;
  301. }
  302. cpi->this_frame_target = (int)target;
  303. /* TODO: if we separate rate targeting from Q targetting, move this.
  304. * Reset the active worst quality to the baseline value for key frames.
  305. */
  306. if (cpi->pass != 2) cpi->active_worst_quality = cpi->worst_quality;
  307. #if 0
  308. {
  309. FILE *f;
  310. f = fopen("kf_boost.stt", "a");
  311. fprintf(f, " %8u %10d %10d %10d\n",
  312. cpi->common.current_video_frame, cpi->gfu_boost, cpi->baseline_gf_interval, cpi->source_alt_ref_pending);
  313. fclose(f);
  314. }
  315. #endif
  316. }
  317. /* Do the best we can to define the parameters for the next GF based on what
  318. * information we have available.
  319. */
  320. static void calc_gf_params(VP8_COMP *cpi) {
  321. int Q =
  322. (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
  323. int Boost = 0;
  324. int gf_frame_useage = 0; /* Golden frame useage since last GF */
  325. int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME] +
  326. cpi->recent_ref_frame_usage[LAST_FRAME] +
  327. cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
  328. cpi->recent_ref_frame_usage[ALTREF_FRAME];
  329. int pct_gf_active = (100 * cpi->gf_active_count) /
  330. (cpi->common.mb_rows * cpi->common.mb_cols);
  331. if (tot_mbs) {
  332. gf_frame_useage = (cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
  333. cpi->recent_ref_frame_usage[ALTREF_FRAME]) *
  334. 100 / tot_mbs;
  335. }
  336. if (pct_gf_active > gf_frame_useage) gf_frame_useage = pct_gf_active;
  337. /* Not two pass */
  338. if (cpi->pass != 2) {
  339. /* Single Pass lagged mode: TBD */
  340. if (0) {
  341. }
  342. /* Single Pass compression: Has to use current and historical data */
  343. else {
  344. #if 0
  345. /* Experimental code */
  346. int index = cpi->one_pass_frame_index;
  347. int frames_to_scan = (cpi->max_gf_interval <= MAX_LAG_BUFFERS) ? cpi->max_gf_interval : MAX_LAG_BUFFERS;
  348. /* ************** Experimental code - incomplete */
  349. /*
  350. double decay_val = 1.0;
  351. double IIAccumulator = 0.0;
  352. double last_iiaccumulator = 0.0;
  353. double IIRatio;
  354. cpi->one_pass_frame_index = cpi->common.current_video_frame%MAX_LAG_BUFFERS;
  355. for ( i = 0; i < (frames_to_scan - 1); i++ )
  356. {
  357. if ( index < 0 )
  358. index = MAX_LAG_BUFFERS;
  359. index --;
  360. if ( cpi->one_pass_frame_stats[index].frame_coded_error > 0.0 )
  361. {
  362. IIRatio = cpi->one_pass_frame_stats[index].frame_intra_error / cpi->one_pass_frame_stats[index].frame_coded_error;
  363. if ( IIRatio > 30.0 )
  364. IIRatio = 30.0;
  365. }
  366. else
  367. IIRatio = 30.0;
  368. IIAccumulator += IIRatio * decay_val;
  369. decay_val = decay_val * cpi->one_pass_frame_stats[index].frame_pcnt_inter;
  370. if ( (i > MIN_GF_INTERVAL) &&
  371. ((IIAccumulator - last_iiaccumulator) < 2.0) )
  372. {
  373. break;
  374. }
  375. last_iiaccumulator = IIAccumulator;
  376. }
  377. Boost = IIAccumulator*100.0/16.0;
  378. cpi->baseline_gf_interval = i;
  379. */
  380. #else
  381. /*************************************************************/
  382. /* OLD code */
  383. /* Adjust boost based upon ambient Q */
  384. Boost = GFQ_ADJUSTMENT;
  385. /* Adjust based upon most recently measure intra useage */
  386. Boost = Boost *
  387. gf_intra_usage_adjustment[(cpi->this_frame_percent_intra < 15)
  388. ? cpi->this_frame_percent_intra
  389. : 14] /
  390. 100;
  391. /* Adjust gf boost based upon GF usage since last GF */
  392. Boost = Boost * gf_adjust_table[gf_frame_useage] / 100;
  393. #endif
  394. }
  395. /* golden frame boost without recode loop often goes awry. be
  396. * safe by keeping numbers down.
  397. */
  398. if (!cpi->sf.recode_loop) {
  399. if (cpi->compressor_speed == 2) Boost = Boost / 2;
  400. }
  401. /* Apply an upper limit based on Q for 1 pass encodes */
  402. if (Boost > kf_gf_boost_qlimits[Q] && (cpi->pass == 0)) {
  403. Boost = kf_gf_boost_qlimits[Q];
  404. /* Apply lower limits to boost. */
  405. } else if (Boost < 110) {
  406. Boost = 110;
  407. }
  408. /* Note the boost used */
  409. cpi->last_boost = Boost;
  410. }
  411. /* Estimate next interval
  412. * This is updated once the real frame size/boost is known.
  413. */
  414. if (cpi->oxcf.fixed_q == -1) {
  415. if (cpi->pass == 2) /* 2 Pass */
  416. {
  417. cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
  418. } else /* 1 Pass */
  419. {
  420. cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
  421. if (cpi->last_boost > 750) cpi->frames_till_gf_update_due++;
  422. if (cpi->last_boost > 1000) cpi->frames_till_gf_update_due++;
  423. if (cpi->last_boost > 1250) cpi->frames_till_gf_update_due++;
  424. if (cpi->last_boost >= 1500) cpi->frames_till_gf_update_due++;
  425. if (gf_interval_table[gf_frame_useage] > cpi->frames_till_gf_update_due) {
  426. cpi->frames_till_gf_update_due = gf_interval_table[gf_frame_useage];
  427. }
  428. if (cpi->frames_till_gf_update_due > cpi->max_gf_interval) {
  429. cpi->frames_till_gf_update_due = cpi->max_gf_interval;
  430. }
  431. }
  432. } else {
  433. cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
  434. }
  435. /* ARF on or off */
  436. if (cpi->pass != 2) {
  437. /* For now Alt ref is not allowed except in 2 pass modes. */
  438. cpi->source_alt_ref_pending = 0;
  439. /*if ( cpi->oxcf.fixed_q == -1)
  440. {
  441. if ( cpi->oxcf.play_alternate && (cpi->last_boost > (100 +
  442. (AF_THRESH*cpi->frames_till_gf_update_due)) ) )
  443. cpi->source_alt_ref_pending = 1;
  444. else
  445. cpi->source_alt_ref_pending = 0;
  446. }*/
  447. }
  448. }
  449. static void calc_pframe_target_size(VP8_COMP *cpi) {
  450. int min_frame_target;
  451. int old_per_frame_bandwidth = cpi->per_frame_bandwidth;
  452. if (cpi->current_layer > 0) {
  453. cpi->per_frame_bandwidth =
  454. cpi->layer_context[cpi->current_layer].avg_frame_size_for_layer;
  455. }
  456. min_frame_target = 0;
  457. if (cpi->pass == 2) {
  458. min_frame_target = cpi->min_frame_bandwidth;
  459. if (min_frame_target < (cpi->av_per_frame_bandwidth >> 5)) {
  460. min_frame_target = cpi->av_per_frame_bandwidth >> 5;
  461. }
  462. } else if (min_frame_target < cpi->per_frame_bandwidth / 4) {
  463. min_frame_target = cpi->per_frame_bandwidth / 4;
  464. }
  465. /* Special alt reference frame case */
  466. if ((cpi->common.refresh_alt_ref_frame) &&
  467. (cpi->oxcf.number_of_layers == 1)) {
  468. if (cpi->pass == 2) {
  469. /* Per frame bit target for the alt ref frame */
  470. cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
  471. cpi->this_frame_target = cpi->per_frame_bandwidth;
  472. }
  473. /* One Pass ??? TBD */
  474. }
  475. /* Normal frames (gf,and inter) */
  476. else {
  477. /* 2 pass */
  478. if (cpi->pass == 2) {
  479. cpi->this_frame_target = cpi->per_frame_bandwidth;
  480. }
  481. /* 1 pass */
  482. else {
  483. int Adjustment;
  484. /* Make rate adjustment to recover bits spent in key frame
  485. * Test to see if the key frame inter data rate correction
  486. * should still be in force
  487. */
  488. if (cpi->kf_overspend_bits > 0) {
  489. Adjustment = (cpi->kf_bitrate_adjustment <= cpi->kf_overspend_bits)
  490. ? cpi->kf_bitrate_adjustment
  491. : cpi->kf_overspend_bits;
  492. if (Adjustment > (cpi->per_frame_bandwidth - min_frame_target)) {
  493. Adjustment = (cpi->per_frame_bandwidth - min_frame_target);
  494. }
  495. cpi->kf_overspend_bits -= Adjustment;
  496. /* Calculate an inter frame bandwidth target for the next
  497. * few frames designed to recover any extra bits spent on
  498. * the key frame.
  499. */
  500. cpi->this_frame_target = cpi->per_frame_bandwidth - Adjustment;
  501. if (cpi->this_frame_target < min_frame_target) {
  502. cpi->this_frame_target = min_frame_target;
  503. }
  504. } else {
  505. cpi->this_frame_target = cpi->per_frame_bandwidth;
  506. }
  507. /* If appropriate make an adjustment to recover bits spent on a
  508. * recent GF
  509. */
  510. if ((cpi->gf_overspend_bits > 0) &&
  511. (cpi->this_frame_target > min_frame_target)) {
  512. Adjustment = (cpi->non_gf_bitrate_adjustment <= cpi->gf_overspend_bits)
  513. ? cpi->non_gf_bitrate_adjustment
  514. : cpi->gf_overspend_bits;
  515. if (Adjustment > (cpi->this_frame_target - min_frame_target)) {
  516. Adjustment = (cpi->this_frame_target - min_frame_target);
  517. }
  518. cpi->gf_overspend_bits -= Adjustment;
  519. cpi->this_frame_target -= Adjustment;
  520. }
  521. /* Apply small + and - boosts for non gf frames */
  522. if ((cpi->last_boost > 150) && (cpi->frames_till_gf_update_due > 0) &&
  523. (cpi->current_gf_interval >= (MIN_GF_INTERVAL << 1))) {
  524. /* % Adjustment limited to the range 1% to 10% */
  525. Adjustment = (cpi->last_boost - 100) >> 5;
  526. if (Adjustment < 1) {
  527. Adjustment = 1;
  528. } else if (Adjustment > 10) {
  529. Adjustment = 10;
  530. }
  531. /* Convert to bits */
  532. Adjustment = (cpi->this_frame_target * Adjustment) / 100;
  533. if (Adjustment > (cpi->this_frame_target - min_frame_target)) {
  534. Adjustment = (cpi->this_frame_target - min_frame_target);
  535. }
  536. if (cpi->frames_since_golden == (cpi->current_gf_interval >> 1)) {
  537. Adjustment = (cpi->current_gf_interval - 1) * Adjustment;
  538. // Limit adjustment to 10% of current target.
  539. if (Adjustment > (10 * cpi->this_frame_target) / 100) {
  540. Adjustment = (10 * cpi->this_frame_target) / 100;
  541. }
  542. cpi->this_frame_target += Adjustment;
  543. } else {
  544. cpi->this_frame_target -= Adjustment;
  545. }
  546. }
  547. }
  548. }
  549. /* Sanity check that the total sum of adjustments is not above the
  550. * maximum allowed That is that having allowed for KF and GF penalties
  551. * we have not pushed the current interframe target to low. If the
  552. * adjustment we apply here is not capable of recovering all the extra
  553. * bits we have spent in the KF or GF then the remainder will have to
  554. * be recovered over a longer time span via other buffer / rate control
  555. * mechanisms.
  556. */
  557. if (cpi->this_frame_target < min_frame_target) {
  558. cpi->this_frame_target = min_frame_target;
  559. }
  560. if (!cpi->common.refresh_alt_ref_frame) {
  561. /* Note the baseline target data rate for this inter frame. */
  562. cpi->inter_frame_target = cpi->this_frame_target;
  563. }
  564. /* One Pass specific code */
  565. if (cpi->pass == 0) {
  566. /* Adapt target frame size with respect to any buffering constraints: */
  567. if (cpi->buffered_mode) {
  568. int one_percent_bits = (int)(1 + cpi->oxcf.optimal_buffer_level / 100);
  569. if ((cpi->buffer_level < cpi->oxcf.optimal_buffer_level) ||
  570. (cpi->bits_off_target < cpi->oxcf.optimal_buffer_level)) {
  571. int percent_low = 0;
  572. /* Decide whether or not we need to adjust the frame data
  573. * rate target.
  574. *
  575. * If we are are below the optimal buffer fullness level
  576. * and adherence to buffering constraints is important to
  577. * the end usage then adjust the per frame target.
  578. */
  579. if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
  580. (cpi->buffer_level < cpi->oxcf.optimal_buffer_level)) {
  581. percent_low =
  582. (int)((cpi->oxcf.optimal_buffer_level - cpi->buffer_level) /
  583. one_percent_bits);
  584. }
  585. /* Are we overshooting the long term clip data rate... */
  586. else if (cpi->bits_off_target < 0) {
  587. /* Adjust per frame data target downwards to compensate. */
  588. percent_low =
  589. (int)(100 * -cpi->bits_off_target / (cpi->total_byte_count * 8));
  590. }
  591. if (percent_low > cpi->oxcf.under_shoot_pct) {
  592. percent_low = cpi->oxcf.under_shoot_pct;
  593. } else if (percent_low < 0) {
  594. percent_low = 0;
  595. }
  596. /* lower the target bandwidth for this frame. */
  597. cpi->this_frame_target -= (cpi->this_frame_target * percent_low) / 200;
  598. /* Are we using allowing control of active_worst_allowed_q
  599. * according to buffer level.
  600. */
  601. if (cpi->auto_worst_q && cpi->ni_frames > 150) {
  602. int64_t critical_buffer_level;
  603. /* For streaming applications the most important factor is
  604. * cpi->buffer_level as this takes into account the
  605. * specified short term buffering constraints. However,
  606. * hitting the long term clip data rate target is also
  607. * important.
  608. */
  609. if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
  610. /* Take the smaller of cpi->buffer_level and
  611. * cpi->bits_off_target
  612. */
  613. critical_buffer_level = (cpi->buffer_level < cpi->bits_off_target)
  614. ? cpi->buffer_level
  615. : cpi->bits_off_target;
  616. }
  617. /* For local file playback short term buffering constraints
  618. * are less of an issue
  619. */
  620. else {
  621. /* Consider only how we are doing for the clip as a
  622. * whole
  623. */
  624. critical_buffer_level = cpi->bits_off_target;
  625. }
  626. /* Set the active worst quality based upon the selected
  627. * buffer fullness number.
  628. */
  629. if (critical_buffer_level < cpi->oxcf.optimal_buffer_level) {
  630. if (critical_buffer_level > (cpi->oxcf.optimal_buffer_level >> 2)) {
  631. int64_t qadjustment_range = cpi->worst_quality - cpi->ni_av_qi;
  632. int64_t above_base = (critical_buffer_level -
  633. (cpi->oxcf.optimal_buffer_level >> 2));
  634. /* Step active worst quality down from
  635. * cpi->ni_av_qi when (critical_buffer_level ==
  636. * cpi->optimal_buffer_level) to
  637. * cpi->worst_quality when
  638. * (critical_buffer_level ==
  639. * cpi->optimal_buffer_level >> 2)
  640. */
  641. cpi->active_worst_quality =
  642. cpi->worst_quality -
  643. (int)((qadjustment_range * above_base) /
  644. (cpi->oxcf.optimal_buffer_level * 3 >> 2));
  645. } else {
  646. cpi->active_worst_quality = cpi->worst_quality;
  647. }
  648. } else {
  649. cpi->active_worst_quality = cpi->ni_av_qi;
  650. }
  651. } else {
  652. cpi->active_worst_quality = cpi->worst_quality;
  653. }
  654. } else {
  655. int percent_high = 0;
  656. if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
  657. (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)) {
  658. percent_high =
  659. (int)((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) /
  660. one_percent_bits);
  661. } else if (cpi->bits_off_target > cpi->oxcf.optimal_buffer_level) {
  662. percent_high =
  663. (int)((100 * cpi->bits_off_target) / (cpi->total_byte_count * 8));
  664. }
  665. if (percent_high > cpi->oxcf.over_shoot_pct) {
  666. percent_high = cpi->oxcf.over_shoot_pct;
  667. } else if (percent_high < 0) {
  668. percent_high = 0;
  669. }
  670. cpi->this_frame_target += (cpi->this_frame_target * percent_high) / 200;
  671. /* Are we allowing control of active_worst_allowed_q according
  672. * to buffer level.
  673. */
  674. if (cpi->auto_worst_q && cpi->ni_frames > 150) {
  675. /* When using the relaxed buffer model stick to the
  676. * user specified value
  677. */
  678. cpi->active_worst_quality = cpi->ni_av_qi;
  679. } else {
  680. cpi->active_worst_quality = cpi->worst_quality;
  681. }
  682. }
  683. /* Set active_best_quality to prevent quality rising too high */
  684. cpi->active_best_quality = cpi->best_quality;
  685. /* Worst quality obviously must not be better than best quality */
  686. if (cpi->active_worst_quality <= cpi->active_best_quality) {
  687. cpi->active_worst_quality = cpi->active_best_quality + 1;
  688. }
  689. if (cpi->active_worst_quality > 127) cpi->active_worst_quality = 127;
  690. }
  691. /* Unbuffered mode (eg. video conferencing) */
  692. else {
  693. /* Set the active worst quality */
  694. cpi->active_worst_quality = cpi->worst_quality;
  695. }
  696. /* Special trap for constrained quality mode
  697. * "active_worst_quality" may never drop below cq level
  698. * for any frame type.
  699. */
  700. if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY &&
  701. cpi->active_worst_quality < cpi->cq_target_quality) {
  702. cpi->active_worst_quality = cpi->cq_target_quality;
  703. }
  704. }
  705. /* Test to see if we have to drop a frame
  706. * The auto-drop frame code is only used in buffered mode.
  707. * In unbufferd mode (eg vide conferencing) the descision to
  708. * code or drop a frame is made outside the codec in response to real
  709. * world comms or buffer considerations.
  710. */
  711. if (cpi->drop_frames_allowed &&
  712. (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
  713. ((cpi->common.frame_type != KEY_FRAME))) {
  714. /* Check for a buffer underun-crisis in which case we have to drop
  715. * a frame
  716. */
  717. if ((cpi->buffer_level < 0)) {
  718. #if 0
  719. FILE *f = fopen("dec.stt", "a");
  720. fprintf(f, "%10d %10d %10d %10d ***** BUFFER EMPTY\n",
  721. (int) cpi->common.current_video_frame,
  722. cpi->decimation_factor, cpi->common.horiz_scale,
  723. (cpi->buffer_level * 100) / cpi->oxcf.optimal_buffer_level);
  724. fclose(f);
  725. #endif
  726. cpi->drop_frame = 1;
  727. /* Update the buffer level variable. */
  728. cpi->bits_off_target += cpi->av_per_frame_bandwidth;
  729. if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
  730. cpi->bits_off_target = (int)cpi->oxcf.maximum_buffer_size;
  731. }
  732. cpi->buffer_level = cpi->bits_off_target;
  733. if (cpi->oxcf.number_of_layers > 1) {
  734. unsigned int i;
  735. // Propagate bits saved by dropping the frame to higher layers.
  736. for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) {
  737. LAYER_CONTEXT *lc = &cpi->layer_context[i];
  738. lc->bits_off_target += (int)(lc->target_bandwidth / lc->framerate);
  739. if (lc->bits_off_target > lc->maximum_buffer_size) {
  740. lc->bits_off_target = lc->maximum_buffer_size;
  741. }
  742. lc->buffer_level = lc->bits_off_target;
  743. }
  744. }
  745. }
  746. }
  747. /* Adjust target frame size for Golden Frames: */
  748. if (cpi->oxcf.error_resilient_mode == 0 &&
  749. (cpi->frames_till_gf_update_due == 0) && !cpi->drop_frame) {
  750. int Q =
  751. (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
  752. int gf_frame_useage = 0; /* Golden frame useage since last GF */
  753. int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME] +
  754. cpi->recent_ref_frame_usage[LAST_FRAME] +
  755. cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
  756. cpi->recent_ref_frame_usage[ALTREF_FRAME];
  757. int pct_gf_active = (100 * cpi->gf_active_count) /
  758. (cpi->common.mb_rows * cpi->common.mb_cols);
  759. if (tot_mbs) {
  760. gf_frame_useage = (cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
  761. cpi->recent_ref_frame_usage[ALTREF_FRAME]) *
  762. 100 / tot_mbs;
  763. }
  764. if (pct_gf_active > gf_frame_useage) gf_frame_useage = pct_gf_active;
  765. /* Is a fixed manual GF frequency being used */
  766. if (cpi->auto_gold) {
  767. /* For one pass throw a GF if recent frame intra useage is
  768. * low or the GF useage is high
  769. */
  770. if ((cpi->pass == 0) &&
  771. (cpi->this_frame_percent_intra < 15 || gf_frame_useage >= 5)) {
  772. cpi->common.refresh_golden_frame = 1;
  773. /* Two pass GF descision */
  774. } else if (cpi->pass == 2) {
  775. cpi->common.refresh_golden_frame = 1;
  776. }
  777. }
  778. #if 0
  779. /* Debug stats */
  780. if (0)
  781. {
  782. FILE *f;
  783. f = fopen("gf_useaget.stt", "a");
  784. fprintf(f, " %8ld %10ld %10ld %10ld %10ld\n",
  785. cpi->common.current_video_frame, cpi->gfu_boost, GFQ_ADJUSTMENT, cpi->gfu_boost, gf_frame_useage);
  786. fclose(f);
  787. }
  788. #endif
  789. if (cpi->common.refresh_golden_frame == 1) {
  790. #if 0
  791. if (0)
  792. {
  793. FILE *f;
  794. f = fopen("GFexit.stt", "a");
  795. fprintf(f, "%8ld GF coded\n", cpi->common.current_video_frame);
  796. fclose(f);
  797. }
  798. #endif
  799. if (cpi->auto_adjust_gold_quantizer) {
  800. calc_gf_params(cpi);
  801. }
  802. /* If we are using alternate ref instead of gf then do not apply the
  803. * boost It will instead be applied to the altref update Jims
  804. * modified boost
  805. */
  806. if (!cpi->source_alt_ref_active) {
  807. if (cpi->oxcf.fixed_q < 0) {
  808. if (cpi->pass == 2) {
  809. /* The spend on the GF is defined in the two pass
  810. * code for two pass encodes
  811. */
  812. cpi->this_frame_target = cpi->per_frame_bandwidth;
  813. } else {
  814. int Boost = cpi->last_boost;
  815. int frames_in_section = cpi->frames_till_gf_update_due + 1;
  816. int allocation_chunks = (frames_in_section * 100) + (Boost - 100);
  817. int bits_in_section = cpi->inter_frame_target * frames_in_section;
  818. /* Normalize Altboost and allocations chunck down to
  819. * prevent overflow
  820. */
  821. while (Boost > 1000) {
  822. Boost /= 2;
  823. allocation_chunks /= 2;
  824. }
  825. /* Avoid loss of precision but avoid overflow */
  826. if ((bits_in_section >> 7) > allocation_chunks) {
  827. cpi->this_frame_target =
  828. Boost * (bits_in_section / allocation_chunks);
  829. } else {
  830. cpi->this_frame_target =
  831. (Boost * bits_in_section) / allocation_chunks;
  832. }
  833. }
  834. } else {
  835. cpi->this_frame_target =
  836. (estimate_bits_at_q(1, Q, cpi->common.MBs, 1.0) *
  837. cpi->last_boost) /
  838. 100;
  839. }
  840. }
  841. /* If there is an active ARF at this location use the minimum
  842. * bits on this frame even if it is a contructed arf.
  843. * The active maximum quantizer insures that an appropriate
  844. * number of bits will be spent if needed for contstructed ARFs.
  845. */
  846. else {
  847. cpi->this_frame_target = 0;
  848. }
  849. cpi->current_gf_interval = cpi->frames_till_gf_update_due;
  850. }
  851. }
  852. cpi->per_frame_bandwidth = old_per_frame_bandwidth;
  853. }
  854. void vp8_update_rate_correction_factors(VP8_COMP *cpi, int damp_var) {
  855. int Q = cpi->common.base_qindex;
  856. int correction_factor = 100;
  857. double rate_correction_factor;
  858. double adjustment_limit;
  859. int projected_size_based_on_q = 0;
  860. /* Clear down mmx registers to allow floating point in what follows */
  861. vp8_clear_system_state();
  862. if (cpi->common.frame_type == KEY_FRAME) {
  863. rate_correction_factor = cpi->key_frame_rate_correction_factor;
  864. } else {
  865. if (cpi->oxcf.number_of_layers == 1 && (cpi->common.refresh_alt_ref_frame ||
  866. cpi->common.refresh_golden_frame)) {
  867. rate_correction_factor = cpi->gf_rate_correction_factor;
  868. } else {
  869. rate_correction_factor = cpi->rate_correction_factor;
  870. }
  871. }
  872. /* Work out how big we would have expected the frame to be at this Q
  873. * given the current correction factor. Stay in double to avoid int
  874. * overflow when values are large
  875. */
  876. projected_size_based_on_q =
  877. (int)(((.5 +
  878. rate_correction_factor *
  879. vp8_bits_per_mb[cpi->common.frame_type][Q]) *
  880. cpi->common.MBs) /
  881. (1 << BPER_MB_NORMBITS));
  882. /* Make some allowance for cpi->zbin_over_quant */
  883. if (cpi->mb.zbin_over_quant > 0) {
  884. int Z = cpi->mb.zbin_over_quant;
  885. double Factor = 0.99;
  886. double factor_adjustment = 0.01 / 256.0;
  887. while (Z > 0) {
  888. Z--;
  889. projected_size_based_on_q = (int)(Factor * projected_size_based_on_q);
  890. Factor += factor_adjustment;
  891. if (Factor >= 0.999) Factor = 0.999;
  892. }
  893. }
  894. /* Work out a size correction factor. */
  895. if (projected_size_based_on_q > 0) {
  896. correction_factor =
  897. (100 * cpi->projected_frame_size) / projected_size_based_on_q;
  898. }
  899. /* More heavily damped adjustment used if we have been oscillating
  900. * either side of target
  901. */
  902. switch (damp_var) {
  903. case 0: adjustment_limit = 0.75; break;
  904. case 1: adjustment_limit = 0.375; break;
  905. case 2:
  906. default: adjustment_limit = 0.25; break;
  907. }
  908. if (correction_factor > 102) {
  909. /* We are not already at the worst allowable quality */
  910. correction_factor =
  911. (int)(100.5 + ((correction_factor - 100) * adjustment_limit));
  912. rate_correction_factor =
  913. ((rate_correction_factor * correction_factor) / 100);
  914. /* Keep rate_correction_factor within limits */
  915. if (rate_correction_factor > MAX_BPB_FACTOR) {
  916. rate_correction_factor = MAX_BPB_FACTOR;
  917. }
  918. } else if (correction_factor < 99) {
  919. /* We are not already at the best allowable quality */
  920. correction_factor =
  921. (int)(100.5 - ((100 - correction_factor) * adjustment_limit));
  922. rate_correction_factor =
  923. ((rate_correction_factor * correction_factor) / 100);
  924. /* Keep rate_correction_factor within limits */
  925. if (rate_correction_factor < MIN_BPB_FACTOR) {
  926. rate_correction_factor = MIN_BPB_FACTOR;
  927. }
  928. }
  929. if (cpi->common.frame_type == KEY_FRAME) {
  930. cpi->key_frame_rate_correction_factor = rate_correction_factor;
  931. } else {
  932. if (cpi->oxcf.number_of_layers == 1 && (cpi->common.refresh_alt_ref_frame ||
  933. cpi->common.refresh_golden_frame)) {
  934. cpi->gf_rate_correction_factor = rate_correction_factor;
  935. } else {
  936. cpi->rate_correction_factor = rate_correction_factor;
  937. }
  938. }
  939. }
  940. int vp8_regulate_q(VP8_COMP *cpi, int target_bits_per_frame) {
  941. int Q = cpi->active_worst_quality;
  942. if (cpi->force_maxqp == 1) {
  943. cpi->active_worst_quality = cpi->worst_quality;
  944. return cpi->worst_quality;
  945. }
  946. /* Reset Zbin OQ value */
  947. cpi->mb.zbin_over_quant = 0;
  948. if (cpi->oxcf.fixed_q >= 0) {
  949. Q = cpi->oxcf.fixed_q;
  950. if (cpi->common.frame_type == KEY_FRAME) {
  951. Q = cpi->oxcf.key_q;
  952. } else if (cpi->oxcf.number_of_layers == 1 &&
  953. cpi->common.refresh_alt_ref_frame) {
  954. Q = cpi->oxcf.alt_q;
  955. } else if (cpi->oxcf.number_of_layers == 1 &&
  956. cpi->common.refresh_golden_frame) {
  957. Q = cpi->oxcf.gold_q;
  958. }
  959. } else {
  960. int i;
  961. int last_error = INT_MAX;
  962. int target_bits_per_mb;
  963. int bits_per_mb_at_this_q;
  964. double correction_factor;
  965. /* Select the appropriate correction factor based upon type of frame. */
  966. if (cpi->common.frame_type == KEY_FRAME) {
  967. correction_factor = cpi->key_frame_rate_correction_factor;
  968. } else {
  969. if (cpi->oxcf.number_of_layers == 1 &&
  970. (cpi->common.refresh_alt_ref_frame ||
  971. cpi->common.refresh_golden_frame)) {
  972. correction_factor = cpi->gf_rate_correction_factor;
  973. } else {
  974. correction_factor = cpi->rate_correction_factor;
  975. }
  976. }
  977. /* Calculate required scaling factor based on target frame size and
  978. * size of frame produced using previous Q
  979. */
  980. if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS)) {
  981. /* Case where we would overflow int */
  982. target_bits_per_mb = (target_bits_per_frame / cpi->common.MBs)
  983. << BPER_MB_NORMBITS;
  984. } else {
  985. target_bits_per_mb =
  986. (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->common.MBs;
  987. }
  988. i = cpi->active_best_quality;
  989. do {
  990. bits_per_mb_at_this_q =
  991. (int)(.5 +
  992. correction_factor * vp8_bits_per_mb[cpi->common.frame_type][i]);
  993. if (bits_per_mb_at_this_q <= target_bits_per_mb) {
  994. if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) {
  995. Q = i;
  996. } else {
  997. Q = i - 1;
  998. }
  999. break;
  1000. } else {
  1001. last_error = bits_per_mb_at_this_q - target_bits_per_mb;
  1002. }
  1003. } while (++i <= cpi->active_worst_quality);
  1004. /* If we are at MAXQ then enable Q over-run which seeks to claw
  1005. * back additional bits through things like the RD multiplier
  1006. * and zero bin size.
  1007. */
  1008. if (Q >= MAXQ) {
  1009. int zbin_oqmax;
  1010. double Factor = 0.99;
  1011. double factor_adjustment = 0.01 / 256.0;
  1012. if (cpi->common.frame_type == KEY_FRAME) {
  1013. zbin_oqmax = 0;
  1014. } else if (cpi->oxcf.number_of_layers == 1 &&
  1015. (cpi->common.refresh_alt_ref_frame ||
  1016. (cpi->common.refresh_golden_frame &&
  1017. !cpi->source_alt_ref_active))) {
  1018. zbin_oqmax = 16;
  1019. } else {
  1020. zbin_oqmax = ZBIN_OQ_MAX;
  1021. }
  1022. /*{
  1023. double Factor =
  1024. (double)target_bits_per_mb/(double)bits_per_mb_at_this_q;
  1025. double Oq;
  1026. Factor = Factor/1.2683;
  1027. Oq = pow( Factor, (1.0/-0.165) );
  1028. if ( Oq > zbin_oqmax )
  1029. Oq = zbin_oqmax;
  1030. cpi->zbin_over_quant = (int)Oq;
  1031. }*/
  1032. /* Each incrment in the zbin is assumed to have a fixed effect
  1033. * on bitrate. This is not of course true. The effect will be
  1034. * highly clip dependent and may well have sudden steps. The
  1035. * idea here is to acheive higher effective quantizers than the
  1036. * normal maximum by expanding the zero bin and hence
  1037. * decreasing the number of low magnitude non zero coefficients.
  1038. */
  1039. while (cpi->mb.zbin_over_quant < zbin_oqmax) {
  1040. cpi->mb.zbin_over_quant++;
  1041. if (cpi->mb.zbin_over_quant > zbin_oqmax) {
  1042. cpi->mb.zbin_over_quant = zbin_oqmax;
  1043. }
  1044. /* Adjust bits_per_mb_at_this_q estimate */
  1045. bits_per_mb_at_this_q = (int)(Factor * bits_per_mb_at_this_q);
  1046. Factor += factor_adjustment;
  1047. if (Factor >= 0.999) Factor = 0.999;
  1048. /* Break out if we get down to the target rate */
  1049. if (bits_per_mb_at_this_q <= target_bits_per_mb) break;
  1050. }
  1051. }
  1052. }
  1053. return Q;
  1054. }
  1055. static int estimate_keyframe_frequency(VP8_COMP *cpi) {
  1056. int i;
  1057. /* Average key frame frequency */
  1058. int av_key_frame_frequency = 0;
  1059. /* First key frame at start of sequence is a special case. We have no
  1060. * frequency data.
  1061. */
  1062. if (cpi->key_frame_count == 1) {
  1063. /* Assume a default of 1 kf every 2 seconds, or the max kf interval,
  1064. * whichever is smaller.
  1065. */
  1066. int key_freq = cpi->oxcf.key_freq > 0 ? cpi->oxcf.key_freq : 1;
  1067. av_key_frame_frequency = 1 + (int)cpi->output_framerate * 2;
  1068. if (cpi->oxcf.auto_key && av_key_frame_frequency > key_freq) {
  1069. av_key_frame_frequency = key_freq;
  1070. }
  1071. cpi->prior_key_frame_distance[KEY_FRAME_CONTEXT - 1] =
  1072. av_key_frame_frequency;
  1073. } else {
  1074. unsigned int total_weight = 0;
  1075. int last_kf_interval =
  1076. (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1;
  1077. /* reset keyframe context and calculate weighted average of last
  1078. * KEY_FRAME_CONTEXT keyframes
  1079. */
  1080. for (i = 0; i < KEY_FRAME_CONTEXT; ++i) {
  1081. if (i < KEY_FRAME_CONTEXT - 1) {
  1082. cpi->prior_key_frame_distance[i] = cpi->prior_key_frame_distance[i + 1];
  1083. } else {
  1084. cpi->prior_key_frame_distance[i] = last_kf_interval;
  1085. }
  1086. av_key_frame_frequency +=
  1087. prior_key_frame_weight[i] * cpi->prior_key_frame_distance[i];
  1088. total_weight += prior_key_frame_weight[i];
  1089. }
  1090. av_key_frame_frequency /= total_weight;
  1091. }
  1092. // TODO (marpan): Given the checks above, |av_key_frame_frequency|
  1093. // should always be above 0. But for now we keep the sanity check in.
  1094. if (av_key_frame_frequency == 0) av_key_frame_frequency = 1;
  1095. return av_key_frame_frequency;
  1096. }
  1097. void vp8_adjust_key_frame_context(VP8_COMP *cpi) {
  1098. /* Clear down mmx registers to allow floating point in what follows */
  1099. vp8_clear_system_state();
  1100. /* Do we have any key frame overspend to recover? */
  1101. /* Two-pass overspend handled elsewhere. */
  1102. if ((cpi->pass != 2) &&
  1103. (cpi->projected_frame_size > cpi->per_frame_bandwidth)) {
  1104. int overspend;
  1105. /* Update the count of key frame overspend to be recovered in
  1106. * subsequent frames. A portion of the KF overspend is treated as gf
  1107. * overspend (and hence recovered more quickly) as the kf is also a
  1108. * gf. Otherwise the few frames following each kf tend to get more
  1109. * bits allocated than those following other gfs.
  1110. */
  1111. overspend = (cpi->projected_frame_size - cpi->per_frame_bandwidth);
  1112. if (cpi->oxcf.number_of_layers > 1) {
  1113. cpi->kf_overspend_bits += overspend;
  1114. } else {
  1115. cpi->kf_overspend_bits += overspend * 7 / 8;
  1116. cpi->gf_overspend_bits += overspend * 1 / 8;
  1117. }
  1118. /* Work out how much to try and recover per frame. */
  1119. cpi->kf_bitrate_adjustment =
  1120. cpi->kf_overspend_bits / estimate_keyframe_frequency(cpi);
  1121. }
  1122. cpi->frames_since_key = 0;
  1123. cpi->key_frame_count++;
  1124. }
  1125. void vp8_compute_frame_size_bounds(VP8_COMP *cpi, int *frame_under_shoot_limit,
  1126. int *frame_over_shoot_limit) {
  1127. /* Set-up bounds on acceptable frame size: */
  1128. if (cpi->oxcf.fixed_q >= 0) {
  1129. /* Fixed Q scenario: frame size never outranges target
  1130. * (there is no target!)
  1131. */
  1132. *frame_under_shoot_limit = 0;
  1133. *frame_over_shoot_limit = INT_MAX;
  1134. } else {
  1135. if (cpi->common.frame_type == KEY_FRAME) {
  1136. *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8;
  1137. *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8;
  1138. } else {
  1139. if (cpi->oxcf.number_of_layers > 1 || cpi->common.refresh_alt_ref_frame ||
  1140. cpi->common.refresh_golden_frame) {
  1141. *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8;
  1142. *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8;
  1143. } else {
  1144. /* For CBR take buffer fullness into account */
  1145. if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
  1146. if (cpi->buffer_level >= ((cpi->oxcf.optimal_buffer_level +
  1147. cpi->oxcf.maximum_buffer_size) >>
  1148. 1)) {
  1149. /* Buffer is too full so relax overshoot and tighten
  1150. * undershoot
  1151. */
  1152. *frame_over_shoot_limit = cpi->this_frame_target * 12 / 8;
  1153. *frame_under_shoot_limit = cpi->this_frame_target * 6 / 8;
  1154. } else if (cpi->buffer_level <=
  1155. (cpi->oxcf.optimal_buffer_level >> 1)) {
  1156. /* Buffer is too low so relax undershoot and tighten
  1157. * overshoot
  1158. */
  1159. *frame_over_shoot_limit = cpi->this_frame_target * 10 / 8;
  1160. *frame_under_shoot_limit = cpi->this_frame_target * 4 / 8;
  1161. } else {
  1162. *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8;
  1163. *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8;
  1164. }
  1165. }
  1166. /* VBR and CQ mode */
  1167. /* Note that tighter restrictions here can help quality
  1168. * but hurt encode speed
  1169. */
  1170. else {
  1171. /* Stron overshoot limit for constrained quality */
  1172. if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
  1173. *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8;
  1174. *frame_under_shoot_limit = cpi->this_frame_target * 2 / 8;
  1175. } else {
  1176. *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8;
  1177. *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8;
  1178. }
  1179. }
  1180. }
  1181. }
  1182. /* For very small rate targets where the fractional adjustment
  1183. * (eg * 7/8) may be tiny make sure there is at least a minimum
  1184. * range.
  1185. */
  1186. *frame_over_shoot_limit += 200;
  1187. *frame_under_shoot_limit -= 200;
  1188. if (*frame_under_shoot_limit < 0) *frame_under_shoot_limit = 0;
  1189. }
  1190. }
  1191. /* return of 0 means drop frame */
  1192. int vp8_pick_frame_size(VP8_COMP *cpi) {
  1193. VP8_COMMON *cm = &cpi->common;
  1194. if (cm->frame_type == KEY_FRAME) {
  1195. calc_iframe_target_size(cpi);
  1196. } else {
  1197. calc_pframe_target_size(cpi);
  1198. /* Check if we're dropping the frame: */
  1199. if (cpi->drop_frame) {
  1200. cpi->drop_frame = 0;
  1201. return 0;
  1202. }
  1203. }
  1204. return 1;
  1205. }
  1206. // If this just encoded frame (mcomp/transform/quant, but before loopfilter and
  1207. // pack_bitstream) has large overshoot, and was not being encoded close to the
  1208. // max QP, then drop this frame and force next frame to be encoded at max QP.
  1209. // Condition this on 1 pass CBR with screen content mode and frame dropper off.
  1210. // TODO(marpan): Should do this exit condition during the encode_frame
  1211. // (i.e., halfway during the encoding of the frame) to save cycles.
  1212. int vp8_drop_encodedframe_overshoot(VP8_COMP *cpi, int Q) {
  1213. if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
  1214. cpi->drop_frames_allowed == 0 && cpi->common.frame_type != KEY_FRAME) {
  1215. // Note: the "projected_frame_size" from encode_frame() only gives estimate
  1216. // of mode/motion vector rate (in non-rd mode): so below we only require
  1217. // that projected_frame_size is somewhat greater than per-frame-bandwidth,
  1218. // but add additional condition with high threshold on prediction residual.
  1219. // QP threshold: only allow dropping if we are not close to qp_max.
  1220. int thresh_qp = 3 * cpi->worst_quality >> 2;
  1221. // Rate threshold, in bytes.
  1222. int thresh_rate = 2 * (cpi->av_per_frame_bandwidth >> 3);
  1223. // Threshold for the average (over all macroblocks) of the pixel-sum
  1224. // residual error over 16x16 block. Should add QP dependence on threshold?
  1225. int thresh_pred_err_mb = (256 << 4);
  1226. int pred_err_mb = (int)(cpi->mb.prediction_error / cpi->common.MBs);
  1227. if (Q < thresh_qp && cpi->projected_frame_size > thresh_rate &&
  1228. pred_err_mb > thresh_pred_err_mb) {
  1229. double new_correction_factor;
  1230. const int target_size = cpi->av_per_frame_bandwidth;
  1231. int target_bits_per_mb;
  1232. // Drop this frame: advance frame counters, and set force_maxqp flag.
  1233. cpi->common.current_video_frame++;
  1234. cpi->frames_since_key++;
  1235. // Flag to indicate we will force next frame to be encoded at max QP.
  1236. cpi->force_maxqp = 1;
  1237. // Reset the buffer levels.
  1238. cpi->buffer_level = cpi->oxcf.optimal_buffer_level;
  1239. cpi->bits_off_target = cpi->oxcf.optimal_buffer_level;
  1240. // Compute a new rate correction factor, corresponding to the current
  1241. // target frame size and max_QP, and adjust the rate correction factor
  1242. // upwards, if needed.
  1243. // This is to prevent a bad state where the re-encoded frame at max_QP
  1244. // undershoots significantly, and then we end up dropping every other
  1245. // frame because the QP/rate_correction_factor may have been too low
  1246. // before the drop and then takes too long to come up.
  1247. if (target_size >= (INT_MAX >> BPER_MB_NORMBITS)) {
  1248. target_bits_per_mb = (target_size / cpi->common.MBs)
  1249. << BPER_MB_NORMBITS;
  1250. } else {
  1251. target_bits_per_mb =
  1252. (target_size << BPER_MB_NORMBITS) / cpi->common.MBs;
  1253. }
  1254. // Rate correction factor based on target_size_per_mb and max_QP.
  1255. new_correction_factor =
  1256. (double)target_bits_per_mb /
  1257. (double)vp8_bits_per_mb[INTER_FRAME][cpi->worst_quality];
  1258. if (new_correction_factor > cpi->rate_correction_factor) {
  1259. cpi->rate_correction_factor =
  1260. VPXMIN(2.0 * cpi->rate_correction_factor, new_correction_factor);
  1261. }
  1262. if (cpi->rate_correction_factor > MAX_BPB_FACTOR) {
  1263. cpi->rate_correction_factor = MAX_BPB_FACTOR;
  1264. }
  1265. return 1;
  1266. } else {
  1267. cpi->force_maxqp = 0;
  1268. return 0;
  1269. }
  1270. cpi->force_maxqp = 0;
  1271. return 0;
  1272. }
  1273. cpi->force_maxqp = 0;
  1274. return 0;
  1275. }