bitrate.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis 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 OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: bitrate tracking and management
  13. last mod: $Id: bitrate.c,v 1.3.2.2 2001/12/18 23:49:16 xiphmont Exp $
  14. ********************************************************************/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include <ogg/ogg.h>
  20. #include "vorbis/codec.h"
  21. #include "codec_internal.h"
  22. #include "os.h"
  23. #include "bitrate.h"
  24. #define BINBITS(pos,bin) ((bin)>0?bm->queue_binned[(pos)*bins+(bin)-1]:0)
  25. #define LIMITBITS(pos,bin) ((bin)>-bins?\
  26. bm->minmax_binstack[(pos)*bins*2+((bin)+bins)-1]:0)
  27. static long LACING_ADJUST(long bits){
  28. int addto=((bits+7)/8+1)/256+1;
  29. return( ((bits+7)/8+addto)*8 );
  30. }
  31. static double floater_interpolate(bitrate_manager_state *bm,vorbis_info *vi,
  32. double desired_rate){
  33. int bin=bm->avgfloat*BITTRACK_DIVISOR-1.;
  34. double lobitrate;
  35. double hibitrate;
  36. lobitrate=(double)(bin==0?0:bm->avg_binacc[bin-1])/bm->avg_sampleacc*vi->rate;
  37. while(lobitrate>desired_rate && bin>0){
  38. bin--;
  39. lobitrate=(double)(bin==0?0:bm->avg_binacc[bin-1])/bm->avg_sampleacc*vi->rate;
  40. }
  41. hibitrate=(double)(bin>=bm->queue_bins?bm->avg_binacc[bm->queue_bins-1]:
  42. bm->avg_binacc[bin])/bm->avg_sampleacc*vi->rate;
  43. while(hibitrate<desired_rate && bin<bm->queue_bins){
  44. bin++;
  45. if(bin<bm->queue_bins)
  46. hibitrate=(double)bm->avg_binacc[bin]/bm->avg_sampleacc*vi->rate;
  47. }
  48. /* interpolate */
  49. if(bin==bm->queue_bins){
  50. return bin/(double)BITTRACK_DIVISOR;
  51. }else{
  52. double delta=(desired_rate-lobitrate)/(hibitrate-lobitrate);
  53. return (bin+delta)/(double)BITTRACK_DIVISOR;
  54. }
  55. }
  56. /* try out a new limit */
  57. static long limit_sum(bitrate_manager_state *bm,int limit){
  58. int i=bm->minmax_stackptr;
  59. long acc=bm->minmax_acctotal;
  60. long bins=bm->queue_bins;
  61. acc-=LIMITBITS(i,0);
  62. acc+=LIMITBITS(i,limit);
  63. while(i-->0){
  64. if(bm->minmax_limitstack[i]<=limit)break;
  65. acc-=LIMITBITS(i,bm->minmax_limitstack[i]);
  66. acc+=LIMITBITS(i,limit);
  67. }
  68. return(acc);
  69. }
  70. /* compute bitrate tracking setup, allocate circular packet size queue */
  71. void vorbis_bitrate_init(vorbis_info *vi,bitrate_manager_state *bm){
  72. int i;
  73. codec_setup_info *ci=vi->codec_setup;
  74. bitrate_manager_info *bi=&ci->bi;
  75. long maxlatency;
  76. memset(bm,0,sizeof(*bm));
  77. if(bi){
  78. bm->avg_sampledesired=bi->queue_avg_time*vi->rate;
  79. bm->avg_centerdesired=bi->queue_avg_time*vi->rate*bi->queue_avg_center;
  80. bm->minmax_sampledesired=bi->queue_minmax_time*vi->rate;
  81. /* first find the max possible needed queue size */
  82. maxlatency=max(bm->avg_sampledesired-bm->avg_centerdesired,
  83. bm->minmax_sampledesired)+bm->avg_centerdesired;
  84. if(maxlatency>0 &&
  85. (bi->queue_avgmin>0 || bi->queue_avgmax>0 || bi->queue_hardmax>0 ||
  86. bi->queue_hardmin>0)){
  87. long maxpackets=maxlatency/(ci->blocksizes[0]>>1)+3;
  88. long bins=BITTRACK_DIVISOR*ci->passlimit[ci->coupling_passes-1];
  89. bm->queue_size=maxpackets;
  90. bm->queue_bins=bins;
  91. bm->queue_binned=_ogg_malloc(maxpackets*bins*sizeof(*bm->queue_binned));
  92. bm->queue_actual=_ogg_malloc(maxpackets*sizeof(*bm->queue_actual));
  93. if((bi->queue_avgmin>0 || bi->queue_avgmax>0) &&
  94. bi->queue_avg_time>0){
  95. bm->avg_binacc=_ogg_malloc(bins*sizeof(*bm->avg_binacc));
  96. bm->avgfloat=bi->avgfloat_initial;
  97. }else{
  98. bm->avg_tail= -1;
  99. }
  100. if((bi->queue_hardmin>0 || bi->queue_hardmax>0) &&
  101. bi->queue_minmax_time>0){
  102. bm->minmax_binstack=_ogg_malloc((bins+1)*bins*2*
  103. sizeof(bm->minmax_binstack));
  104. bm->minmax_posstack=_ogg_malloc((bins+1)*
  105. sizeof(bm->minmax_posstack));
  106. bm->minmax_limitstack=_ogg_malloc((bins+1)*
  107. sizeof(bm->minmax_limitstack));
  108. }else{
  109. bm->minmax_tail= -1;
  110. }
  111. /* space for the packet queueing */
  112. bm->queue_packet_buffers=calloc(maxpackets,sizeof(*bm->queue_packet_buffers));
  113. bm->queue_packets=calloc(maxpackets,sizeof(*bm->queue_packets));
  114. for(i=0;i<maxpackets;i++)
  115. oggpack_writeinit(bm->queue_packet_buffers+i);
  116. }else{
  117. bm->queue_packet_buffers=calloc(1,sizeof(*bm->queue_packet_buffers));
  118. bm->queue_packets=calloc(1,sizeof(*bm->queue_packets));
  119. oggpack_writeinit(bm->queue_packet_buffers);
  120. }
  121. }
  122. }
  123. void vorbis_bitrate_clear(bitrate_manager_state *bm){
  124. int i;
  125. if(bm){
  126. if(bm->queue_binned)_ogg_free(bm->queue_binned);
  127. if(bm->queue_actual)_ogg_free(bm->queue_actual);
  128. if(bm->avg_binacc)_ogg_free(bm->avg_binacc);
  129. if(bm->minmax_binstack)_ogg_free(bm->minmax_binstack);
  130. if(bm->minmax_posstack)_ogg_free(bm->minmax_posstack);
  131. if(bm->minmax_limitstack)_ogg_free(bm->minmax_limitstack);
  132. if(bm->queue_packet_buffers){
  133. if(bm->queue_size==0){
  134. oggpack_writeclear(bm->queue_packet_buffers);
  135. _ogg_free(bm->queue_packet_buffers);
  136. }else{
  137. for(i=0;i<bm->queue_size;i++)
  138. oggpack_writeclear(bm->queue_packet_buffers+i);
  139. _ogg_free(bm->queue_packet_buffers);
  140. }
  141. }
  142. if(bm->queue_packets)_ogg_free(bm->queue_packets);
  143. memset(bm,0,sizeof(*bm));
  144. }
  145. }
  146. int vorbis_bitrate_managed(vorbis_block *vb){
  147. vorbis_dsp_state *vd=vb->vd;
  148. backend_lookup_state *b=vd->backend_state;
  149. bitrate_manager_state *bm=&b->bms;
  150. if(bm->queue_binned)return(1);
  151. return(0);
  152. }
  153. int vorbis_bitrate_maxmarkers(void){
  154. return 8*BITTRACK_DIVISOR;
  155. }
  156. /* finish taking in the block we just processed */
  157. int vorbis_bitrate_addblock(vorbis_block *vb){
  158. int i;
  159. vorbis_block_internal *vbi=vb->internal;
  160. vorbis_dsp_state *vd=vb->vd;
  161. backend_lookup_state *b=vd->backend_state;
  162. bitrate_manager_state *bm=&b->bms;
  163. vorbis_info *vi=vd->vi;
  164. codec_setup_info *ci=vi->codec_setup;
  165. bitrate_manager_info *bi=&ci->bi;
  166. int eofflag=vb->eofflag;
  167. int head=bm->queue_head;
  168. int next_head=head+1;
  169. int bins=bm->queue_bins;
  170. int minmax_head,new_minmax_head;
  171. ogg_uint32_t *head_ptr;
  172. oggpack_buffer temp;
  173. if(!bm->queue_binned){
  174. oggpack_buffer temp;
  175. /* not a bitrate managed stream, but for API simplicity, we'll
  176. buffer one packet to keep the code path clean */
  177. if(bm->queue_head)return(-1); /* one has been submitted without
  178. being claimed */
  179. bm->queue_head++;
  180. bm->queue_packets[0].packet=oggpack_get_buffer(&vb->opb);
  181. bm->queue_packets[0].bytes=oggpack_bytes(&vb->opb);
  182. bm->queue_packets[0].b_o_s=0;
  183. bm->queue_packets[0].e_o_s=vb->eofflag;
  184. bm->queue_packets[0].granulepos=vb->granulepos;
  185. bm->queue_packets[0].packetno=vb->sequence; /* for sake of completeness */
  186. memcpy(&temp,bm->queue_packet_buffers,sizeof(vb->opb));
  187. memcpy(bm->queue_packet_buffers,&vb->opb,sizeof(vb->opb));
  188. memcpy(&vb->opb,&temp,sizeof(vb->opb));
  189. return(0);
  190. }
  191. /* add encoded packet to head */
  192. if(next_head>=bm->queue_size)next_head=0;
  193. head_ptr=bm->queue_binned+bins*head;
  194. /* is there room to add a block? In proper use of the API, this will
  195. never come up... but guard it anyway */
  196. if(next_head==bm->avg_tail || next_head==bm->minmax_tail)return(-1);
  197. /* add the block to the toplevel queue */
  198. bm->queue_head=next_head;
  199. bm->queue_actual[head]=(vb->W?0x80000000UL:0);
  200. /* buffer packet fields */
  201. bm->queue_packets[head].packet=oggpack_get_buffer(&vb->opb);
  202. bm->queue_packets[head].bytes=oggpack_bytes(&vb->opb);
  203. bm->queue_packets[head].b_o_s=0;
  204. bm->queue_packets[head].e_o_s=vb->eofflag;
  205. bm->queue_packets[head].granulepos=vb->granulepos;
  206. bm->queue_packets[head].packetno=vb->sequence; /* for sake of completeness */
  207. /* swap packet buffers */
  208. memcpy(&temp,bm->queue_packet_buffers+head,sizeof(vb->opb));
  209. memcpy(bm->queue_packet_buffers+head,&vb->opb,sizeof(vb->opb));
  210. memcpy(&vb->opb,&temp,sizeof(vb->opb));
  211. /* save markers */
  212. memcpy(head_ptr,vbi->packet_markers,sizeof(*head_ptr)*bins);
  213. if(bm->avg_binacc)
  214. new_minmax_head=minmax_head=bm->avg_center;
  215. else
  216. new_minmax_head=minmax_head=head;
  217. /* the average tracking queue is updated first; its results (if it's
  218. in use) are taken into account by the min/max limiter (if min/max
  219. is in use) */
  220. if(bm->avg_binacc){
  221. unsigned long desired_center=bm->avg_centerdesired;
  222. if(eofflag)desired_center=0;
  223. /* update the avg head */
  224. for(i=0;i<bins;i++)
  225. bm->avg_binacc[i]+=LACING_ADJUST(head_ptr[i]);
  226. bm->avg_sampleacc+=ci->blocksizes[vb->W]>>1;
  227. bm->avg_centeracc+=ci->blocksizes[vb->W]>>1;
  228. /* update the avg tail if needed */
  229. while(bm->avg_sampleacc>bm->avg_sampledesired){
  230. int samples=
  231. ci->blocksizes[bm->queue_actual[bm->avg_tail]&0x80000000UL?1:0]>>1;
  232. for(i=0;i<bm->queue_bins;i++)
  233. bm->avg_binacc[i]-=LACING_ADJUST(bm->queue_binned[bins*bm->avg_tail+i]);
  234. bm->avg_sampleacc-=samples;
  235. bm->avg_tail++;
  236. if(bm->avg_tail>=bm->queue_size)bm->avg_tail=0;
  237. }
  238. /* update the avg center */
  239. if(bm->avg_centeracc>desired_center){
  240. /* choose the new average floater */
  241. double upper=floater_interpolate(bm,vi,bi->queue_avgmax);
  242. double lower=floater_interpolate(bm,vi,bi->queue_avgmin);
  243. double new=bi->avgfloat_initial,slew;
  244. int bin;
  245. if(upper>0. && upper<new)new=upper;
  246. if(lower<bi->avgfloat_minimum)
  247. lower=bi->avgfloat_minimum;
  248. if(lower>new)new=lower;
  249. slew=new-bm->avgfloat;
  250. if(slew<bi->avgfloat_downhyst || slew>bi->avgfloat_uphyst){
  251. if(slew<bi->avgfloat_downslew_max)
  252. new=bm->avgfloat+bi->avgfloat_downslew_max;
  253. if(slew>bi->avgfloat_upslew_max)
  254. new=bm->avgfloat+bi->avgfloat_upslew_max;
  255. bm->avgfloat=new;
  256. }
  257. /* apply the average floater to new blocks */
  258. bin=bm->avgfloat*BITTRACK_DIVISOR; /* truncate on purpose */
  259. fprintf(stderr,"float:%d ",bin);
  260. while(bm->avg_centeracc>desired_center){
  261. int samples=
  262. samples=ci->blocksizes[bm->queue_actual[bm->avg_center]&
  263. 0x80000000UL?1:0]>>1;
  264. bm->queue_actual[bm->avg_center]|=bin;
  265. bm->avg_centeracc-=samples;
  266. bm->avg_center++;
  267. if(bm->noisetrigger_postpone)bm->noisetrigger_postpone-=samples;
  268. if(bm->avg_center>=bm->queue_size)bm->avg_center=0;
  269. }
  270. new_minmax_head=bm->avg_center;
  271. /* track noise bias triggers and noise bias */
  272. if(bm->avgfloat<bi->avgfloat_noise_lowtrigger)
  273. bm->noisetrigger_request+=1.f;
  274. if(bm->avgfloat>bi->avgfloat_noise_hightrigger)
  275. bm->noisetrigger_request-=1.f;
  276. if(bm->noisetrigger_postpone<=0){
  277. if(bm->noisetrigger_request<0.){
  278. bm->avgnoise-=1.f;
  279. if(bm->noisetrigger_request<(signed long)(bm->avg_sampleacc)/2)
  280. bm->avgnoise-=1.f;
  281. bm->noisetrigger_postpone=bm->avg_sampleacc/2;
  282. }
  283. if(bm->noisetrigger_request>0.){
  284. bm->avgnoise+=1.f;
  285. if(bm->noisetrigger_request>(signed long)(bm->avg_sampleacc)/2)
  286. bm->avgnoise+=1.f;
  287. bm->noisetrigger_postpone=bm->avg_sampleacc/2;
  288. }
  289. /* we generally want the noise bias to drift back to zero */
  290. bm->noisetrigger_request=0.f;
  291. if(bm->avgnoise>0)
  292. bm->noisetrigger_request= -1.;
  293. if(bm->avgnoise<0)
  294. bm->noisetrigger_request= +1.;
  295. if(bm->avgnoise<bi->avgfloat_noise_minval)
  296. bm->avgnoise=bi->avgfloat_noise_minval;
  297. if(bm->avgnoise>bi->avgfloat_noise_maxval)
  298. bm->avgnoise=bi->avgfloat_noise_maxval;
  299. }
  300. fprintf(stderr,"noise:%f req:%ld trigger:%ld\n",bm->avgnoise,
  301. bm->noisetrigger_request,bm->noisetrigger_postpone);
  302. }
  303. }else{
  304. /* if we're not using an average tracker, the 'float' is nailed to
  305. the avgfloat_initial value. It needs to be set for the min/max
  306. to deal properly */
  307. long bin=bi->avgfloat_initial*BITTRACK_DIVISOR; /* truncate on purpose */
  308. bm->queue_actual[head]|=bin;
  309. new_minmax_head=next_head;
  310. }
  311. /* update the min/max queues and enforce limits */
  312. if(bm->minmax_binstack){
  313. unsigned long sampledesired=eofflag?0:bm->minmax_sampledesired;
  314. /* add to stack recent */
  315. while(minmax_head!=new_minmax_head){
  316. unsigned int i;
  317. int samples=ci->blocksizes[bm->queue_actual[minmax_head]&
  318. 0x80000000UL?1:0]>>1;
  319. /* the construction here is not parallel to the floater's
  320. stack.
  321. floater[bin-1] <-> floater supported at bin
  322. ...
  323. floater[0] <-> floater supported at 1
  324. supported at zero is implicit.
  325. the BINBITS macro performs offsetting
  326. bin minmax[bin*2-1] <-> floater supported at bin
  327. ...
  328. 1 minmax[bin] <-> floater supported at 1
  329. 0 minmax[bin-1] <-> no limit/support (limited to/supported at bin 0,
  330. ie, no effect)
  331. -1 minmax[bin-2] <-> floater limited to bin-1
  332. ...
  333. -bin+1 minmax[0] <-> floater limited to 1
  334. limited to zero (val= -bin) is implicit
  335. */
  336. for(i=0;i<(unsigned int)bins;i++){
  337. bm->minmax_binstack[bm->minmax_stackptr*bins*2+bins+i]+=
  338. LACING_ADJUST(
  339. BINBITS(minmax_head,
  340. (bm->queue_actual[minmax_head]&0x7fffffffUL)>i+1?
  341. (bm->queue_actual[minmax_head]&0x7fffffffUL):i+1));
  342. bm->minmax_binstack[bm->minmax_stackptr*bins*2+i]+=
  343. LACING_ADJUST(
  344. BINBITS(minmax_head,
  345. (bm->queue_actual[minmax_head]&0x7fffffffUL)<i+1?
  346. (bm->queue_actual[minmax_head]&0x7fffffffUL):i+1));
  347. }
  348. bm->minmax_posstack[bm->minmax_stackptr]=minmax_head; /* not one
  349. past
  350. like
  351. typical */
  352. bm->minmax_limitstack[bm->minmax_stackptr]=0;
  353. bm->minmax_sampleacc+=samples;
  354. bm->minmax_acctotal+=
  355. LACING_ADJUST(
  356. BINBITS(minmax_head,(bm->queue_actual[minmax_head]&0x7fffffffUL)));
  357. minmax_head++;
  358. if(minmax_head>=bm->queue_size)minmax_head=0;
  359. }
  360. /* check limits, enforce changes */
  361. if(bm->minmax_sampleacc>sampledesired){
  362. double bitrate=(double)bm->minmax_acctotal/bm->minmax_sampleacc*vi->rate;
  363. int limit=0;
  364. fprintf(stderr,"prelimit:%dkbps ",(int)bitrate/1000);
  365. if(bitrate>bi->queue_hardmax || bitrate<bi->queue_hardmin){
  366. int newstack;
  367. int stackctr;
  368. long bitsum=limit_sum(bm,0);
  369. bitrate=(double)bitsum/bm->minmax_sampleacc*vi->rate;
  370. /* we're off rate. Iteratively try out new hard floater
  371. limits until we find one that brings us inside. Here's
  372. where we see the whole point of the limit stacks. */
  373. if(bitrate>bi->queue_hardmax){
  374. for(limit=-1;limit>-bins;limit--){
  375. long bitsum=limit_sum(bm,limit);
  376. bitrate=(double)bitsum/bm->minmax_sampleacc*vi->rate;
  377. if(bitrate<=bi->queue_hardmax)break;
  378. }
  379. }else if(bitrate<bi->queue_hardmin){
  380. for(limit=1;limit<bins;limit++){
  381. long bitsum=limit_sum(bm,limit);
  382. bitrate=(double)bitsum/bm->minmax_sampleacc*vi->rate;
  383. if(bitrate>=bi->queue_hardmin)break;
  384. }
  385. if(bitrate>bi->queue_hardmax)limit--;
  386. }
  387. bitsum=limit_sum(bm,limit);
  388. bitrate=(double)bitsum/bm->minmax_sampleacc*vi->rate;
  389. fprintf(stderr,"postlimit:%dkbps ",(int)bitrate/1000);
  390. /* trace the limit backward, stop when we see a lower limit */
  391. newstack=bm->minmax_stackptr-1;
  392. while(newstack>=0){
  393. if(bm->minmax_limitstack[newstack]<limit)break;
  394. newstack--;
  395. }
  396. /* update bit counter with new limit and replace any stack
  397. limits that have been replaced by our new lower limit */
  398. stackctr=bm->minmax_stackptr;
  399. while(stackctr>newstack){
  400. bm->minmax_acctotal-=
  401. LIMITBITS(stackctr,bm->minmax_limitstack[stackctr]);
  402. bm->minmax_acctotal+=LIMITBITS(stackctr,limit);
  403. if(stackctr<bm->minmax_stackptr)
  404. for(i=0;i<bins*2;i++)
  405. bm->minmax_binstack[stackctr*bins*2+i]+=
  406. bm->minmax_binstack[(stackctr+1)*bins*2+i];
  407. stackctr--;
  408. }
  409. stackctr++;
  410. bm->minmax_posstack[stackctr]=bm->minmax_posstack[bm->minmax_stackptr];
  411. bm->minmax_limitstack[stackctr]=limit;
  412. fprintf(stderr,"limit:%d\n",limit);
  413. /* set up new blank stack entry */
  414. stackctr++;
  415. bm->minmax_stackptr=stackctr;
  416. memset(&bm->minmax_binstack[stackctr*bins*2],
  417. 0,
  418. sizeof(*bm->minmax_binstack)*bins*2);
  419. bm->minmax_limitstack[stackctr]=0;
  420. bm->minmax_posstack[stackctr]=-1;
  421. }
  422. }
  423. /* remove from tail */
  424. while(bm->minmax_sampleacc>sampledesired){
  425. int samples=
  426. ci->blocksizes[bm->queue_actual[bm->minmax_tail]&0x80000000UL?1:0]>>1;
  427. int actual=bm->queue_actual[bm->minmax_tail]&0x7fffffffUL;
  428. for(i=0;i<bins;i++){
  429. bm->minmax_binstack[bins+i]-= /* always comes off the stack bottom */
  430. LACING_ADJUST(BINBITS(bm->minmax_tail,actual>i+1?actual:i+1));
  431. bm->minmax_binstack[i]-=
  432. LACING_ADJUST(BINBITS(bm->minmax_tail,actual<i+1?actual:i+1));
  433. }
  434. /* always perform in this order; max overrules min */
  435. if(bm->minmax_limitstack[0]>actual)
  436. actual=bm->minmax_limitstack[0];
  437. if(bins+bm->minmax_limitstack[0]<actual)
  438. actual=bins+bm->minmax_limitstack[0];
  439. bm->minmax_acctotal-=LACING_ADJUST(BINBITS(bm->minmax_tail,actual));
  440. bm->minmax_sampleacc-=samples;
  441. /* revise queue_actual to reflect the limit */
  442. bm->queue_actual[bm->minmax_tail]=actual;
  443. if(bm->minmax_tail==bm->minmax_posstack[0]){
  444. /* the stack becomes a FIFO; the first data has fallen off */
  445. memmove(bm->minmax_binstack,bm->minmax_binstack+bins*2,
  446. sizeof(*bm->minmax_binstack)*bins*2*bm->minmax_stackptr);
  447. memmove(bm->minmax_posstack,bm->minmax_posstack+1,
  448. sizeof(*bm->minmax_posstack)*bm->minmax_stackptr);
  449. memmove(bm->minmax_limitstack,bm->minmax_limitstack+1,
  450. sizeof(*bm->minmax_limitstack)*bm->minmax_stackptr);
  451. bm->minmax_stackptr--;
  452. }
  453. bm->minmax_tail++;
  454. if(bm->minmax_tail>=bm->queue_size)bm->minmax_tail=0;
  455. }
  456. bm->last_to_flush=bm->minmax_tail;
  457. }else{
  458. bm->last_to_flush=bm->avg_center;
  459. }
  460. if(eofflag)
  461. bm->last_to_flush=bm->queue_head;
  462. return(0);
  463. }
  464. int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,ogg_packet *op){
  465. backend_lookup_state *b=vd->backend_state;
  466. bitrate_manager_state *bm=&b->bms;
  467. if(bm->queue_size==0){
  468. if(bm->queue_head==0)return(0);
  469. memcpy(op,bm->queue_packets,sizeof(*op));
  470. bm->queue_head=0;
  471. }else{
  472. long bins=bm->queue_bins;
  473. long bin;
  474. long bytes;
  475. if(bm->next_to_flush==bm->last_to_flush)return(0);
  476. bin=bm->queue_actual[bm->next_to_flush]&0x7fffffffUL;
  477. bytes=(BINBITS(bm->next_to_flush,bin)+7)/8;
  478. memcpy(op,bm->queue_packets+bm->next_to_flush,sizeof(*op));
  479. if(bytes<op->bytes)op->bytes=bytes;
  480. bm->next_to_flush++;
  481. if(bm->next_to_flush>=bm->queue_size)bm->next_to_flush=0;
  482. }
  483. return(1);
  484. }