bitrate.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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-2007 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: bitrate tracking and management
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include <ogg/ogg.h>
  19. #include "vorbis/codec.h"
  20. #include "codec_internal.h"
  21. #include "os.h"
  22. #include "misc.h"
  23. #include "bitrate.h"
  24. /* compute bitrate tracking setup */
  25. void vorbis_bitrate_init(vorbis_info *vi,bitrate_manager_state *bm){
  26. codec_setup_info *ci=vi->codec_setup;
  27. bitrate_manager_info *bi=&ci->bi;
  28. memset(bm,0,sizeof(*bm));
  29. if(bi && (bi->reservoir_bits>0)){
  30. long ratesamples=vi->rate;
  31. int halfsamples=ci->blocksizes[0]>>1;
  32. bm->short_per_long=ci->blocksizes[1]/ci->blocksizes[0];
  33. bm->managed=1;
  34. bm->avg_bitsper= rint(1.*bi->avg_rate*halfsamples/ratesamples);
  35. bm->min_bitsper= rint(1.*bi->min_rate*halfsamples/ratesamples);
  36. bm->max_bitsper= rint(1.*bi->max_rate*halfsamples/ratesamples);
  37. bm->avgfloat=PACKETBLOBS/2;
  38. /* not a necessary fix, but one that leads to a more balanced
  39. typical initialization */
  40. {
  41. long desired_fill=bi->reservoir_bits*bi->reservoir_bias;
  42. bm->minmax_reservoir=desired_fill;
  43. bm->avg_reservoir=desired_fill;
  44. }
  45. }
  46. }
  47. void vorbis_bitrate_clear(bitrate_manager_state *bm){
  48. memset(bm,0,sizeof(*bm));
  49. return;
  50. }
  51. int vorbis_bitrate_managed(vorbis_block *vb){
  52. vorbis_dsp_state *vd=vb->vd;
  53. private_state *b=vd->backend_state;
  54. bitrate_manager_state *bm=&b->bms;
  55. if(bm && bm->managed)return(1);
  56. return(0);
  57. }
  58. /* finish taking in the block we just processed */
  59. int vorbis_bitrate_addblock(vorbis_block *vb){
  60. vorbis_block_internal *vbi=vb->internal;
  61. vorbis_dsp_state *vd=vb->vd;
  62. private_state *b=vd->backend_state;
  63. bitrate_manager_state *bm=&b->bms;
  64. vorbis_info *vi=vd->vi;
  65. codec_setup_info *ci=vi->codec_setup;
  66. bitrate_manager_info *bi=&ci->bi;
  67. int choice=rint(bm->avgfloat);
  68. long this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
  69. long min_target_bits=(vb->W?bm->min_bitsper*bm->short_per_long:bm->min_bitsper);
  70. long max_target_bits=(vb->W?bm->max_bitsper*bm->short_per_long:bm->max_bitsper);
  71. int samples=ci->blocksizes[vb->W]>>1;
  72. long desired_fill=bi->reservoir_bits*bi->reservoir_bias;
  73. if(!bm->managed){
  74. /* not a bitrate managed stream, but for API simplicity, we'll
  75. buffer the packet to keep the code path clean */
  76. if(bm->vb)return(-1); /* one has been submitted without
  77. being claimed */
  78. bm->vb=vb;
  79. return(0);
  80. }
  81. bm->vb=vb;
  82. /* look ahead for avg floater */
  83. if(bm->avg_bitsper>0){
  84. double slew=0.;
  85. long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper);
  86. double slewlimit= 15./bi->slew_damp;
  87. /* choosing a new floater:
  88. if we're over target, we slew down
  89. if we're under target, we slew up
  90. choose slew as follows: look through packetblobs of this frame
  91. and set slew as the first in the appropriate direction that
  92. gives us the slew we want. This may mean no slew if delta is
  93. already favorable.
  94. Then limit slew to slew max */
  95. if(bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){
  96. while(choice>0 && this_bits>avg_target_bits &&
  97. bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){
  98. choice--;
  99. this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
  100. }
  101. }else if(bm->avg_reservoir+(this_bits-avg_target_bits)<desired_fill){
  102. while(choice+1<PACKETBLOBS && this_bits<avg_target_bits &&
  103. bm->avg_reservoir+(this_bits-avg_target_bits)<desired_fill){
  104. choice++;
  105. this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
  106. }
  107. }
  108. slew=rint(choice-bm->avgfloat)/samples*vi->rate;
  109. if(slew<-slewlimit)slew=-slewlimit;
  110. if(slew>slewlimit)slew=slewlimit;
  111. choice=rint(bm->avgfloat+= slew/vi->rate*samples);
  112. this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
  113. }
  114. /* enforce min(if used) on the current floater (if used) */
  115. if(bm->min_bitsper>0){
  116. /* do we need to force the bitrate up? */
  117. if(this_bits<min_target_bits){
  118. while(bm->minmax_reservoir-(min_target_bits-this_bits)<0){
  119. choice++;
  120. if(choice>=PACKETBLOBS)break;
  121. this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
  122. }
  123. }
  124. }
  125. /* enforce max (if used) on the current floater (if used) */
  126. if(bm->max_bitsper>0){
  127. /* do we need to force the bitrate down? */
  128. if(this_bits>max_target_bits){
  129. while(bm->minmax_reservoir+(this_bits-max_target_bits)>bi->reservoir_bits){
  130. choice--;
  131. if(choice<0)break;
  132. this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
  133. }
  134. }
  135. }
  136. /* Choice of packetblobs now made based on floater, and min/max
  137. requirements. Now boundary check extreme choices */
  138. if(choice<0){
  139. /* choosing a smaller packetblob is insufficient to trim bitrate.
  140. frame will need to be truncated */
  141. long maxsize=(max_target_bits+(bi->reservoir_bits-bm->minmax_reservoir))/8;
  142. bm->choice=choice=0;
  143. if(oggpack_bytes(vbi->packetblob[choice])>maxsize){
  144. oggpack_writetrunc(vbi->packetblob[choice],maxsize*8);
  145. this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
  146. }
  147. }else{
  148. long minsize=(min_target_bits-bm->minmax_reservoir+7)/8;
  149. if(choice>=PACKETBLOBS)
  150. choice=PACKETBLOBS-1;
  151. bm->choice=choice;
  152. /* prop up bitrate according to demand. pad this frame out with zeroes */
  153. minsize-=oggpack_bytes(vbi->packetblob[choice]);
  154. while(minsize-->0)oggpack_write(vbi->packetblob[choice],0,8);
  155. this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
  156. }
  157. /* now we have the final packet and the final packet size. Update statistics */
  158. /* min and max reservoir */
  159. if(bm->min_bitsper>0 || bm->max_bitsper>0){
  160. if(max_target_bits>0 && this_bits>max_target_bits){
  161. bm->minmax_reservoir+=(this_bits-max_target_bits);
  162. }else if(min_target_bits>0 && this_bits<min_target_bits){
  163. bm->minmax_reservoir+=(this_bits-min_target_bits);
  164. }else{
  165. /* inbetween; we want to take reservoir toward but not past desired_fill */
  166. if(bm->minmax_reservoir>desired_fill){
  167. if(max_target_bits>0){ /* logical bulletproofing against initialization state */
  168. bm->minmax_reservoir+=(this_bits-max_target_bits);
  169. if(bm->minmax_reservoir<desired_fill)bm->minmax_reservoir=desired_fill;
  170. }else{
  171. bm->minmax_reservoir=desired_fill;
  172. }
  173. }else{
  174. if(min_target_bits>0){ /* logical bulletproofing against initialization state */
  175. bm->minmax_reservoir+=(this_bits-min_target_bits);
  176. if(bm->minmax_reservoir>desired_fill)bm->minmax_reservoir=desired_fill;
  177. }else{
  178. bm->minmax_reservoir=desired_fill;
  179. }
  180. }
  181. }
  182. }
  183. /* avg reservoir */
  184. if(bm->avg_bitsper>0){
  185. long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper);
  186. bm->avg_reservoir+=this_bits-avg_target_bits;
  187. }
  188. return(0);
  189. }
  190. int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,ogg_packet *op){
  191. private_state *b=vd->backend_state;
  192. bitrate_manager_state *bm=&b->bms;
  193. vorbis_block *vb=bm->vb;
  194. int choice=PACKETBLOBS/2;
  195. if(!vb)return 0;
  196. if(op){
  197. vorbis_block_internal *vbi=vb->internal;
  198. if(vorbis_bitrate_managed(vb))
  199. choice=bm->choice;
  200. op->packet=oggpack_get_buffer(vbi->packetblob[choice]);
  201. op->bytes=oggpack_bytes(vbi->packetblob[choice]);
  202. op->b_o_s=0;
  203. op->e_o_s=vb->eofflag;
  204. op->granulepos=vb->granulepos;
  205. op->packetno=vb->sequence; /* for sake of completeness */
  206. }
  207. bm->vb=0;
  208. return(1);
  209. }