synthesis.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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-2015 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: single-block PCM synthesis
  13. last mod: $Id: synthesis.c 19441 2015-01-21 01:17:41Z xiphmont $
  14. ********************************************************************/
  15. #include <stdio.h>
  16. #include <ogg/ogg.h>
  17. #include "vorbis/codec.h"
  18. #include "codec_internal.h"
  19. #include "registry.h"
  20. #include "misc.h"
  21. #include "os.h"
  22. int vorbis_synthesis(vorbis_block *vb,ogg_packet *op){
  23. vorbis_dsp_state *vd= vb ? vb->vd : 0;
  24. private_state *b= vd ? vd->backend_state : 0;
  25. vorbis_info *vi= vd ? vd->vi : 0;
  26. codec_setup_info *ci= vi ? vi->codec_setup : 0;
  27. oggpack_buffer *opb=vb ? &vb->opb : 0;
  28. int type,mode,i;
  29. if (!vd || !b || !vi || !ci || !opb) {
  30. return OV_EBADPACKET;
  31. }
  32. /* first things first. Make sure decode is ready */
  33. _vorbis_block_ripcord(vb);
  34. oggpack_readinit(opb,op->packet,op->bytes);
  35. /* Check the packet type */
  36. if(oggpack_read(opb,1)!=0){
  37. /* Oops. This is not an audio data packet */
  38. return(OV_ENOTAUDIO);
  39. }
  40. /* read our mode and pre/post windowsize */
  41. mode=oggpack_read(opb,b->modebits);
  42. if(mode==-1){
  43. return(OV_EBADPACKET);
  44. }
  45. vb->mode=mode;
  46. if(!ci->mode_param[mode]){
  47. return(OV_EBADPACKET);
  48. }
  49. vb->W=ci->mode_param[mode]->blockflag;
  50. if(vb->W){
  51. /* this doesn;t get mapped through mode selection as it's used
  52. only for window selection */
  53. vb->lW=oggpack_read(opb,1);
  54. vb->nW=oggpack_read(opb,1);
  55. if(vb->nW==-1){
  56. return(OV_EBADPACKET);
  57. }
  58. }else{
  59. vb->lW=0;
  60. vb->nW=0;
  61. }
  62. /* more setup */
  63. vb->granulepos=op->granulepos;
  64. vb->sequence=op->packetno;
  65. vb->eofflag=op->e_o_s;
  66. /* alloc pcm passback storage */
  67. vb->pcmend=ci->blocksizes[vb->W];
  68. vb->pcm=_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels);
  69. for(i=0;i<vi->channels;i++)
  70. vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i]));
  71. /* unpack_header enforces range checking */
  72. type=ci->map_type[ci->mode_param[mode]->mapping];
  73. return(_mapping_P[type]->inverse(vb,ci->map_param[ci->mode_param[mode]->
  74. mapping]));
  75. }
  76. /* used to track pcm position without actually performing decode.
  77. Useful for sequential 'fast forward' */
  78. int vorbis_synthesis_trackonly(vorbis_block *vb,ogg_packet *op){
  79. vorbis_dsp_state *vd=vb->vd;
  80. private_state *b=vd->backend_state;
  81. vorbis_info *vi=vd->vi;
  82. codec_setup_info *ci=vi->codec_setup;
  83. oggpack_buffer *opb=&vb->opb;
  84. int mode;
  85. /* first things first. Make sure decode is ready */
  86. _vorbis_block_ripcord(vb);
  87. oggpack_readinit(opb,op->packet,op->bytes);
  88. /* Check the packet type */
  89. if(oggpack_read(opb,1)!=0){
  90. /* Oops. This is not an audio data packet */
  91. return(OV_ENOTAUDIO);
  92. }
  93. /* read our mode and pre/post windowsize */
  94. mode=oggpack_read(opb,b->modebits);
  95. if(mode==-1)return(OV_EBADPACKET);
  96. vb->mode=mode;
  97. if(!ci->mode_param[mode]){
  98. return(OV_EBADPACKET);
  99. }
  100. vb->W=ci->mode_param[mode]->blockflag;
  101. if(vb->W){
  102. vb->lW=oggpack_read(opb,1);
  103. vb->nW=oggpack_read(opb,1);
  104. if(vb->nW==-1) return(OV_EBADPACKET);
  105. }else{
  106. vb->lW=0;
  107. vb->nW=0;
  108. }
  109. /* more setup */
  110. vb->granulepos=op->granulepos;
  111. vb->sequence=op->packetno;
  112. vb->eofflag=op->e_o_s;
  113. /* no pcm */
  114. vb->pcmend=0;
  115. vb->pcm=NULL;
  116. return(0);
  117. }
  118. long vorbis_packet_blocksize(vorbis_info *vi,ogg_packet *op){
  119. codec_setup_info *ci=vi->codec_setup;
  120. oggpack_buffer opb;
  121. int mode;
  122. if(ci==NULL || ci->modes<=0){
  123. /* codec setup not properly intialized */
  124. return(OV_EFAULT);
  125. }
  126. oggpack_readinit(&opb,op->packet,op->bytes);
  127. /* Check the packet type */
  128. if(oggpack_read(&opb,1)!=0){
  129. /* Oops. This is not an audio data packet */
  130. return(OV_ENOTAUDIO);
  131. }
  132. /* read our mode and pre/post windowsize */
  133. mode=oggpack_read(&opb,ov_ilog(ci->modes-1));
  134. if(mode==-1 || !ci->mode_param[mode])return(OV_EBADPACKET);
  135. return(ci->blocksizes[ci->mode_param[mode]->blockflag]);
  136. }
  137. int vorbis_synthesis_halfrate(vorbis_info *vi,int flag){
  138. /* set / clear half-sample-rate mode */
  139. codec_setup_info *ci=vi->codec_setup;
  140. /* right now, our MDCT can't handle < 64 sample windows. */
  141. if(ci->blocksizes[0]<=64 && flag)return -1;
  142. ci->halfrate_flag=(flag?1:0);
  143. return 0;
  144. }
  145. int vorbis_synthesis_halfrate_p(vorbis_info *vi){
  146. codec_setup_info *ci=vi->codec_setup;
  147. return ci->halfrate_flag;
  148. }