dsp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /************************************************************************
  2. * Copyright (C) 2002-2009, Xiph.org Foundation
  3. * Copyright (C) 2010, Robin Watts for Pinknoise Productions Ltd
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the names of the Xiph.org Foundation nor Pinknoise
  17. * Productions Ltd nor the names of its contributors may be used to
  18. * endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ************************************************************************
  33. function: PCM data vector blocking, windowing and dis/reassembly
  34. ************************************************************************/
  35. #include <stdlib.h>
  36. #include "ogg.h"
  37. #include "mdct.h"
  38. #include "ivorbiscodec.h"
  39. #include "codec_internal.h"
  40. #include "misc.h"
  41. #include "window_lookup.h"
  42. int vorbis_dsp_restart(vorbis_dsp_state *v){
  43. if(!v)return -1;
  44. {
  45. vorbis_info *vi=v->vi;
  46. codec_setup_info *ci;
  47. if(!vi)return -1;
  48. ci=vi->codec_setup;
  49. if(!ci)return -1;
  50. v->out_end=-1;
  51. v->out_begin=-1;
  52. v->granulepos=-1;
  53. v->sequence=-1;
  54. v->sample_count=-1;
  55. }
  56. return 0;
  57. }
  58. int vorbis_dsp_init(vorbis_dsp_state *v,vorbis_info *vi){
  59. int i;
  60. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  61. v->vi=vi;
  62. v->work=(ogg_int32_t **)_ogg_malloc(vi->channels*sizeof(*v->work));
  63. v->mdctright=(ogg_int32_t **)_ogg_malloc(vi->channels*sizeof(*v->mdctright));
  64. for(i=0;i<vi->channels;i++){
  65. v->work[i]=(ogg_int32_t *)_ogg_calloc(1,(ci->blocksizes[1]>>1)*
  66. sizeof(*v->work[i]));
  67. v->mdctright[i]=(ogg_int32_t *)_ogg_calloc(1,(ci->blocksizes[1]>>2)*
  68. sizeof(*v->mdctright[i]));
  69. }
  70. v->lW=0; /* previous window size */
  71. v->W=0; /* current window size */
  72. vorbis_dsp_restart(v);
  73. return 0;
  74. }
  75. vorbis_dsp_state *vorbis_dsp_create(vorbis_info *vi){
  76. vorbis_dsp_state *v=_ogg_calloc(1,sizeof(*v));
  77. vorbis_dsp_init(v,vi);
  78. return v;
  79. }
  80. void vorbis_dsp_clear(vorbis_dsp_state *v){
  81. int i;
  82. if(v){
  83. vorbis_info *vi=v->vi;
  84. if(v->work){
  85. for(i=0;i<vi->channels;i++)
  86. if(v->work[i])_ogg_free(v->work[i]);
  87. _ogg_free(v->work);
  88. }
  89. if(v->mdctright){
  90. for(i=0;i<vi->channels;i++)
  91. if(v->mdctright[i])_ogg_free(v->mdctright[i]);
  92. _ogg_free(v->mdctright);
  93. }
  94. }
  95. }
  96. void vorbis_dsp_destroy(vorbis_dsp_state *v){
  97. vorbis_dsp_clear(v);
  98. _ogg_free(v);
  99. }
  100. static LOOKUP_T *_vorbis_window(int left){
  101. switch(left){
  102. case 32:
  103. return vwin64;
  104. case 64:
  105. return vwin128;
  106. case 128:
  107. return vwin256;
  108. case 256:
  109. return vwin512;
  110. case 512:
  111. return vwin1024;
  112. case 1024:
  113. return vwin2048;
  114. case 2048:
  115. return vwin4096;
  116. #ifndef LIMIT_TO_64kHz
  117. case 4096:
  118. return vwin8192;
  119. #endif
  120. default:
  121. return(0);
  122. }
  123. }
  124. /* pcm==0 indicates we just want the pending samples, no more */
  125. int vorbis_dsp_pcmout(vorbis_dsp_state *v,ogg_int16_t *pcm,int samples){
  126. vorbis_info *vi=v->vi;
  127. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  128. if(v->out_begin>-1 && v->out_begin<v->out_end){
  129. int n=v->out_end-v->out_begin;
  130. if(pcm){
  131. int i;
  132. if(n>samples)n=samples;
  133. for(i=0;i<vi->channels;i++)
  134. mdct_unroll_lap(ci->blocksizes[0],ci->blocksizes[1],
  135. v->lW,v->W,v->work[i],v->mdctright[i],
  136. _vorbis_window(ci->blocksizes[0]>>1),
  137. _vorbis_window(ci->blocksizes[1]>>1),
  138. pcm+i,vi->channels,
  139. v->out_begin,v->out_begin+n);
  140. }
  141. return(n);
  142. }
  143. return(0);
  144. }
  145. int vorbis_dsp_read(vorbis_dsp_state *v,int s){
  146. if(s && v->out_begin+s>v->out_end)return(OV_EINVAL);
  147. v->out_begin+=s;
  148. return(0);
  149. }
  150. long vorbis_packet_blocksize(vorbis_info *vi,ogg_packet *op){
  151. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  152. oggpack_buffer opb;
  153. int mode;
  154. int modebits=0;
  155. int v=ci->modes;
  156. oggpack_readinit(&opb,op->packet);
  157. /* Check the packet type */
  158. if(oggpack_read(&opb,1)!=0){
  159. /* Oops. This is not an audio data packet */
  160. return(OV_ENOTAUDIO);
  161. }
  162. while(v>1){
  163. modebits++;
  164. v>>=1;
  165. }
  166. /* read our mode and pre/post windowsize */
  167. mode=oggpack_read(&opb,modebits);
  168. if(mode==-1)return(OV_EBADPACKET);
  169. return(ci->blocksizes[ci->mode_param[mode].blockflag]);
  170. }
  171. static int ilog(ogg_uint32_t v){
  172. int ret=0;
  173. if(v)--v;
  174. while(v){
  175. ret++;
  176. v>>=1;
  177. }
  178. return(ret);
  179. }
  180. int vorbis_dsp_synthesis(vorbis_dsp_state *vd,ogg_packet *op,int decodep){
  181. vorbis_info *vi=vd->vi;
  182. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  183. int mode,i;
  184. oggpack_readinit(&vd->opb,op->packet);
  185. /* Check the packet type */
  186. if(oggpack_read(&vd->opb,1)!=0){
  187. /* Oops. This is not an audio data packet */
  188. return OV_ENOTAUDIO ;
  189. }
  190. /* read our mode and pre/post windowsize */
  191. mode=oggpack_read(&vd->opb,ilog(ci->modes));
  192. if(mode==-1 || mode>=ci->modes) return OV_EBADPACKET;
  193. /* shift information we still need from last window */
  194. vd->lW=vd->W;
  195. vd->W=ci->mode_param[mode].blockflag;
  196. for(i=0;i<vi->channels;i++)
  197. mdct_shift_right(ci->blocksizes[vd->lW],vd->work[i],vd->mdctright[i]);
  198. if(vd->W){
  199. int temp;
  200. oggpack_read(&vd->opb,1);
  201. temp=oggpack_read(&vd->opb,1);
  202. if(temp==-1) return OV_EBADPACKET;
  203. }
  204. /* packet decode and portions of synthesis that rely on only this block */
  205. if(decodep){
  206. mapping_inverse(vd,ci->map_param+ci->mode_param[mode].mapping);
  207. if(vd->out_begin==-1){
  208. vd->out_begin=0;
  209. vd->out_end=0;
  210. }else{
  211. vd->out_begin=0;
  212. vd->out_end=ci->blocksizes[vd->lW]/4+ci->blocksizes[vd->W]/4;
  213. }
  214. }
  215. /* track the frame number... This is for convenience, but also
  216. making sure our last packet doesn't end with added padding.
  217. This is not foolproof! It will be confused if we begin
  218. decoding at the last page after a seek or hole. In that case,
  219. we don't have a starting point to judge where the last frame
  220. is. For this reason, vorbisfile will always try to make sure
  221. it reads the last two marked pages in proper sequence */
  222. /* if we're out of sequence, dump granpos tracking until we sync back up */
  223. if(vd->sequence==-1 || vd->sequence+1 != op->packetno-3){
  224. /* out of sequence; lose count */
  225. vd->granulepos=-1;
  226. vd->sample_count=-1;
  227. }
  228. vd->sequence=op->packetno;
  229. vd->sequence=vd->sequence-3;
  230. if(vd->sample_count==-1){
  231. vd->sample_count=0;
  232. }else{
  233. vd->sample_count+=
  234. ci->blocksizes[vd->lW]/4+ci->blocksizes[vd->W]/4;
  235. }
  236. if(vd->granulepos==-1){
  237. if(op->granulepos!=-1){ /* only set if we have a
  238. position to set to */
  239. vd->granulepos=op->granulepos;
  240. /* is this a short page? */
  241. if(vd->sample_count>vd->granulepos){
  242. /* corner case; if this is both the first and last audio page,
  243. then spec says the end is cut, not beginning */
  244. if(op->e_o_s){
  245. /* trim the end */
  246. /* no preceeding granulepos; assume we started at zero (we'd
  247. have to in a short single-page stream) */
  248. /* granulepos could be -1 due to a seek, but that would result
  249. in a long coun t, not short count */
  250. vd->out_end-=(int)(vd->sample_count-vd->granulepos);
  251. }else{
  252. /* trim the beginning */
  253. vd->out_begin+=(int)(vd->sample_count-vd->granulepos);
  254. if(vd->out_begin>vd->out_end)
  255. vd->out_begin=vd->out_end;
  256. }
  257. }
  258. }
  259. }else{
  260. vd->granulepos+=
  261. ci->blocksizes[vd->lW]/4+ci->blocksizes[vd->W]/4;
  262. if(op->granulepos!=-1 && vd->granulepos!=op->granulepos){
  263. if(vd->granulepos>op->granulepos){
  264. long extra=(long)(vd->granulepos-op->granulepos);
  265. if(extra)
  266. if(op->e_o_s){
  267. /* partial last frame. Strip the extra samples off */
  268. vd->out_end-=extra;
  269. } /* else {Shouldn't happen *unless* the bitstream is out of
  270. spec. Either way, believe the bitstream } */
  271. } /* else {Shouldn't happen *unless* the bitstream is out of
  272. spec. Either way, believe the bitstream } */
  273. vd->granulepos=op->granulepos;
  274. }
  275. }
  276. return(0);
  277. }