synthesis.c 5.1 KB

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