bitrate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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-2002 *
  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.21 2002/10/11 11:14:41 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 "misc.h"
  24. #include "bitrate.h"
  25. static long BINBYTES(bitrate_manager_state *bm,long pos,long bin){
  26. int bins=bm->queue_bins;
  27. return(bm->queue_binned[pos*bins+bin]);
  28. }
  29. #define LIMITBYTES(pos,bin) (bm->minmax_binstack[(pos)*bins*2+((bin)+bins)])
  30. static long LACING_ADJUST(long bytes){
  31. int addto=bytes/255+1;
  32. return(bytes+addto);
  33. }
  34. static int floater_interpolate(bitrate_manager_state *bm,vorbis_info *vi,
  35. double desired_rate){
  36. int bin=rint(bm->avgfloat);
  37. double lobitrate,hibitrate;
  38. lobitrate=(double)(bm->avg_binacc[bin]*8)/bm->avg_sampleacc*vi->rate;
  39. while(lobitrate>desired_rate && bin>0){
  40. bin--;
  41. lobitrate=(double)(bm->avg_binacc[bin]*8)/bm->avg_sampleacc*vi->rate;
  42. }
  43. if(bin+1<bm->queue_bins){
  44. hibitrate=(double)(bm->avg_binacc[bin+1]*8)/bm->avg_sampleacc*vi->rate;
  45. if(fabs(hibitrate-desired_rate) < fabs(lobitrate-desired_rate))bin++;
  46. }
  47. return(bin);
  48. }
  49. /* try out a new limit */
  50. static long limit_sum(bitrate_manager_state *bm,int limit){
  51. int i=bm->minmax_stackptr;
  52. long acc=bm->minmax_acctotal;
  53. long bins=bm->queue_bins;
  54. acc-=LIMITBYTES(i,0);
  55. acc+=LIMITBYTES(i,limit);
  56. while(i-->0){
  57. if(bm->minmax_limitstack[i]<=limit)break;
  58. acc-=LIMITBYTES(i,bm->minmax_limitstack[i]);
  59. acc+=LIMITBYTES(i,limit);
  60. }
  61. return(acc);
  62. }
  63. /* compute bitrate tracking setup, allocate circular packet size queue */
  64. void vorbis_bitrate_init(vorbis_info *vi,bitrate_manager_state *bm){
  65. int i;
  66. codec_setup_info *ci=vi->codec_setup;
  67. bitrate_manager_info *bi=&ci->bi;
  68. long maxlatency;
  69. memset(bm,0,sizeof(*bm));
  70. if(bi){
  71. bm->avg_sampledesired=bi->queue_avg_time*vi->rate;
  72. bm->avg_centerdesired=bi->queue_avg_time*vi->rate*bi->queue_avg_center;
  73. bm->minmax_sampledesired=bi->queue_minmax_time*vi->rate;
  74. /* first find the max possible needed queue size */
  75. maxlatency=max(bm->avg_sampledesired-bm->avg_centerdesired,
  76. bm->minmax_sampledesired)+bm->avg_centerdesired;
  77. if(maxlatency>0 &&
  78. (bi->queue_avgmin>0 || bi->queue_avgmax>0 || bi->queue_hardmax>0 ||
  79. bi->queue_hardmin>0)){
  80. long maxpackets=maxlatency/(ci->blocksizes[0]>>1)+3;
  81. long bins=PACKETBLOBS;
  82. bm->queue_size=maxpackets;
  83. bm->queue_bins=bins;
  84. bm->queue_binned=_ogg_calloc(maxpackets,bins*sizeof(*bm->queue_binned));
  85. bm->queue_actual=_ogg_calloc(maxpackets,sizeof(*bm->queue_actual));
  86. if((bi->queue_avgmin>0 || bi->queue_avgmax>0) &&
  87. bi->queue_avg_time>0){
  88. bm->avg_binacc=_ogg_calloc(bins,sizeof(*bm->avg_binacc));
  89. bm->avgfloat=PACKETBLOBS/2;
  90. }else{
  91. bm->avg_tail= -1;
  92. }
  93. if((bi->queue_hardmin>0 || bi->queue_hardmax>0) &&
  94. bi->queue_minmax_time>0){
  95. bm->minmax_binstack=_ogg_calloc((bins*2+1)*bins*2,
  96. sizeof(*bm->minmax_binstack));
  97. bm->minmax_posstack=_ogg_calloc((bins*2+1),
  98. sizeof(*bm->minmax_posstack));
  99. bm->minmax_limitstack=_ogg_calloc((bins*2+1),
  100. sizeof(*bm->minmax_limitstack));
  101. }else{
  102. bm->minmax_tail= -1;
  103. }
  104. /* space for the packet queueing */
  105. bm->packetbuffers=_ogg_calloc(maxpackets,sizeof(*bm->packetbuffers));
  106. bm->packets=_ogg_calloc(maxpackets,sizeof(*bm->packets));
  107. for(i=0;i<maxpackets;i++)
  108. oggpack_writeinit(bm->packetbuffers+i);
  109. }else{
  110. bm->packetbuffers=_ogg_calloc(1,sizeof(*bm->packetbuffers));
  111. bm->packets=_ogg_calloc(1,sizeof(*bm->packets));
  112. oggpack_writeinit(bm->packetbuffers);
  113. }
  114. }
  115. }
  116. void vorbis_bitrate_clear(bitrate_manager_state *bm){
  117. int i;
  118. if(bm){
  119. if(bm->queue_binned)_ogg_free(bm->queue_binned);
  120. if(bm->queue_actual)_ogg_free(bm->queue_actual);
  121. if(bm->avg_binacc)_ogg_free(bm->avg_binacc);
  122. if(bm->minmax_binstack)_ogg_free(bm->minmax_binstack);
  123. if(bm->minmax_posstack)_ogg_free(bm->minmax_posstack);
  124. if(bm->minmax_limitstack)_ogg_free(bm->minmax_limitstack);
  125. if(bm->packetbuffers){
  126. if(bm->queue_size==0){
  127. oggpack_writeclear(bm->packetbuffers);
  128. }else{
  129. for(i=0;i<bm->queue_size;i++)
  130. oggpack_writeclear(bm->packetbuffers+i);
  131. }
  132. _ogg_free(bm->packetbuffers);
  133. }
  134. if(bm->packets)_ogg_free(bm->packets);
  135. memset(bm,0,sizeof(*bm));
  136. }
  137. }
  138. int vorbis_bitrate_managed(vorbis_block *vb){
  139. vorbis_dsp_state *vd=vb->vd;
  140. private_state *b=vd->backend_state;
  141. bitrate_manager_state *bm=&b->bms;
  142. if(bm->queue_binned)return(1);
  143. return(0);
  144. }
  145. /* finish taking in the block we just processed */
  146. int vorbis_bitrate_addblock(vorbis_block *vb){
  147. int i;
  148. vorbis_block_internal *vbi=vb->internal;
  149. vorbis_dsp_state *vd=vb->vd;
  150. private_state *b=vd->backend_state;
  151. bitrate_manager_state *bm=&b->bms;
  152. vorbis_info *vi=vd->vi;
  153. codec_setup_info *ci=vi->codec_setup;
  154. bitrate_manager_info *bi=&ci->bi;
  155. int eofflag=vb->eofflag;
  156. int head=bm->queue_head;
  157. int next_head=head+1;
  158. int bins=bm->queue_bins;
  159. int minmax_head,new_minmax_head;
  160. ogg_uint32_t *head_ptr;
  161. oggpack_buffer temp;
  162. if(!bm->queue_binned){
  163. oggpack_buffer temp;
  164. /* not a bitrate managed stream, but for API simplicity, we'll
  165. buffer one packet to keep the code path clean */
  166. if(bm->queue_head)return(-1); /* one has been submitted without
  167. being claimed */
  168. bm->queue_head++;
  169. bm->packets[0].packet=oggpack_get_buffer(&vb->opb);
  170. bm->packets[0].bytes=oggpack_bytes(&vb->opb);
  171. bm->packets[0].b_o_s=0;
  172. bm->packets[0].e_o_s=vb->eofflag;
  173. bm->packets[0].granulepos=vb->granulepos;
  174. bm->packets[0].packetno=vb->sequence; /* for sake of completeness */
  175. memcpy(&temp,bm->packetbuffers,sizeof(vb->opb));
  176. memcpy(bm->packetbuffers,&vb->opb,sizeof(vb->opb));
  177. memcpy(&vb->opb,&temp,sizeof(vb->opb));
  178. return(0);
  179. }
  180. /* add encoded packet to head */
  181. if(next_head>=bm->queue_size)next_head=0;
  182. head_ptr=bm->queue_binned+bins*head;
  183. /* is there room to add a block? In proper use of the API, this will
  184. never come up... but guard it anyway */
  185. if(next_head==bm->avg_tail || next_head==bm->minmax_tail)return(-1);
  186. /* add the block to the toplevel queue */
  187. bm->queue_head=next_head;
  188. bm->queue_actual[head]=(vb->W?0x80000000UL:0);
  189. /* buffer packet fields */
  190. bm->packets[head].packet=oggpack_get_buffer(&vb->opb);
  191. bm->packets[head].bytes=oggpack_bytes(&vb->opb);
  192. bm->packets[head].b_o_s=0;
  193. bm->packets[head].e_o_s=vb->eofflag;
  194. bm->packets[head].granulepos=vb->granulepos;
  195. bm->packets[head].packetno=vb->sequence; /* for sake of completeness */
  196. /* swap packet buffers */
  197. memcpy(&temp,bm->packetbuffers+head,sizeof(vb->opb));
  198. memcpy(bm->packetbuffers+head,&vb->opb,sizeof(vb->opb));
  199. memcpy(&vb->opb,&temp,sizeof(vb->opb));
  200. /* save markers */
  201. head_ptr[0]=vbi->packetblob_markers[0];
  202. for(i=1;i<PACKETBLOBS;i++){
  203. head_ptr[i]=vbi->packetblob_markers[i]-vbi->packetblob_markers[i-1];
  204. }
  205. if(bm->avg_binacc)
  206. new_minmax_head=minmax_head=bm->avg_center;
  207. else
  208. new_minmax_head=minmax_head=head;
  209. /* the average tracking queue is updated first; its results (if it's
  210. in use) are taken into account by the min/max limiter (if min/max
  211. is in use) */
  212. if(bm->avg_binacc){
  213. unsigned long desired_center=bm->avg_centerdesired;
  214. if(eofflag)desired_center=0;
  215. /* update the avg head */
  216. for(i=0;i<bins;i++)
  217. bm->avg_binacc[i]+=LACING_ADJUST(head_ptr[i]);
  218. bm->avg_sampleacc+=ci->blocksizes[vb->W]>>1;
  219. bm->avg_centeracc+=ci->blocksizes[vb->W]>>1;
  220. if(bm->avg_sampleacc>bm->avg_sampledesired || eofflag){
  221. /* update the avg center */
  222. if(bm->avg_centeracc>desired_center){
  223. /* choose the new average floater */
  224. int samples=ci->blocksizes[vb->W]>>1;
  225. double upper=floater_interpolate(bm,vi,bi->queue_avgmax);
  226. double lower=floater_interpolate(bm,vi,bi->queue_avgmin);
  227. double new=PACKETBLOBS/2.,slew;
  228. int bin;
  229. if(upper<new)new=upper;
  230. if(lower>new)new=lower;
  231. slew=(new-bm->avgfloat)/samples*vi->rate;
  232. if(slew<bi->avgfloat_downslew_max)
  233. new=bm->avgfloat+bi->avgfloat_downslew_max/vi->rate*samples;
  234. if(slew>bi->avgfloat_upslew_max)
  235. new=bm->avgfloat+bi->avgfloat_upslew_max/vi->rate*samples;
  236. bm->avgfloat=new;
  237. /* apply the average floater to new blocks */
  238. bin=rint(bm->avgfloat);
  239. /*fprintf(stderr,"%d ",bin);*/
  240. while(bm->avg_centeracc>desired_center){
  241. samples=ci->blocksizes[bm->queue_actual[bm->avg_center]&
  242. 0x80000000UL?1:0]>>1;
  243. bm->queue_actual[bm->avg_center]|=bin;
  244. bm->avg_centeracc-=samples;
  245. bm->avg_center++;
  246. if(bm->avg_center>=bm->queue_size)bm->avg_center=0;
  247. }
  248. new_minmax_head=bm->avg_center;
  249. }
  250. /* update the avg tail if needed */
  251. while(bm->avg_sampleacc>bm->avg_sampledesired){
  252. int samples=
  253. ci->blocksizes[bm->queue_actual[bm->avg_tail]&0x80000000UL?1:0]>>1;
  254. for(i=0;i<bm->queue_bins;i++)
  255. bm->avg_binacc[i]-=LACING_ADJUST(bm->queue_binned[bins*bm->avg_tail+i]);
  256. bm->avg_sampleacc-=samples;
  257. bm->avg_tail++;
  258. if(bm->avg_tail>=bm->queue_size)bm->avg_tail=0;
  259. }
  260. }
  261. }else{
  262. /* if we're not using an average tracker, the 'float' is nailed to
  263. the avgfloat_initial value. It needs to be set for the min/max
  264. to deal properly */
  265. long bin=PACKETBLOBS/2;
  266. bm->queue_actual[head]|=bin;
  267. new_minmax_head=next_head;
  268. }
  269. /* update the min/max queues and enforce limits */
  270. if(bm->minmax_binstack){
  271. unsigned long sampledesired=eofflag?0:bm->minmax_sampledesired;
  272. /* add to stack recent */
  273. while(minmax_head!=new_minmax_head){
  274. unsigned int i;
  275. int samples=ci->blocksizes[bm->queue_actual[minmax_head]&
  276. 0x80000000UL?1:0]>>1;
  277. int actual=bm->queue_actual[minmax_head]&0x7fffffffUL;
  278. for(i=0;i<(unsigned int)bins;i++){
  279. bm->minmax_binstack[bm->minmax_stackptr*bins*2+bins+i]+=
  280. LACING_ADJUST(BINBYTES(bm,minmax_head,
  281. actual>i?actual:i));
  282. bm->minmax_binstack[bm->minmax_stackptr*bins*2+i]+=
  283. LACING_ADJUST(BINBYTES(bm,minmax_head,
  284. actual<i?actual:i));
  285. }
  286. bm->minmax_posstack[bm->minmax_stackptr]=minmax_head; /* not one
  287. past
  288. like
  289. typical */
  290. bm->minmax_limitstack[bm->minmax_stackptr]=0;
  291. bm->minmax_sampleacc+=samples;
  292. bm->minmax_acctotal+=
  293. LACING_ADJUST(BINBYTES(bm,minmax_head,actual));
  294. minmax_head++;
  295. if(minmax_head>=bm->queue_size)minmax_head=0;
  296. }
  297. /* check limits, enforce changes */
  298. if(bm->minmax_sampleacc>sampledesired){
  299. double bitrate=(double)(bm->minmax_acctotal*8)/
  300. bm->minmax_sampleacc*vi->rate;
  301. int limit=0;
  302. if((bi->queue_hardmax>0 && bitrate>bi->queue_hardmax) ||
  303. (bi->queue_hardmin>0 && bitrate<bi->queue_hardmin)){
  304. int newstack;
  305. int stackctr;
  306. long bitsum=bm->minmax_acctotal*8;
  307. bitrate=(double)bitsum/bm->minmax_sampleacc*vi->rate;
  308. /* we're off rate. Iteratively try out new hard floater
  309. limits until we find one that brings us inside. Here's
  310. where we see the whole point of the limit stacks. */
  311. if(bi->queue_hardmax>0 && bitrate>bi->queue_hardmax){
  312. for(limit=-1;limit>-bins+1;limit--){
  313. long bitsum=limit_sum(bm,limit)*8;
  314. bitrate=(double)bitsum/bm->minmax_sampleacc*vi->rate;
  315. if(bitrate<=bi->queue_hardmax)break;
  316. }
  317. }else if(bitrate<bi->queue_hardmin){
  318. for(limit=1;limit<bins-1;limit++){
  319. long bitsum=limit_sum(bm,limit)*8;
  320. bitrate=(double)bitsum/bm->minmax_sampleacc*vi->rate;
  321. if(bitrate>=bi->queue_hardmin)break;
  322. }
  323. if(bitrate>bi->queue_hardmax)limit--;
  324. }
  325. /* trace the limit backward, stop when we see a lower limit */
  326. newstack=bm->minmax_stackptr-1;
  327. while(newstack>=0){
  328. if(bm->minmax_limitstack[newstack]<limit)break;
  329. newstack--;
  330. }
  331. /* update bit counter with new limit and replace any stack
  332. limits that have been replaced by our new lower limit */
  333. stackctr=bm->minmax_stackptr;
  334. while(stackctr>newstack){
  335. bm->minmax_acctotal-=
  336. LIMITBYTES(stackctr,bm->minmax_limitstack[stackctr]);
  337. bm->minmax_acctotal+=LIMITBYTES(stackctr,limit);
  338. if(stackctr<bm->minmax_stackptr)
  339. for(i=0;i<bins*2;i++)
  340. bm->minmax_binstack[stackctr*bins*2+i]+=
  341. bm->minmax_binstack[(stackctr+1)*bins*2+i];
  342. stackctr--;
  343. }
  344. stackctr++;
  345. bm->minmax_posstack[stackctr]=bm->minmax_posstack[bm->minmax_stackptr];
  346. bm->minmax_limitstack[stackctr]=limit;
  347. /* set up new blank stack entry */
  348. stackctr++;
  349. bm->minmax_stackptr=stackctr;
  350. memset(&bm->minmax_binstack[stackctr*bins*2],
  351. 0,
  352. sizeof(*bm->minmax_binstack)*bins*2);
  353. bm->minmax_limitstack[stackctr]=0;
  354. bm->minmax_posstack[stackctr]=-1;
  355. }
  356. }
  357. /* remove from tail */
  358. while(bm->minmax_sampleacc>sampledesired){
  359. int samples=
  360. ci->blocksizes[bm->queue_actual[bm->minmax_tail]&0x80000000UL?1:0]>>1;
  361. int actual=bm->queue_actual[bm->minmax_tail]&0x7fffffffUL;
  362. for(i=0;i<bins;i++){
  363. bm->minmax_binstack[bins+i]-= /* always comes off the stack bottom */
  364. LACING_ADJUST(BINBYTES(bm,bm->minmax_tail,
  365. actual>i?
  366. actual:i));
  367. bm->minmax_binstack[i]-=
  368. LACING_ADJUST(BINBYTES(bm,bm->minmax_tail,
  369. actual<i?
  370. actual:i));
  371. }
  372. if(bm->minmax_limitstack[0]>actual)
  373. actual=bm->minmax_limitstack[0];
  374. if(bins+bm->minmax_limitstack[0]<actual)
  375. actual=bins+bm->minmax_limitstack[0];
  376. bm->minmax_acctotal-=LACING_ADJUST(BINBYTES(bm,bm->minmax_tail,actual));
  377. bm->minmax_sampleacc-=samples;
  378. /* revise queue_actual to reflect the limit */
  379. bm->queue_actual[bm->minmax_tail]&=0x80000000UL;
  380. bm->queue_actual[bm->minmax_tail]|=actual;
  381. if(bm->minmax_tail==bm->minmax_posstack[0]){
  382. /* the stack becomes a FIFO; the first data has fallen off */
  383. memmove(bm->minmax_binstack,bm->minmax_binstack+bins*2,
  384. sizeof(*bm->minmax_binstack)*bins*2*bm->minmax_stackptr);
  385. memmove(bm->minmax_posstack,bm->minmax_posstack+1,
  386. sizeof(*bm->minmax_posstack)*bm->minmax_stackptr);
  387. memmove(bm->minmax_limitstack,bm->minmax_limitstack+1,
  388. sizeof(*bm->minmax_limitstack)*bm->minmax_stackptr);
  389. bm->minmax_stackptr--;
  390. }
  391. bm->minmax_tail++;
  392. if(bm->minmax_tail>=bm->queue_size)bm->minmax_tail=0;
  393. }
  394. bm->last_to_flush=bm->minmax_tail;
  395. }else{
  396. bm->last_to_flush=bm->avg_center;
  397. }
  398. if(eofflag)
  399. bm->last_to_flush=bm->queue_head;
  400. return(0);
  401. }
  402. int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,ogg_packet *op){
  403. private_state *b=vd->backend_state;
  404. bitrate_manager_state *bm=&b->bms;
  405. if(bm->queue_size==0){
  406. if(bm->queue_head==0)return(0);
  407. memcpy(op,bm->packets,sizeof(*op));
  408. bm->queue_head=0;
  409. }else{
  410. if(bm->next_to_flush==bm->last_to_flush)return(0);
  411. {
  412. long bin=bm->queue_actual[bm->next_to_flush]&0x7fffffff,i;
  413. long bins=bm->queue_bins;
  414. ogg_uint32_t *markers=bm->queue_binned+bins*bm->next_to_flush;
  415. long bytes=markers[bin];
  416. memcpy(op,bm->packets+bm->next_to_flush,sizeof(*op));
  417. /* we have [PACKETBLOBS] possible packets all squished together in
  418. the buffer, in sequence. count in to number [bin] */
  419. for(i=0;i<bin;i++)
  420. op->packet+=markers[i];
  421. op->bytes=bytes;
  422. }
  423. bm->next_to_flush++;
  424. if(bm->next_to_flush>=bm->queue_size)bm->next_to_flush=0;
  425. }
  426. return(1);
  427. }