michael@0: /******************************************************************** michael@0: * * michael@0: * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. * michael@0: * * michael@0: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * michael@0: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * michael@0: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * michael@0: * * michael@0: * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003 * michael@0: * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ * michael@0: * * michael@0: ******************************************************************** michael@0: michael@0: function: single-block PCM synthesis michael@0: last mod: $Id: synthesis.c,v 1.4 2003/03/29 03:07:21 xiphmont Exp $ michael@0: michael@0: ********************************************************************/ michael@0: michael@0: #include michael@0: #include michael@0: #include "ivorbiscodec.h" michael@0: #include "codec_internal.h" michael@0: #include "registry.h" michael@0: #include "misc.h" michael@0: #include "block.h" michael@0: michael@0: static int _vorbis_synthesis1(vorbis_block *vb,ogg_packet *op,int decodep){ michael@0: vorbis_dsp_state *vd= vb ? vb->vd : 0; michael@0: private_state *b= vd ? (private_state *)vd->backend_state: 0; michael@0: vorbis_info *vi= vd ? vd->vi : 0; michael@0: codec_setup_info *ci= vi ? (codec_setup_info *)vi->codec_setup : 0; michael@0: oggpack_buffer *opb=vb ? &vb->opb : 0; michael@0: int type,mode,i; michael@0: michael@0: if (!vd || !b || !vi || !ci || !opb) { michael@0: return OV_EBADPACKET; michael@0: } michael@0: michael@0: /* first things first. Make sure decode is ready */ michael@0: _vorbis_block_ripcord(vb); michael@0: oggpack_readinit(opb,op->packet,op->bytes); michael@0: michael@0: /* Check the packet type */ michael@0: if(oggpack_read(opb,1)!=0){ michael@0: /* Oops. This is not an audio data packet */ michael@0: return(OV_ENOTAUDIO); michael@0: } michael@0: michael@0: /* read our mode and pre/post windowsize */ michael@0: mode=oggpack_read(opb,b->modebits); michael@0: if(mode==-1)return(OV_EBADPACKET); michael@0: michael@0: vb->mode=mode; michael@0: if(!ci->mode_param[mode]){ michael@0: return(OV_EBADPACKET); michael@0: } michael@0: michael@0: vb->W=ci->mode_param[mode]->blockflag; michael@0: if(vb->W){ michael@0: vb->lW=oggpack_read(opb,1); michael@0: vb->nW=oggpack_read(opb,1); michael@0: if(vb->nW==-1) return(OV_EBADPACKET); michael@0: }else{ michael@0: vb->lW=0; michael@0: vb->nW=0; michael@0: } michael@0: michael@0: /* more setup */ michael@0: vb->granulepos=op->granulepos; michael@0: vb->sequence=op->packetno-3; /* first block is third packet */ michael@0: vb->eofflag=op->e_o_s; michael@0: michael@0: if(decodep){ michael@0: /* alloc pcm passback storage */ michael@0: vb->pcmend=ci->blocksizes[vb->W]; michael@0: vb->pcm=(ogg_int32_t **)_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels); michael@0: for(i=0;ichannels;i++) michael@0: vb->pcm[i]=(ogg_int32_t *)_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i])); michael@0: michael@0: /* unpack_header enforces range checking */ michael@0: type=ci->map_type[ci->mode_param[mode]->mapping]; michael@0: michael@0: return(_mapping_P[type]->inverse(vb,b->mode[mode])); michael@0: }else{ michael@0: /* no pcm */ michael@0: vb->pcmend=0; michael@0: vb->pcm=NULL; michael@0: michael@0: return(0); michael@0: } michael@0: } michael@0: michael@0: int vorbis_synthesis(vorbis_block *vb,ogg_packet *op){ michael@0: return _vorbis_synthesis1(vb,op,1); michael@0: } michael@0: michael@0: /* used to track pcm position without actually performing decode. michael@0: Useful for sequential 'fast forward' */ michael@0: int vorbis_synthesis_trackonly(vorbis_block *vb,ogg_packet *op){ michael@0: return _vorbis_synthesis1(vb,op,0); michael@0: } michael@0: michael@0: long vorbis_packet_blocksize(vorbis_info *vi,ogg_packet *op){ michael@0: codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; michael@0: oggpack_buffer opb; michael@0: int mode; michael@0: michael@0: oggpack_readinit(&opb,op->packet,op->bytes); michael@0: michael@0: /* Check the packet type */ michael@0: if(oggpack_read(&opb,1)!=0){ michael@0: /* Oops. This is not an audio data packet */ michael@0: return(OV_ENOTAUDIO); michael@0: } michael@0: michael@0: { michael@0: int modebits=0; michael@0: int v=ci->modes; michael@0: while(v>1){ michael@0: modebits++; michael@0: v>>=1; michael@0: } michael@0: michael@0: /* read our mode and pre/post windowsize */ michael@0: mode=oggpack_read(&opb,modebits); michael@0: } michael@0: if(mode==-1)return(OV_EBADPACKET); michael@0: return(ci->blocksizes[ci->mode_param[mode]->blockflag]); michael@0: } michael@0: michael@0: