ratectrl.c 57 KB

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