rate.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function:
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "encint.h"
  18. /*A rough lookup table for tan(x), 0<=x<pi/2.
  19. The values are Q12 fixed-point and spaced at 5 degree intervals.
  20. These decisions are somewhat arbitrary, but sufficient for the 2nd order
  21. Bessel follower below.
  22. Values of x larger than 85 degrees are extrapolated from the last inteval,
  23. which is way off, but "good enough".*/
  24. static unsigned short OC_ROUGH_TAN_LOOKUP[18]={
  25. 0, 358, 722, 1098, 1491, 1910,
  26. 2365, 2868, 3437, 4096, 4881, 5850,
  27. 7094, 8784,11254,15286,23230,46817
  28. };
  29. /*_alpha is Q24 in the range [0,0.5).
  30. The return values is 5.12.*/
  31. static int oc_warp_alpha(int _alpha){
  32. int i;
  33. int d;
  34. int t0;
  35. int t1;
  36. i=_alpha*36>>24;
  37. if(i>=17)i=16;
  38. t0=OC_ROUGH_TAN_LOOKUP[i];
  39. t1=OC_ROUGH_TAN_LOOKUP[i+1];
  40. d=_alpha*36-(i<<24);
  41. return (int)(((ogg_int64_t)t0<<32)+(t1-t0<<8)*(ogg_int64_t)d>>32);
  42. }
  43. /*Re-initialize the Bessel filter coefficients with the specified delay.
  44. This does not alter the x/y state, but changes the reaction time of the
  45. filter.
  46. Altering the time constant of a reactive filter without alterning internal
  47. state is something that has to be done carefuly, but our design operates at
  48. high enough delays and with small enough time constant changes to make it
  49. safe.*/
  50. static void oc_iir_filter_reinit(oc_iir_filter *_f,int _delay){
  51. int alpha;
  52. ogg_int64_t one48;
  53. ogg_int64_t warp;
  54. ogg_int64_t k1;
  55. ogg_int64_t k2;
  56. ogg_int64_t d;
  57. ogg_int64_t a;
  58. ogg_int64_t ik2;
  59. ogg_int64_t b1;
  60. ogg_int64_t b2;
  61. /*This borrows some code from an unreleased version of Postfish.
  62. See the recipe at http://unicorn.us.com/alex/2polefilters.html for details
  63. on deriving the filter coefficients.*/
  64. /*alpha is Q24*/
  65. alpha=(1<<24)/_delay;
  66. one48=(ogg_int64_t)1<<48;
  67. /*warp is 7.12*/
  68. warp=OC_MAXI(oc_warp_alpha(alpha),1);
  69. /*k1 is 9.12*/
  70. k1=3*warp;
  71. /*k2 is 16.24.*/
  72. k2=k1*warp;
  73. /*d is 16.15.*/
  74. d=((1<<12)+k1<<12)+k2+256>>9;
  75. /*a is 0.32, since d is larger than both 1.0 and k2.*/
  76. a=(k2<<23)/d;
  77. /*ik2 is 25.24.*/
  78. ik2=one48/k2;
  79. /*b1 is Q56; in practice, the integer ranges between -2 and 2.*/
  80. b1=2*a*(ik2-(1<<24));
  81. /*b2 is Q56; in practice, the integer ranges between -2 and 2.*/
  82. b2=(one48<<8)-(4*a<<24)-b1;
  83. /*All of the filter parameters are Q24.*/
  84. _f->c[0]=(ogg_int32_t)(b1+((ogg_int64_t)1<<31)>>32);
  85. _f->c[1]=(ogg_int32_t)(b2+((ogg_int64_t)1<<31)>>32);
  86. _f->g=(ogg_int32_t)(a+128>>8);
  87. }
  88. /*Initialize a 2nd order low-pass Bessel filter with the corresponding delay
  89. and initial value.
  90. _value is Q24.*/
  91. static void oc_iir_filter_init(oc_iir_filter *_f,int _delay,ogg_int32_t _value){
  92. oc_iir_filter_reinit(_f,_delay);
  93. _f->y[1]=_f->y[0]=_f->x[1]=_f->x[0]=_value;
  94. }
  95. static ogg_int64_t oc_iir_filter_update(oc_iir_filter *_f,ogg_int32_t _x){
  96. ogg_int64_t c0;
  97. ogg_int64_t c1;
  98. ogg_int64_t g;
  99. ogg_int64_t x0;
  100. ogg_int64_t x1;
  101. ogg_int64_t y0;
  102. ogg_int64_t y1;
  103. ogg_int64_t ya;
  104. c0=_f->c[0];
  105. c1=_f->c[1];
  106. g=_f->g;
  107. x0=_f->x[0];
  108. x1=_f->x[1];
  109. y0=_f->y[0];
  110. y1=_f->y[1];
  111. ya=(_x+x0*2+x1)*g+y0*c0+y1*c1+(1<<23)>>24;
  112. _f->x[1]=(ogg_int32_t)x0;
  113. _f->x[0]=_x;
  114. _f->y[1]=(ogg_int32_t)y0;
  115. _f->y[0]=(ogg_int32_t)ya;
  116. return ya;
  117. }
  118. /*Search for the quantizer that matches the target most closely.
  119. We don't assume a linear ordering, but when there are ties we pick the
  120. quantizer closest to the old one.*/
  121. static int oc_enc_find_qi_for_target(oc_enc_ctx *_enc,int _qti,int _qi_old,
  122. int _qi_min,ogg_int64_t _log_qtarget){
  123. ogg_int64_t best_qdiff;
  124. int best_qi;
  125. int qi;
  126. best_qi=_qi_min;
  127. best_qdiff=_enc->log_qavg[_qti][best_qi]-_log_qtarget;
  128. best_qdiff=best_qdiff+OC_SIGNMASK(best_qdiff)^OC_SIGNMASK(best_qdiff);
  129. for(qi=_qi_min+1;qi<64;qi++){
  130. ogg_int64_t qdiff;
  131. qdiff=_enc->log_qavg[_qti][qi]-_log_qtarget;
  132. qdiff=qdiff+OC_SIGNMASK(qdiff)^OC_SIGNMASK(qdiff);
  133. if(qdiff<best_qdiff||
  134. qdiff==best_qdiff&&abs(qi-_qi_old)<abs(best_qi-_qi_old)){
  135. best_qi=qi;
  136. best_qdiff=qdiff;
  137. }
  138. }
  139. return best_qi;
  140. }
  141. void oc_enc_calc_lambda(oc_enc_ctx *_enc,int _qti){
  142. ogg_int64_t lq;
  143. int qi;
  144. int qi1;
  145. int nqis;
  146. /*For now, lambda is fixed depending on the qi value and frame type:
  147. lambda=qscale*(qavg[qti][qi]**2),
  148. where qscale=0.2125.
  149. This was derived by exhaustively searching for the optimal quantizer for
  150. the AC coefficients in each block from a number of test sequences for a
  151. number of fixed lambda values and fitting the peaks of the resulting
  152. histograms (on the log(qavg) scale).
  153. The same model applies to both inter and intra frames.
  154. A more adaptive scheme might perform better.*/
  155. qi=_enc->state.qis[0];
  156. /*If rate control is active, use the lambda for the _target_ quantizer.
  157. This allows us to scale to rates slightly lower than we'd normally be able
  158. to reach, and give the rate control a semblance of "fractional qi"
  159. precision.
  160. TODO: Add API for changing QI, and allow extra precision.*/
  161. if(_enc->state.info.target_bitrate>0)lq=_enc->rc.log_qtarget;
  162. else lq=_enc->log_qavg[_qti][qi];
  163. /*The resulting lambda value is less than 0x500000.*/
  164. _enc->lambda=(int)oc_bexp64(2*lq-0x4780BD468D6B62BLL);
  165. /*Select additional quantizers.
  166. The R-D optimal block AC quantizer statistics suggest that the distribution
  167. is roughly Gaussian-like with a slight positive skew.
  168. K-means clustering on log_qavg to select 3 quantizers produces cluster
  169. centers of {log_qavg-0.6,log_qavg,log_qavg+0.7}.
  170. Experiments confirm these are relatively good choices.
  171. Although we do greedy R-D optimization of the qii flags to avoid switching
  172. too frequently, this becomes ineffective at low rates, either because we
  173. do a poor job of predicting the actual R-D cost, or the greedy
  174. optimization is not sufficient.
  175. Therefore adaptive quantization is disabled above an (experimentally
  176. suggested) threshold of log_qavg=7.00 (e.g., below INTRA qi=12 or
  177. INTER qi=20 with current matrices).
  178. This may need to be revised if the R-D cost estimation or qii flag
  179. optimization strategies change.*/
  180. nqis=1;
  181. if(lq<(OC_Q57(56)>>3)&&!_enc->vp3_compatible&&
  182. _enc->sp_level<OC_SP_LEVEL_FAST_ANALYSIS){
  183. qi1=oc_enc_find_qi_for_target(_enc,_qti,OC_MAXI(qi-1,0),0,
  184. lq+(OC_Q57(7)+5)/10);
  185. if(qi1!=qi)_enc->state.qis[nqis++]=qi1;
  186. qi1=oc_enc_find_qi_for_target(_enc,_qti,OC_MINI(qi+1,63),0,
  187. lq-(OC_Q57(6)+5)/10);
  188. if(qi1!=qi&&qi1!=_enc->state.qis[nqis-1])_enc->state.qis[nqis++]=qi1;
  189. }
  190. _enc->state.nqis=nqis;
  191. }
  192. /*Binary exponential of _log_scale with 24-bit fractional precision and
  193. saturation.
  194. _log_scale: A binary logarithm in Q24 format.
  195. Return: The binary exponential in Q24 format, saturated to 2**47-1 if
  196. _log_scale was too large.*/
  197. static ogg_int64_t oc_bexp_q24(ogg_int32_t _log_scale){
  198. if(_log_scale<(ogg_int32_t)23<<24){
  199. ogg_int64_t ret;
  200. ret=oc_bexp64(((ogg_int64_t)_log_scale<<33)+OC_Q57(24));
  201. return ret<0x7FFFFFFFFFFFLL?ret:0x7FFFFFFFFFFFLL;
  202. }
  203. return 0x7FFFFFFFFFFFLL;
  204. }
  205. /*Convenience function converts Q57 value to a clamped 32-bit Q24 value
  206. _in: input in Q57 format.
  207. Return: same number in Q24 */
  208. static ogg_int32_t oc_q57_to_q24(ogg_int64_t _in){
  209. ogg_int64_t ret;
  210. ret=_in+((ogg_int64_t)1<<32)>>33;
  211. /*0x80000000 is automatically converted to unsigned on 32-bit systems.
  212. -0x7FFFFFFF-1 is needed to avoid "promoting" the whole expression to
  213. unsigned.*/
  214. return (ogg_int32_t)OC_CLAMPI(-0x7FFFFFFF-1,ret,0x7FFFFFFF);
  215. }
  216. /*Binary exponential of _log_scale with 24-bit fractional precision and
  217. saturation.
  218. _log_scale: A binary logarithm in Q57 format.
  219. Return: The binary exponential in Q24 format, saturated to 2**31-1 if
  220. _log_scale was too large.*/
  221. static ogg_int32_t oc_bexp64_q24(ogg_int64_t _log_scale){
  222. if(_log_scale<OC_Q57(8)){
  223. ogg_int64_t ret;
  224. ret=oc_bexp64(_log_scale+OC_Q57(24));
  225. return ret<0x7FFFFFFF?(ogg_int32_t)ret:0x7FFFFFFF;
  226. }
  227. return 0x7FFFFFFF;
  228. }
  229. static void oc_enc_rc_reset(oc_enc_ctx *_enc){
  230. ogg_int64_t npixels;
  231. ogg_int64_t ibpp;
  232. int inter_delay;
  233. /*TODO: These parameters should be exposed in a th_encode_ctl() API.*/
  234. _enc->rc.bits_per_frame=(_enc->state.info.target_bitrate*
  235. (ogg_int64_t)_enc->state.info.fps_denominator)/
  236. _enc->state.info.fps_numerator;
  237. /*Insane framerates or frame sizes mean insane bitrates.
  238. Let's not get carried away.*/
  239. if(_enc->rc.bits_per_frame>0x400000000000LL){
  240. _enc->rc.bits_per_frame=(ogg_int64_t)0x400000000000LL;
  241. }
  242. else if(_enc->rc.bits_per_frame<32)_enc->rc.bits_per_frame=32;
  243. _enc->rc.buf_delay=OC_MAXI(_enc->rc.buf_delay,12);
  244. _enc->rc.max=_enc->rc.bits_per_frame*_enc->rc.buf_delay;
  245. /*Start with a buffer fullness of 50% plus 25% of the amount we plan to spend
  246. on a single keyframe interval.
  247. We can require fully half the bits in an interval for a keyframe, so this
  248. initial level gives us maximum flexibility for over/under-shooting in
  249. subsequent frames.*/
  250. _enc->rc.target=(_enc->rc.max+1>>1)+(_enc->rc.bits_per_frame+2>>2)*
  251. OC_MINI(_enc->keyframe_frequency_force,_enc->rc.buf_delay);
  252. _enc->rc.fullness=_enc->rc.target;
  253. /*Pick exponents and initial scales for quantizer selection.*/
  254. npixels=_enc->state.info.frame_width*
  255. (ogg_int64_t)_enc->state.info.frame_height;
  256. _enc->rc.log_npixels=oc_blog64(npixels);
  257. ibpp=npixels/_enc->rc.bits_per_frame;
  258. if(ibpp<1){
  259. _enc->rc.exp[0]=59;
  260. _enc->rc.log_scale[0]=oc_blog64(1997)-OC_Q57(8);
  261. }
  262. else if(ibpp<2){
  263. _enc->rc.exp[0]=55;
  264. _enc->rc.log_scale[0]=oc_blog64(1604)-OC_Q57(8);
  265. }
  266. else{
  267. _enc->rc.exp[0]=48;
  268. _enc->rc.log_scale[0]=oc_blog64(834)-OC_Q57(8);
  269. }
  270. if(ibpp<4){
  271. _enc->rc.exp[1]=100;
  272. _enc->rc.log_scale[1]=oc_blog64(2249)-OC_Q57(8);
  273. }
  274. else if(ibpp<8){
  275. _enc->rc.exp[1]=95;
  276. _enc->rc.log_scale[1]=oc_blog64(1751)-OC_Q57(8);
  277. }
  278. else{
  279. _enc->rc.exp[1]=73;
  280. _enc->rc.log_scale[1]=oc_blog64(1260)-OC_Q57(8);
  281. }
  282. _enc->rc.prev_drop_count=0;
  283. _enc->rc.log_drop_scale=OC_Q57(0);
  284. /*Set up second order followers, initialized according to corresponding
  285. time constants.*/
  286. oc_iir_filter_init(&_enc->rc.scalefilter[0],4,
  287. oc_q57_to_q24(_enc->rc.log_scale[0]));
  288. inter_delay=(_enc->rc.twopass?
  289. OC_MAXI(_enc->keyframe_frequency_force,12):_enc->rc.buf_delay)>>1;
  290. _enc->rc.inter_count=0;
  291. /*We clamp the actual inter_delay to a minimum of 10 to work within the range
  292. of values where later incrementing the delay works as designed.
  293. 10 is not an exact choice, but rather a good working trade-off.*/
  294. _enc->rc.inter_delay=10;
  295. _enc->rc.inter_delay_target=inter_delay;
  296. oc_iir_filter_init(&_enc->rc.scalefilter[1],_enc->rc.inter_delay,
  297. oc_q57_to_q24(_enc->rc.log_scale[1]));
  298. oc_iir_filter_init(&_enc->rc.vfrfilter,4,
  299. oc_bexp64_q24(_enc->rc.log_drop_scale));
  300. }
  301. void oc_rc_state_init(oc_rc_state *_rc,oc_enc_ctx *_enc){
  302. _rc->twopass=0;
  303. _rc->twopass_buffer_bytes=0;
  304. _rc->twopass_force_kf=0;
  305. _rc->frame_metrics=NULL;
  306. _rc->rate_bias=0;
  307. if(_enc->state.info.target_bitrate>0){
  308. /*The buffer size is set equal to the keyframe interval, clamped to the
  309. range [12,256] frames.
  310. The 12 frame minimum gives us some chance to distribute bit estimation
  311. errors.
  312. The 256 frame maximum means we'll require 8-10 seconds of pre-buffering
  313. at 24-30 fps, which is not unreasonable.*/
  314. _rc->buf_delay=_enc->keyframe_frequency_force>256?
  315. 256:_enc->keyframe_frequency_force;
  316. /*By default, enforce all buffer constraints.*/
  317. _rc->drop_frames=1;
  318. _rc->cap_overflow=1;
  319. _rc->cap_underflow=0;
  320. oc_enc_rc_reset(_enc);
  321. }
  322. }
  323. void oc_rc_state_clear(oc_rc_state *_rc){
  324. _ogg_free(_rc->frame_metrics);
  325. }
  326. void oc_enc_rc_resize(oc_enc_ctx *_enc){
  327. /*If encoding has not yet begun, reset the buffer state.*/
  328. if(_enc->state.curframe_num<0)oc_enc_rc_reset(_enc);
  329. else{
  330. int idt;
  331. /*Otherwise, update the bounds on the buffer, but not the current
  332. fullness.*/
  333. _enc->rc.bits_per_frame=(_enc->state.info.target_bitrate*
  334. (ogg_int64_t)_enc->state.info.fps_denominator)/
  335. _enc->state.info.fps_numerator;
  336. /*Insane framerates or frame sizes mean insane bitrates.
  337. Let's not get carried away.*/
  338. if(_enc->rc.bits_per_frame>0x400000000000LL){
  339. _enc->rc.bits_per_frame=(ogg_int64_t)0x400000000000LL;
  340. }
  341. else if(_enc->rc.bits_per_frame<32)_enc->rc.bits_per_frame=32;
  342. _enc->rc.buf_delay=OC_MAXI(_enc->rc.buf_delay,12);
  343. _enc->rc.max=_enc->rc.bits_per_frame*_enc->rc.buf_delay;
  344. _enc->rc.target=(_enc->rc.max+1>>1)+(_enc->rc.bits_per_frame+2>>2)*
  345. OC_MINI(_enc->keyframe_frequency_force,_enc->rc.buf_delay);
  346. /*Update the INTER-frame scale filter delay.
  347. We jump to it immediately if we've already seen enough frames; otherwise
  348. it is simply set as the new target.*/
  349. _enc->rc.inter_delay_target=idt=OC_MAXI(_enc->rc.buf_delay>>1,10);
  350. if(idt<OC_MINI(_enc->rc.inter_delay,_enc->rc.inter_count)){
  351. oc_iir_filter_init(&_enc->rc.scalefilter[1],idt,
  352. _enc->rc.scalefilter[1].y[0]);
  353. _enc->rc.inter_delay=idt;
  354. }
  355. }
  356. /*If we're in pass-2 mode, make sure the frame metrics array is big enough
  357. to hold frame statistics for the full buffer.*/
  358. if(_enc->rc.twopass==2){
  359. int cfm;
  360. int buf_delay;
  361. int reset_window;
  362. buf_delay=_enc->rc.buf_delay;
  363. reset_window=_enc->rc.frame_metrics==NULL&&(_enc->rc.frames_total[0]==0||
  364. buf_delay<_enc->rc.frames_total[0]+_enc->rc.frames_total[1]
  365. +_enc->rc.frames_total[2]);
  366. cfm=_enc->rc.cframe_metrics;
  367. /*Only try to resize the frame metrics buffer if a) it's too small and
  368. b) we were using a finite buffer, or are about to start.*/
  369. if(cfm<buf_delay&&(_enc->rc.frame_metrics!=NULL||reset_window)){
  370. oc_frame_metrics *fm;
  371. int nfm;
  372. int fmh;
  373. fm=(oc_frame_metrics *)_ogg_realloc(_enc->rc.frame_metrics,
  374. buf_delay*sizeof(*_enc->rc.frame_metrics));
  375. if(fm==NULL){
  376. /*We failed to allocate a finite buffer.*/
  377. /*If we don't have a valid 2-pass header yet, just return; we'll reset
  378. the buffer size when we read the header.*/
  379. if(_enc->rc.frames_total[0]==0)return;
  380. /*Otherwise revert to the largest finite buffer previously set, or to
  381. whole-file buffering if we were still using that.*/
  382. _enc->rc.buf_delay=_enc->rc.frame_metrics!=NULL?
  383. cfm:_enc->rc.frames_total[0]+_enc->rc.frames_total[1]
  384. +_enc->rc.frames_total[2];
  385. oc_enc_rc_resize(_enc);
  386. return;
  387. }
  388. _enc->rc.frame_metrics=fm;
  389. _enc->rc.cframe_metrics=buf_delay;
  390. /*Re-organize the circular buffer.*/
  391. fmh=_enc->rc.frame_metrics_head;
  392. nfm=_enc->rc.nframe_metrics;
  393. if(fmh+nfm>cfm){
  394. int shift;
  395. shift=OC_MINI(fmh+nfm-cfm,buf_delay-cfm);
  396. memcpy(fm+cfm,fm,OC_MINI(fmh+nfm-cfm,buf_delay-cfm)*sizeof(*fm));
  397. if(fmh+nfm>buf_delay)memmove(fm,fm+shift,fmh+nfm-buf_delay);
  398. }
  399. }
  400. /*We were using whole-file buffering; now we're not.*/
  401. if(reset_window){
  402. _enc->rc.nframes[0]=_enc->rc.nframes[1]=_enc->rc.nframes[2]=0;
  403. _enc->rc.scale_sum[0]=_enc->rc.scale_sum[1]=0;
  404. _enc->rc.scale_window_end=_enc->rc.scale_window0=
  405. _enc->state.curframe_num+_enc->prev_dup_count+1;
  406. if(_enc->rc.twopass_buffer_bytes){
  407. int qti;
  408. /*We already read the metrics for the first frame in the window.*/
  409. *(_enc->rc.frame_metrics)=*&_enc->rc.cur_metrics;
  410. _enc->rc.nframe_metrics++;
  411. qti=_enc->rc.cur_metrics.frame_type;
  412. _enc->rc.nframes[qti]++;
  413. _enc->rc.nframes[2]+=_enc->rc.cur_metrics.dup_count;
  414. _enc->rc.scale_sum[qti]+=oc_bexp_q24(_enc->rc.cur_metrics.log_scale);
  415. _enc->rc.scale_window_end+=_enc->rc.cur_metrics.dup_count+1;
  416. if(_enc->rc.scale_window_end-_enc->rc.scale_window0<buf_delay){
  417. /*We need more frame data.*/
  418. _enc->rc.twopass_buffer_bytes=0;
  419. }
  420. }
  421. }
  422. /*Otherwise, we could shrink the size of the current window, if necessary,
  423. but leaving it like it is lets us adapt to the new buffer size more
  424. gracefully.*/
  425. }
  426. }
  427. /*Scale the number of frames by the number of expected drops/duplicates.*/
  428. static int oc_rc_scale_drop(oc_rc_state *_rc,int _nframes){
  429. if(_rc->prev_drop_count>0||_rc->log_drop_scale>OC_Q57(0)){
  430. ogg_int64_t dup_scale;
  431. dup_scale=oc_bexp64((_rc->log_drop_scale
  432. +oc_blog64(_rc->prev_drop_count+1)>>1)+OC_Q57(8));
  433. if(dup_scale<_nframes<<8){
  434. int dup_scalei;
  435. dup_scalei=(int)dup_scale;
  436. if(dup_scalei>0)_nframes=((_nframes<<8)+dup_scalei-1)/dup_scalei;
  437. }
  438. else _nframes=!!_nframes;
  439. }
  440. return _nframes;
  441. }
  442. int oc_enc_select_qi(oc_enc_ctx *_enc,int _qti,int _clamp){
  443. ogg_int64_t rate_total;
  444. ogg_int64_t rate_bias;
  445. int nframes[2];
  446. int buf_delay;
  447. int buf_pad;
  448. ogg_int64_t log_qtarget;
  449. ogg_int64_t log_scale0;
  450. ogg_int64_t log_cur_scale;
  451. ogg_int64_t log_qexp;
  452. int exp0;
  453. int old_qi;
  454. int qi;
  455. /*Figure out how to re-distribute bits so that we hit our fullness target
  456. before the last keyframe in our current buffer window (after the current
  457. frame), or the end of the buffer window, whichever comes first.*/
  458. log_cur_scale=(ogg_int64_t)_enc->rc.scalefilter[_qti].y[0]<<33;
  459. buf_pad=0;
  460. switch(_enc->rc.twopass){
  461. default:{
  462. ogg_uint32_t next_key_frame;
  463. /*Single pass mode: assume only forced keyframes and attempt to estimate
  464. the drop count for VFR content.*/
  465. next_key_frame=_qti?_enc->keyframe_frequency_force
  466. -(_enc->state.curframe_num-_enc->state.keyframe_num):0;
  467. nframes[0]=(_enc->rc.buf_delay-OC_MINI(next_key_frame,_enc->rc.buf_delay)
  468. +_enc->keyframe_frequency_force-1)/_enc->keyframe_frequency_force;
  469. if(nframes[0]+_qti>1){
  470. nframes[0]--;
  471. buf_delay=next_key_frame+nframes[0]*_enc->keyframe_frequency_force;
  472. }
  473. else buf_delay=_enc->rc.buf_delay;
  474. nframes[1]=buf_delay-nframes[0];
  475. /*Downgrade the delta frame rate to correspond to the recent drop count
  476. history.*/
  477. nframes[1]=oc_rc_scale_drop(&_enc->rc,nframes[1]);
  478. }break;
  479. case 1:{
  480. /*Pass 1 mode: use a fixed qi value.*/
  481. qi=_enc->state.qis[0];
  482. _enc->rc.log_qtarget=_enc->log_qavg[_qti][qi];
  483. return qi;
  484. }break;
  485. case 2:{
  486. ogg_int64_t scale_sum[2];
  487. int qti;
  488. /*Pass 2 mode: we know exactly how much of each frame type there is in
  489. the current buffer window, and have estimates for the scales.*/
  490. nframes[0]=_enc->rc.nframes[0];
  491. nframes[1]=_enc->rc.nframes[1];
  492. scale_sum[0]=_enc->rc.scale_sum[0];
  493. scale_sum[1]=_enc->rc.scale_sum[1];
  494. /*The window size can be slightly larger than the buffer window for VFR
  495. content; clamp it down, if appropriate (the excess will all be dup
  496. frames).*/
  497. buf_delay=OC_MINI(_enc->rc.scale_window_end-_enc->rc.scale_window0,
  498. _enc->rc.buf_delay);
  499. /*If we're approaching the end of the file, add some slack to keep us
  500. from slamming into a rail.
  501. Our rate accuracy goes down, but it keeps the result sensible.
  502. We position the target where the first forced keyframe beyond the end
  503. of the file would be (for consistency with 1-pass mode).*/
  504. buf_pad=OC_MINI(_enc->rc.buf_delay,_enc->state.keyframe_num
  505. +_enc->keyframe_frequency_force-_enc->rc.scale_window0);
  506. if(buf_delay<buf_pad)buf_pad-=buf_delay;
  507. else{
  508. /*Otherwise, search for the last keyframe in the buffer window and
  509. target that.*/
  510. buf_pad=0;
  511. /*TODO: Currently we only do this when using a finite buffer; we could
  512. save the position of the last keyframe in the summary data and do it
  513. with a whole-file buffer as well, but it isn't likely to make a
  514. difference.*/
  515. if(_enc->rc.frame_metrics!=NULL){
  516. int fmi;
  517. int fm_tail;
  518. fm_tail=_enc->rc.frame_metrics_head+_enc->rc.nframe_metrics;
  519. if(fm_tail>=_enc->rc.cframe_metrics)fm_tail-=_enc->rc.cframe_metrics;
  520. for(fmi=fm_tail;;){
  521. oc_frame_metrics *m;
  522. fmi--;
  523. if(fmi<0)fmi+=_enc->rc.cframe_metrics;
  524. /*Stop before we remove the first frame.*/
  525. if(fmi==_enc->rc.frame_metrics_head)break;
  526. m=_enc->rc.frame_metrics+fmi;
  527. /*If we find a keyframe, remove it and everything past it.*/
  528. if(m->frame_type==OC_INTRA_FRAME){
  529. do{
  530. qti=m->frame_type;
  531. nframes[qti]--;
  532. scale_sum[qti]-=oc_bexp_q24(m->log_scale);
  533. buf_delay-=m->dup_count+1;
  534. fmi++;
  535. if(fmi>=_enc->rc.cframe_metrics)fmi=0;
  536. m=_enc->rc.frame_metrics+fmi;
  537. }
  538. while(fmi!=fm_tail);
  539. /*And stop scanning backwards.*/
  540. break;
  541. }
  542. }
  543. }
  544. }
  545. /*If we're not using the same frame type as in pass 1 (because someone
  546. changed the keyframe interval), remove that scale estimate.
  547. We'll add in a replacement for the correct frame type below.*/
  548. qti=_enc->rc.cur_metrics.frame_type;
  549. if(qti!=_qti){
  550. nframes[qti]--;
  551. scale_sum[qti]-=oc_bexp_q24(_enc->rc.cur_metrics.log_scale);
  552. }
  553. /*Compute log_scale estimates for each frame type from the pass-1 scales
  554. we measured in the current window.*/
  555. for(qti=0;qti<2;qti++){
  556. _enc->rc.log_scale[qti]=nframes[qti]>0?
  557. oc_blog64(scale_sum[qti])-oc_blog64(nframes[qti])-OC_Q57(24):
  558. -_enc->rc.log_npixels;
  559. }
  560. /*If we're not using the same frame type as in pass 1, add a scale
  561. estimate for the corresponding frame using the current low-pass
  562. filter value.
  563. This is mostly to ensure we have a valid estimate even when pass 1 had
  564. no frames of this type in the buffer window.
  565. TODO: We could also plan ahead and figure out how many keyframes we'll
  566. be forced to add in the current buffer window.*/
  567. qti=_enc->rc.cur_metrics.frame_type;
  568. if(qti!=_qti){
  569. ogg_int64_t scale;
  570. scale=_enc->rc.log_scale[_qti]<OC_Q57(23)?
  571. oc_bexp64(_enc->rc.log_scale[_qti]+OC_Q57(24)):0x7FFFFFFFFFFFLL;
  572. scale*=nframes[_qti];
  573. nframes[_qti]++;
  574. scale+=oc_bexp_q24(log_cur_scale>>33);
  575. _enc->rc.log_scale[_qti]=oc_blog64(scale)
  576. -oc_blog64(nframes[qti])-OC_Q57(24);
  577. }
  578. else log_cur_scale=(ogg_int64_t)_enc->rc.cur_metrics.log_scale<<33;
  579. /*Add the padding from above.
  580. This basically reverts to 1-pass estimations in the last keyframe
  581. interval.*/
  582. if(buf_pad>0){
  583. ogg_int64_t scale;
  584. int nextra_frames;
  585. /*Extend the buffer.*/
  586. buf_delay+=buf_pad;
  587. /*Add virtual delta frames according to the estimated drop count.*/
  588. nextra_frames=oc_rc_scale_drop(&_enc->rc,buf_pad);
  589. /*And blend in the low-pass filtered scale according to how many frames
  590. we added.*/
  591. scale=
  592. oc_bexp64(_enc->rc.log_scale[1]+OC_Q57(24))*(ogg_int64_t)nframes[1]
  593. +oc_bexp_q24(_enc->rc.scalefilter[1].y[0])*(ogg_int64_t)nextra_frames;
  594. nframes[1]+=nextra_frames;
  595. _enc->rc.log_scale[1]=oc_blog64(scale)-oc_blog64(nframes[1])-OC_Q57(24);
  596. }
  597. }break;
  598. }
  599. /*If we've been missing our target, add a penalty term.*/
  600. rate_bias=(_enc->rc.rate_bias/(_enc->state.curframe_num+1000))*
  601. (buf_delay-buf_pad);
  602. /*rate_total is the total bits available over the next buf_delay frames.*/
  603. rate_total=_enc->rc.fullness-_enc->rc.target+rate_bias
  604. +buf_delay*_enc->rc.bits_per_frame;
  605. log_scale0=_enc->rc.log_scale[_qti]+_enc->rc.log_npixels;
  606. /*If there aren't enough bits to achieve our desired fullness level, use the
  607. minimum quality permitted.*/
  608. if(rate_total<=buf_delay)log_qtarget=OC_QUANT_MAX_LOG;
  609. else{
  610. static const ogg_int64_t LOG_KEY_RATIO=0x0137222BB70747BALL;
  611. ogg_int64_t log_scale1;
  612. ogg_int64_t rlo;
  613. ogg_int64_t rhi;
  614. log_scale1=_enc->rc.log_scale[1-_qti]+_enc->rc.log_npixels;
  615. rlo=0;
  616. rhi=(rate_total+nframes[_qti]-1)/nframes[_qti];
  617. while(rlo<rhi){
  618. ogg_int64_t curr;
  619. ogg_int64_t rdiff;
  620. ogg_int64_t log_rpow;
  621. ogg_int64_t rscale;
  622. curr=rlo+rhi>>1;
  623. log_rpow=oc_blog64(curr)-log_scale0;
  624. log_rpow=(log_rpow+(_enc->rc.exp[_qti]>>1))/_enc->rc.exp[_qti];
  625. if(_qti)log_rpow+=LOG_KEY_RATIO>>6;
  626. else log_rpow-=LOG_KEY_RATIO>>6;
  627. log_rpow*=_enc->rc.exp[1-_qti];
  628. rscale=nframes[1-_qti]*oc_bexp64(log_scale1+log_rpow);
  629. rdiff=nframes[_qti]*curr+rscale-rate_total;
  630. if(rdiff<0)rlo=curr+1;
  631. else if(rdiff>0)rhi=curr-1;
  632. else break;
  633. }
  634. log_qtarget=OC_Q57(2)-((oc_blog64(rlo)-log_scale0+(_enc->rc.exp[_qti]>>1))/
  635. _enc->rc.exp[_qti]<<6);
  636. log_qtarget=OC_MINI(log_qtarget,OC_QUANT_MAX_LOG);
  637. }
  638. /*The above allocation looks only at the total rate we'll accumulate in the
  639. next buf_delay frames.
  640. However, we could overflow the buffer on the very next frame, so check for
  641. that here, if we're not using a soft target.*/
  642. exp0=_enc->rc.exp[_qti];
  643. if(_enc->rc.cap_overflow){
  644. ogg_int64_t margin;
  645. ogg_int64_t soft_limit;
  646. ogg_int64_t log_soft_limit;
  647. /*Allow 3% of the buffer for prediction error.
  648. This should be plenty, and we don't mind if we go a bit over; we only
  649. want to keep these bits from being completely wasted.*/
  650. margin=_enc->rc.max+31>>5;
  651. /*We want to use at least this many bits next frame.*/
  652. soft_limit=_enc->rc.fullness+_enc->rc.bits_per_frame-(_enc->rc.max-margin);
  653. log_soft_limit=oc_blog64(soft_limit);
  654. /*If we're predicting we won't use that many...*/
  655. log_qexp=(log_qtarget-OC_Q57(2)>>6)*exp0;
  656. if(log_scale0-log_qexp<log_soft_limit){
  657. /*Scale the adjustment based on how far into the margin we are.*/
  658. log_qexp+=(log_scale0-log_soft_limit-log_qexp>>32)*
  659. ((OC_MINI(margin,soft_limit)<<32)/margin);
  660. log_qtarget=((log_qexp+(exp0>>1))/exp0<<6)+OC_Q57(2);
  661. }
  662. }
  663. /*If this was not one of the initial frames, limit the change in quality.*/
  664. old_qi=_enc->state.qis[0];
  665. if(_clamp){
  666. ogg_int64_t log_qmin;
  667. ogg_int64_t log_qmax;
  668. /*Clamp the target quantizer to within [0.8*Q,1.2*Q], where Q is the
  669. current quantizer.
  670. TODO: With user-specified quant matrices, we need to enlarge these limits
  671. if they don't actually let us change qi values.*/
  672. log_qmin=_enc->log_qavg[_qti][old_qi]-0x00A4D3C25E68DC58LL;
  673. log_qmax=_enc->log_qavg[_qti][old_qi]+0x00A4D3C25E68DC58LL;
  674. log_qtarget=OC_CLAMPI(log_qmin,log_qtarget,log_qmax);
  675. }
  676. /*The above allocation looks only at the total rate we'll accumulate in the
  677. next buf_delay frames.
  678. However, we could bust the budget on the very next frame, so check for that
  679. here, if we're not using a soft target.*/
  680. /* Disabled when our minimum qi > 0; if we saturate log_qtarget to
  681. to the maximum possible size when we have a minimum qi, the
  682. resulting lambda will interact very strangely with SKIP. The
  683. resulting artifacts look like waterfalls. */
  684. if(_enc->state.info.quality==0){
  685. ogg_int64_t log_hard_limit;
  686. /*Compute the maximum number of bits we can use in the next frame.
  687. Allow 50% of the rate for a single frame for prediction error.
  688. This may not be enough for keyframes or sudden changes in complexity.*/
  689. log_hard_limit=oc_blog64(_enc->rc.fullness+(_enc->rc.bits_per_frame>>1));
  690. /*If we're predicting we'll use more than this...*/
  691. log_qexp=(log_qtarget-OC_Q57(2)>>6)*exp0;
  692. if(log_scale0-log_qexp>log_hard_limit){
  693. /*Force the target to hit our limit exactly.*/
  694. log_qexp=log_scale0-log_hard_limit;
  695. log_qtarget=((log_qexp+(exp0>>1))/exp0<<6)+OC_Q57(2);
  696. /*If that target is unreasonable, oh well; we'll have to drop.*/
  697. log_qtarget=OC_MINI(log_qtarget,OC_QUANT_MAX_LOG);
  698. }
  699. }
  700. /*Compute a final estimate of the number of bits we plan to use.*/
  701. log_qexp=(log_qtarget-OC_Q57(2)>>6)*_enc->rc.exp[_qti];
  702. _enc->rc.rate_bias+=oc_bexp64(log_cur_scale+_enc->rc.log_npixels-log_qexp);
  703. qi=oc_enc_find_qi_for_target(_enc,_qti,old_qi,
  704. _enc->state.info.quality,log_qtarget);
  705. /*Save the quantizer target for lambda calculations.*/
  706. _enc->rc.log_qtarget=log_qtarget;
  707. return qi;
  708. }
  709. int oc_enc_update_rc_state(oc_enc_ctx *_enc,
  710. long _bits,int _qti,int _qi,int _trial,int _droppable){
  711. ogg_int64_t buf_delta;
  712. ogg_int64_t log_scale;
  713. int dropped;
  714. dropped=0;
  715. /* Drop frames also disabled for now in the case of infinite-buffer
  716. two-pass mode */
  717. if(!_enc->rc.drop_frames||_enc->rc.twopass&&_enc->rc.frame_metrics==NULL){
  718. _droppable=0;
  719. }
  720. buf_delta=_enc->rc.bits_per_frame*(1+_enc->dup_count);
  721. if(_bits<=0){
  722. /*We didn't code any blocks in this frame.*/
  723. log_scale=OC_Q57(-64);
  724. _bits=0;
  725. }
  726. else{
  727. ogg_int64_t log_bits;
  728. ogg_int64_t log_qexp;
  729. /*Compute the estimated scale factor for this frame type.*/
  730. log_bits=oc_blog64(_bits);
  731. log_qexp=_enc->rc.log_qtarget-OC_Q57(2);
  732. log_qexp=(log_qexp>>6)*(_enc->rc.exp[_qti]);
  733. log_scale=OC_MINI(log_bits-_enc->rc.log_npixels+log_qexp,OC_Q57(16));
  734. }
  735. /*Special two-pass processing.*/
  736. switch(_enc->rc.twopass){
  737. case 1:{
  738. /*Pass 1 mode: save the metrics for this frame.*/
  739. _enc->rc.cur_metrics.log_scale=oc_q57_to_q24(log_scale);
  740. _enc->rc.cur_metrics.dup_count=_enc->dup_count;
  741. _enc->rc.cur_metrics.frame_type=_enc->state.frame_type;
  742. _enc->rc.cur_metrics.activity_avg=_enc->activity_avg;
  743. _enc->rc.twopass_buffer_bytes=0;
  744. }break;
  745. case 2:{
  746. /*Pass 2 mode:*/
  747. if(!_trial){
  748. ogg_int64_t next_frame_num;
  749. int qti;
  750. /*Move the current metrics back one frame.*/
  751. *&_enc->rc.prev_metrics=*&_enc->rc.cur_metrics;
  752. next_frame_num=_enc->state.curframe_num+_enc->dup_count+1;
  753. /*Back out the last frame's statistics from the sliding window.*/
  754. qti=_enc->rc.prev_metrics.frame_type;
  755. _enc->rc.frames_left[qti]--;
  756. _enc->rc.frames_left[2]-=_enc->rc.prev_metrics.dup_count;
  757. _enc->rc.nframes[qti]--;
  758. _enc->rc.nframes[2]-=_enc->rc.prev_metrics.dup_count;
  759. _enc->rc.scale_sum[qti]-=oc_bexp_q24(_enc->rc.prev_metrics.log_scale);
  760. _enc->rc.scale_window0=(int)next_frame_num;
  761. /*Free the corresponding entry in the circular buffer.*/
  762. if(_enc->rc.frame_metrics!=NULL){
  763. _enc->rc.nframe_metrics--;
  764. _enc->rc.frame_metrics_head++;
  765. if(_enc->rc.frame_metrics_head>=_enc->rc.cframe_metrics){
  766. _enc->rc.frame_metrics_head=0;
  767. }
  768. }
  769. /*Mark us ready for the next 2-pass packet.*/
  770. _enc->rc.twopass_buffer_bytes=0;
  771. /*Update state, so the user doesn't have to keep calling 2pass_in after
  772. they've fed in all the data when we're using a finite buffer.*/
  773. _enc->prev_dup_count=_enc->dup_count;
  774. oc_enc_rc_2pass_in(_enc,NULL,0);
  775. }
  776. }break;
  777. }
  778. /*Common to all passes:*/
  779. if(_bits>0){
  780. if(_trial){
  781. oc_iir_filter *f;
  782. /*Use the estimated scale factor directly if this was a trial.*/
  783. f=_enc->rc.scalefilter+_qti;
  784. f->y[1]=f->y[0]=f->x[1]=f->x[0]=oc_q57_to_q24(log_scale);
  785. _enc->rc.log_scale[_qti]=log_scale;
  786. }
  787. else{
  788. /*Lengthen the time constant for the INTER filter as we collect more
  789. frame statistics, until we reach our target.*/
  790. if(_enc->rc.inter_delay<_enc->rc.inter_delay_target&&
  791. _enc->rc.inter_count>=_enc->rc.inter_delay&&_qti==OC_INTER_FRAME){
  792. oc_iir_filter_reinit(&_enc->rc.scalefilter[1],++_enc->rc.inter_delay);
  793. }
  794. /*Otherwise update the low-pass scale filter for this frame type,
  795. regardless of whether or not we dropped this frame.*/
  796. _enc->rc.log_scale[_qti]=oc_iir_filter_update(
  797. _enc->rc.scalefilter+_qti,oc_q57_to_q24(log_scale))<<33;
  798. /*If this frame busts our budget, it must be dropped.*/
  799. if(_droppable&&_enc->rc.fullness+buf_delta<_bits){
  800. _enc->rc.prev_drop_count+=1+_enc->dup_count;
  801. _bits=0;
  802. dropped=1;
  803. }
  804. else{
  805. ogg_uint32_t drop_count;
  806. /*Update a low-pass filter to estimate the "real" frame rate taking
  807. drops and duplicates into account.
  808. This is only done if the frame is coded, as it needs the final
  809. count of dropped frames.*/
  810. drop_count=_enc->rc.prev_drop_count+1;
  811. if(drop_count>0x7F)drop_count=0x7FFFFFFF;
  812. else drop_count<<=24;
  813. _enc->rc.log_drop_scale=oc_blog64(oc_iir_filter_update(
  814. &_enc->rc.vfrfilter,drop_count))-OC_Q57(24);
  815. /*Initialize the drop count for this frame to the user-requested dup
  816. count.
  817. It will be increased if we drop more frames.*/
  818. _enc->rc.prev_drop_count=_enc->dup_count;
  819. }
  820. }
  821. /*Increment the INTER frame count, for filter adaptation purposes.*/
  822. if(_enc->rc.inter_count<INT_MAX)_enc->rc.inter_count+=_qti;
  823. }
  824. /*Increase the drop count.*/
  825. else _enc->rc.prev_drop_count+=1+_enc->dup_count;
  826. /*And update the buffer fullness level.*/
  827. if(!_trial){
  828. _enc->rc.fullness+=buf_delta-_bits;
  829. /*If we're too quick filling the buffer and overflow is capped,
  830. that rate is lost forever.*/
  831. if(_enc->rc.cap_overflow&&_enc->rc.fullness>_enc->rc.max){
  832. _enc->rc.fullness=_enc->rc.max;
  833. }
  834. /*If we're too quick draining the buffer and underflow is capped,
  835. don't try to make up that rate later.*/
  836. if(_enc->rc.cap_underflow&&_enc->rc.fullness<0){
  837. _enc->rc.fullness=0;
  838. }
  839. /*Adjust the bias for the real bits we've used.*/
  840. _enc->rc.rate_bias-=_bits;
  841. }
  842. return dropped;
  843. }
  844. #define OC_RC_2PASS_VERSION (2)
  845. #define OC_RC_2PASS_HDR_SZ (38)
  846. #define OC_RC_2PASS_PACKET_SZ (12)
  847. static void oc_rc_buffer_val(oc_rc_state *_rc,ogg_int64_t _val,int _bytes){
  848. while(_bytes-->0){
  849. _rc->twopass_buffer[_rc->twopass_buffer_bytes++]=(unsigned char)(_val&0xFF);
  850. _val>>=8;
  851. }
  852. }
  853. int oc_enc_rc_2pass_out(oc_enc_ctx *_enc,unsigned char **_buf){
  854. if(_enc->rc.twopass_buffer_bytes==0){
  855. if(_enc->rc.twopass==0){
  856. int qi;
  857. /*Pick first-pass qi for scale calculations.*/
  858. qi=oc_enc_select_qi(_enc,0,0);
  859. _enc->state.nqis=1;
  860. _enc->state.qis[0]=qi;
  861. _enc->rc.twopass=1;
  862. _enc->rc.frames_total[0]=_enc->rc.frames_total[1]=
  863. _enc->rc.frames_total[2]=0;
  864. _enc->rc.scale_sum[0]=_enc->rc.scale_sum[1]=0;
  865. /*Fill in dummy summary values.*/
  866. oc_rc_buffer_val(&_enc->rc,0x5032544F,4);
  867. oc_rc_buffer_val(&_enc->rc,OC_RC_2PASS_VERSION,4);
  868. oc_rc_buffer_val(&_enc->rc,0,OC_RC_2PASS_HDR_SZ-8);
  869. }
  870. else{
  871. int qti;
  872. qti=_enc->rc.cur_metrics.frame_type;
  873. _enc->rc.scale_sum[qti]+=oc_bexp_q24(_enc->rc.cur_metrics.log_scale);
  874. _enc->rc.frames_total[qti]++;
  875. _enc->rc.frames_total[2]+=_enc->rc.cur_metrics.dup_count;
  876. oc_rc_buffer_val(&_enc->rc,
  877. _enc->rc.cur_metrics.dup_count|_enc->rc.cur_metrics.frame_type<<31,4);
  878. oc_rc_buffer_val(&_enc->rc,_enc->rc.cur_metrics.log_scale,4);
  879. oc_rc_buffer_val(&_enc->rc,_enc->rc.cur_metrics.activity_avg,4);
  880. }
  881. }
  882. else if(_enc->packet_state==OC_PACKET_DONE&&
  883. _enc->rc.twopass_buffer_bytes!=OC_RC_2PASS_HDR_SZ){
  884. _enc->rc.twopass_buffer_bytes=0;
  885. oc_rc_buffer_val(&_enc->rc,0x5032544F,4);
  886. oc_rc_buffer_val(&_enc->rc,OC_RC_2PASS_VERSION,4);
  887. oc_rc_buffer_val(&_enc->rc,_enc->rc.frames_total[0],4);
  888. oc_rc_buffer_val(&_enc->rc,_enc->rc.frames_total[1],4);
  889. oc_rc_buffer_val(&_enc->rc,_enc->rc.frames_total[2],4);
  890. oc_rc_buffer_val(&_enc->rc,_enc->rc.exp[0],1);
  891. oc_rc_buffer_val(&_enc->rc,_enc->rc.exp[1],1);
  892. oc_rc_buffer_val(&_enc->rc,_enc->rc.scale_sum[0],8);
  893. oc_rc_buffer_val(&_enc->rc,_enc->rc.scale_sum[1],8);
  894. }
  895. else{
  896. /*The data for this frame has already been retrieved.*/
  897. *_buf=NULL;
  898. return 0;
  899. }
  900. *_buf=_enc->rc.twopass_buffer;
  901. return _enc->rc.twopass_buffer_bytes;
  902. }
  903. static size_t oc_rc_buffer_fill(oc_rc_state *_rc,
  904. unsigned char *_buf,size_t _bytes,size_t _consumed,size_t _goal){
  905. while(_rc->twopass_buffer_fill<_goal&&_consumed<_bytes){
  906. _rc->twopass_buffer[_rc->twopass_buffer_fill++]=_buf[_consumed++];
  907. }
  908. return _consumed;
  909. }
  910. static ogg_int64_t oc_rc_unbuffer_val(oc_rc_state *_rc,int _bytes){
  911. ogg_int64_t ret;
  912. int shift;
  913. ret=0;
  914. shift=0;
  915. while(_bytes-->0){
  916. ret|=((ogg_int64_t)_rc->twopass_buffer[_rc->twopass_buffer_bytes++])<<shift;
  917. shift+=8;
  918. }
  919. return ret;
  920. }
  921. int oc_enc_rc_2pass_in(oc_enc_ctx *_enc,unsigned char *_buf,size_t _bytes){
  922. size_t consumed;
  923. consumed=0;
  924. /*Enable pass 2 mode if this is the first call.*/
  925. if(_enc->rc.twopass==0){
  926. _enc->rc.twopass=2;
  927. _enc->rc.twopass_buffer_fill=0;
  928. _enc->rc.frames_total[0]=0;
  929. _enc->rc.nframe_metrics=0;
  930. _enc->rc.cframe_metrics=0;
  931. _enc->rc.frame_metrics_head=0;
  932. _enc->rc.scale_window0=0;
  933. _enc->rc.scale_window_end=0;
  934. }
  935. /*If we haven't got a valid summary header yet, try to parse one.*/
  936. if(_enc->rc.frames_total[0]==0){
  937. if(!_buf){
  938. int frames_needed;
  939. /*If we're using a whole-file buffer, we just need the first frame.
  940. Otherwise, we may need as many as one per buffer slot.*/
  941. frames_needed=_enc->rc.frame_metrics==NULL?1:_enc->rc.buf_delay;
  942. return OC_RC_2PASS_HDR_SZ+frames_needed*OC_RC_2PASS_PACKET_SZ
  943. -_enc->rc.twopass_buffer_fill;
  944. }
  945. consumed=oc_rc_buffer_fill(&_enc->rc,
  946. _buf,_bytes,consumed,OC_RC_2PASS_HDR_SZ);
  947. if(_enc->rc.twopass_buffer_fill>=OC_RC_2PASS_HDR_SZ){
  948. ogg_int64_t scale_sum[2];
  949. int exp[2];
  950. int buf_delay;
  951. /*Read the summary header data.*/
  952. /*Check the magic value and version number.*/
  953. if(oc_rc_unbuffer_val(&_enc->rc,4)!=0x5032544F||
  954. oc_rc_unbuffer_val(&_enc->rc,4)!=OC_RC_2PASS_VERSION){
  955. _enc->rc.twopass_buffer_bytes=0;
  956. return TH_ENOTFORMAT;
  957. }
  958. _enc->rc.frames_total[0]=(ogg_uint32_t)oc_rc_unbuffer_val(&_enc->rc,4);
  959. _enc->rc.frames_total[1]=(ogg_uint32_t)oc_rc_unbuffer_val(&_enc->rc,4);
  960. _enc->rc.frames_total[2]=(ogg_uint32_t)oc_rc_unbuffer_val(&_enc->rc,4);
  961. exp[0]=(int)oc_rc_unbuffer_val(&_enc->rc,1);
  962. exp[1]=(int)oc_rc_unbuffer_val(&_enc->rc,1);
  963. scale_sum[0]=oc_rc_unbuffer_val(&_enc->rc,8);
  964. scale_sum[1]=oc_rc_unbuffer_val(&_enc->rc,8);
  965. /*Make sure the file claims to have at least one frame.
  966. Otherwise we probably got the placeholder data from an aborted pass 1.
  967. Also make sure the total frame count doesn't overflow an integer.*/
  968. buf_delay=_enc->rc.frames_total[0]+_enc->rc.frames_total[1]
  969. +_enc->rc.frames_total[2];
  970. if(_enc->rc.frames_total[0]==0||buf_delay<0||
  971. (ogg_uint32_t)buf_delay<_enc->rc.frames_total[0]||
  972. (ogg_uint32_t)buf_delay<_enc->rc.frames_total[1]){
  973. _enc->rc.frames_total[0]=0;
  974. _enc->rc.twopass_buffer_bytes=0;
  975. return TH_EBADHEADER;
  976. }
  977. /*Got a valid header; set up pass 2.*/
  978. _enc->rc.frames_left[0]=_enc->rc.frames_total[0];
  979. _enc->rc.frames_left[1]=_enc->rc.frames_total[1];
  980. _enc->rc.frames_left[2]=_enc->rc.frames_total[2];
  981. /*If the user hasn't specified a buffer size, use the whole file.*/
  982. if(_enc->rc.frame_metrics==NULL){
  983. _enc->rc.buf_delay=buf_delay;
  984. _enc->rc.nframes[0]=_enc->rc.frames_total[0];
  985. _enc->rc.nframes[1]=_enc->rc.frames_total[1];
  986. _enc->rc.nframes[2]=_enc->rc.frames_total[2];
  987. _enc->rc.scale_sum[0]=scale_sum[0];
  988. _enc->rc.scale_sum[1]=scale_sum[1];
  989. _enc->rc.scale_window_end=buf_delay;
  990. oc_enc_rc_reset(_enc);
  991. }
  992. _enc->rc.exp[0]=exp[0];
  993. _enc->rc.exp[1]=exp[1];
  994. /*Clear the header data from the buffer to make room for packet data.*/
  995. _enc->rc.twopass_buffer_fill=0;
  996. _enc->rc.twopass_buffer_bytes=0;
  997. }
  998. }
  999. if(_enc->rc.frames_total[0]!=0){
  1000. ogg_int64_t curframe_num;
  1001. int nframes_total;
  1002. curframe_num=_enc->state.curframe_num;
  1003. if(curframe_num>=0){
  1004. /*We just encoded a frame; make sure things matched.*/
  1005. if(_enc->rc.prev_metrics.dup_count!=_enc->prev_dup_count){
  1006. _enc->rc.twopass_buffer_bytes=0;
  1007. return TH_EINVAL;
  1008. }
  1009. }
  1010. curframe_num+=_enc->prev_dup_count+1;
  1011. nframes_total=_enc->rc.frames_total[0]+_enc->rc.frames_total[1]
  1012. +_enc->rc.frames_total[2];
  1013. if(curframe_num>=nframes_total){
  1014. /*We don't want any more data after the last frame, and we don't want to
  1015. allow any more frames to be encoded.*/
  1016. _enc->rc.twopass_buffer_bytes=0;
  1017. }
  1018. else if(_enc->rc.twopass_buffer_bytes==0){
  1019. if(_enc->rc.frame_metrics==NULL){
  1020. /*We're using a whole-file buffer:*/
  1021. if(!_buf)return OC_RC_2PASS_PACKET_SZ-_enc->rc.twopass_buffer_fill;
  1022. consumed=oc_rc_buffer_fill(&_enc->rc,
  1023. _buf,_bytes,consumed,OC_RC_2PASS_PACKET_SZ);
  1024. if(_enc->rc.twopass_buffer_fill>=OC_RC_2PASS_PACKET_SZ){
  1025. ogg_uint32_t dup_count;
  1026. ogg_int32_t log_scale;
  1027. unsigned activity;
  1028. int qti;
  1029. int arg;
  1030. /*Read the metrics for the next frame.*/
  1031. dup_count=oc_rc_unbuffer_val(&_enc->rc,4);
  1032. log_scale=oc_rc_unbuffer_val(&_enc->rc,4);
  1033. activity=oc_rc_unbuffer_val(&_enc->rc,4);
  1034. _enc->rc.cur_metrics.log_scale=log_scale;
  1035. qti=(dup_count&0x80000000)>>31;
  1036. _enc->rc.cur_metrics.dup_count=dup_count&0x7FFFFFFF;
  1037. _enc->rc.cur_metrics.frame_type=qti;
  1038. _enc->rc.twopass_force_kf=qti==OC_INTRA_FRAME;
  1039. _enc->activity_avg=_enc->rc.cur_metrics.activity_avg=activity;
  1040. /*"Helpfully" set the dup count back to what it was in pass 1.*/
  1041. arg=_enc->rc.cur_metrics.dup_count;
  1042. th_encode_ctl(_enc,TH_ENCCTL_SET_DUP_COUNT,&arg,sizeof(arg));
  1043. /*Clear the buffer for the next frame.*/
  1044. _enc->rc.twopass_buffer_fill=0;
  1045. }
  1046. }
  1047. else{
  1048. int frames_needed;
  1049. /*We're using a finite buffer:*/
  1050. frames_needed=OC_MINI(_enc->rc.buf_delay-OC_MINI(_enc->rc.buf_delay,
  1051. _enc->rc.scale_window_end-_enc->rc.scale_window0),
  1052. _enc->rc.frames_left[0]+_enc->rc.frames_left[1]
  1053. -_enc->rc.nframes[0]-_enc->rc.nframes[1]);
  1054. while(frames_needed>0){
  1055. if(!_buf){
  1056. return OC_RC_2PASS_PACKET_SZ*frames_needed
  1057. -_enc->rc.twopass_buffer_fill;
  1058. }
  1059. consumed=oc_rc_buffer_fill(&_enc->rc,
  1060. _buf,_bytes,consumed,OC_RC_2PASS_PACKET_SZ);
  1061. if(_enc->rc.twopass_buffer_fill>=OC_RC_2PASS_PACKET_SZ){
  1062. oc_frame_metrics *m;
  1063. int fmi;
  1064. ogg_uint32_t dup_count;
  1065. ogg_int32_t log_scale;
  1066. int qti;
  1067. unsigned activity;
  1068. /*Read the metrics for the next frame.*/
  1069. dup_count=oc_rc_unbuffer_val(&_enc->rc,4);
  1070. log_scale=oc_rc_unbuffer_val(&_enc->rc,4);
  1071. activity=oc_rc_unbuffer_val(&_enc->rc,4);
  1072. /*Add the to the circular buffer.*/
  1073. fmi=_enc->rc.frame_metrics_head+_enc->rc.nframe_metrics++;
  1074. if(fmi>=_enc->rc.cframe_metrics)fmi-=_enc->rc.cframe_metrics;
  1075. m=_enc->rc.frame_metrics+fmi;
  1076. m->log_scale=log_scale;
  1077. qti=(dup_count&0x80000000)>>31;
  1078. m->dup_count=dup_count&0x7FFFFFFF;
  1079. m->frame_type=qti;
  1080. m->activity_avg=activity;
  1081. /*And accumulate the statistics over the window.*/
  1082. _enc->rc.nframes[qti]++;
  1083. _enc->rc.nframes[2]+=m->dup_count;
  1084. _enc->rc.scale_sum[qti]+=oc_bexp_q24(m->log_scale);
  1085. _enc->rc.scale_window_end+=m->dup_count+1;
  1086. /*Compute an upper bound on the number of remaining packets needed
  1087. for the current window.*/
  1088. frames_needed=OC_MINI(_enc->rc.buf_delay-OC_MINI(_enc->rc.buf_delay,
  1089. _enc->rc.scale_window_end-_enc->rc.scale_window0),
  1090. _enc->rc.frames_left[0]+_enc->rc.frames_left[1]
  1091. -_enc->rc.nframes[0]-_enc->rc.nframes[1]);
  1092. /*Clear the buffer for the next frame.*/
  1093. _enc->rc.twopass_buffer_fill=0;
  1094. _enc->rc.twopass_buffer_bytes=0;
  1095. }
  1096. /*Go back for more data.*/
  1097. else break;
  1098. }
  1099. /*If we've got all the frames we need, fill in the current metrics.
  1100. We're ready to go.*/
  1101. if(frames_needed<=0){
  1102. int arg;
  1103. *&_enc->rc.cur_metrics=
  1104. *(_enc->rc.frame_metrics+_enc->rc.frame_metrics_head);
  1105. _enc->rc.twopass_force_kf=
  1106. _enc->rc.cur_metrics.frame_type==OC_INTRA_FRAME;
  1107. _enc->activity_avg=_enc->rc.cur_metrics.activity_avg;
  1108. /*"Helpfully" set the dup count back to what it was in pass 1.*/
  1109. arg=_enc->rc.cur_metrics.dup_count;
  1110. th_encode_ctl(_enc,TH_ENCCTL_SET_DUP_COUNT,&arg,sizeof(arg));
  1111. /*Mark us ready for the next frame.*/
  1112. _enc->rc.twopass_buffer_bytes=1;
  1113. }
  1114. }
  1115. }
  1116. }
  1117. return (int)consumed;
  1118. }