ratectrl.c 56 KB

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