firstpass.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157
  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 <math.h>
  11. #include <limits.h>
  12. #include <stdio.h>
  13. #include "./vpx_dsp_rtcd.h"
  14. #include "./vpx_scale_rtcd.h"
  15. #include "block.h"
  16. #include "onyx_int.h"
  17. #include "vpx_dsp/variance.h"
  18. #include "encodeintra.h"
  19. #include "vp8/common/common.h"
  20. #include "vp8/common/setupintrarecon.h"
  21. #include "vp8/common/systemdependent.h"
  22. #include "mcomp.h"
  23. #include "firstpass.h"
  24. #include "vpx_scale/vpx_scale.h"
  25. #include "encodemb.h"
  26. #include "vp8/common/extend.h"
  27. #include "vpx_mem/vpx_mem.h"
  28. #include "vp8/common/swapyv12buffer.h"
  29. #include "rdopt.h"
  30. #include "vp8/common/quant_common.h"
  31. #include "encodemv.h"
  32. #include "encodeframe.h"
  33. #define OUTPUT_FPF 0
  34. extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi);
  35. #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
  36. extern int vp8_kf_boost_qadjustment[QINDEX_RANGE];
  37. extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE];
  38. #define IIFACTOR 1.5
  39. #define IIKFACTOR1 1.40
  40. #define IIKFACTOR2 1.5
  41. #define RMAX 14.0
  42. #define GF_RMAX 48.0
  43. #define KF_MB_INTRA_MIN 300
  44. #define GF_MB_INTRA_MIN 200
  45. #define DOUBLE_DIVIDE_CHECK(X) ((X) < 0 ? (X)-.000001 : (X) + .000001)
  46. #define POW1 (double)cpi->oxcf.two_pass_vbrbias / 100.0
  47. #define POW2 (double)cpi->oxcf.two_pass_vbrbias / 100.0
  48. #define NEW_BOOST 1
  49. static int vscale_lookup[7] = { 0, 1, 1, 2, 2, 3, 3 };
  50. static int hscale_lookup[7] = { 0, 0, 1, 1, 2, 2, 3 };
  51. static const int cq_level[QINDEX_RANGE] = {
  52. 0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11,
  53. 11, 12, 13, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24,
  54. 24, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 33, 33, 34, 35, 36, 36, 37, 38,
  55. 39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53,
  56. 54, 55, 55, 56, 57, 58, 59, 60, 60, 61, 62, 63, 64, 65, 66, 67, 67, 68, 69,
  57. 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 86,
  58. 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100
  59. };
  60. static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
  61. /* Resets the first pass file to the given position using a relative seek
  62. * from the current position
  63. */
  64. static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position) {
  65. cpi->twopass.stats_in = Position;
  66. }
  67. static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame) {
  68. if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) return EOF;
  69. *next_frame = *cpi->twopass.stats_in;
  70. return 1;
  71. }
  72. /* Read frame stats at an offset from the current position */
  73. static int read_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *frame_stats,
  74. int offset) {
  75. FIRSTPASS_STATS *fps_ptr = cpi->twopass.stats_in;
  76. /* Check legality of offset */
  77. if (offset >= 0) {
  78. if (&fps_ptr[offset] >= cpi->twopass.stats_in_end) return EOF;
  79. } else if (offset < 0) {
  80. if (&fps_ptr[offset] < cpi->twopass.stats_in_start) return EOF;
  81. }
  82. *frame_stats = fps_ptr[offset];
  83. return 1;
  84. }
  85. static int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps) {
  86. if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) return EOF;
  87. *fps = *cpi->twopass.stats_in;
  88. cpi->twopass.stats_in =
  89. (void *)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
  90. return 1;
  91. }
  92. static void output_stats(const VP8_COMP *cpi,
  93. struct vpx_codec_pkt_list *pktlist,
  94. FIRSTPASS_STATS *stats) {
  95. struct vpx_codec_cx_pkt pkt;
  96. (void)cpi;
  97. pkt.kind = VPX_CODEC_STATS_PKT;
  98. pkt.data.twopass_stats.buf = stats;
  99. pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
  100. vpx_codec_pkt_list_add(pktlist, &pkt);
  101. /* TEMP debug code */
  102. #if OUTPUT_FPF
  103. {
  104. FILE *fpfile;
  105. fpfile = fopen("firstpass.stt", "a");
  106. fprintf(fpfile,
  107. "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f"
  108. " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
  109. " %12.0f %12.0f %12.4f\n",
  110. stats->frame, stats->intra_error, stats->coded_error,
  111. stats->ssim_weighted_pred_err, stats->pcnt_inter,
  112. stats->pcnt_motion, stats->pcnt_second_ref, stats->pcnt_neutral,
  113. stats->MVr, stats->mvr_abs, stats->MVc, stats->mvc_abs, stats->MVrv,
  114. stats->MVcv, stats->mv_in_out_count, stats->new_mv_count,
  115. stats->count, stats->duration);
  116. fclose(fpfile);
  117. }
  118. #endif
  119. }
  120. static void zero_stats(FIRSTPASS_STATS *section) {
  121. section->frame = 0.0;
  122. section->intra_error = 0.0;
  123. section->coded_error = 0.0;
  124. section->ssim_weighted_pred_err = 0.0;
  125. section->pcnt_inter = 0.0;
  126. section->pcnt_motion = 0.0;
  127. section->pcnt_second_ref = 0.0;
  128. section->pcnt_neutral = 0.0;
  129. section->MVr = 0.0;
  130. section->mvr_abs = 0.0;
  131. section->MVc = 0.0;
  132. section->mvc_abs = 0.0;
  133. section->MVrv = 0.0;
  134. section->MVcv = 0.0;
  135. section->mv_in_out_count = 0.0;
  136. section->new_mv_count = 0.0;
  137. section->count = 0.0;
  138. section->duration = 1.0;
  139. }
  140. static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
  141. section->frame += frame->frame;
  142. section->intra_error += frame->intra_error;
  143. section->coded_error += frame->coded_error;
  144. section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
  145. section->pcnt_inter += frame->pcnt_inter;
  146. section->pcnt_motion += frame->pcnt_motion;
  147. section->pcnt_second_ref += frame->pcnt_second_ref;
  148. section->pcnt_neutral += frame->pcnt_neutral;
  149. section->MVr += frame->MVr;
  150. section->mvr_abs += frame->mvr_abs;
  151. section->MVc += frame->MVc;
  152. section->mvc_abs += frame->mvc_abs;
  153. section->MVrv += frame->MVrv;
  154. section->MVcv += frame->MVcv;
  155. section->mv_in_out_count += frame->mv_in_out_count;
  156. section->new_mv_count += frame->new_mv_count;
  157. section->count += frame->count;
  158. section->duration += frame->duration;
  159. }
  160. static void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
  161. section->frame -= frame->frame;
  162. section->intra_error -= frame->intra_error;
  163. section->coded_error -= frame->coded_error;
  164. section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err;
  165. section->pcnt_inter -= frame->pcnt_inter;
  166. section->pcnt_motion -= frame->pcnt_motion;
  167. section->pcnt_second_ref -= frame->pcnt_second_ref;
  168. section->pcnt_neutral -= frame->pcnt_neutral;
  169. section->MVr -= frame->MVr;
  170. section->mvr_abs -= frame->mvr_abs;
  171. section->MVc -= frame->MVc;
  172. section->mvc_abs -= frame->mvc_abs;
  173. section->MVrv -= frame->MVrv;
  174. section->MVcv -= frame->MVcv;
  175. section->mv_in_out_count -= frame->mv_in_out_count;
  176. section->new_mv_count -= frame->new_mv_count;
  177. section->count -= frame->count;
  178. section->duration -= frame->duration;
  179. }
  180. static void avg_stats(FIRSTPASS_STATS *section) {
  181. if (section->count < 1.0) return;
  182. section->intra_error /= section->count;
  183. section->coded_error /= section->count;
  184. section->ssim_weighted_pred_err /= section->count;
  185. section->pcnt_inter /= section->count;
  186. section->pcnt_second_ref /= section->count;
  187. section->pcnt_neutral /= section->count;
  188. section->pcnt_motion /= section->count;
  189. section->MVr /= section->count;
  190. section->mvr_abs /= section->count;
  191. section->MVc /= section->count;
  192. section->mvc_abs /= section->count;
  193. section->MVrv /= section->count;
  194. section->MVcv /= section->count;
  195. section->mv_in_out_count /= section->count;
  196. section->duration /= section->count;
  197. }
  198. /* Calculate a modified Error used in distributing bits between easier
  199. * and harder frames
  200. */
  201. static double calculate_modified_err(VP8_COMP *cpi,
  202. FIRSTPASS_STATS *this_frame) {
  203. double av_err = (cpi->twopass.total_stats.ssim_weighted_pred_err /
  204. cpi->twopass.total_stats.count);
  205. double this_err = this_frame->ssim_weighted_pred_err;
  206. double modified_err;
  207. if (this_err > av_err) {
  208. modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1);
  209. } else {
  210. modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2);
  211. }
  212. return modified_err;
  213. }
  214. static const double weight_table[256] = {
  215. 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
  216. 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
  217. 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
  218. 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
  219. 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.031250, 0.062500,
  220. 0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 0.250000, 0.281250,
  221. 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 0.500000,
  222. 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
  223. 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500,
  224. 0.968750, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  225. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  226. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  227. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  228. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  229. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  230. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  231. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  232. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  233. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  234. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  235. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  236. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  237. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  238. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  239. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  240. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  241. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  242. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  243. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  244. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  245. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  246. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  247. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  248. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  249. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  250. 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
  251. 1.000000, 1.000000, 1.000000, 1.000000
  252. };
  253. static double simple_weight(YV12_BUFFER_CONFIG *source) {
  254. int i, j;
  255. unsigned char *src = source->y_buffer;
  256. double sum_weights = 0.0;
  257. /* Loop throught the Y plane raw examining levels and creating a weight
  258. * for the image
  259. */
  260. i = source->y_height;
  261. do {
  262. j = source->y_width;
  263. do {
  264. sum_weights += weight_table[*src];
  265. src++;
  266. } while (--j);
  267. src -= source->y_width;
  268. src += source->y_stride;
  269. } while (--i);
  270. sum_weights /= (source->y_height * source->y_width);
  271. return sum_weights;
  272. }
  273. /* This function returns the current per frame maximum bitrate target */
  274. static int frame_max_bits(VP8_COMP *cpi) {
  275. /* Max allocation for a single frame based on the max section guidelines
  276. * passed in and how many bits are left
  277. */
  278. int max_bits;
  279. /* For CBR we need to also consider buffer fullness.
  280. * If we are running below the optimal level then we need to gradually
  281. * tighten up on max_bits.
  282. */
  283. if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
  284. double buffer_fullness_ratio =
  285. (double)cpi->buffer_level /
  286. DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
  287. /* For CBR base this on the target average bits per frame plus the
  288. * maximum sedction rate passed in by the user
  289. */
  290. max_bits = (int)(cpi->av_per_frame_bandwidth *
  291. ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
  292. /* If our buffer is below the optimum level */
  293. if (buffer_fullness_ratio < 1.0) {
  294. /* The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4. */
  295. int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2))
  296. ? cpi->av_per_frame_bandwidth >> 2
  297. : max_bits >> 2;
  298. max_bits = (int)(max_bits * buffer_fullness_ratio);
  299. /* Lowest value we will set ... which should allow the buffer to
  300. * refill.
  301. */
  302. if (max_bits < min_max_bits) max_bits = min_max_bits;
  303. }
  304. }
  305. /* VBR */
  306. else {
  307. /* For VBR base this on the bits and frames left plus the
  308. * two_pass_vbrmax_section rate passed in by the user
  309. */
  310. max_bits = (int)(((double)cpi->twopass.bits_left /
  311. (cpi->twopass.total_stats.count -
  312. (double)cpi->common.current_video_frame)) *
  313. ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
  314. }
  315. /* Trap case where we are out of bits */
  316. if (max_bits < 0) max_bits = 0;
  317. return max_bits;
  318. }
  319. void vp8_init_first_pass(VP8_COMP *cpi) {
  320. zero_stats(&cpi->twopass.total_stats);
  321. }
  322. void vp8_end_first_pass(VP8_COMP *cpi) {
  323. output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats);
  324. }
  325. static void zz_motion_search(VP8_COMP *cpi, MACROBLOCK *x,
  326. YV12_BUFFER_CONFIG *raw_buffer,
  327. int *raw_motion_err,
  328. YV12_BUFFER_CONFIG *recon_buffer,
  329. int *best_motion_err, int recon_yoffset) {
  330. MACROBLOCKD *const xd = &x->e_mbd;
  331. BLOCK *b = &x->block[0];
  332. BLOCKD *d = &x->e_mbd.block[0];
  333. unsigned char *src_ptr = (*(b->base_src) + b->src);
  334. int src_stride = b->src_stride;
  335. unsigned char *raw_ptr;
  336. int raw_stride = raw_buffer->y_stride;
  337. unsigned char *ref_ptr;
  338. int ref_stride = x->e_mbd.pre.y_stride;
  339. (void)cpi;
  340. /* Set up pointers for this macro block raw buffer */
  341. raw_ptr = (unsigned char *)(raw_buffer->y_buffer + recon_yoffset + d->offset);
  342. vpx_mse16x16(src_ptr, src_stride, raw_ptr, raw_stride,
  343. (unsigned int *)(raw_motion_err));
  344. /* Set up pointers for this macro block recon buffer */
  345. xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
  346. ref_ptr = (unsigned char *)(xd->pre.y_buffer + d->offset);
  347. vpx_mse16x16(src_ptr, src_stride, ref_ptr, ref_stride,
  348. (unsigned int *)(best_motion_err));
  349. }
  350. static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x,
  351. int_mv *ref_mv, MV *best_mv,
  352. YV12_BUFFER_CONFIG *recon_buffer,
  353. int *best_motion_err, int recon_yoffset) {
  354. MACROBLOCKD *const xd = &x->e_mbd;
  355. BLOCK *b = &x->block[0];
  356. BLOCKD *d = &x->e_mbd.block[0];
  357. int num00;
  358. int_mv tmp_mv;
  359. int_mv ref_mv_full;
  360. int tmp_err;
  361. int step_param = 3; /* Dont search over full range for first pass */
  362. int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
  363. int n;
  364. vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
  365. int new_mv_mode_penalty = 256;
  366. /* override the default variance function to use MSE */
  367. v_fn_ptr.vf = vpx_mse16x16;
  368. /* Set up pointers for this macro block recon buffer */
  369. xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
  370. /* Initial step/diamond search centred on best mv */
  371. tmp_mv.as_int = 0;
  372. ref_mv_full.as_mv.col = ref_mv->as_mv.col >> 3;
  373. ref_mv_full.as_mv.row = ref_mv->as_mv.row >> 3;
  374. tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param,
  375. x->sadperbit16, &num00, &v_fn_ptr,
  376. x->mvcost, ref_mv);
  377. if (tmp_err < INT_MAX - new_mv_mode_penalty) tmp_err += new_mv_mode_penalty;
  378. if (tmp_err < *best_motion_err) {
  379. *best_motion_err = tmp_err;
  380. best_mv->row = tmp_mv.as_mv.row;
  381. best_mv->col = tmp_mv.as_mv.col;
  382. }
  383. /* Further step/diamond searches as necessary */
  384. n = num00;
  385. num00 = 0;
  386. while (n < further_steps) {
  387. n++;
  388. if (num00) {
  389. num00--;
  390. } else {
  391. tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv,
  392. step_param + n, x->sadperbit16, &num00,
  393. &v_fn_ptr, x->mvcost, ref_mv);
  394. if (tmp_err < INT_MAX - new_mv_mode_penalty) {
  395. tmp_err += new_mv_mode_penalty;
  396. }
  397. if (tmp_err < *best_motion_err) {
  398. *best_motion_err = tmp_err;
  399. best_mv->row = tmp_mv.as_mv.row;
  400. best_mv->col = tmp_mv.as_mv.col;
  401. }
  402. }
  403. }
  404. }
  405. void vp8_first_pass(VP8_COMP *cpi) {
  406. int mb_row, mb_col;
  407. MACROBLOCK *const x = &cpi->mb;
  408. VP8_COMMON *const cm = &cpi->common;
  409. MACROBLOCKD *const xd = &x->e_mbd;
  410. int recon_yoffset, recon_uvoffset;
  411. YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
  412. YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
  413. YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
  414. int recon_y_stride = lst_yv12->y_stride;
  415. int recon_uv_stride = lst_yv12->uv_stride;
  416. int64_t intra_error = 0;
  417. int64_t coded_error = 0;
  418. int sum_mvr = 0, sum_mvc = 0;
  419. int sum_mvr_abs = 0, sum_mvc_abs = 0;
  420. int sum_mvrs = 0, sum_mvcs = 0;
  421. int mvcount = 0;
  422. int intercount = 0;
  423. int second_ref_count = 0;
  424. int intrapenalty = 256;
  425. int neutral_count = 0;
  426. int new_mv_count = 0;
  427. int sum_in_vectors = 0;
  428. uint32_t lastmv_as_int = 0;
  429. int_mv zero_ref_mv;
  430. zero_ref_mv.as_int = 0;
  431. vp8_clear_system_state();
  432. x->src = *cpi->Source;
  433. xd->pre = *lst_yv12;
  434. xd->dst = *new_yv12;
  435. x->partition_info = x->pi;
  436. xd->mode_info_context = cm->mi;
  437. if (!cm->use_bilinear_mc_filter) {
  438. xd->subpixel_predict = vp8_sixtap_predict4x4;
  439. xd->subpixel_predict8x4 = vp8_sixtap_predict8x4;
  440. xd->subpixel_predict8x8 = vp8_sixtap_predict8x8;
  441. xd->subpixel_predict16x16 = vp8_sixtap_predict16x16;
  442. } else {
  443. xd->subpixel_predict = vp8_bilinear_predict4x4;
  444. xd->subpixel_predict8x4 = vp8_bilinear_predict8x4;
  445. xd->subpixel_predict8x8 = vp8_bilinear_predict8x8;
  446. xd->subpixel_predict16x16 = vp8_bilinear_predict16x16;
  447. }
  448. vp8_build_block_offsets(x);
  449. /* set up frame new frame for intra coded blocks */
  450. vp8_setup_intra_recon(new_yv12);
  451. vp8cx_frame_init_quantizer(cpi);
  452. /* Initialise the MV cost table to the defaults */
  453. {
  454. int flag[2] = { 1, 1 };
  455. vp8_initialize_rd_consts(cpi, x,
  456. vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
  457. memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
  458. vp8_build_component_cost_table(cpi->mb.mvcost,
  459. (const MV_CONTEXT *)cm->fc.mvc, flag);
  460. }
  461. /* for each macroblock row in image */
  462. for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
  463. int_mv best_ref_mv;
  464. best_ref_mv.as_int = 0;
  465. /* reset above block coeffs */
  466. xd->up_available = (mb_row != 0);
  467. recon_yoffset = (mb_row * recon_y_stride * 16);
  468. recon_uvoffset = (mb_row * recon_uv_stride * 8);
  469. /* Set up limit values for motion vectors to prevent them extending
  470. * outside the UMV borders
  471. */
  472. x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
  473. x->mv_row_max =
  474. ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
  475. /* for each macroblock col in image */
  476. for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
  477. int this_error;
  478. int gf_motion_error = INT_MAX;
  479. int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
  480. xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
  481. xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
  482. xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
  483. xd->left_available = (mb_col != 0);
  484. /* Copy current mb to a buffer */
  485. vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
  486. /* do intra 16x16 prediction */
  487. this_error = vp8_encode_intra(cpi, x, use_dc_pred);
  488. /* "intrapenalty" below deals with situations where the intra
  489. * and inter error scores are very low (eg a plain black frame)
  490. * We do not have special cases in first pass for 0,0 and
  491. * nearest etc so all inter modes carry an overhead cost
  492. * estimate fot the mv. When the error score is very low this
  493. * causes us to pick all or lots of INTRA modes and throw lots
  494. * of key frames. This penalty adds a cost matching that of a
  495. * 0,0 mv to the intra case.
  496. */
  497. this_error += intrapenalty;
  498. /* Cumulative intra error total */
  499. intra_error += (int64_t)this_error;
  500. /* Set up limit values for motion vectors to prevent them
  501. * extending outside the UMV borders
  502. */
  503. x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
  504. x->mv_col_max =
  505. ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
  506. /* Other than for the first frame do a motion search */
  507. if (cm->current_video_frame > 0) {
  508. BLOCKD *d = &x->e_mbd.block[0];
  509. MV tmp_mv = { 0, 0 };
  510. int tmp_err;
  511. int motion_error = INT_MAX;
  512. int raw_motion_error = INT_MAX;
  513. /* Simple 0,0 motion with no mv overhead */
  514. zz_motion_search(cpi, x, cpi->last_frame_unscaled_source,
  515. &raw_motion_error, lst_yv12, &motion_error,
  516. recon_yoffset);
  517. d->bmi.mv.as_mv.row = 0;
  518. d->bmi.mv.as_mv.col = 0;
  519. if (raw_motion_error < cpi->oxcf.encode_breakout) {
  520. goto skip_motion_search;
  521. }
  522. /* Test last reference frame using the previous best mv as the
  523. * starting point (best reference) for the search
  524. */
  525. first_pass_motion_search(cpi, x, &best_ref_mv, &d->bmi.mv.as_mv,
  526. lst_yv12, &motion_error, recon_yoffset);
  527. /* If the current best reference mv is not centred on 0,0
  528. * then do a 0,0 based search as well
  529. */
  530. if (best_ref_mv.as_int) {
  531. tmp_err = INT_MAX;
  532. first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, lst_yv12,
  533. &tmp_err, recon_yoffset);
  534. if (tmp_err < motion_error) {
  535. motion_error = tmp_err;
  536. d->bmi.mv.as_mv.row = tmp_mv.row;
  537. d->bmi.mv.as_mv.col = tmp_mv.col;
  538. }
  539. }
  540. /* Experimental search in a second reference frame ((0,0)
  541. * based only)
  542. */
  543. if (cm->current_video_frame > 1) {
  544. first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12,
  545. &gf_motion_error, recon_yoffset);
  546. if ((gf_motion_error < motion_error) &&
  547. (gf_motion_error < this_error)) {
  548. second_ref_count++;
  549. }
  550. /* Reset to last frame as reference buffer */
  551. xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
  552. xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
  553. xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
  554. }
  555. skip_motion_search:
  556. /* Intra assumed best */
  557. best_ref_mv.as_int = 0;
  558. if (motion_error <= this_error) {
  559. /* Keep a count of cases where the inter and intra were
  560. * very close and very low. This helps with scene cut
  561. * detection for example in cropped clips with black bars
  562. * at the sides or top and bottom.
  563. */
  564. if ((((this_error - intrapenalty) * 9) <= (motion_error * 10)) &&
  565. (this_error < (2 * intrapenalty))) {
  566. neutral_count++;
  567. }
  568. d->bmi.mv.as_mv.row *= 8;
  569. d->bmi.mv.as_mv.col *= 8;
  570. this_error = motion_error;
  571. vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv);
  572. vp8_encode_inter16x16y(x);
  573. sum_mvr += d->bmi.mv.as_mv.row;
  574. sum_mvr_abs += abs(d->bmi.mv.as_mv.row);
  575. sum_mvc += d->bmi.mv.as_mv.col;
  576. sum_mvc_abs += abs(d->bmi.mv.as_mv.col);
  577. sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row;
  578. sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col;
  579. intercount++;
  580. best_ref_mv.as_int = d->bmi.mv.as_int;
  581. /* Was the vector non-zero */
  582. if (d->bmi.mv.as_int) {
  583. mvcount++;
  584. /* Was it different from the last non zero vector */
  585. if (d->bmi.mv.as_int != lastmv_as_int) new_mv_count++;
  586. lastmv_as_int = d->bmi.mv.as_int;
  587. /* Does the Row vector point inwards or outwards */
  588. if (mb_row < cm->mb_rows / 2) {
  589. if (d->bmi.mv.as_mv.row > 0) {
  590. sum_in_vectors--;
  591. } else if (d->bmi.mv.as_mv.row < 0) {
  592. sum_in_vectors++;
  593. }
  594. } else if (mb_row > cm->mb_rows / 2) {
  595. if (d->bmi.mv.as_mv.row > 0) {
  596. sum_in_vectors++;
  597. } else if (d->bmi.mv.as_mv.row < 0) {
  598. sum_in_vectors--;
  599. }
  600. }
  601. /* Does the Row vector point inwards or outwards */
  602. if (mb_col < cm->mb_cols / 2) {
  603. if (d->bmi.mv.as_mv.col > 0) {
  604. sum_in_vectors--;
  605. } else if (d->bmi.mv.as_mv.col < 0) {
  606. sum_in_vectors++;
  607. }
  608. } else if (mb_col > cm->mb_cols / 2) {
  609. if (d->bmi.mv.as_mv.col > 0) {
  610. sum_in_vectors++;
  611. } else if (d->bmi.mv.as_mv.col < 0) {
  612. sum_in_vectors--;
  613. }
  614. }
  615. }
  616. }
  617. }
  618. coded_error += (int64_t)this_error;
  619. /* adjust to the next column of macroblocks */
  620. x->src.y_buffer += 16;
  621. x->src.u_buffer += 8;
  622. x->src.v_buffer += 8;
  623. recon_yoffset += 16;
  624. recon_uvoffset += 8;
  625. }
  626. /* adjust to the next row of mbs */
  627. x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
  628. x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
  629. x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
  630. /* extend the recon for intra prediction */
  631. vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8,
  632. xd->dst.v_buffer + 8);
  633. vp8_clear_system_state();
  634. }
  635. vp8_clear_system_state();
  636. {
  637. double weight = 0.0;
  638. FIRSTPASS_STATS fps;
  639. fps.frame = cm->current_video_frame;
  640. fps.intra_error = (double)(intra_error >> 8);
  641. fps.coded_error = (double)(coded_error >> 8);
  642. weight = simple_weight(cpi->Source);
  643. if (weight < 0.1) weight = 0.1;
  644. fps.ssim_weighted_pred_err = fps.coded_error * weight;
  645. fps.pcnt_inter = 0.0;
  646. fps.pcnt_motion = 0.0;
  647. fps.MVr = 0.0;
  648. fps.mvr_abs = 0.0;
  649. fps.MVc = 0.0;
  650. fps.mvc_abs = 0.0;
  651. fps.MVrv = 0.0;
  652. fps.MVcv = 0.0;
  653. fps.mv_in_out_count = 0.0;
  654. fps.new_mv_count = 0.0;
  655. fps.count = 1.0;
  656. fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs;
  657. fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
  658. fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;
  659. if (mvcount > 0) {
  660. fps.MVr = (double)sum_mvr / (double)mvcount;
  661. fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
  662. fps.MVc = (double)sum_mvc / (double)mvcount;
  663. fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
  664. fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) /
  665. (double)mvcount;
  666. fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) /
  667. (double)mvcount;
  668. fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
  669. fps.new_mv_count = new_mv_count;
  670. fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
  671. }
  672. /* TODO: handle the case when duration is set to 0, or something less
  673. * than the full time between subsequent cpi->source_time_stamps
  674. */
  675. fps.duration = (double)(cpi->source->ts_end - cpi->source->ts_start);
  676. /* don't want to do output stats with a stack variable! */
  677. memcpy(&cpi->twopass.this_frame_stats, &fps, sizeof(FIRSTPASS_STATS));
  678. output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats);
  679. accumulate_stats(&cpi->twopass.total_stats, &fps);
  680. }
  681. /* Copy the previous Last Frame into the GF buffer if specific
  682. * conditions for doing so are met
  683. */
  684. if ((cm->current_video_frame > 0) &&
  685. (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) &&
  686. ((cpi->twopass.this_frame_stats.intra_error /
  687. DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) >
  688. 2.0)) {
  689. vp8_yv12_copy_frame(lst_yv12, gld_yv12);
  690. }
  691. /* swap frame pointers so last frame refers to the frame we just
  692. * compressed
  693. */
  694. vp8_swap_yv12_buffer(lst_yv12, new_yv12);
  695. vp8_yv12_extend_frame_borders(lst_yv12);
  696. /* Special case for the first frame. Copy into the GF buffer as a
  697. * second reference.
  698. */
  699. if (cm->current_video_frame == 0) {
  700. vp8_yv12_copy_frame(lst_yv12, gld_yv12);
  701. }
  702. /* use this to see what the first pass reconstruction looks like */
  703. if (0) {
  704. char filename[512];
  705. FILE *recon_file;
  706. sprintf(filename, "enc%04d.yuv", (int)cm->current_video_frame);
  707. if (cm->current_video_frame == 0) {
  708. recon_file = fopen(filename, "wb");
  709. } else {
  710. recon_file = fopen(filename, "ab");
  711. }
  712. (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
  713. fclose(recon_file);
  714. }
  715. cm->current_video_frame++;
  716. }
  717. extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
  718. /* Estimate a cost per mb attributable to overheads such as the coding of
  719. * modes and motion vectors.
  720. * Currently simplistic in its assumptions for testing.
  721. */
  722. static double bitcost(double prob) {
  723. if (prob > 0.000122) {
  724. return -log(prob) / log(2.0);
  725. } else {
  726. return 13.0;
  727. }
  728. }
  729. static int64_t estimate_modemvcost(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats) {
  730. int mv_cost;
  731. int64_t mode_cost;
  732. double av_pct_inter = fpstats->pcnt_inter / fpstats->count;
  733. double av_pct_motion = fpstats->pcnt_motion / fpstats->count;
  734. double av_intra = (1.0 - av_pct_inter);
  735. double zz_cost;
  736. double motion_cost;
  737. double intra_cost;
  738. zz_cost = bitcost(av_pct_inter - av_pct_motion);
  739. motion_cost = bitcost(av_pct_motion);
  740. intra_cost = bitcost(av_intra);
  741. /* Estimate of extra bits per mv overhead for mbs
  742. * << 9 is the normalization to the (bits * 512) used in vp8_bits_per_mb
  743. */
  744. mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9;
  745. /* Crude estimate of overhead cost from modes
  746. * << 9 is the normalization to (bits * 512) used in vp8_bits_per_mb
  747. */
  748. mode_cost =
  749. (int64_t)((((av_pct_inter - av_pct_motion) * zz_cost) +
  750. (av_pct_motion * motion_cost) + (av_intra * intra_cost)) *
  751. cpi->common.MBs) *
  752. 512;
  753. return mv_cost + mode_cost;
  754. }
  755. static double calc_correction_factor(double err_per_mb, double err_devisor,
  756. double pt_low, double pt_high, int Q) {
  757. double power_term;
  758. double error_term = err_per_mb / err_devisor;
  759. double correction_factor;
  760. /* Adjustment based on Q to power term. */
  761. power_term = pt_low + (Q * 0.01);
  762. power_term = (power_term > pt_high) ? pt_high : power_term;
  763. /* Adjustments to error term */
  764. /* TBD */
  765. /* Calculate correction factor */
  766. correction_factor = pow(error_term, power_term);
  767. /* Clip range */
  768. correction_factor = (correction_factor < 0.05)
  769. ? 0.05
  770. : (correction_factor > 5.0) ? 5.0 : correction_factor;
  771. return correction_factor;
  772. }
  773. static int estimate_max_q(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats,
  774. int section_target_bandwitdh, int overhead_bits) {
  775. int Q;
  776. int num_mbs = cpi->common.MBs;
  777. int target_norm_bits_per_mb;
  778. double section_err = (fpstats->coded_error / fpstats->count);
  779. double err_per_mb = section_err / num_mbs;
  780. double err_correction_factor;
  781. double speed_correction = 1.0;
  782. int overhead_bits_per_mb;
  783. if (section_target_bandwitdh <= 0) {
  784. return cpi->twopass.maxq_max_limit; /* Highest value allowed */
  785. }
  786. target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
  787. ? (512 * section_target_bandwitdh) / num_mbs
  788. : 512 * (section_target_bandwitdh / num_mbs);
  789. /* Calculate a corrective factor based on a rolling ratio of bits spent
  790. * vs target bits
  791. */
  792. if ((cpi->rolling_target_bits > 0) &&
  793. (cpi->active_worst_quality < cpi->worst_quality)) {
  794. double rolling_ratio;
  795. rolling_ratio =
  796. (double)cpi->rolling_actual_bits / (double)cpi->rolling_target_bits;
  797. if (rolling_ratio < 0.95) {
  798. cpi->twopass.est_max_qcorrection_factor -= 0.005;
  799. } else if (rolling_ratio > 1.05) {
  800. cpi->twopass.est_max_qcorrection_factor += 0.005;
  801. }
  802. cpi->twopass.est_max_qcorrection_factor =
  803. (cpi->twopass.est_max_qcorrection_factor < 0.1)
  804. ? 0.1
  805. : (cpi->twopass.est_max_qcorrection_factor > 10.0)
  806. ? 10.0
  807. : cpi->twopass.est_max_qcorrection_factor;
  808. }
  809. /* Corrections for higher compression speed settings
  810. * (reduced compression expected)
  811. */
  812. if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
  813. if (cpi->oxcf.cpu_used <= 5) {
  814. speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
  815. } else {
  816. speed_correction = 1.25;
  817. }
  818. }
  819. /* Estimate of overhead bits per mb */
  820. /* Correction to overhead bits for min allowed Q. */
  821. overhead_bits_per_mb = overhead_bits / num_mbs;
  822. overhead_bits_per_mb = (int)(overhead_bits_per_mb *
  823. pow(0.98, (double)cpi->twopass.maxq_min_limit));
  824. /* Try and pick a max Q that will be high enough to encode the
  825. * content at the given rate.
  826. */
  827. for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; ++Q) {
  828. int bits_per_mb_at_this_q;
  829. /* Error per MB based correction factor */
  830. err_correction_factor =
  831. calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
  832. bits_per_mb_at_this_q =
  833. vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
  834. bits_per_mb_at_this_q = (int)(.5 +
  835. err_correction_factor * speed_correction *
  836. cpi->twopass.est_max_qcorrection_factor *
  837. cpi->twopass.section_max_qfactor *
  838. (double)bits_per_mb_at_this_q);
  839. /* Mode and motion overhead */
  840. /* As Q rises in real encode loop rd code will force overhead down
  841. * We make a crude adjustment for this here as *.98 per Q step.
  842. */
  843. overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
  844. if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
  845. }
  846. /* Restriction on active max q for constrained quality mode. */
  847. if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
  848. (Q < cpi->cq_target_quality)) {
  849. Q = cpi->cq_target_quality;
  850. }
  851. /* Adjust maxq_min_limit and maxq_max_limit limits based on
  852. * average q observed in clip for non kf/gf.arf frames
  853. * Give average a chance to settle though.
  854. */
  855. if ((cpi->ni_frames > ((int)cpi->twopass.total_stats.count >> 8)) &&
  856. (cpi->ni_frames > 150)) {
  857. cpi->twopass.maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality)
  858. ? (cpi->ni_av_qi + 32)
  859. : cpi->worst_quality;
  860. cpi->twopass.maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
  861. ? (cpi->ni_av_qi - 32)
  862. : cpi->best_quality;
  863. }
  864. return Q;
  865. }
  866. /* For cq mode estimate a cq level that matches the observed
  867. * complexity and data rate.
  868. */
  869. static int estimate_cq(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats,
  870. int section_target_bandwitdh, int overhead_bits) {
  871. int Q;
  872. int num_mbs = cpi->common.MBs;
  873. int target_norm_bits_per_mb;
  874. double section_err = (fpstats->coded_error / fpstats->count);
  875. double err_per_mb = section_err / num_mbs;
  876. double err_correction_factor;
  877. double speed_correction = 1.0;
  878. double clip_iiratio;
  879. double clip_iifactor;
  880. int overhead_bits_per_mb;
  881. if (0) {
  882. FILE *f = fopen("epmp.stt", "a");
  883. fprintf(f, "%10.2f\n", err_per_mb);
  884. fclose(f);
  885. }
  886. target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
  887. ? (512 * section_target_bandwitdh) / num_mbs
  888. : 512 * (section_target_bandwitdh / num_mbs);
  889. /* Estimate of overhead bits per mb */
  890. overhead_bits_per_mb = overhead_bits / num_mbs;
  891. /* Corrections for higher compression speed settings
  892. * (reduced compression expected)
  893. */
  894. if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
  895. if (cpi->oxcf.cpu_used <= 5) {
  896. speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
  897. } else {
  898. speed_correction = 1.25;
  899. }
  900. }
  901. /* II ratio correction factor for clip as a whole */
  902. clip_iiratio = cpi->twopass.total_stats.intra_error /
  903. DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error);
  904. clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
  905. if (clip_iifactor < 0.80) clip_iifactor = 0.80;
  906. /* Try and pick a Q that can encode the content at the given rate. */
  907. for (Q = 0; Q < MAXQ; ++Q) {
  908. int bits_per_mb_at_this_q;
  909. /* Error per MB based correction factor */
  910. err_correction_factor =
  911. calc_correction_factor(err_per_mb, 100.0, 0.40, 0.90, Q);
  912. bits_per_mb_at_this_q =
  913. vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
  914. bits_per_mb_at_this_q =
  915. (int)(.5 +
  916. err_correction_factor * speed_correction * clip_iifactor *
  917. (double)bits_per_mb_at_this_q);
  918. /* Mode and motion overhead */
  919. /* As Q rises in real encode loop rd code will force overhead down
  920. * We make a crude adjustment for this here as *.98 per Q step.
  921. */
  922. overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
  923. if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
  924. }
  925. /* Clip value to range "best allowed to (worst allowed - 1)" */
  926. Q = cq_level[Q];
  927. if (Q >= cpi->worst_quality) Q = cpi->worst_quality - 1;
  928. if (Q < cpi->best_quality) Q = cpi->best_quality;
  929. return Q;
  930. }
  931. static int estimate_q(VP8_COMP *cpi, double section_err,
  932. int section_target_bandwitdh) {
  933. int Q;
  934. int num_mbs = cpi->common.MBs;
  935. int target_norm_bits_per_mb;
  936. double err_per_mb = section_err / num_mbs;
  937. double err_correction_factor;
  938. double speed_correction = 1.0;
  939. target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
  940. ? (512 * section_target_bandwitdh) / num_mbs
  941. : 512 * (section_target_bandwitdh / num_mbs);
  942. /* Corrections for higher compression speed settings
  943. * (reduced compression expected)
  944. */
  945. if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
  946. if (cpi->oxcf.cpu_used <= 5) {
  947. speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
  948. } else {
  949. speed_correction = 1.25;
  950. }
  951. }
  952. /* Try and pick a Q that can encode the content at the given rate. */
  953. for (Q = 0; Q < MAXQ; ++Q) {
  954. int bits_per_mb_at_this_q;
  955. /* Error per MB based correction factor */
  956. err_correction_factor =
  957. calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
  958. bits_per_mb_at_this_q =
  959. (int)(.5 + (err_correction_factor * speed_correction *
  960. cpi->twopass.est_max_qcorrection_factor *
  961. (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0));
  962. if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
  963. }
  964. return Q;
  965. }
  966. /* Estimate a worst case Q for a KF group */
  967. static int estimate_kf_group_q(VP8_COMP *cpi, double section_err,
  968. int section_target_bandwitdh,
  969. double group_iiratio) {
  970. int Q;
  971. int num_mbs = cpi->common.MBs;
  972. int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
  973. int bits_per_mb_at_this_q;
  974. double err_per_mb = section_err / num_mbs;
  975. double err_correction_factor;
  976. double speed_correction = 1.0;
  977. double current_spend_ratio = 1.0;
  978. double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90;
  979. double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80;
  980. double iiratio_correction_factor = 1.0;
  981. double combined_correction_factor;
  982. /* Trap special case where the target is <= 0 */
  983. if (target_norm_bits_per_mb <= 0) return MAXQ * 2;
  984. /* Calculate a corrective factor based on a rolling ratio of bits spent
  985. * vs target bits
  986. * This is clamped to the range 0.1 to 10.0
  987. */
  988. if (cpi->long_rolling_target_bits <= 0) {
  989. current_spend_ratio = 10.0;
  990. } else {
  991. current_spend_ratio = (double)cpi->long_rolling_actual_bits /
  992. (double)cpi->long_rolling_target_bits;
  993. current_spend_ratio =
  994. (current_spend_ratio > 10.0) ? 10.0 : (current_spend_ratio < 0.1)
  995. ? 0.1
  996. : current_spend_ratio;
  997. }
  998. /* Calculate a correction factor based on the quality of prediction in
  999. * the sequence as indicated by intra_inter error score ratio (IIRatio)
  1000. * The idea here is to favour subsampling in the hardest sections vs
  1001. * the easyest.
  1002. */
  1003. iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1);
  1004. if (iiratio_correction_factor < 0.5) iiratio_correction_factor = 0.5;
  1005. /* Corrections for higher compression speed settings
  1006. * (reduced compression expected)
  1007. */
  1008. if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
  1009. if (cpi->oxcf.cpu_used <= 5) {
  1010. speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
  1011. } else {
  1012. speed_correction = 1.25;
  1013. }
  1014. }
  1015. /* Combine the various factors calculated above */
  1016. combined_correction_factor =
  1017. speed_correction * iiratio_correction_factor * current_spend_ratio;
  1018. /* Try and pick a Q that should be high enough to encode the content at
  1019. * the given rate.
  1020. */
  1021. for (Q = 0; Q < MAXQ; ++Q) {
  1022. /* Error per MB based correction factor */
  1023. err_correction_factor =
  1024. calc_correction_factor(err_per_mb, 150.0, pow_lowq, pow_highq, Q);
  1025. bits_per_mb_at_this_q =
  1026. (int)(.5 + (err_correction_factor * combined_correction_factor *
  1027. (double)vp8_bits_per_mb[INTER_FRAME][Q]));
  1028. if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
  1029. }
  1030. /* If we could not hit the target even at Max Q then estimate what Q
  1031. * would have been required
  1032. */
  1033. while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) &&
  1034. (Q < (MAXQ * 2))) {
  1035. bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q);
  1036. Q++;
  1037. }
  1038. if (0) {
  1039. FILE *f = fopen("estkf_q.stt", "a");
  1040. fprintf(f, "%8d %8d %8d %8.2f %8.3f %8.2f %8.3f %8.3f %8.3f %8d\n",
  1041. cpi->common.current_video_frame, bits_per_mb_at_this_q,
  1042. target_norm_bits_per_mb, err_per_mb, err_correction_factor,
  1043. current_spend_ratio, group_iiratio, iiratio_correction_factor,
  1044. (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level,
  1045. Q);
  1046. fclose(f);
  1047. }
  1048. return Q;
  1049. }
  1050. void vp8_init_second_pass(VP8_COMP *cpi) {
  1051. FIRSTPASS_STATS this_frame;
  1052. FIRSTPASS_STATS *start_pos;
  1053. double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth *
  1054. cpi->oxcf.two_pass_vbrmin_section / 100);
  1055. zero_stats(&cpi->twopass.total_stats);
  1056. zero_stats(&cpi->twopass.total_left_stats);
  1057. if (!cpi->twopass.stats_in_end) return;
  1058. cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
  1059. cpi->twopass.total_left_stats = cpi->twopass.total_stats;
  1060. /* each frame can have a different duration, as the frame rate in the
  1061. * source isn't guaranteed to be constant. The frame rate prior to
  1062. * the first frame encoded in the second pass is a guess. However the
  1063. * sum duration is not. Its calculated based on the actual durations of
  1064. * all frames from the first pass.
  1065. */
  1066. vp8_new_framerate(cpi, 10000000.0 * cpi->twopass.total_stats.count /
  1067. cpi->twopass.total_stats.duration);
  1068. cpi->output_framerate = cpi->framerate;
  1069. cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats.duration *
  1070. cpi->oxcf.target_bandwidth / 10000000.0);
  1071. cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats.duration *
  1072. two_pass_min_rate / 10000000.0);
  1073. /* Calculate a minimum intra value to be used in determining the IIratio
  1074. * scores used in the second pass. We have this minimum to make sure
  1075. * that clips that are static but "low complexity" in the intra domain
  1076. * are still boosted appropriately for KF/GF/ARF
  1077. */
  1078. cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
  1079. cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
  1080. /* Scan the first pass file and calculate an average Intra / Inter error
  1081. * score ratio for the sequence
  1082. */
  1083. {
  1084. double sum_iiratio = 0.0;
  1085. double IIRatio;
  1086. start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
  1087. while (input_stats(cpi, &this_frame) != EOF) {
  1088. IIRatio =
  1089. this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
  1090. IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
  1091. sum_iiratio += IIRatio;
  1092. }
  1093. cpi->twopass.avg_iiratio =
  1094. sum_iiratio /
  1095. DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count);
  1096. /* Reset file position */
  1097. reset_fpf_position(cpi, start_pos);
  1098. }
  1099. /* Scan the first pass file and calculate a modified total error based
  1100. * upon the bias/power function used to allocate bits
  1101. */
  1102. {
  1103. start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
  1104. cpi->twopass.modified_error_total = 0.0;
  1105. cpi->twopass.modified_error_used = 0.0;
  1106. while (input_stats(cpi, &this_frame) != EOF) {
  1107. cpi->twopass.modified_error_total +=
  1108. calculate_modified_err(cpi, &this_frame);
  1109. }
  1110. cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
  1111. reset_fpf_position(cpi, start_pos); /* Reset file position */
  1112. }
  1113. }
  1114. void vp8_end_second_pass(VP8_COMP *cpi) { (void)cpi; }
  1115. /* This function gives and estimate of how badly we believe the prediction
  1116. * quality is decaying from frame to frame.
  1117. */
  1118. static double get_prediction_decay_rate(VP8_COMP *cpi,
  1119. FIRSTPASS_STATS *next_frame) {
  1120. double prediction_decay_rate;
  1121. double motion_decay;
  1122. double motion_pct = next_frame->pcnt_motion;
  1123. (void)cpi;
  1124. /* Initial basis is the % mbs inter coded */
  1125. prediction_decay_rate = next_frame->pcnt_inter;
  1126. /* High % motion -> somewhat higher decay rate */
  1127. motion_decay = (1.0 - (motion_pct / 20.0));
  1128. if (motion_decay < prediction_decay_rate) {
  1129. prediction_decay_rate = motion_decay;
  1130. }
  1131. /* Adjustment to decay rate based on speed of motion */
  1132. {
  1133. double this_mv_rabs;
  1134. double this_mv_cabs;
  1135. double distance_factor;
  1136. this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct);
  1137. this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct);
  1138. distance_factor =
  1139. sqrt((this_mv_rabs * this_mv_rabs) + (this_mv_cabs * this_mv_cabs)) /
  1140. 250.0;
  1141. distance_factor = ((distance_factor > 1.0) ? 0.0 : (1.0 - distance_factor));
  1142. if (distance_factor < prediction_decay_rate) {
  1143. prediction_decay_rate = distance_factor;
  1144. }
  1145. }
  1146. return prediction_decay_rate;
  1147. }
  1148. /* Function to test for a condition where a complex transition is followed
  1149. * by a static section. For example in slide shows where there is a fade
  1150. * between slides. This is to help with more optimal kf and gf positioning.
  1151. */
  1152. static int detect_transition_to_still(VP8_COMP *cpi, int frame_interval,
  1153. int still_interval,
  1154. double loop_decay_rate,
  1155. double decay_accumulator) {
  1156. int trans_to_still = 0;
  1157. /* Break clause to detect very still sections after motion
  1158. * For example a static image after a fade or other transition
  1159. * instead of a clean scene cut.
  1160. */
  1161. if ((frame_interval > MIN_GF_INTERVAL) && (loop_decay_rate >= 0.999) &&
  1162. (decay_accumulator < 0.9)) {
  1163. int j;
  1164. FIRSTPASS_STATS *position = cpi->twopass.stats_in;
  1165. FIRSTPASS_STATS tmp_next_frame;
  1166. double decay_rate;
  1167. /* Look ahead a few frames to see if static condition persists... */
  1168. for (j = 0; j < still_interval; ++j) {
  1169. if (EOF == input_stats(cpi, &tmp_next_frame)) break;
  1170. decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame);
  1171. if (decay_rate < 0.999) break;
  1172. }
  1173. /* Reset file position */
  1174. reset_fpf_position(cpi, position);
  1175. /* Only if it does do we signal a transition to still */
  1176. if (j == still_interval) trans_to_still = 1;
  1177. }
  1178. return trans_to_still;
  1179. }
  1180. /* This function detects a flash through the high relative pcnt_second_ref
  1181. * score in the frame following a flash frame. The offset passed in should
  1182. * reflect this
  1183. */
  1184. static int detect_flash(VP8_COMP *cpi, int offset) {
  1185. FIRSTPASS_STATS next_frame;
  1186. int flash_detected = 0;
  1187. /* Read the frame data. */
  1188. /* The return is 0 (no flash detected) if not a valid frame */
  1189. if (read_frame_stats(cpi, &next_frame, offset) != EOF) {
  1190. /* What we are looking for here is a situation where there is a
  1191. * brief break in prediction (such as a flash) but subsequent frames
  1192. * are reasonably well predicted by an earlier (pre flash) frame.
  1193. * The recovery after a flash is indicated by a high pcnt_second_ref
  1194. * comapred to pcnt_inter.
  1195. */
  1196. if ((next_frame.pcnt_second_ref > next_frame.pcnt_inter) &&
  1197. (next_frame.pcnt_second_ref >= 0.5)) {
  1198. flash_detected = 1;
  1199. /*if (1)
  1200. {
  1201. FILE *f = fopen("flash.stt", "a");
  1202. fprintf(f, "%8.0f %6.2f %6.2f\n",
  1203. next_frame.frame,
  1204. next_frame.pcnt_inter,
  1205. next_frame.pcnt_second_ref);
  1206. fclose(f);
  1207. }*/
  1208. }
  1209. }
  1210. return flash_detected;
  1211. }
  1212. /* Update the motion related elements to the GF arf boost calculation */
  1213. static void accumulate_frame_motion_stats(VP8_COMP *cpi,
  1214. FIRSTPASS_STATS *this_frame,
  1215. double *this_frame_mv_in_out,
  1216. double *mv_in_out_accumulator,
  1217. double *abs_mv_in_out_accumulator,
  1218. double *mv_ratio_accumulator) {
  1219. double this_frame_mvr_ratio;
  1220. double this_frame_mvc_ratio;
  1221. double motion_pct;
  1222. (void)cpi;
  1223. /* Accumulate motion stats. */
  1224. motion_pct = this_frame->pcnt_motion;
  1225. /* Accumulate Motion In/Out of frame stats */
  1226. *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
  1227. *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
  1228. *abs_mv_in_out_accumulator += fabs(this_frame->mv_in_out_count * motion_pct);
  1229. /* Accumulate a measure of how uniform (or conversely how random)
  1230. * the motion field is. (A ratio of absmv / mv)
  1231. */
  1232. if (motion_pct > 0.05) {
  1233. this_frame_mvr_ratio =
  1234. fabs(this_frame->mvr_abs) / DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
  1235. this_frame_mvc_ratio =
  1236. fabs(this_frame->mvc_abs) / DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
  1237. *mv_ratio_accumulator += (this_frame_mvr_ratio < this_frame->mvr_abs)
  1238. ? (this_frame_mvr_ratio * motion_pct)
  1239. : this_frame->mvr_abs * motion_pct;
  1240. *mv_ratio_accumulator += (this_frame_mvc_ratio < this_frame->mvc_abs)
  1241. ? (this_frame_mvc_ratio * motion_pct)
  1242. : this_frame->mvc_abs * motion_pct;
  1243. }
  1244. }
  1245. /* Calculate a baseline boost number for the current frame. */
  1246. static double calc_frame_boost(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame,
  1247. double this_frame_mv_in_out) {
  1248. double frame_boost;
  1249. /* Underlying boost factor is based on inter intra error ratio */
  1250. if (this_frame->intra_error > cpi->twopass.gf_intra_err_min) {
  1251. frame_boost = (IIFACTOR * this_frame->intra_error /
  1252. DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
  1253. } else {
  1254. frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
  1255. DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
  1256. }
  1257. /* Increase boost for frames where new data coming into frame
  1258. * (eg zoom out). Slightly reduce boost if there is a net balance
  1259. * of motion out of the frame (zoom in).
  1260. * The range for this_frame_mv_in_out is -1.0 to +1.0
  1261. */
  1262. if (this_frame_mv_in_out > 0.0) {
  1263. frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
  1264. /* In extreme case boost is halved */
  1265. } else {
  1266. frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
  1267. }
  1268. /* Clip to maximum */
  1269. if (frame_boost > GF_RMAX) frame_boost = GF_RMAX;
  1270. return frame_boost;
  1271. }
  1272. #if NEW_BOOST
  1273. static int calc_arf_boost(VP8_COMP *cpi, int offset, int f_frames, int b_frames,
  1274. int *f_boost, int *b_boost) {
  1275. FIRSTPASS_STATS this_frame;
  1276. int i;
  1277. double boost_score = 0.0;
  1278. double mv_ratio_accumulator = 0.0;
  1279. double decay_accumulator = 1.0;
  1280. double this_frame_mv_in_out = 0.0;
  1281. double mv_in_out_accumulator = 0.0;
  1282. double abs_mv_in_out_accumulator = 0.0;
  1283. double r;
  1284. int flash_detected = 0;
  1285. /* Search forward from the proposed arf/next gf position */
  1286. for (i = 0; i < f_frames; ++i) {
  1287. if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF) break;
  1288. /* Update the motion related elements to the boost calculation */
  1289. accumulate_frame_motion_stats(
  1290. cpi, &this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
  1291. &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
  1292. /* Calculate the baseline boost number for this frame */
  1293. r = calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out);
  1294. /* We want to discount the the flash frame itself and the recovery
  1295. * frame that follows as both will have poor scores.
  1296. */
  1297. flash_detected =
  1298. detect_flash(cpi, (i + offset)) || detect_flash(cpi, (i + offset + 1));
  1299. /* Cumulative effect of prediction quality decay */
  1300. if (!flash_detected) {
  1301. decay_accumulator =
  1302. decay_accumulator * get_prediction_decay_rate(cpi, &this_frame);
  1303. decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
  1304. }
  1305. boost_score += (decay_accumulator * r);
  1306. /* Break out conditions. */
  1307. if ((!flash_detected) &&
  1308. ((mv_ratio_accumulator > 100.0) || (abs_mv_in_out_accumulator > 3.0) ||
  1309. (mv_in_out_accumulator < -2.0))) {
  1310. break;
  1311. }
  1312. }
  1313. *f_boost = (int)(boost_score * 100.0) >> 4;
  1314. /* Reset for backward looking loop */
  1315. boost_score = 0.0;
  1316. mv_ratio_accumulator = 0.0;
  1317. decay_accumulator = 1.0;
  1318. this_frame_mv_in_out = 0.0;
  1319. mv_in_out_accumulator = 0.0;
  1320. abs_mv_in_out_accumulator = 0.0;
  1321. /* Search forward from the proposed arf/next gf position */
  1322. for (i = -1; i >= -b_frames; i--) {
  1323. if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF) break;
  1324. /* Update the motion related elements to the boost calculation */
  1325. accumulate_frame_motion_stats(
  1326. cpi, &this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
  1327. &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
  1328. /* Calculate the baseline boost number for this frame */
  1329. r = calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out);
  1330. /* We want to discount the the flash frame itself and the recovery
  1331. * frame that follows as both will have poor scores.
  1332. */
  1333. flash_detected =
  1334. detect_flash(cpi, (i + offset)) || detect_flash(cpi, (i + offset + 1));
  1335. /* Cumulative effect of prediction quality decay */
  1336. if (!flash_detected) {
  1337. decay_accumulator =
  1338. decay_accumulator * get_prediction_decay_rate(cpi, &this_frame);
  1339. decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
  1340. }
  1341. boost_score += (decay_accumulator * r);
  1342. /* Break out conditions. */
  1343. if ((!flash_detected) &&
  1344. ((mv_ratio_accumulator > 100.0) || (abs_mv_in_out_accumulator > 3.0) ||
  1345. (mv_in_out_accumulator < -2.0))) {
  1346. break;
  1347. }
  1348. }
  1349. *b_boost = (int)(boost_score * 100.0) >> 4;
  1350. return (*f_boost + *b_boost);
  1351. }
  1352. #endif
  1353. /* Analyse and define a gf/arf group . */
  1354. static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) {
  1355. FIRSTPASS_STATS next_frame;
  1356. FIRSTPASS_STATS *start_pos;
  1357. int i;
  1358. double r;
  1359. double boost_score = 0.0;
  1360. double old_boost_score = 0.0;
  1361. double gf_group_err = 0.0;
  1362. double gf_first_frame_err = 0.0;
  1363. double mod_frame_err = 0.0;
  1364. double mv_ratio_accumulator = 0.0;
  1365. double decay_accumulator = 1.0;
  1366. double loop_decay_rate = 1.00; /* Starting decay rate */
  1367. double this_frame_mv_in_out = 0.0;
  1368. double mv_in_out_accumulator = 0.0;
  1369. double abs_mv_in_out_accumulator = 0.0;
  1370. double mod_err_per_mb_accumulator = 0.0;
  1371. int max_bits = frame_max_bits(cpi); /* Max for a single frame */
  1372. unsigned int allow_alt_ref =
  1373. cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
  1374. int alt_boost = 0;
  1375. int f_boost = 0;
  1376. int b_boost = 0;
  1377. int flash_detected;
  1378. cpi->twopass.gf_group_bits = 0;
  1379. cpi->twopass.gf_decay_rate = 0;
  1380. vp8_clear_system_state();
  1381. start_pos = cpi->twopass.stats_in;
  1382. memset(&next_frame, 0, sizeof(next_frame)); /* assure clean */
  1383. /* Load stats for the current frame. */
  1384. mod_frame_err = calculate_modified_err(cpi, this_frame);
  1385. /* Note the error of the frame at the start of the group (this will be
  1386. * the GF frame error if we code a normal gf
  1387. */
  1388. gf_first_frame_err = mod_frame_err;
  1389. /* Special treatment if the current frame is a key frame (which is also
  1390. * a gf). If it is then its error score (and hence bit allocation) need
  1391. * to be subtracted out from the calculation for the GF group
  1392. */
  1393. if (cpi->common.frame_type == KEY_FRAME) gf_group_err -= gf_first_frame_err;
  1394. /* Scan forward to try and work out how many frames the next gf group
  1395. * should contain and what level of boost is appropriate for the GF
  1396. * or ARF that will be coded with the group
  1397. */
  1398. i = 0;
  1399. while (((i < cpi->twopass.static_scene_max_gf_interval) ||
  1400. ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
  1401. (i < cpi->twopass.frames_to_key)) {
  1402. i++;
  1403. /* Accumulate error score of frames in this gf group */
  1404. mod_frame_err = calculate_modified_err(cpi, this_frame);
  1405. gf_group_err += mod_frame_err;
  1406. mod_err_per_mb_accumulator +=
  1407. mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
  1408. if (EOF == input_stats(cpi, &next_frame)) break;
  1409. /* Test for the case where there is a brief flash but the prediction
  1410. * quality back to an earlier frame is then restored.
  1411. */
  1412. flash_detected = detect_flash(cpi, 0);
  1413. /* Update the motion related elements to the boost calculation */
  1414. accumulate_frame_motion_stats(
  1415. cpi, &next_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
  1416. &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
  1417. /* Calculate a baseline boost number for this frame */
  1418. r = calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out);
  1419. /* Cumulative effect of prediction quality decay */
  1420. if (!flash_detected) {
  1421. loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
  1422. decay_accumulator = decay_accumulator * loop_decay_rate;
  1423. decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
  1424. }
  1425. boost_score += (decay_accumulator * r);
  1426. /* Break clause to detect very still sections after motion
  1427. * For example a staic image after a fade or other transition.
  1428. */
  1429. if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
  1430. decay_accumulator)) {
  1431. allow_alt_ref = 0;
  1432. boost_score = old_boost_score;
  1433. break;
  1434. }
  1435. /* Break out conditions. */
  1436. if (
  1437. /* Break at cpi->max_gf_interval unless almost totally static */
  1438. (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
  1439. (
  1440. /* Dont break out with a very short interval */
  1441. (i > MIN_GF_INTERVAL) &&
  1442. /* Dont break out very close to a key frame */
  1443. ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
  1444. ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
  1445. (!flash_detected) && ((mv_ratio_accumulator > 100.0) ||
  1446. (abs_mv_in_out_accumulator > 3.0) ||
  1447. (mv_in_out_accumulator < -2.0) ||
  1448. ((boost_score - old_boost_score) < 2.0)))) {
  1449. boost_score = old_boost_score;
  1450. break;
  1451. }
  1452. memcpy(this_frame, &next_frame, sizeof(*this_frame));
  1453. old_boost_score = boost_score;
  1454. }
  1455. cpi->twopass.gf_decay_rate =
  1456. (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
  1457. /* When using CBR apply additional buffer related upper limits */
  1458. if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
  1459. double max_boost;
  1460. /* For cbr apply buffer related limits */
  1461. if (cpi->drop_frames_allowed) {
  1462. int64_t df_buffer_level = cpi->oxcf.drop_frames_water_mark *
  1463. (cpi->oxcf.optimal_buffer_level / 100);
  1464. if (cpi->buffer_level > df_buffer_level) {
  1465. max_boost =
  1466. ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) /
  1467. DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
  1468. } else {
  1469. max_boost = 0.0;
  1470. }
  1471. } else if (cpi->buffer_level > 0) {
  1472. max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) /
  1473. DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
  1474. } else {
  1475. max_boost = 0.0;
  1476. }
  1477. if (boost_score > max_boost) boost_score = max_boost;
  1478. }
  1479. /* Dont allow conventional gf too near the next kf */
  1480. if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL) {
  1481. while (i < cpi->twopass.frames_to_key) {
  1482. i++;
  1483. if (EOF == input_stats(cpi, this_frame)) break;
  1484. if (i < cpi->twopass.frames_to_key) {
  1485. mod_frame_err = calculate_modified_err(cpi, this_frame);
  1486. gf_group_err += mod_frame_err;
  1487. }
  1488. }
  1489. }
  1490. cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
  1491. #if NEW_BOOST
  1492. /* Alterrnative boost calculation for alt ref */
  1493. alt_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, &b_boost);
  1494. #endif
  1495. /* Should we use the alternate refernce frame */
  1496. if (allow_alt_ref && (i >= MIN_GF_INTERVAL) &&
  1497. /* dont use ARF very near next kf */
  1498. (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
  1499. #if NEW_BOOST
  1500. ((next_frame.pcnt_inter > 0.75) || (next_frame.pcnt_second_ref > 0.5)) &&
  1501. ((mv_in_out_accumulator / (double)i > -0.2) ||
  1502. (mv_in_out_accumulator > -2.0)) &&
  1503. (b_boost > 100) && (f_boost > 100))
  1504. #else
  1505. (next_frame.pcnt_inter > 0.75) &&
  1506. ((mv_in_out_accumulator / (double)i > -0.2) ||
  1507. (mv_in_out_accumulator > -2.0)) &&
  1508. (cpi->gfu_boost > 100) && (cpi->twopass.gf_decay_rate <=
  1509. (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))))
  1510. #endif
  1511. {
  1512. int Boost;
  1513. int allocation_chunks;
  1514. int Q =
  1515. (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
  1516. int tmp_q;
  1517. int arf_frame_bits = 0;
  1518. int group_bits;
  1519. #if NEW_BOOST
  1520. cpi->gfu_boost = alt_boost;
  1521. #endif
  1522. /* Estimate the bits to be allocated to the group as a whole */
  1523. if ((cpi->twopass.kf_group_bits > 0) &&
  1524. (cpi->twopass.kf_group_error_left > 0)) {
  1525. group_bits =
  1526. (int)((double)cpi->twopass.kf_group_bits *
  1527. (gf_group_err / (double)cpi->twopass.kf_group_error_left));
  1528. } else {
  1529. group_bits = 0;
  1530. }
  1531. /* Boost for arf frame */
  1532. #if NEW_BOOST
  1533. Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
  1534. #else
  1535. Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
  1536. #endif
  1537. Boost += (i * 50);
  1538. /* Set max and minimum boost and hence minimum allocation */
  1539. if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) {
  1540. Boost = ((cpi->baseline_gf_interval + 1) * 200);
  1541. } else if (Boost < 125) {
  1542. Boost = 125;
  1543. }
  1544. allocation_chunks = (i * 100) + Boost;
  1545. /* Normalize Altboost and allocations chunck down to prevent overflow */
  1546. while (Boost > 1000) {
  1547. Boost /= 2;
  1548. allocation_chunks /= 2;
  1549. }
  1550. /* Calculate the number of bits to be spent on the arf based on the
  1551. * boost number
  1552. */
  1553. arf_frame_bits =
  1554. (int)((double)Boost * (group_bits / (double)allocation_chunks));
  1555. /* Estimate if there are enough bits available to make worthwhile use
  1556. * of an arf.
  1557. */
  1558. tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits);
  1559. /* Only use an arf if it is likely we will be able to code
  1560. * it at a lower Q than the surrounding frames.
  1561. */
  1562. if (tmp_q < cpi->worst_quality) {
  1563. int half_gf_int;
  1564. int frames_after_arf;
  1565. int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
  1566. int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
  1567. cpi->source_alt_ref_pending = 1;
  1568. /*
  1569. * For alt ref frames the error score for the end frame of the
  1570. * group (the alt ref frame) should not contribute to the group
  1571. * total and hence the number of bit allocated to the group.
  1572. * Rather it forms part of the next group (it is the GF at the
  1573. * start of the next group)
  1574. * gf_group_err -= mod_frame_err;
  1575. *
  1576. * For alt ref frames alt ref frame is technically part of the
  1577. * GF frame for the next group but we always base the error
  1578. * calculation and bit allocation on the current group of frames.
  1579. *
  1580. * Set the interval till the next gf or arf.
  1581. * For ARFs this is the number of frames to be coded before the
  1582. * future frame that is coded as an ARF.
  1583. * The future frame itself is part of the next group
  1584. */
  1585. cpi->baseline_gf_interval = i;
  1586. /*
  1587. * Define the arnr filter width for this group of frames:
  1588. * We only filter frames that lie within a distance of half
  1589. * the GF interval from the ARF frame. We also have to trap
  1590. * cases where the filter extends beyond the end of clip.
  1591. * Note: this_frame->frame has been updated in the loop
  1592. * so it now points at the ARF frame.
  1593. */
  1594. half_gf_int = cpi->baseline_gf_interval >> 1;
  1595. frames_after_arf =
  1596. (int)(cpi->twopass.total_stats.count - this_frame->frame - 1);
  1597. switch (cpi->oxcf.arnr_type) {
  1598. case 1: /* Backward filter */
  1599. frames_fwd = 0;
  1600. if (frames_bwd > half_gf_int) frames_bwd = half_gf_int;
  1601. break;
  1602. case 2: /* Forward filter */
  1603. if (frames_fwd > half_gf_int) frames_fwd = half_gf_int;
  1604. if (frames_fwd > frames_after_arf) frames_fwd = frames_after_arf;
  1605. frames_bwd = 0;
  1606. break;
  1607. case 3: /* Centered filter */
  1608. default:
  1609. frames_fwd >>= 1;
  1610. if (frames_fwd > frames_after_arf) frames_fwd = frames_after_arf;
  1611. if (frames_fwd > half_gf_int) frames_fwd = half_gf_int;
  1612. frames_bwd = frames_fwd;
  1613. /* For even length filter there is one more frame backward
  1614. * than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
  1615. */
  1616. if (frames_bwd < half_gf_int) {
  1617. frames_bwd += (cpi->oxcf.arnr_max_frames + 1) & 0x1;
  1618. }
  1619. break;
  1620. }
  1621. cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
  1622. } else {
  1623. cpi->source_alt_ref_pending = 0;
  1624. cpi->baseline_gf_interval = i;
  1625. }
  1626. } else {
  1627. cpi->source_alt_ref_pending = 0;
  1628. cpi->baseline_gf_interval = i;
  1629. }
  1630. /*
  1631. * Now decide how many bits should be allocated to the GF group as a
  1632. * proportion of those remaining in the kf group.
  1633. * The final key frame group in the clip is treated as a special case
  1634. * where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
  1635. * This is also important for short clips where there may only be one
  1636. * key frame.
  1637. */
  1638. if (cpi->twopass.frames_to_key >=
  1639. (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame)) {
  1640. cpi->twopass.kf_group_bits =
  1641. (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
  1642. }
  1643. /* Calculate the bits to be allocated to the group as a whole */
  1644. if ((cpi->twopass.kf_group_bits > 0) &&
  1645. (cpi->twopass.kf_group_error_left > 0)) {
  1646. cpi->twopass.gf_group_bits =
  1647. (int64_t)(cpi->twopass.kf_group_bits *
  1648. (gf_group_err / cpi->twopass.kf_group_error_left));
  1649. } else {
  1650. cpi->twopass.gf_group_bits = 0;
  1651. }
  1652. cpi->twopass.gf_group_bits =
  1653. (cpi->twopass.gf_group_bits < 0)
  1654. ? 0
  1655. : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
  1656. ? cpi->twopass.kf_group_bits
  1657. : cpi->twopass.gf_group_bits;
  1658. /* Clip cpi->twopass.gf_group_bits based on user supplied data rate
  1659. * variability limit (cpi->oxcf.two_pass_vbrmax_section)
  1660. */
  1661. if (cpi->twopass.gf_group_bits >
  1662. (int64_t)max_bits * cpi->baseline_gf_interval) {
  1663. cpi->twopass.gf_group_bits = (int64_t)max_bits * cpi->baseline_gf_interval;
  1664. }
  1665. /* Reset the file position */
  1666. reset_fpf_position(cpi, start_pos);
  1667. /* Update the record of error used so far (only done once per gf group) */
  1668. cpi->twopass.modified_error_used += gf_group_err;
  1669. /* Assign bits to the arf or gf. */
  1670. for (i = 0; i <= (cpi->source_alt_ref_pending &&
  1671. cpi->common.frame_type != KEY_FRAME);
  1672. i++) {
  1673. int Boost;
  1674. int allocation_chunks;
  1675. int Q =
  1676. (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
  1677. int gf_bits;
  1678. /* For ARF frames */
  1679. if (cpi->source_alt_ref_pending && i == 0) {
  1680. #if NEW_BOOST
  1681. Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
  1682. #else
  1683. Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
  1684. #endif
  1685. Boost += (cpi->baseline_gf_interval * 50);
  1686. /* Set max and minimum boost and hence minimum allocation */
  1687. if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) {
  1688. Boost = ((cpi->baseline_gf_interval + 1) * 200);
  1689. } else if (Boost < 125) {
  1690. Boost = 125;
  1691. }
  1692. allocation_chunks = ((cpi->baseline_gf_interval + 1) * 100) + Boost;
  1693. }
  1694. /* Else for standard golden frames */
  1695. else {
  1696. /* boost based on inter / intra ratio of subsequent frames */
  1697. Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
  1698. /* Set max and minimum boost and hence minimum allocation */
  1699. if (Boost > (cpi->baseline_gf_interval * 150)) {
  1700. Boost = (cpi->baseline_gf_interval * 150);
  1701. } else if (Boost < 125) {
  1702. Boost = 125;
  1703. }
  1704. allocation_chunks = (cpi->baseline_gf_interval * 100) + (Boost - 100);
  1705. }
  1706. /* Normalize Altboost and allocations chunck down to prevent overflow */
  1707. while (Boost > 1000) {
  1708. Boost /= 2;
  1709. allocation_chunks /= 2;
  1710. }
  1711. /* Calculate the number of bits to be spent on the gf or arf based on
  1712. * the boost number
  1713. */
  1714. gf_bits = (int)((double)Boost *
  1715. (cpi->twopass.gf_group_bits / (double)allocation_chunks));
  1716. /* If the frame that is to be boosted is simpler than the average for
  1717. * the gf/arf group then use an alternative calculation
  1718. * based on the error score of the frame itself
  1719. */
  1720. if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval) {
  1721. double alt_gf_grp_bits;
  1722. int alt_gf_bits;
  1723. alt_gf_grp_bits =
  1724. (double)cpi->twopass.kf_group_bits *
  1725. (mod_frame_err * (double)cpi->baseline_gf_interval) /
  1726. DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left);
  1727. alt_gf_bits =
  1728. (int)((double)Boost * (alt_gf_grp_bits / (double)allocation_chunks));
  1729. if (gf_bits > alt_gf_bits) {
  1730. gf_bits = alt_gf_bits;
  1731. }
  1732. }
  1733. /* Else if it is harder than other frames in the group make sure it at
  1734. * least receives an allocation in keeping with its relative error
  1735. * score, otherwise it may be worse off than an "un-boosted" frame
  1736. */
  1737. else {
  1738. int alt_gf_bits =
  1739. (int)((double)cpi->twopass.kf_group_bits * mod_frame_err /
  1740. DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left));
  1741. if (alt_gf_bits > gf_bits) {
  1742. gf_bits = alt_gf_bits;
  1743. }
  1744. }
  1745. /* Apply an additional limit for CBR */
  1746. if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
  1747. if (cpi->twopass.gf_bits > (int)(cpi->buffer_level >> 1)) {
  1748. cpi->twopass.gf_bits = (int)(cpi->buffer_level >> 1);
  1749. }
  1750. }
  1751. /* Dont allow a negative value for gf_bits */
  1752. if (gf_bits < 0) gf_bits = 0;
  1753. /* Add in minimum for a frame */
  1754. gf_bits += cpi->min_frame_bandwidth;
  1755. if (i == 0) {
  1756. cpi->twopass.gf_bits = gf_bits;
  1757. }
  1758. if (i == 1 || (!cpi->source_alt_ref_pending &&
  1759. (cpi->common.frame_type != KEY_FRAME))) {
  1760. /* Per frame bit target for this frame */
  1761. cpi->per_frame_bandwidth = gf_bits;
  1762. }
  1763. }
  1764. {
  1765. /* Adjust KF group bits and error remainin */
  1766. cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err;
  1767. cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
  1768. if (cpi->twopass.kf_group_bits < 0) cpi->twopass.kf_group_bits = 0;
  1769. /* Note the error score left in the remaining frames of the group.
  1770. * For normal GFs we want to remove the error score for the first
  1771. * frame of the group (except in Key frame case where this has
  1772. * already happened)
  1773. */
  1774. if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME) {
  1775. cpi->twopass.gf_group_error_left =
  1776. (int)(gf_group_err - gf_first_frame_err);
  1777. } else {
  1778. cpi->twopass.gf_group_error_left = (int)gf_group_err;
  1779. }
  1780. cpi->twopass.gf_group_bits -=
  1781. cpi->twopass.gf_bits - cpi->min_frame_bandwidth;
  1782. if (cpi->twopass.gf_group_bits < 0) cpi->twopass.gf_group_bits = 0;
  1783. /* This condition could fail if there are two kfs very close together
  1784. * despite (MIN_GF_INTERVAL) and would cause a devide by 0 in the
  1785. * calculation of cpi->twopass.alt_extra_bits.
  1786. */
  1787. if (cpi->baseline_gf_interval >= 3) {
  1788. #if NEW_BOOST
  1789. int boost = (cpi->source_alt_ref_pending) ? b_boost : cpi->gfu_boost;
  1790. #else
  1791. int boost = cpi->gfu_boost;
  1792. #endif
  1793. if (boost >= 150) {
  1794. int pct_extra;
  1795. pct_extra = (boost - 100) / 50;
  1796. pct_extra = (pct_extra > 20) ? 20 : pct_extra;
  1797. cpi->twopass.alt_extra_bits =
  1798. (int)(cpi->twopass.gf_group_bits * pct_extra) / 100;
  1799. cpi->twopass.gf_group_bits -= cpi->twopass.alt_extra_bits;
  1800. cpi->twopass.alt_extra_bits /= ((cpi->baseline_gf_interval - 1) >> 1);
  1801. } else {
  1802. cpi->twopass.alt_extra_bits = 0;
  1803. }
  1804. } else {
  1805. cpi->twopass.alt_extra_bits = 0;
  1806. }
  1807. }
  1808. /* Adjustments based on a measure of complexity of the section */
  1809. if (cpi->common.frame_type != KEY_FRAME) {
  1810. FIRSTPASS_STATS sectionstats;
  1811. double Ratio;
  1812. zero_stats(&sectionstats);
  1813. reset_fpf_position(cpi, start_pos);
  1814. for (i = 0; i < cpi->baseline_gf_interval; ++i) {
  1815. input_stats(cpi, &next_frame);
  1816. accumulate_stats(&sectionstats, &next_frame);
  1817. }
  1818. avg_stats(&sectionstats);
  1819. cpi->twopass.section_intra_rating =
  1820. (unsigned int)(sectionstats.intra_error /
  1821. DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
  1822. Ratio = sectionstats.intra_error /
  1823. DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
  1824. cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
  1825. if (cpi->twopass.section_max_qfactor < 0.80) {
  1826. cpi->twopass.section_max_qfactor = 0.80;
  1827. }
  1828. reset_fpf_position(cpi, start_pos);
  1829. }
  1830. }
  1831. /* Allocate bits to a normal frame that is neither a gf an arf or a key frame.
  1832. */
  1833. static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) {
  1834. int target_frame_size;
  1835. double modified_err;
  1836. double err_fraction;
  1837. int max_bits = frame_max_bits(cpi); /* Max for a single frame */
  1838. /* Calculate modified prediction error used in bit allocation */
  1839. modified_err = calculate_modified_err(cpi, this_frame);
  1840. /* What portion of the remaining GF group error is used by this frame */
  1841. if (cpi->twopass.gf_group_error_left > 0) {
  1842. err_fraction = modified_err / cpi->twopass.gf_group_error_left;
  1843. } else {
  1844. err_fraction = 0.0;
  1845. }
  1846. /* How many of those bits available for allocation should we give it? */
  1847. target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction);
  1848. /* Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits)
  1849. * at the top end.
  1850. */
  1851. if (target_frame_size < 0) {
  1852. target_frame_size = 0;
  1853. } else {
  1854. if (target_frame_size > max_bits) target_frame_size = max_bits;
  1855. if (target_frame_size > cpi->twopass.gf_group_bits) {
  1856. target_frame_size = (int)cpi->twopass.gf_group_bits;
  1857. }
  1858. }
  1859. /* Adjust error and bits remaining */
  1860. cpi->twopass.gf_group_error_left -= (int)modified_err;
  1861. cpi->twopass.gf_group_bits -= target_frame_size;
  1862. if (cpi->twopass.gf_group_bits < 0) cpi->twopass.gf_group_bits = 0;
  1863. /* Add in the minimum number of bits that is set aside for every frame. */
  1864. target_frame_size += cpi->min_frame_bandwidth;
  1865. /* Every other frame gets a few extra bits */
  1866. if ((cpi->frames_since_golden & 0x01) &&
  1867. (cpi->frames_till_gf_update_due > 0)) {
  1868. target_frame_size += cpi->twopass.alt_extra_bits;
  1869. }
  1870. /* Per frame bit target for this frame */
  1871. cpi->per_frame_bandwidth = target_frame_size;
  1872. }
  1873. void vp8_second_pass(VP8_COMP *cpi) {
  1874. int tmp_q;
  1875. int frames_left =
  1876. (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame);
  1877. FIRSTPASS_STATS this_frame;
  1878. FIRSTPASS_STATS this_frame_copy;
  1879. double this_frame_intra_error;
  1880. double this_frame_coded_error;
  1881. int overhead_bits;
  1882. vp8_zero(this_frame);
  1883. if (!cpi->twopass.stats_in) {
  1884. return;
  1885. }
  1886. vp8_clear_system_state();
  1887. if (EOF == input_stats(cpi, &this_frame)) return;
  1888. this_frame_intra_error = this_frame.intra_error;
  1889. this_frame_coded_error = this_frame.coded_error;
  1890. /* keyframe and section processing ! */
  1891. if (cpi->twopass.frames_to_key == 0) {
  1892. /* Define next KF group and assign bits to it */
  1893. memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
  1894. find_next_key_frame(cpi, &this_frame_copy);
  1895. /* Special case: Error error_resilient_mode mode does not make much
  1896. * sense for two pass but with its current meaning this code is
  1897. * designed to stop outlandish behaviour if someone does set it when
  1898. * using two pass. It effectively disables GF groups. This is
  1899. * temporary code until we decide what should really happen in this
  1900. * case.
  1901. */
  1902. if (cpi->oxcf.error_resilient_mode) {
  1903. cpi->twopass.gf_group_bits = cpi->twopass.kf_group_bits;
  1904. cpi->twopass.gf_group_error_left = (int)cpi->twopass.kf_group_error_left;
  1905. cpi->baseline_gf_interval = cpi->twopass.frames_to_key;
  1906. cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
  1907. cpi->source_alt_ref_pending = 0;
  1908. }
  1909. }
  1910. /* Is this a GF / ARF (Note that a KF is always also a GF) */
  1911. if (cpi->frames_till_gf_update_due == 0) {
  1912. /* Define next gf group and assign bits to it */
  1913. memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
  1914. define_gf_group(cpi, &this_frame_copy);
  1915. /* If we are going to code an altref frame at the end of the group
  1916. * and the current frame is not a key frame.... If the previous
  1917. * group used an arf this frame has already benefited from that arf
  1918. * boost and it should not be given extra bits If the previous
  1919. * group was NOT coded using arf we may want to apply some boost to
  1920. * this GF as well
  1921. */
  1922. if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) {
  1923. /* Assign a standard frames worth of bits from those allocated
  1924. * to the GF group
  1925. */
  1926. int bak = cpi->per_frame_bandwidth;
  1927. memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
  1928. assign_std_frame_bits(cpi, &this_frame_copy);
  1929. cpi->per_frame_bandwidth = bak;
  1930. }
  1931. }
  1932. /* Otherwise this is an ordinary frame */
  1933. else {
  1934. /* Special case: Error error_resilient_mode mode does not make much
  1935. * sense for two pass but with its current meaning but this code is
  1936. * designed to stop outlandish behaviour if someone does set it
  1937. * when using two pass. It effectively disables GF groups. This is
  1938. * temporary code till we decide what should really happen in this
  1939. * case.
  1940. */
  1941. if (cpi->oxcf.error_resilient_mode) {
  1942. cpi->frames_till_gf_update_due = cpi->twopass.frames_to_key;
  1943. if (cpi->common.frame_type != KEY_FRAME) {
  1944. /* Assign bits from those allocated to the GF group */
  1945. memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
  1946. assign_std_frame_bits(cpi, &this_frame_copy);
  1947. }
  1948. } else {
  1949. /* Assign bits from those allocated to the GF group */
  1950. memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
  1951. assign_std_frame_bits(cpi, &this_frame_copy);
  1952. }
  1953. }
  1954. /* Keep a globally available copy of this and the next frame's iiratio. */
  1955. cpi->twopass.this_iiratio =
  1956. (unsigned int)(this_frame_intra_error /
  1957. DOUBLE_DIVIDE_CHECK(this_frame_coded_error));
  1958. {
  1959. FIRSTPASS_STATS next_frame;
  1960. if (lookup_next_frame_stats(cpi, &next_frame) != EOF) {
  1961. cpi->twopass.next_iiratio =
  1962. (unsigned int)(next_frame.intra_error /
  1963. DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
  1964. }
  1965. }
  1966. /* Set nominal per second bandwidth for this frame */
  1967. cpi->target_bandwidth =
  1968. (int)(cpi->per_frame_bandwidth * cpi->output_framerate);
  1969. if (cpi->target_bandwidth < 0) cpi->target_bandwidth = 0;
  1970. /* Account for mv, mode and other overheads. */
  1971. overhead_bits = (int)estimate_modemvcost(cpi, &cpi->twopass.total_left_stats);
  1972. /* Special case code for first frame. */
  1973. if (cpi->common.current_video_frame == 0) {
  1974. cpi->twopass.est_max_qcorrection_factor = 1.0;
  1975. /* Set a cq_level in constrained quality mode. */
  1976. if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
  1977. int est_cq;
  1978. est_cq = estimate_cq(cpi, &cpi->twopass.total_left_stats,
  1979. (int)(cpi->twopass.bits_left / frames_left),
  1980. overhead_bits);
  1981. cpi->cq_target_quality = cpi->oxcf.cq_level;
  1982. if (est_cq > cpi->cq_target_quality) cpi->cq_target_quality = est_cq;
  1983. }
  1984. /* guess at maxq needed in 2nd pass */
  1985. cpi->twopass.maxq_max_limit = cpi->worst_quality;
  1986. cpi->twopass.maxq_min_limit = cpi->best_quality;
  1987. tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats,
  1988. (int)(cpi->twopass.bits_left / frames_left),
  1989. overhead_bits);
  1990. /* Limit the maxq value returned subsequently.
  1991. * This increases the risk of overspend or underspend if the initial
  1992. * estimate for the clip is bad, but helps prevent excessive
  1993. * variation in Q, especially near the end of a clip
  1994. * where for example a small overspend may cause Q to crash
  1995. */
  1996. cpi->twopass.maxq_max_limit =
  1997. ((tmp_q + 32) < cpi->worst_quality) ? (tmp_q + 32) : cpi->worst_quality;
  1998. cpi->twopass.maxq_min_limit =
  1999. ((tmp_q - 32) > cpi->best_quality) ? (tmp_q - 32) : cpi->best_quality;
  2000. cpi->active_worst_quality = tmp_q;
  2001. cpi->ni_av_qi = tmp_q;
  2002. }
  2003. /* The last few frames of a clip almost always have to few or too many
  2004. * bits and for the sake of over exact rate control we dont want to make
  2005. * radical adjustments to the allowed quantizer range just to use up a
  2006. * few surplus bits or get beneath the target rate.
  2007. */
  2008. else if ((cpi->common.current_video_frame <
  2009. (((unsigned int)cpi->twopass.total_stats.count * 255) >> 8)) &&
  2010. ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
  2011. (unsigned int)cpi->twopass.total_stats.count)) {
  2012. if (frames_left < 1) frames_left = 1;
  2013. tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats,
  2014. (int)(cpi->twopass.bits_left / frames_left),
  2015. overhead_bits);
  2016. /* Move active_worst_quality but in a damped way */
  2017. if (tmp_q > cpi->active_worst_quality) {
  2018. cpi->active_worst_quality++;
  2019. } else if (tmp_q < cpi->active_worst_quality) {
  2020. cpi->active_worst_quality--;
  2021. }
  2022. cpi->active_worst_quality =
  2023. ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
  2024. }
  2025. cpi->twopass.frames_to_key--;
  2026. /* Update the total stats remaining sturcture */
  2027. subtract_stats(&cpi->twopass.total_left_stats, &this_frame);
  2028. }
  2029. static int test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame,
  2030. FIRSTPASS_STATS *this_frame,
  2031. FIRSTPASS_STATS *next_frame) {
  2032. int is_viable_kf = 0;
  2033. /* Does the frame satisfy the primary criteria of a key frame
  2034. * If so, then examine how well it predicts subsequent frames
  2035. */
  2036. if ((this_frame->pcnt_second_ref < 0.10) &&
  2037. (next_frame->pcnt_second_ref < 0.10) &&
  2038. ((this_frame->pcnt_inter < 0.05) ||
  2039. (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) &&
  2040. ((this_frame->intra_error /
  2041. DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
  2042. ((fabs(last_frame->coded_error - this_frame->coded_error) /
  2043. DOUBLE_DIVIDE_CHECK(this_frame->coded_error) >
  2044. .40) ||
  2045. (fabs(last_frame->intra_error - this_frame->intra_error) /
  2046. DOUBLE_DIVIDE_CHECK(this_frame->intra_error) >
  2047. .40) ||
  2048. ((next_frame->intra_error /
  2049. DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) {
  2050. int i;
  2051. FIRSTPASS_STATS *start_pos;
  2052. FIRSTPASS_STATS local_next_frame;
  2053. double boost_score = 0.0;
  2054. double old_boost_score = 0.0;
  2055. double decay_accumulator = 1.0;
  2056. double next_iiratio;
  2057. memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
  2058. /* Note the starting file position so we can reset to it */
  2059. start_pos = cpi->twopass.stats_in;
  2060. /* Examine how well the key frame predicts subsequent frames */
  2061. for (i = 0; i < 16; ++i) {
  2062. next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error /
  2063. DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
  2064. if (next_iiratio > RMAX) next_iiratio = RMAX;
  2065. /* Cumulative effect of decay in prediction quality */
  2066. if (local_next_frame.pcnt_inter > 0.85) {
  2067. decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
  2068. } else {
  2069. decay_accumulator =
  2070. decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
  2071. }
  2072. /* Keep a running total */
  2073. boost_score += (decay_accumulator * next_iiratio);
  2074. /* Test various breakout clauses */
  2075. if ((local_next_frame.pcnt_inter < 0.05) || (next_iiratio < 1.5) ||
  2076. (((local_next_frame.pcnt_inter - local_next_frame.pcnt_neutral) <
  2077. 0.20) &&
  2078. (next_iiratio < 3.0)) ||
  2079. ((boost_score - old_boost_score) < 0.5) ||
  2080. (local_next_frame.intra_error < 200)) {
  2081. break;
  2082. }
  2083. old_boost_score = boost_score;
  2084. /* Get the next frame details */
  2085. if (EOF == input_stats(cpi, &local_next_frame)) break;
  2086. }
  2087. /* If there is tolerable prediction for at least the next 3 frames
  2088. * then break out else discard this pottential key frame and move on
  2089. */
  2090. if (boost_score > 5.0 && (i > 3)) {
  2091. is_viable_kf = 1;
  2092. } else {
  2093. /* Reset the file position */
  2094. reset_fpf_position(cpi, start_pos);
  2095. is_viable_kf = 0;
  2096. }
  2097. }
  2098. return is_viable_kf;
  2099. }
  2100. static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) {
  2101. int i, j;
  2102. FIRSTPASS_STATS last_frame;
  2103. FIRSTPASS_STATS first_frame;
  2104. FIRSTPASS_STATS next_frame;
  2105. FIRSTPASS_STATS *start_position;
  2106. double decay_accumulator = 1.0;
  2107. double boost_score = 0;
  2108. double old_boost_score = 0.0;
  2109. double loop_decay_rate;
  2110. double kf_mod_err = 0.0;
  2111. double kf_group_err = 0.0;
  2112. double kf_group_intra_err = 0.0;
  2113. double kf_group_coded_err = 0.0;
  2114. double recent_loop_decay[8] = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
  2115. memset(&next_frame, 0, sizeof(next_frame));
  2116. vp8_clear_system_state();
  2117. start_position = cpi->twopass.stats_in;
  2118. cpi->common.frame_type = KEY_FRAME;
  2119. /* is this a forced key frame by interval */
  2120. cpi->this_key_frame_forced = cpi->next_key_frame_forced;
  2121. /* Clear the alt ref active flag as this can never be active on a key
  2122. * frame
  2123. */
  2124. cpi->source_alt_ref_active = 0;
  2125. /* Kf is always a gf so clear frames till next gf counter */
  2126. cpi->frames_till_gf_update_due = 0;
  2127. cpi->twopass.frames_to_key = 1;
  2128. /* Take a copy of the initial frame details */
  2129. memcpy(&first_frame, this_frame, sizeof(*this_frame));
  2130. cpi->twopass.kf_group_bits = 0;
  2131. cpi->twopass.kf_group_error_left = 0;
  2132. kf_mod_err = calculate_modified_err(cpi, this_frame);
  2133. /* find the next keyframe */
  2134. i = 0;
  2135. while (cpi->twopass.stats_in < cpi->twopass.stats_in_end) {
  2136. /* Accumulate kf group error */
  2137. kf_group_err += calculate_modified_err(cpi, this_frame);
  2138. /* These figures keep intra and coded error counts for all frames
  2139. * including key frames in the group. The effect of the key frame
  2140. * itself can be subtracted out using the first_frame data
  2141. * collected above
  2142. */
  2143. kf_group_intra_err += this_frame->intra_error;
  2144. kf_group_coded_err += this_frame->coded_error;
  2145. /* Load the next frame's stats. */
  2146. memcpy(&last_frame, this_frame, sizeof(*this_frame));
  2147. input_stats(cpi, this_frame);
  2148. /* Provided that we are not at the end of the file... */
  2149. if (cpi->oxcf.auto_key &&
  2150. lookup_next_frame_stats(cpi, &next_frame) != EOF) {
  2151. /* Normal scene cut check */
  2152. if ((i >= MIN_GF_INTERVAL) &&
  2153. test_candidate_kf(cpi, &last_frame, this_frame, &next_frame)) {
  2154. break;
  2155. }
  2156. /* How fast is prediction quality decaying */
  2157. loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
  2158. /* We want to know something about the recent past... rather than
  2159. * as used elsewhere where we are concened with decay in prediction
  2160. * quality since the last GF or KF.
  2161. */
  2162. recent_loop_decay[i % 8] = loop_decay_rate;
  2163. decay_accumulator = 1.0;
  2164. for (j = 0; j < 8; ++j) {
  2165. decay_accumulator = decay_accumulator * recent_loop_decay[j];
  2166. }
  2167. /* Special check for transition or high motion followed by a
  2168. * static scene.
  2169. */
  2170. if (detect_transition_to_still(cpi, i,
  2171. ((int)(cpi->key_frame_frequency) - (int)i),
  2172. loop_decay_rate, decay_accumulator)) {
  2173. break;
  2174. }
  2175. /* Step on to the next frame */
  2176. cpi->twopass.frames_to_key++;
  2177. /* If we don't have a real key frame within the next two
  2178. * forcekeyframeevery intervals then break out of the loop.
  2179. */
  2180. if (cpi->twopass.frames_to_key >= 2 * (int)cpi->key_frame_frequency) {
  2181. break;
  2182. }
  2183. } else {
  2184. cpi->twopass.frames_to_key++;
  2185. }
  2186. i++;
  2187. }
  2188. /* If there is a max kf interval set by the user we must obey it.
  2189. * We already breakout of the loop above at 2x max.
  2190. * This code centers the extra kf if the actual natural
  2191. * interval is between 1x and 2x
  2192. */
  2193. if (cpi->oxcf.auto_key &&
  2194. cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency) {
  2195. FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
  2196. FIRSTPASS_STATS tmp_frame;
  2197. cpi->twopass.frames_to_key /= 2;
  2198. /* Copy first frame details */
  2199. memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
  2200. /* Reset to the start of the group */
  2201. reset_fpf_position(cpi, start_position);
  2202. kf_group_err = 0;
  2203. kf_group_intra_err = 0;
  2204. kf_group_coded_err = 0;
  2205. /* Rescan to get the correct error data for the forced kf group */
  2206. for (i = 0; i < cpi->twopass.frames_to_key; ++i) {
  2207. /* Accumulate kf group errors */
  2208. kf_group_err += calculate_modified_err(cpi, &tmp_frame);
  2209. kf_group_intra_err += tmp_frame.intra_error;
  2210. kf_group_coded_err += tmp_frame.coded_error;
  2211. /* Load a the next frame's stats */
  2212. input_stats(cpi, &tmp_frame);
  2213. }
  2214. /* Reset to the start of the group */
  2215. reset_fpf_position(cpi, current_pos);
  2216. cpi->next_key_frame_forced = 1;
  2217. } else {
  2218. cpi->next_key_frame_forced = 0;
  2219. }
  2220. /* Special case for the last frame of the file */
  2221. if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) {
  2222. /* Accumulate kf group error */
  2223. kf_group_err += calculate_modified_err(cpi, this_frame);
  2224. /* These figures keep intra and coded error counts for all frames
  2225. * including key frames in the group. The effect of the key frame
  2226. * itself can be subtracted out using the first_frame data
  2227. * collected above
  2228. */
  2229. kf_group_intra_err += this_frame->intra_error;
  2230. kf_group_coded_err += this_frame->coded_error;
  2231. }
  2232. /* Calculate the number of bits that should be assigned to the kf group. */
  2233. if ((cpi->twopass.bits_left > 0) &&
  2234. (cpi->twopass.modified_error_left > 0.0)) {
  2235. /* Max for a single normal frame (not key frame) */
  2236. int max_bits = frame_max_bits(cpi);
  2237. /* Maximum bits for the kf group */
  2238. int64_t max_grp_bits;
  2239. /* Default allocation based on bits left and relative
  2240. * complexity of the section
  2241. */
  2242. cpi->twopass.kf_group_bits =
  2243. (int64_t)(cpi->twopass.bits_left *
  2244. (kf_group_err / cpi->twopass.modified_error_left));
  2245. /* Clip based on maximum per frame rate defined by the user. */
  2246. max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
  2247. if (cpi->twopass.kf_group_bits > max_grp_bits) {
  2248. cpi->twopass.kf_group_bits = max_grp_bits;
  2249. }
  2250. /* Additional special case for CBR if buffer is getting full. */
  2251. if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
  2252. int64_t opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
  2253. int64_t buffer_lvl = cpi->buffer_level;
  2254. /* If the buffer is near or above the optimal and this kf group is
  2255. * not being allocated much then increase the allocation a bit.
  2256. */
  2257. if (buffer_lvl >= opt_buffer_lvl) {
  2258. int64_t high_water_mark =
  2259. (opt_buffer_lvl + cpi->oxcf.maximum_buffer_size) >> 1;
  2260. int64_t av_group_bits;
  2261. /* Av bits per frame * number of frames */
  2262. av_group_bits = (int64_t)cpi->av_per_frame_bandwidth *
  2263. (int64_t)cpi->twopass.frames_to_key;
  2264. /* We are at or above the maximum. */
  2265. if (cpi->buffer_level >= high_water_mark) {
  2266. int64_t min_group_bits;
  2267. min_group_bits =
  2268. av_group_bits + (int64_t)(buffer_lvl - high_water_mark);
  2269. if (cpi->twopass.kf_group_bits < min_group_bits) {
  2270. cpi->twopass.kf_group_bits = min_group_bits;
  2271. }
  2272. }
  2273. /* We are above optimal but below the maximum */
  2274. else if (cpi->twopass.kf_group_bits < av_group_bits) {
  2275. int64_t bits_below_av = av_group_bits - cpi->twopass.kf_group_bits;
  2276. cpi->twopass.kf_group_bits += (int64_t)(
  2277. (double)bits_below_av * (double)(buffer_lvl - opt_buffer_lvl) /
  2278. (double)(high_water_mark - opt_buffer_lvl));
  2279. }
  2280. }
  2281. }
  2282. } else {
  2283. cpi->twopass.kf_group_bits = 0;
  2284. }
  2285. /* Reset the first pass file position */
  2286. reset_fpf_position(cpi, start_position);
  2287. /* determine how big to make this keyframe based on how well the
  2288. * subsequent frames use inter blocks
  2289. */
  2290. decay_accumulator = 1.0;
  2291. boost_score = 0.0;
  2292. for (i = 0; i < cpi->twopass.frames_to_key; ++i) {
  2293. double r;
  2294. if (EOF == input_stats(cpi, &next_frame)) break;
  2295. if (next_frame.intra_error > cpi->twopass.kf_intra_err_min) {
  2296. r = (IIKFACTOR2 * next_frame.intra_error /
  2297. DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
  2298. } else {
  2299. r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
  2300. DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
  2301. }
  2302. if (r > RMAX) r = RMAX;
  2303. /* How fast is prediction quality decaying */
  2304. loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
  2305. decay_accumulator = decay_accumulator * loop_decay_rate;
  2306. decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
  2307. boost_score += (decay_accumulator * r);
  2308. if ((i > MIN_GF_INTERVAL) && ((boost_score - old_boost_score) < 1.0)) {
  2309. break;
  2310. }
  2311. old_boost_score = boost_score;
  2312. }
  2313. if (1) {
  2314. FIRSTPASS_STATS sectionstats;
  2315. double Ratio;
  2316. zero_stats(&sectionstats);
  2317. reset_fpf_position(cpi, start_position);
  2318. for (i = 0; i < cpi->twopass.frames_to_key; ++i) {
  2319. input_stats(cpi, &next_frame);
  2320. accumulate_stats(&sectionstats, &next_frame);
  2321. }
  2322. avg_stats(&sectionstats);
  2323. cpi->twopass.section_intra_rating =
  2324. (unsigned int)(sectionstats.intra_error /
  2325. DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
  2326. Ratio = sectionstats.intra_error /
  2327. DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
  2328. cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
  2329. if (cpi->twopass.section_max_qfactor < 0.80) {
  2330. cpi->twopass.section_max_qfactor = 0.80;
  2331. }
  2332. }
  2333. /* When using CBR apply additional buffer fullness related upper limits */
  2334. if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
  2335. double max_boost;
  2336. if (cpi->drop_frames_allowed) {
  2337. int df_buffer_level = (int)(cpi->oxcf.drop_frames_water_mark *
  2338. (cpi->oxcf.optimal_buffer_level / 100));
  2339. if (cpi->buffer_level > df_buffer_level) {
  2340. max_boost =
  2341. ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) /
  2342. DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
  2343. } else {
  2344. max_boost = 0.0;
  2345. }
  2346. } else if (cpi->buffer_level > 0) {
  2347. max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) /
  2348. DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
  2349. } else {
  2350. max_boost = 0.0;
  2351. }
  2352. if (boost_score > max_boost) boost_score = max_boost;
  2353. }
  2354. /* Reset the first pass file position */
  2355. reset_fpf_position(cpi, start_position);
  2356. /* Work out how many bits to allocate for the key frame itself */
  2357. if (1) {
  2358. int kf_boost = (int)boost_score;
  2359. int allocation_chunks;
  2360. int Counter = cpi->twopass.frames_to_key;
  2361. int alt_kf_bits;
  2362. YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
  2363. /* Min boost based on kf interval */
  2364. #if 0
  2365. while ((kf_boost < 48) && (Counter > 0))
  2366. {
  2367. Counter -= 2;
  2368. kf_boost ++;
  2369. }
  2370. #endif
  2371. if (kf_boost < 48) {
  2372. kf_boost += ((Counter + 1) >> 1);
  2373. if (kf_boost > 48) kf_boost = 48;
  2374. }
  2375. /* bigger frame sizes need larger kf boosts, smaller frames smaller
  2376. * boosts...
  2377. */
  2378. if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240)) {
  2379. kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
  2380. } else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240)) {
  2381. kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
  2382. }
  2383. /* Min KF boost */
  2384. kf_boost = (int)((double)kf_boost * 100.0) >> 4; /* Scale 16 to 100 */
  2385. if (kf_boost < 250) kf_boost = 250;
  2386. /*
  2387. * We do three calculations for kf size.
  2388. * The first is based on the error score for the whole kf group.
  2389. * The second (optionaly) on the key frames own error if this is
  2390. * smaller than the average for the group.
  2391. * The final one insures that the frame receives at least the
  2392. * allocation it would have received based on its own error score vs
  2393. * the error score remaining
  2394. * Special case if the sequence appears almost totaly static
  2395. * as measured by the decay accumulator. In this case we want to
  2396. * spend almost all of the bits on the key frame.
  2397. * cpi->twopass.frames_to_key-1 because key frame itself is taken
  2398. * care of by kf_boost.
  2399. */
  2400. if (decay_accumulator >= 0.99) {
  2401. allocation_chunks = ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost;
  2402. } else {
  2403. allocation_chunks = ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost;
  2404. }
  2405. /* Normalize Altboost and allocations chunck down to prevent overflow */
  2406. while (kf_boost > 1000) {
  2407. kf_boost /= 2;
  2408. allocation_chunks /= 2;
  2409. }
  2410. cpi->twopass.kf_group_bits =
  2411. (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
  2412. /* Calculate the number of bits to be spent on the key frame */
  2413. cpi->twopass.kf_bits =
  2414. (int)((double)kf_boost *
  2415. ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
  2416. /* Apply an additional limit for CBR */
  2417. if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
  2418. if (cpi->twopass.kf_bits > (int)((3 * cpi->buffer_level) >> 2)) {
  2419. cpi->twopass.kf_bits = (int)((3 * cpi->buffer_level) >> 2);
  2420. }
  2421. }
  2422. /* If the key frame is actually easier than the average for the
  2423. * kf group (which does sometimes happen... eg a blank intro frame)
  2424. * Then use an alternate calculation based on the kf error score
  2425. * which should give a smaller key frame.
  2426. */
  2427. if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key) {
  2428. double alt_kf_grp_bits =
  2429. ((double)cpi->twopass.bits_left *
  2430. (kf_mod_err * (double)cpi->twopass.frames_to_key) /
  2431. DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
  2432. alt_kf_bits = (int)((double)kf_boost *
  2433. (alt_kf_grp_bits / (double)allocation_chunks));
  2434. if (cpi->twopass.kf_bits > alt_kf_bits) {
  2435. cpi->twopass.kf_bits = alt_kf_bits;
  2436. }
  2437. }
  2438. /* Else if it is much harder than other frames in the group make sure
  2439. * it at least receives an allocation in keeping with its relative
  2440. * error score
  2441. */
  2442. else {
  2443. alt_kf_bits = (int)((double)cpi->twopass.bits_left *
  2444. (kf_mod_err / DOUBLE_DIVIDE_CHECK(
  2445. cpi->twopass.modified_error_left)));
  2446. if (alt_kf_bits > cpi->twopass.kf_bits) {
  2447. cpi->twopass.kf_bits = alt_kf_bits;
  2448. }
  2449. }
  2450. cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
  2451. /* Add in the minimum frame allowance */
  2452. cpi->twopass.kf_bits += cpi->min_frame_bandwidth;
  2453. /* Peer frame bit target for this frame */
  2454. cpi->per_frame_bandwidth = cpi->twopass.kf_bits;
  2455. /* Convert to a per second bitrate */
  2456. cpi->target_bandwidth = (int)(cpi->twopass.kf_bits * cpi->output_framerate);
  2457. }
  2458. /* Note the total error score of the kf group minus the key frame itself */
  2459. cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
  2460. /* Adjust the count of total modified error left. The count of bits left
  2461. * is adjusted elsewhere based on real coded frame sizes
  2462. */
  2463. cpi->twopass.modified_error_left -= kf_group_err;
  2464. if (cpi->oxcf.allow_spatial_resampling) {
  2465. int resample_trigger = 0;
  2466. int last_kf_resampled = 0;
  2467. int kf_q;
  2468. int scale_val = 0;
  2469. int hr, hs, vr, vs;
  2470. int new_width = cpi->oxcf.Width;
  2471. int new_height = cpi->oxcf.Height;
  2472. int projected_buffer_level;
  2473. int tmp_q;
  2474. double projected_bits_perframe;
  2475. double group_iiratio = (kf_group_intra_err - first_frame.intra_error) /
  2476. (kf_group_coded_err - first_frame.coded_error);
  2477. double err_per_frame = kf_group_err / cpi->twopass.frames_to_key;
  2478. double bits_per_frame;
  2479. double av_bits_per_frame;
  2480. double effective_size_ratio;
  2481. if ((cpi->common.Width != cpi->oxcf.Width) ||
  2482. (cpi->common.Height != cpi->oxcf.Height)) {
  2483. last_kf_resampled = 1;
  2484. }
  2485. /* Set back to unscaled by defaults */
  2486. cpi->common.horiz_scale = NORMAL;
  2487. cpi->common.vert_scale = NORMAL;
  2488. /* Calculate Average bits per frame. */
  2489. av_bits_per_frame = cpi->oxcf.target_bandwidth /
  2490. DOUBLE_DIVIDE_CHECK((double)cpi->framerate);
  2491. /* CBR... Use the clip average as the target for deciding resample */
  2492. if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
  2493. bits_per_frame = av_bits_per_frame;
  2494. }
  2495. /* In VBR we want to avoid downsampling in easy section unless we
  2496. * are under extreme pressure So use the larger of target bitrate
  2497. * for this section or average bitrate for sequence
  2498. */
  2499. else {
  2500. /* This accounts for how hard the section is... */
  2501. bits_per_frame =
  2502. (double)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key);
  2503. /* Dont turn to resampling in easy sections just because they
  2504. * have been assigned a small number of bits
  2505. */
  2506. if (bits_per_frame < av_bits_per_frame) {
  2507. bits_per_frame = av_bits_per_frame;
  2508. }
  2509. }
  2510. /* bits_per_frame should comply with our minimum */
  2511. if (bits_per_frame < (cpi->oxcf.target_bandwidth *
  2512. cpi->oxcf.two_pass_vbrmin_section / 100)) {
  2513. bits_per_frame = (cpi->oxcf.target_bandwidth *
  2514. cpi->oxcf.two_pass_vbrmin_section / 100);
  2515. }
  2516. /* Work out if spatial resampling is necessary */
  2517. kf_q = estimate_kf_group_q(cpi, err_per_frame, (int)bits_per_frame,
  2518. group_iiratio);
  2519. /* If we project a required Q higher than the maximum allowed Q then
  2520. * make a guess at the actual size of frames in this section
  2521. */
  2522. projected_bits_perframe = bits_per_frame;
  2523. tmp_q = kf_q;
  2524. while (tmp_q > cpi->worst_quality) {
  2525. projected_bits_perframe *= 1.04;
  2526. tmp_q--;
  2527. }
  2528. /* Guess at buffer level at the end of the section */
  2529. projected_buffer_level =
  2530. (int)(cpi->buffer_level -
  2531. (int)((projected_bits_perframe - av_bits_per_frame) *
  2532. cpi->twopass.frames_to_key));
  2533. if (0) {
  2534. FILE *f = fopen("Subsamle.stt", "a");
  2535. fprintf(f, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n",
  2536. cpi->common.current_video_frame, kf_q, cpi->common.horiz_scale,
  2537. cpi->common.vert_scale, kf_group_err / cpi->twopass.frames_to_key,
  2538. (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key),
  2539. new_height, new_width);
  2540. fclose(f);
  2541. }
  2542. /* The trigger for spatial resampling depends on the various
  2543. * parameters such as whether we are streaming (CBR) or VBR.
  2544. */
  2545. if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
  2546. /* Trigger resample if we are projected to fall below down
  2547. * sample level or resampled last time and are projected to
  2548. * remain below the up sample level
  2549. */
  2550. if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark *
  2551. cpi->oxcf.optimal_buffer_level / 100)) ||
  2552. (last_kf_resampled &&
  2553. (projected_buffer_level < (cpi->oxcf.resample_up_water_mark *
  2554. cpi->oxcf.optimal_buffer_level / 100)))) {
  2555. resample_trigger = 1;
  2556. } else {
  2557. resample_trigger = 0;
  2558. }
  2559. } else {
  2560. int64_t clip_bits = (int64_t)(
  2561. cpi->twopass.total_stats.count * cpi->oxcf.target_bandwidth /
  2562. DOUBLE_DIVIDE_CHECK((double)cpi->framerate));
  2563. int64_t over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
  2564. /* If triggered last time the threshold for triggering again is
  2565. * reduced:
  2566. *
  2567. * Projected Q higher than allowed and Overspend > 5% of total
  2568. * bits
  2569. */
  2570. if ((last_kf_resampled && (kf_q > cpi->worst_quality)) ||
  2571. ((kf_q > cpi->worst_quality) && (over_spend > clip_bits / 20))) {
  2572. resample_trigger = 1;
  2573. } else {
  2574. resample_trigger = 0;
  2575. }
  2576. }
  2577. if (resample_trigger) {
  2578. while ((kf_q >= cpi->worst_quality) && (scale_val < 6)) {
  2579. scale_val++;
  2580. cpi->common.vert_scale = vscale_lookup[scale_val];
  2581. cpi->common.horiz_scale = hscale_lookup[scale_val];
  2582. Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
  2583. Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
  2584. new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
  2585. new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
  2586. /* Reducing the area to 1/4 does not reduce the complexity
  2587. * (err_per_frame) to 1/4... effective_sizeratio attempts
  2588. * to provide a crude correction for this
  2589. */
  2590. effective_size_ratio = (double)(new_width * new_height) /
  2591. (double)(cpi->oxcf.Width * cpi->oxcf.Height);
  2592. effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
  2593. /* Now try again and see what Q we get with the smaller
  2594. * image size
  2595. */
  2596. kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_ratio,
  2597. (int)bits_per_frame, group_iiratio);
  2598. if (0) {
  2599. FILE *f = fopen("Subsamle.stt", "a");
  2600. fprintf(
  2601. f, "******** %8d %8d %8d %12.0f %8d %8d %8d\n", kf_q,
  2602. cpi->common.horiz_scale, cpi->common.vert_scale,
  2603. kf_group_err / cpi->twopass.frames_to_key,
  2604. (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key),
  2605. new_height, new_width);
  2606. fclose(f);
  2607. }
  2608. }
  2609. }
  2610. if ((cpi->common.Width != new_width) ||
  2611. (cpi->common.Height != new_height)) {
  2612. cpi->common.Width = new_width;
  2613. cpi->common.Height = new_height;
  2614. vp8_alloc_compressor_data(cpi);
  2615. }
  2616. }
  2617. }