2
0

dsp.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
  4. * *
  5. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  6. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  7. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  8. * *
  9. * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003 *
  10. * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: PCM data vector blocking, windowing and dis/reassembly
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include "ogg.h"
  17. #include "mdct.h"
  18. #include "ivorbiscodec.h"
  19. #include "codec_internal.h"
  20. #include "misc.h"
  21. #include "window_lookup.h"
  22. int vorbis_dsp_restart(vorbis_dsp_state *v){
  23. if(!v)return -1;
  24. {
  25. vorbis_info *vi=v->vi;
  26. codec_setup_info *ci;
  27. if(!vi)return -1;
  28. ci=vi->codec_setup;
  29. if(!ci)return -1;
  30. v->out_end=-1;
  31. v->out_begin=-1;
  32. v->granulepos=-1;
  33. v->sequence=-1;
  34. v->sample_count=-1;
  35. }
  36. return 0;
  37. }
  38. vorbis_dsp_state *vorbis_dsp_create(vorbis_info *vi){
  39. int i;
  40. vorbis_dsp_state *v=_ogg_calloc(1,sizeof(*v));
  41. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  42. v->vi=vi;
  43. v->work=(ogg_int32_t **)_ogg_malloc(vi->channels*sizeof(*v->work));
  44. v->mdctright=(ogg_int32_t **)_ogg_malloc(vi->channels*sizeof(*v->mdctright));
  45. for(i=0;i<vi->channels;i++){
  46. v->work[i]=(ogg_int32_t *)_ogg_calloc(1,(ci->blocksizes[1]>>1)*
  47. sizeof(*v->work[i]));
  48. v->mdctright[i]=(ogg_int32_t *)_ogg_calloc(1,(ci->blocksizes[1]>>2)*
  49. sizeof(*v->mdctright[i]));
  50. }
  51. v->lW=0; /* previous window size */
  52. v->W=0; /* current window size */
  53. vorbis_dsp_restart(v);
  54. return v;
  55. }
  56. void vorbis_dsp_destroy(vorbis_dsp_state *v){
  57. int i;
  58. if(v){
  59. vorbis_info *vi=v->vi;
  60. if(v->work){
  61. for(i=0;i<vi->channels;i++)
  62. if(v->work[i])_ogg_free(v->work[i]);
  63. _ogg_free(v->work);
  64. }
  65. if(v->mdctright){
  66. for(i=0;i<vi->channels;i++)
  67. if(v->mdctright[i])_ogg_free(v->mdctright[i]);
  68. _ogg_free(v->mdctright);
  69. }
  70. _ogg_free(v);
  71. }
  72. }
  73. static LOOKUP_T *_vorbis_window(int left){
  74. switch(left){
  75. case 32:
  76. return vwin64;
  77. case 64:
  78. return vwin128;
  79. case 128:
  80. return vwin256;
  81. case 256:
  82. return vwin512;
  83. case 512:
  84. return vwin1024;
  85. case 1024:
  86. return vwin2048;
  87. case 2048:
  88. return vwin4096;
  89. #ifndef LIMIT_TO_64kHz
  90. case 4096:
  91. return vwin8192;
  92. #endif
  93. default:
  94. return(0);
  95. }
  96. }
  97. /* pcm==0 indicates we just want the pending samples, no more */
  98. int vorbis_dsp_pcmout(vorbis_dsp_state *v,ogg_int16_t *pcm,int samples){
  99. vorbis_info *vi=v->vi;
  100. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  101. if(v->out_begin>-1 && v->out_begin<v->out_end){
  102. int n=v->out_end-v->out_begin;
  103. if(pcm){
  104. int i;
  105. if(n>samples)n=samples;
  106. for(i=0;i<vi->channels;i++)
  107. mdct_unroll_lap(ci->blocksizes[0],ci->blocksizes[1],
  108. v->lW,v->W,v->work[i],v->mdctright[i],
  109. _vorbis_window(ci->blocksizes[0]>>1),
  110. _vorbis_window(ci->blocksizes[1]>>1),
  111. pcm+i,vi->channels,
  112. v->out_begin,v->out_begin+n);
  113. }
  114. return(n);
  115. }
  116. return(0);
  117. }
  118. int vorbis_dsp_read(vorbis_dsp_state *v,int s){
  119. if(s && v->out_begin+s>v->out_end)return(OV_EINVAL);
  120. v->out_begin+=s;
  121. return(0);
  122. }
  123. long vorbis_packet_blocksize(vorbis_info *vi,ogg_packet *op){
  124. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  125. oggpack_buffer opb;
  126. int mode;
  127. int modebits=0;
  128. int v=ci->modes;
  129. oggpack_readinit(&opb,op->packet);
  130. /* Check the packet type */
  131. if(oggpack_read(&opb,1)!=0){
  132. /* Oops. This is not an audio data packet */
  133. return(OV_ENOTAUDIO);
  134. }
  135. while(v>1){
  136. modebits++;
  137. v>>=1;
  138. }
  139. /* read our mode and pre/post windowsize */
  140. mode=oggpack_read(&opb,modebits);
  141. if(mode==-1)return(OV_EBADPACKET);
  142. return(ci->blocksizes[ci->mode_param[mode].blockflag]);
  143. }
  144. static int ilog(ogg_uint32_t v){
  145. int ret=0;
  146. if(v)--v;
  147. while(v){
  148. ret++;
  149. v>>=1;
  150. }
  151. return(ret);
  152. }
  153. int vorbis_dsp_synthesis(vorbis_dsp_state *vd,ogg_packet *op,int decodep){
  154. vorbis_info *vi=vd->vi;
  155. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  156. int mode,i;
  157. oggpack_readinit(&vd->opb,op->packet);
  158. /* Check the packet type */
  159. if(oggpack_read(&vd->opb,1)!=0){
  160. /* Oops. This is not an audio data packet */
  161. return OV_ENOTAUDIO ;
  162. }
  163. /* read our mode and pre/post windowsize */
  164. mode=oggpack_read(&vd->opb,ilog(ci->modes));
  165. if(mode==-1 || mode>=ci->modes) return OV_EBADPACKET;
  166. /* shift information we still need from last window */
  167. vd->lW=vd->W;
  168. vd->W=ci->mode_param[mode].blockflag;
  169. for(i=0;i<vi->channels;i++)
  170. mdct_shift_right(ci->blocksizes[vd->lW],vd->work[i],vd->mdctright[i]);
  171. if(vd->W){
  172. int temp;
  173. oggpack_read(&vd->opb,1);
  174. temp=oggpack_read(&vd->opb,1);
  175. if(temp==-1) return OV_EBADPACKET;
  176. }
  177. /* packet decode and portions of synthesis that rely on only this block */
  178. if(decodep){
  179. mapping_inverse(vd,ci->map_param+ci->mode_param[mode].mapping);
  180. if(vd->out_begin==-1){
  181. vd->out_begin=0;
  182. vd->out_end=0;
  183. }else{
  184. vd->out_begin=0;
  185. vd->out_end=ci->blocksizes[vd->lW]/4+ci->blocksizes[vd->W]/4;
  186. }
  187. }
  188. /* track the frame number... This is for convenience, but also
  189. making sure our last packet doesn't end with added padding.
  190. This is not foolproof! It will be confused if we begin
  191. decoding at the last page after a seek or hole. In that case,
  192. we don't have a starting point to judge where the last frame
  193. is. For this reason, vorbisfile will always try to make sure
  194. it reads the last two marked pages in proper sequence */
  195. /* if we're out of sequence, dump granpos tracking until we sync back up */
  196. if(vd->sequence==-1 || vd->sequence+1 != op->packetno-3){
  197. /* out of sequence; lose count */
  198. vd->granulepos=-1;
  199. vd->sample_count=-1;
  200. }
  201. vd->sequence=op->packetno;
  202. vd->sequence=vd->sequence-3;
  203. if(vd->sample_count==-1){
  204. vd->sample_count=0;
  205. }else{
  206. vd->sample_count+=
  207. ci->blocksizes[vd->lW]/4+ci->blocksizes[vd->W]/4;
  208. }
  209. if(vd->granulepos==-1){
  210. if(op->granulepos!=-1){ /* only set if we have a
  211. position to set to */
  212. vd->granulepos=op->granulepos;
  213. /* is this a short page? */
  214. if(vd->sample_count>vd->granulepos){
  215. /* corner case; if this is both the first and last audio page,
  216. then spec says the end is cut, not beginning */
  217. if(op->e_o_s){
  218. /* trim the end */
  219. /* no preceeding granulepos; assume we started at zero (we'd
  220. have to in a short single-page stream) */
  221. /* granulepos could be -1 due to a seek, but that would result
  222. in a long coun t, not short count */
  223. vd->out_end-=vd->sample_count-vd->granulepos;
  224. }else{
  225. /* trim the beginning */
  226. vd->out_begin+=vd->sample_count-vd->granulepos;
  227. if(vd->out_begin>vd->out_end)
  228. vd->out_begin=vd->out_end;
  229. }
  230. }
  231. }
  232. }else{
  233. vd->granulepos+=
  234. ci->blocksizes[vd->lW]/4+ci->blocksizes[vd->W]/4;
  235. if(op->granulepos!=-1 && vd->granulepos!=op->granulepos){
  236. if(vd->granulepos>op->granulepos){
  237. long extra=vd->granulepos-op->granulepos;
  238. if(extra)
  239. if(op->e_o_s){
  240. /* partial last frame. Strip the extra samples off */
  241. vd->out_end-=extra;
  242. } /* else {Shouldn't happen *unless* the bitstream is out of
  243. spec. Either way, believe the bitstream } */
  244. } /* else {Shouldn't happen *unless* the bitstream is out of
  245. spec. Either way, believe the bitstream } */
  246. vd->granulepos=op->granulepos;
  247. }
  248. }
  249. return(0);
  250. }