2
0

ratectrl.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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. cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
  418. } else { /* 1 Pass */
  419. cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
  420. if (cpi->last_boost > 750) cpi->frames_till_gf_update_due++;
  421. if (cpi->last_boost > 1000) cpi->frames_till_gf_update_due++;
  422. if (cpi->last_boost > 1250) cpi->frames_till_gf_update_due++;
  423. if (cpi->last_boost >= 1500) cpi->frames_till_gf_update_due++;
  424. if (gf_interval_table[gf_frame_useage] > cpi->frames_till_gf_update_due) {
  425. cpi->frames_till_gf_update_due = gf_interval_table[gf_frame_useage];
  426. }
  427. if (cpi->frames_till_gf_update_due > cpi->max_gf_interval) {
  428. cpi->frames_till_gf_update_due = cpi->max_gf_interval;
  429. }
  430. }
  431. } else {
  432. cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
  433. }
  434. /* ARF on or off */
  435. if (cpi->pass != 2) {
  436. /* For now Alt ref is not allowed except in 2 pass modes. */
  437. cpi->source_alt_ref_pending = 0;
  438. /*if ( cpi->oxcf.fixed_q == -1)
  439. {
  440. if ( cpi->oxcf.play_alternate && (cpi->last_boost > (100 +
  441. (AF_THRESH*cpi->frames_till_gf_update_due)) ) )
  442. cpi->source_alt_ref_pending = 1;
  443. else
  444. cpi->source_alt_ref_pending = 0;
  445. }*/
  446. }
  447. }
  448. static void calc_pframe_target_size(VP8_COMP *cpi) {
  449. int min_frame_target;
  450. int old_per_frame_bandwidth = cpi->per_frame_bandwidth;
  451. if (cpi->current_layer > 0) {
  452. cpi->per_frame_bandwidth =
  453. cpi->layer_context[cpi->current_layer].avg_frame_size_for_layer;
  454. }
  455. min_frame_target = 0;
  456. if (cpi->pass == 2) {
  457. min_frame_target = cpi->min_frame_bandwidth;
  458. if (min_frame_target < (cpi->av_per_frame_bandwidth >> 5)) {
  459. min_frame_target = cpi->av_per_frame_bandwidth >> 5;
  460. }
  461. } else if (min_frame_target < cpi->per_frame_bandwidth / 4) {
  462. min_frame_target = cpi->per_frame_bandwidth / 4;
  463. }
  464. /* Special alt reference frame case */
  465. if ((cpi->common.refresh_alt_ref_frame) &&
  466. (cpi->oxcf.number_of_layers == 1)) {
  467. if (cpi->pass == 2) {
  468. /* Per frame bit target for the alt ref frame */
  469. cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
  470. cpi->this_frame_target = cpi->per_frame_bandwidth;
  471. }
  472. /* One Pass ??? TBD */
  473. }
  474. /* Normal frames (gf,and inter) */
  475. else {
  476. /* 2 pass */
  477. if (cpi->pass == 2) {
  478. cpi->this_frame_target = cpi->per_frame_bandwidth;
  479. }
  480. /* 1 pass */
  481. else {
  482. int Adjustment;
  483. /* Make rate adjustment to recover bits spent in key frame
  484. * Test to see if the key frame inter data rate correction
  485. * should still be in force
  486. */
  487. if (cpi->kf_overspend_bits > 0) {
  488. Adjustment = (cpi->kf_bitrate_adjustment <= cpi->kf_overspend_bits)
  489. ? cpi->kf_bitrate_adjustment
  490. : cpi->kf_overspend_bits;
  491. if (Adjustment > (cpi->per_frame_bandwidth - min_frame_target)) {
  492. Adjustment = (cpi->per_frame_bandwidth - min_frame_target);
  493. }
  494. cpi->kf_overspend_bits -= Adjustment;
  495. /* Calculate an inter frame bandwidth target for the next
  496. * few frames designed to recover any extra bits spent on
  497. * the key frame.
  498. */
  499. cpi->this_frame_target = cpi->per_frame_bandwidth - Adjustment;
  500. if (cpi->this_frame_target < min_frame_target) {
  501. cpi->this_frame_target = min_frame_target;
  502. }
  503. } else {
  504. cpi->this_frame_target = cpi->per_frame_bandwidth;
  505. }
  506. /* If appropriate make an adjustment to recover bits spent on a
  507. * recent GF
  508. */
  509. if ((cpi->gf_overspend_bits > 0) &&
  510. (cpi->this_frame_target > min_frame_target)) {
  511. Adjustment = (cpi->non_gf_bitrate_adjustment <= cpi->gf_overspend_bits)
  512. ? cpi->non_gf_bitrate_adjustment
  513. : cpi->gf_overspend_bits;
  514. if (Adjustment > (cpi->this_frame_target - min_frame_target)) {
  515. Adjustment = (cpi->this_frame_target - min_frame_target);
  516. }
  517. cpi->gf_overspend_bits -= Adjustment;
  518. cpi->this_frame_target -= Adjustment;
  519. }
  520. /* Apply small + and - boosts for non gf frames */
  521. if ((cpi->last_boost > 150) && (cpi->frames_till_gf_update_due > 0) &&
  522. (cpi->current_gf_interval >= (MIN_GF_INTERVAL << 1))) {
  523. /* % Adjustment limited to the range 1% to 10% */
  524. Adjustment = (cpi->last_boost - 100) >> 5;
  525. if (Adjustment < 1) {
  526. Adjustment = 1;
  527. } else if (Adjustment > 10) {
  528. Adjustment = 10;
  529. }
  530. /* Convert to bits */
  531. Adjustment = (cpi->this_frame_target * Adjustment) / 100;
  532. if (Adjustment > (cpi->this_frame_target - min_frame_target)) {
  533. Adjustment = (cpi->this_frame_target - min_frame_target);
  534. }
  535. if (cpi->frames_since_golden == (cpi->current_gf_interval >> 1)) {
  536. Adjustment = (cpi->current_gf_interval - 1) * Adjustment;
  537. // Limit adjustment to 10% of current target.
  538. if (Adjustment > (10 * cpi->this_frame_target) / 100) {
  539. Adjustment = (10 * cpi->this_frame_target) / 100;
  540. }
  541. cpi->this_frame_target += Adjustment;
  542. } else {
  543. cpi->this_frame_target -= Adjustment;
  544. }
  545. }
  546. }
  547. }
  548. /* Sanity check that the total sum of adjustments is not above the
  549. * maximum allowed That is that having allowed for KF and GF penalties
  550. * we have not pushed the current interframe target to low. If the
  551. * adjustment we apply here is not capable of recovering all the extra
  552. * bits we have spent in the KF or GF then the remainder will have to
  553. * be recovered over a longer time span via other buffer / rate control
  554. * mechanisms.
  555. */
  556. if (cpi->this_frame_target < min_frame_target) {
  557. cpi->this_frame_target = min_frame_target;
  558. }
  559. if (!cpi->common.refresh_alt_ref_frame) {
  560. /* Note the baseline target data rate for this inter frame. */
  561. cpi->inter_frame_target = cpi->this_frame_target;
  562. }
  563. /* One Pass specific code */
  564. if (cpi->pass == 0) {
  565. /* Adapt target frame size with respect to any buffering constraints: */
  566. if (cpi->buffered_mode) {
  567. int one_percent_bits = (int)(1 + cpi->oxcf.optimal_buffer_level / 100);
  568. if ((cpi->buffer_level < cpi->oxcf.optimal_buffer_level) ||
  569. (cpi->bits_off_target < cpi->oxcf.optimal_buffer_level)) {
  570. int percent_low = 0;
  571. /* Decide whether or not we need to adjust the frame data
  572. * rate target.
  573. *
  574. * If we are are below the optimal buffer fullness level
  575. * and adherence to buffering constraints is important to
  576. * the end usage then adjust the per frame target.
  577. */
  578. if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
  579. (cpi->buffer_level < cpi->oxcf.optimal_buffer_level)) {
  580. percent_low =
  581. (int)((cpi->oxcf.optimal_buffer_level - cpi->buffer_level) /
  582. one_percent_bits);
  583. }
  584. /* Are we overshooting the long term clip data rate... */
  585. else if (cpi->bits_off_target < 0) {
  586. /* Adjust per frame data target downwards to compensate. */
  587. percent_low =
  588. (int)(100 * -cpi->bits_off_target / (cpi->total_byte_count * 8));
  589. }
  590. if (percent_low > cpi->oxcf.under_shoot_pct) {
  591. percent_low = cpi->oxcf.under_shoot_pct;
  592. } else if (percent_low < 0) {
  593. percent_low = 0;
  594. }
  595. /* lower the target bandwidth for this frame. */
  596. cpi->this_frame_target -= (cpi->this_frame_target * percent_low) / 200;
  597. /* Are we using allowing control of active_worst_allowed_q
  598. * according to buffer level.
  599. */
  600. if (cpi->auto_worst_q && cpi->ni_frames > 150) {
  601. int64_t critical_buffer_level;
  602. /* For streaming applications the most important factor is
  603. * cpi->buffer_level as this takes into account the
  604. * specified short term buffering constraints. However,
  605. * hitting the long term clip data rate target is also
  606. * important.
  607. */
  608. if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
  609. /* Take the smaller of cpi->buffer_level and
  610. * cpi->bits_off_target
  611. */
  612. critical_buffer_level = (cpi->buffer_level < cpi->bits_off_target)
  613. ? cpi->buffer_level
  614. : cpi->bits_off_target;
  615. }
  616. /* For local file playback short term buffering constraints
  617. * are less of an issue
  618. */
  619. else {
  620. /* Consider only how we are doing for the clip as a
  621. * whole
  622. */
  623. critical_buffer_level = cpi->bits_off_target;
  624. }
  625. /* Set the active worst quality based upon the selected
  626. * buffer fullness number.
  627. */
  628. if (critical_buffer_level < cpi->oxcf.optimal_buffer_level) {
  629. if (critical_buffer_level > (cpi->oxcf.optimal_buffer_level >> 2)) {
  630. int64_t qadjustment_range = cpi->worst_quality - cpi->ni_av_qi;
  631. int64_t above_base = (critical_buffer_level -
  632. (cpi->oxcf.optimal_buffer_level >> 2));
  633. /* Step active worst quality down from
  634. * cpi->ni_av_qi when (critical_buffer_level ==
  635. * cpi->optimal_buffer_level) to
  636. * cpi->worst_quality when
  637. * (critical_buffer_level ==
  638. * cpi->optimal_buffer_level >> 2)
  639. */
  640. cpi->active_worst_quality =
  641. cpi->worst_quality -
  642. (int)((qadjustment_range * above_base) /
  643. (cpi->oxcf.optimal_buffer_level * 3 >> 2));
  644. } else {
  645. cpi->active_worst_quality = cpi->worst_quality;
  646. }
  647. } else {
  648. cpi->active_worst_quality = cpi->ni_av_qi;
  649. }
  650. } else {
  651. cpi->active_worst_quality = cpi->worst_quality;
  652. }
  653. } else {
  654. int percent_high = 0;
  655. if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
  656. (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)) {
  657. percent_high =
  658. (int)((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) /
  659. one_percent_bits);
  660. } else if (cpi->bits_off_target > cpi->oxcf.optimal_buffer_level) {
  661. percent_high =
  662. (int)((100 * cpi->bits_off_target) / (cpi->total_byte_count * 8));
  663. }
  664. if (percent_high > cpi->oxcf.over_shoot_pct) {
  665. percent_high = cpi->oxcf.over_shoot_pct;
  666. } else if (percent_high < 0) {
  667. percent_high = 0;
  668. }
  669. cpi->this_frame_target += (cpi->this_frame_target * percent_high) / 200;
  670. /* Are we allowing control of active_worst_allowed_q according
  671. * to buffer level.
  672. */
  673. if (cpi->auto_worst_q && cpi->ni_frames > 150) {
  674. /* When using the relaxed buffer model stick to the
  675. * user specified value
  676. */
  677. cpi->active_worst_quality = cpi->ni_av_qi;
  678. } else {
  679. cpi->active_worst_quality = cpi->worst_quality;
  680. }
  681. }
  682. /* Set active_best_quality to prevent quality rising too high */
  683. cpi->active_best_quality = cpi->best_quality;
  684. /* Worst quality obviously must not be better than best quality */
  685. if (cpi->active_worst_quality <= cpi->active_best_quality) {
  686. cpi->active_worst_quality = cpi->active_best_quality + 1;
  687. }
  688. if (cpi->active_worst_quality > 127) cpi->active_worst_quality = 127;
  689. }
  690. /* Unbuffered mode (eg. video conferencing) */
  691. else {
  692. /* Set the active worst quality */
  693. cpi->active_worst_quality = cpi->worst_quality;
  694. }
  695. /* Special trap for constrained quality mode
  696. * "active_worst_quality" may never drop below cq level
  697. * for any frame type.
  698. */
  699. if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY &&
  700. cpi->active_worst_quality < cpi->cq_target_quality) {
  701. cpi->active_worst_quality = cpi->cq_target_quality;
  702. }
  703. }
  704. /* Test to see if we have to drop a frame
  705. * The auto-drop frame code is only used in buffered mode.
  706. * In unbufferd mode (eg vide conferencing) the descision to
  707. * code or drop a frame is made outside the codec in response to real
  708. * world comms or buffer considerations.
  709. */
  710. if (cpi->drop_frames_allowed &&
  711. (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
  712. ((cpi->common.frame_type != KEY_FRAME))) {
  713. /* Check for a buffer underun-crisis in which case we have to drop
  714. * a frame
  715. */
  716. if ((cpi->buffer_level < 0)) {
  717. #if 0
  718. FILE *f = fopen("dec.stt", "a");
  719. fprintf(f, "%10d %10d %10d %10d ***** BUFFER EMPTY\n",
  720. (int) cpi->common.current_video_frame,
  721. cpi->decimation_factor, cpi->common.horiz_scale,
  722. (cpi->buffer_level * 100) / cpi->oxcf.optimal_buffer_level);
  723. fclose(f);
  724. #endif
  725. cpi->drop_frame = 1;
  726. /* Update the buffer level variable. */
  727. cpi->bits_off_target += cpi->av_per_frame_bandwidth;
  728. if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
  729. cpi->bits_off_target = (int)cpi->oxcf.maximum_buffer_size;
  730. }
  731. cpi->buffer_level = cpi->bits_off_target;
  732. if (cpi->oxcf.number_of_layers > 1) {
  733. unsigned int i;
  734. // Propagate bits saved by dropping the frame to higher layers.
  735. for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) {
  736. LAYER_CONTEXT *lc = &cpi->layer_context[i];
  737. lc->bits_off_target += (int)(lc->target_bandwidth / lc->framerate);
  738. if (lc->bits_off_target > lc->maximum_buffer_size) {
  739. lc->bits_off_target = lc->maximum_buffer_size;
  740. }
  741. lc->buffer_level = lc->bits_off_target;
  742. }
  743. }
  744. }
  745. }
  746. /* Adjust target frame size for Golden Frames: */
  747. if (cpi->oxcf.error_resilient_mode == 0 &&
  748. (cpi->frames_till_gf_update_due == 0) && !cpi->drop_frame) {
  749. if (!cpi->gf_update_onepass_cbr) {
  750. int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME]
  751. : 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. FILE *f;
  782. f = fopen("gf_useaget.stt", "a");
  783. fprintf(f, " %8ld %10ld %10ld %10ld %10ld\n",
  784. cpi->common.current_video_frame, cpi->gfu_boost,
  785. 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. FILE *f;
  793. f = fopen("GFexit.stt", "a");
  794. fprintf(f, "%8ld GF coded\n", cpi->common.current_video_frame);
  795. fclose(f);
  796. }
  797. #endif
  798. if (cpi->auto_adjust_gold_quantizer) {
  799. calc_gf_params(cpi);
  800. }
  801. /* If we are using alternate ref instead of gf then do not apply the
  802. * boost It will instead be applied to the altref update Jims
  803. * modified boost
  804. */
  805. if (!cpi->source_alt_ref_active) {
  806. if (cpi->oxcf.fixed_q < 0) {
  807. if (cpi->pass == 2) {
  808. /* The spend on the GF is defined in the two pass
  809. * code for two pass encodes
  810. */
  811. cpi->this_frame_target = cpi->per_frame_bandwidth;
  812. } else {
  813. int Boost = cpi->last_boost;
  814. int frames_in_section = cpi->frames_till_gf_update_due + 1;
  815. int allocation_chunks = (frames_in_section * 100) + (Boost - 100);
  816. int bits_in_section = cpi->inter_frame_target * frames_in_section;
  817. /* Normalize Altboost and allocations chunck down to
  818. * prevent overflow
  819. */
  820. while (Boost > 1000) {
  821. Boost /= 2;
  822. allocation_chunks /= 2;
  823. }
  824. /* Avoid loss of precision but avoid overflow */
  825. if ((bits_in_section >> 7) > allocation_chunks) {
  826. cpi->this_frame_target =
  827. Boost * (bits_in_section / allocation_chunks);
  828. } else {
  829. cpi->this_frame_target =
  830. (Boost * bits_in_section) / allocation_chunks;
  831. }
  832. }
  833. } else {
  834. cpi->this_frame_target =
  835. (estimate_bits_at_q(1, Q, cpi->common.MBs, 1.0) *
  836. cpi->last_boost) /
  837. 100;
  838. }
  839. } else {
  840. /* If there is an active ARF at this location use the minimum
  841. * bits on this frame even if it is a contructed arf.
  842. * The active maximum quantizer insures that an appropriate
  843. * number of bits will be spent if needed for contstructed ARFs.
  844. */
  845. cpi->this_frame_target = 0;
  846. }
  847. cpi->current_gf_interval = cpi->frames_till_gf_update_due;
  848. }
  849. } else {
  850. // Special case for 1 pass CBR: fixed gf period.
  851. // TODO(marpan): Adjust this boost/interval logic.
  852. // If gf_cbr_boost_pct is small (below threshold) set the flag
  853. // gf_noboost_onepass_cbr = 1, which forces the gf to use the same
  854. // rate correction factor as last.
  855. cpi->gf_noboost_onepass_cbr = (cpi->oxcf.gf_cbr_boost_pct <= 100);
  856. cpi->baseline_gf_interval = cpi->gf_interval_onepass_cbr;
  857. // Skip this update if the zero_mvcount is low.
  858. if (cpi->zeromv_count > (cpi->common.MBs >> 1)) {
  859. cpi->common.refresh_golden_frame = 1;
  860. cpi->this_frame_target =
  861. (cpi->this_frame_target * (100 + cpi->oxcf.gf_cbr_boost_pct)) / 100;
  862. }
  863. cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
  864. cpi->current_gf_interval = cpi->frames_till_gf_update_due;
  865. }
  866. }
  867. cpi->per_frame_bandwidth = old_per_frame_bandwidth;
  868. }
  869. void vp8_update_rate_correction_factors(VP8_COMP *cpi, int damp_var) {
  870. int Q = cpi->common.base_qindex;
  871. int correction_factor = 100;
  872. double rate_correction_factor;
  873. double adjustment_limit;
  874. int projected_size_based_on_q = 0;
  875. /* Clear down mmx registers to allow floating point in what follows */
  876. vpx_clear_system_state();
  877. if (cpi->common.frame_type == KEY_FRAME) {
  878. rate_correction_factor = cpi->key_frame_rate_correction_factor;
  879. } else {
  880. if (cpi->oxcf.number_of_layers == 1 && !cpi->gf_noboost_onepass_cbr &&
  881. (cpi->common.refresh_alt_ref_frame ||
  882. cpi->common.refresh_golden_frame)) {
  883. rate_correction_factor = cpi->gf_rate_correction_factor;
  884. } else {
  885. rate_correction_factor = cpi->rate_correction_factor;
  886. }
  887. }
  888. /* Work out how big we would have expected the frame to be at this Q
  889. * given the current correction factor. Stay in double to avoid int
  890. * overflow when values are large
  891. */
  892. projected_size_based_on_q =
  893. (int)(((.5 + rate_correction_factor *
  894. vp8_bits_per_mb[cpi->common.frame_type][Q]) *
  895. cpi->common.MBs) /
  896. (1 << BPER_MB_NORMBITS));
  897. /* Make some allowance for cpi->zbin_over_quant */
  898. if (cpi->mb.zbin_over_quant > 0) {
  899. int Z = cpi->mb.zbin_over_quant;
  900. double Factor = 0.99;
  901. double factor_adjustment = 0.01 / 256.0;
  902. while (Z > 0) {
  903. Z--;
  904. projected_size_based_on_q = (int)(Factor * projected_size_based_on_q);
  905. Factor += factor_adjustment;
  906. if (Factor >= 0.999) Factor = 0.999;
  907. }
  908. }
  909. /* Work out a size correction factor. */
  910. if (projected_size_based_on_q > 0) {
  911. correction_factor =
  912. (100 * cpi->projected_frame_size) / projected_size_based_on_q;
  913. }
  914. /* More heavily damped adjustment used if we have been oscillating
  915. * either side of target
  916. */
  917. switch (damp_var) {
  918. case 0: adjustment_limit = 0.75; break;
  919. case 1: adjustment_limit = 0.375; break;
  920. case 2:
  921. default: adjustment_limit = 0.25; break;
  922. }
  923. if (correction_factor > 102) {
  924. /* We are not already at the worst allowable quality */
  925. correction_factor =
  926. (int)(100.5 + ((correction_factor - 100) * adjustment_limit));
  927. rate_correction_factor =
  928. ((rate_correction_factor * correction_factor) / 100);
  929. /* Keep rate_correction_factor within limits */
  930. if (rate_correction_factor > MAX_BPB_FACTOR) {
  931. rate_correction_factor = MAX_BPB_FACTOR;
  932. }
  933. } else if (correction_factor < 99) {
  934. /* We are not already at the best allowable quality */
  935. correction_factor =
  936. (int)(100.5 - ((100 - correction_factor) * adjustment_limit));
  937. rate_correction_factor =
  938. ((rate_correction_factor * correction_factor) / 100);
  939. /* Keep rate_correction_factor within limits */
  940. if (rate_correction_factor < MIN_BPB_FACTOR) {
  941. rate_correction_factor = MIN_BPB_FACTOR;
  942. }
  943. }
  944. if (cpi->common.frame_type == KEY_FRAME) {
  945. cpi->key_frame_rate_correction_factor = rate_correction_factor;
  946. } else {
  947. if (cpi->oxcf.number_of_layers == 1 && !cpi->gf_noboost_onepass_cbr &&
  948. (cpi->common.refresh_alt_ref_frame ||
  949. cpi->common.refresh_golden_frame)) {
  950. cpi->gf_rate_correction_factor = rate_correction_factor;
  951. } else {
  952. cpi->rate_correction_factor = rate_correction_factor;
  953. }
  954. }
  955. }
  956. static int limit_q_cbr_inter(int last_q, int current_q) {
  957. int limit_down = 12;
  958. if (last_q - current_q > limit_down)
  959. return (last_q - limit_down);
  960. else
  961. return current_q;
  962. }
  963. int vp8_regulate_q(VP8_COMP *cpi, int target_bits_per_frame) {
  964. int Q = cpi->active_worst_quality;
  965. if (cpi->force_maxqp == 1) {
  966. cpi->active_worst_quality = cpi->worst_quality;
  967. return cpi->worst_quality;
  968. }
  969. /* Reset Zbin OQ value */
  970. cpi->mb.zbin_over_quant = 0;
  971. if (cpi->oxcf.fixed_q >= 0) {
  972. Q = cpi->oxcf.fixed_q;
  973. if (cpi->common.frame_type == KEY_FRAME) {
  974. Q = cpi->oxcf.key_q;
  975. } else if (cpi->oxcf.number_of_layers == 1 &&
  976. cpi->common.refresh_alt_ref_frame &&
  977. !cpi->gf_noboost_onepass_cbr) {
  978. Q = cpi->oxcf.alt_q;
  979. } else if (cpi->oxcf.number_of_layers == 1 &&
  980. cpi->common.refresh_golden_frame &&
  981. !cpi->gf_noboost_onepass_cbr) {
  982. Q = cpi->oxcf.gold_q;
  983. }
  984. } else {
  985. int i;
  986. int last_error = INT_MAX;
  987. int target_bits_per_mb;
  988. int bits_per_mb_at_this_q;
  989. double correction_factor;
  990. /* Select the appropriate correction factor based upon type of frame. */
  991. if (cpi->common.frame_type == KEY_FRAME) {
  992. correction_factor = cpi->key_frame_rate_correction_factor;
  993. } else {
  994. if (cpi->oxcf.number_of_layers == 1 && !cpi->gf_noboost_onepass_cbr &&
  995. (cpi->common.refresh_alt_ref_frame ||
  996. cpi->common.refresh_golden_frame)) {
  997. correction_factor = cpi->gf_rate_correction_factor;
  998. } else {
  999. correction_factor = cpi->rate_correction_factor;
  1000. }
  1001. }
  1002. /* Calculate required scaling factor based on target frame size and
  1003. * size of frame produced using previous Q
  1004. */
  1005. if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS)) {
  1006. /* Case where we would overflow int */
  1007. target_bits_per_mb = (target_bits_per_frame / cpi->common.MBs)
  1008. << BPER_MB_NORMBITS;
  1009. } else {
  1010. target_bits_per_mb =
  1011. (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->common.MBs;
  1012. }
  1013. i = cpi->active_best_quality;
  1014. do {
  1015. bits_per_mb_at_this_q =
  1016. (int)(.5 +
  1017. correction_factor * vp8_bits_per_mb[cpi->common.frame_type][i]);
  1018. if (bits_per_mb_at_this_q <= target_bits_per_mb) {
  1019. if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) {
  1020. Q = i;
  1021. } else {
  1022. Q = i - 1;
  1023. }
  1024. break;
  1025. } else {
  1026. last_error = bits_per_mb_at_this_q - target_bits_per_mb;
  1027. }
  1028. } while (++i <= cpi->active_worst_quality);
  1029. /* If we are at MAXQ then enable Q over-run which seeks to claw
  1030. * back additional bits through things like the RD multiplier
  1031. * and zero bin size.
  1032. */
  1033. if (Q >= MAXQ) {
  1034. int zbin_oqmax;
  1035. double Factor = 0.99;
  1036. double factor_adjustment = 0.01 / 256.0;
  1037. if (cpi->common.frame_type == KEY_FRAME) {
  1038. zbin_oqmax = 0;
  1039. } else if (cpi->oxcf.number_of_layers == 1 &&
  1040. !cpi->gf_noboost_onepass_cbr &&
  1041. (cpi->common.refresh_alt_ref_frame ||
  1042. (cpi->common.refresh_golden_frame &&
  1043. !cpi->source_alt_ref_active))) {
  1044. zbin_oqmax = 16;
  1045. } else {
  1046. zbin_oqmax = ZBIN_OQ_MAX;
  1047. }
  1048. /*{
  1049. double Factor =
  1050. (double)target_bits_per_mb/(double)bits_per_mb_at_this_q;
  1051. double Oq;
  1052. Factor = Factor/1.2683;
  1053. Oq = pow( Factor, (1.0/-0.165) );
  1054. if ( Oq > zbin_oqmax )
  1055. Oq = zbin_oqmax;
  1056. cpi->zbin_over_quant = (int)Oq;
  1057. }*/
  1058. /* Each incrment in the zbin is assumed to have a fixed effect
  1059. * on bitrate. This is not of course true. The effect will be
  1060. * highly clip dependent and may well have sudden steps. The
  1061. * idea here is to acheive higher effective quantizers than the
  1062. * normal maximum by expanding the zero bin and hence
  1063. * decreasing the number of low magnitude non zero coefficients.
  1064. */
  1065. while (cpi->mb.zbin_over_quant < zbin_oqmax) {
  1066. cpi->mb.zbin_over_quant++;
  1067. if (cpi->mb.zbin_over_quant > zbin_oqmax) {
  1068. cpi->mb.zbin_over_quant = zbin_oqmax;
  1069. }
  1070. /* Adjust bits_per_mb_at_this_q estimate */
  1071. bits_per_mb_at_this_q = (int)(Factor * bits_per_mb_at_this_q);
  1072. Factor += factor_adjustment;
  1073. if (Factor >= 0.999) Factor = 0.999;
  1074. /* Break out if we get down to the target rate */
  1075. if (bits_per_mb_at_this_q <= target_bits_per_mb) break;
  1076. }
  1077. }
  1078. }
  1079. // Limit decrease in Q for 1 pass CBR screen content mode.
  1080. if (cpi->common.frame_type != KEY_FRAME && cpi->pass == 0 &&
  1081. cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
  1082. cpi->oxcf.screen_content_mode)
  1083. Q = limit_q_cbr_inter(cpi->last_q[1], Q);
  1084. return Q;
  1085. }
  1086. static int estimate_keyframe_frequency(VP8_COMP *cpi) {
  1087. int i;
  1088. /* Average key frame frequency */
  1089. int av_key_frame_frequency = 0;
  1090. /* First key frame at start of sequence is a special case. We have no
  1091. * frequency data.
  1092. */
  1093. if (cpi->key_frame_count == 1) {
  1094. /* Assume a default of 1 kf every 2 seconds, or the max kf interval,
  1095. * whichever is smaller.
  1096. */
  1097. int key_freq = cpi->oxcf.key_freq > 0 ? cpi->oxcf.key_freq : 1;
  1098. av_key_frame_frequency = 1 + (int)cpi->output_framerate * 2;
  1099. if (cpi->oxcf.auto_key && av_key_frame_frequency > key_freq) {
  1100. av_key_frame_frequency = key_freq;
  1101. }
  1102. cpi->prior_key_frame_distance[KEY_FRAME_CONTEXT - 1] =
  1103. av_key_frame_frequency;
  1104. } else {
  1105. unsigned int total_weight = 0;
  1106. int last_kf_interval =
  1107. (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1;
  1108. /* reset keyframe context and calculate weighted average of last
  1109. * KEY_FRAME_CONTEXT keyframes
  1110. */
  1111. for (i = 0; i < KEY_FRAME_CONTEXT; ++i) {
  1112. if (i < KEY_FRAME_CONTEXT - 1) {
  1113. cpi->prior_key_frame_distance[i] = cpi->prior_key_frame_distance[i + 1];
  1114. } else {
  1115. cpi->prior_key_frame_distance[i] = last_kf_interval;
  1116. }
  1117. av_key_frame_frequency +=
  1118. prior_key_frame_weight[i] * cpi->prior_key_frame_distance[i];
  1119. total_weight += prior_key_frame_weight[i];
  1120. }
  1121. av_key_frame_frequency /= total_weight;
  1122. }
  1123. // TODO (marpan): Given the checks above, |av_key_frame_frequency|
  1124. // should always be above 0. But for now we keep the sanity check in.
  1125. if (av_key_frame_frequency == 0) av_key_frame_frequency = 1;
  1126. return av_key_frame_frequency;
  1127. }
  1128. void vp8_adjust_key_frame_context(VP8_COMP *cpi) {
  1129. /* Clear down mmx registers to allow floating point in what follows */
  1130. vpx_clear_system_state();
  1131. /* Do we have any key frame overspend to recover? */
  1132. /* Two-pass overspend handled elsewhere. */
  1133. if ((cpi->pass != 2) &&
  1134. (cpi->projected_frame_size > cpi->per_frame_bandwidth)) {
  1135. int overspend;
  1136. /* Update the count of key frame overspend to be recovered in
  1137. * subsequent frames. A portion of the KF overspend is treated as gf
  1138. * overspend (and hence recovered more quickly) as the kf is also a
  1139. * gf. Otherwise the few frames following each kf tend to get more
  1140. * bits allocated than those following other gfs.
  1141. */
  1142. overspend = (cpi->projected_frame_size - cpi->per_frame_bandwidth);
  1143. if (cpi->oxcf.number_of_layers > 1) {
  1144. cpi->kf_overspend_bits += overspend;
  1145. } else {
  1146. cpi->kf_overspend_bits += overspend * 7 / 8;
  1147. cpi->gf_overspend_bits += overspend * 1 / 8;
  1148. }
  1149. /* Work out how much to try and recover per frame. */
  1150. cpi->kf_bitrate_adjustment =
  1151. cpi->kf_overspend_bits / estimate_keyframe_frequency(cpi);
  1152. }
  1153. cpi->frames_since_key = 0;
  1154. cpi->key_frame_count++;
  1155. }
  1156. void vp8_compute_frame_size_bounds(VP8_COMP *cpi, int *frame_under_shoot_limit,
  1157. int *frame_over_shoot_limit) {
  1158. /* Set-up bounds on acceptable frame size: */
  1159. if (cpi->oxcf.fixed_q >= 0) {
  1160. /* Fixed Q scenario: frame size never outranges target
  1161. * (there is no target!)
  1162. */
  1163. *frame_under_shoot_limit = 0;
  1164. *frame_over_shoot_limit = INT_MAX;
  1165. } else {
  1166. if (cpi->common.frame_type == KEY_FRAME) {
  1167. *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8;
  1168. *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8;
  1169. } else {
  1170. if (cpi->oxcf.number_of_layers > 1 || cpi->common.refresh_alt_ref_frame ||
  1171. cpi->common.refresh_golden_frame) {
  1172. *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8;
  1173. *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8;
  1174. } else {
  1175. /* For CBR take buffer fullness into account */
  1176. if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
  1177. if (cpi->buffer_level >= ((cpi->oxcf.optimal_buffer_level +
  1178. cpi->oxcf.maximum_buffer_size) >>
  1179. 1)) {
  1180. /* Buffer is too full so relax overshoot and tighten
  1181. * undershoot
  1182. */
  1183. *frame_over_shoot_limit = cpi->this_frame_target * 12 / 8;
  1184. *frame_under_shoot_limit = cpi->this_frame_target * 6 / 8;
  1185. } else if (cpi->buffer_level <=
  1186. (cpi->oxcf.optimal_buffer_level >> 1)) {
  1187. /* Buffer is too low so relax undershoot and tighten
  1188. * overshoot
  1189. */
  1190. *frame_over_shoot_limit = cpi->this_frame_target * 10 / 8;
  1191. *frame_under_shoot_limit = cpi->this_frame_target * 4 / 8;
  1192. } else {
  1193. *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8;
  1194. *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8;
  1195. }
  1196. }
  1197. /* VBR and CQ mode */
  1198. /* Note that tighter restrictions here can help quality
  1199. * but hurt encode speed
  1200. */
  1201. else {
  1202. /* Stron overshoot limit for constrained quality */
  1203. if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
  1204. *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8;
  1205. *frame_under_shoot_limit = cpi->this_frame_target * 2 / 8;
  1206. } else {
  1207. *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8;
  1208. *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8;
  1209. }
  1210. }
  1211. }
  1212. }
  1213. /* For very small rate targets where the fractional adjustment
  1214. * (eg * 7/8) may be tiny make sure there is at least a minimum
  1215. * range.
  1216. */
  1217. *frame_over_shoot_limit += 200;
  1218. *frame_under_shoot_limit -= 200;
  1219. if (*frame_under_shoot_limit < 0) *frame_under_shoot_limit = 0;
  1220. }
  1221. }
  1222. /* return of 0 means drop frame */
  1223. int vp8_pick_frame_size(VP8_COMP *cpi) {
  1224. VP8_COMMON *cm = &cpi->common;
  1225. if (cm->frame_type == KEY_FRAME) {
  1226. calc_iframe_target_size(cpi);
  1227. } else {
  1228. calc_pframe_target_size(cpi);
  1229. /* Check if we're dropping the frame: */
  1230. if (cpi->drop_frame) {
  1231. cpi->drop_frame = 0;
  1232. return 0;
  1233. }
  1234. }
  1235. return 1;
  1236. }
  1237. // If this just encoded frame (mcomp/transform/quant, but before loopfilter and
  1238. // pack_bitstream) has large overshoot, and was not being encoded close to the
  1239. // max QP, then drop this frame and force next frame to be encoded at max QP.
  1240. // Allow this for screen_content_mode = 2, or if drop frames is allowed.
  1241. // TODO(marpan): Should do this exit condition during the encode_frame
  1242. // (i.e., halfway during the encoding of the frame) to save cycles.
  1243. int vp8_drop_encodedframe_overshoot(VP8_COMP *cpi, int Q) {
  1244. int force_drop_overshoot = 0;
  1245. #if CONFIG_MULTI_RES_ENCODING
  1246. // Only check for dropping due to overshoot on the lowest stream.
  1247. // If the lowest stream of the multi-res encoding was dropped due to
  1248. // overshoot, then force dropping on all upper layer streams
  1249. // (mr_encoder_id > 0).
  1250. LOWER_RES_FRAME_INFO *low_res_frame_info =
  1251. (LOWER_RES_FRAME_INFO *)cpi->oxcf.mr_low_res_mode_info;
  1252. if (cpi->oxcf.mr_total_resolutions > 1 && cpi->oxcf.mr_encoder_id > 0) {
  1253. force_drop_overshoot = low_res_frame_info->is_frame_dropped_overshoot_maxqp;
  1254. if (!force_drop_overshoot) {
  1255. cpi->force_maxqp = 0;
  1256. cpi->frames_since_last_drop_overshoot++;
  1257. return 0;
  1258. }
  1259. }
  1260. #endif
  1261. if (cpi->common.frame_type != KEY_FRAME &&
  1262. (cpi->oxcf.screen_content_mode == 2 ||
  1263. (cpi->drop_frames_allowed &&
  1264. (force_drop_overshoot ||
  1265. (cpi->rate_correction_factor < (8.0f * MIN_BPB_FACTOR) &&
  1266. cpi->frames_since_last_drop_overshoot > (int)cpi->framerate))))) {
  1267. // Note: the "projected_frame_size" from encode_frame() only gives estimate
  1268. // of mode/motion vector rate (in non-rd mode): so below we only require
  1269. // that projected_frame_size is somewhat greater than per-frame-bandwidth,
  1270. // but add additional condition with high threshold on prediction residual.
  1271. // QP threshold: only allow dropping if we are not close to qp_max.
  1272. int thresh_qp = 3 * cpi->worst_quality >> 2;
  1273. // Rate threshold, in bytes.
  1274. int thresh_rate = 2 * (cpi->av_per_frame_bandwidth >> 3);
  1275. // Threshold for the average (over all macroblocks) of the pixel-sum
  1276. // residual error over 16x16 block.
  1277. int thresh_pred_err_mb = (200 << 4);
  1278. int pred_err_mb = (int)(cpi->mb.prediction_error / cpi->common.MBs);
  1279. // Reduce/ignore thresh_rate if pred_err_mb much larger than its threshold,
  1280. // give more weight to pred_err metric for overshoot detection.
  1281. if (cpi->drop_frames_allowed && pred_err_mb > (thresh_pred_err_mb << 4))
  1282. thresh_rate = thresh_rate >> 3;
  1283. if ((Q < thresh_qp && cpi->projected_frame_size > thresh_rate &&
  1284. pred_err_mb > thresh_pred_err_mb &&
  1285. pred_err_mb > 2 * cpi->last_pred_err_mb) ||
  1286. force_drop_overshoot) {
  1287. unsigned int i;
  1288. double new_correction_factor;
  1289. int target_bits_per_mb;
  1290. const int target_size = cpi->av_per_frame_bandwidth;
  1291. // Flag to indicate we will force next frame to be encoded at max QP.
  1292. cpi->force_maxqp = 1;
  1293. // Reset the buffer levels.
  1294. cpi->buffer_level = cpi->oxcf.optimal_buffer_level;
  1295. cpi->bits_off_target = cpi->oxcf.optimal_buffer_level;
  1296. // Compute a new rate correction factor, corresponding to the current
  1297. // target frame size and max_QP, and adjust the rate correction factor
  1298. // upwards, if needed.
  1299. // This is to prevent a bad state where the re-encoded frame at max_QP
  1300. // undershoots significantly, and then we end up dropping every other
  1301. // frame because the QP/rate_correction_factor may have been too low
  1302. // before the drop and then takes too long to come up.
  1303. if (target_size >= (INT_MAX >> BPER_MB_NORMBITS)) {
  1304. target_bits_per_mb = (target_size / cpi->common.MBs)
  1305. << BPER_MB_NORMBITS;
  1306. } else {
  1307. target_bits_per_mb =
  1308. (target_size << BPER_MB_NORMBITS) / cpi->common.MBs;
  1309. }
  1310. // Rate correction factor based on target_size_per_mb and max_QP.
  1311. new_correction_factor =
  1312. (double)target_bits_per_mb /
  1313. (double)vp8_bits_per_mb[INTER_FRAME][cpi->worst_quality];
  1314. if (new_correction_factor > cpi->rate_correction_factor) {
  1315. cpi->rate_correction_factor =
  1316. VPXMIN(2.0 * cpi->rate_correction_factor, new_correction_factor);
  1317. }
  1318. if (cpi->rate_correction_factor > MAX_BPB_FACTOR) {
  1319. cpi->rate_correction_factor = MAX_BPB_FACTOR;
  1320. }
  1321. // Drop this frame: update frame counters.
  1322. cpi->common.current_video_frame++;
  1323. cpi->frames_since_key++;
  1324. cpi->temporal_pattern_counter++;
  1325. cpi->frames_since_last_drop_overshoot = 0;
  1326. if (cpi->oxcf.number_of_layers > 1) {
  1327. // Set max_qp and rate correction for all temporal layers if overshoot
  1328. // is detected.
  1329. for (i = 0; i < cpi->oxcf.number_of_layers; ++i) {
  1330. LAYER_CONTEXT *lc = &cpi->layer_context[i];
  1331. lc->force_maxqp = 1;
  1332. lc->frames_since_last_drop_overshoot = 0;
  1333. lc->rate_correction_factor = cpi->rate_correction_factor;
  1334. }
  1335. }
  1336. #if CONFIG_MULTI_RES_ENCODING
  1337. if (cpi->oxcf.mr_total_resolutions > 1)
  1338. low_res_frame_info->is_frame_dropped_overshoot_maxqp = 1;
  1339. #endif
  1340. return 1;
  1341. }
  1342. cpi->force_maxqp = 0;
  1343. cpi->frames_since_last_drop_overshoot++;
  1344. #if CONFIG_MULTI_RES_ENCODING
  1345. if (cpi->oxcf.mr_total_resolutions > 1)
  1346. low_res_frame_info->is_frame_dropped_overshoot_maxqp = 0;
  1347. #endif
  1348. return 0;
  1349. }
  1350. cpi->force_maxqp = 0;
  1351. cpi->frames_since_last_drop_overshoot++;
  1352. #if CONFIG_MULTI_RES_ENCODING
  1353. if (cpi->oxcf.mr_total_resolutions > 1)
  1354. low_res_frame_info->is_frame_dropped_overshoot_maxqp = 0;
  1355. #endif
  1356. return 0;
  1357. }