/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
 *                                                                  *
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
 * by the Xiph.Org Foundation http://www.xiph.org/                  *

 ********************************************************************

 function: libvorbis codec headers
 last mod: $Id: codec.h 17021 2010-03-24 09:29:41Z xiphmont $

 ********************************************************************/

#ifndef _vorbis_codec_h_
#define _vorbis_codec_h_

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

#include "ogg.h"

typedef struct vorbis_info{
	int     version;
	int     channels;
	int32_t rate;

  /* The below bitrate declarations are *hints*.
     Combinations of the three values carry the following implications:

     all three set to the same value:
       implies a fixed rate bitstream
     only nominal set:
       implies a VBR stream that averages the nominal bitrate.  No hard
       upper/lower limit
     upper and or lower set:
       implies a VBR bitstream that obeys the bitrate limits. nominal
       may also be set to give a nominal rate.
     none set:
       the coder does not care to speculate.
  */

	int32_t bitrate_upper;
	int32_t bitrate_nominal;
	int32_t bitrate_lower;

	void*   codec_setup;
} vorbis_info;

/* vorbis_dsp_state buffers the current vorbis audio
   analysis/synthesis state.  The DSP state belongs to a specific
   logical bitstream ****************************************************/
typedef struct vorbis_dsp_state{
	vorbis_info*    vi;

	xint**  pcm;
	xint**  pcmret;
	int     pcm_storage;
	int     pcm_current;
	int     pcm_returned;

	int     preextrapolate;
	int     eofflag;

	int32_t lW;
	int32_t W;
	int32_t nW;
	int32_t centerW;

	int64_t granulepos;
	int64_t sequence;

	void*   backend_state;
} vorbis_dsp_state;

typedef struct vorbis_block{
	/* necessary stream state for linking to the framing abstraction */
	xint**          pcm;       /* this is a pointer into local storage */
	oggpack_buffer  opb;

	int32_t lW;
	int32_t W;
	int32_t nW;
	int32_t pcmend;
	int     mode;

	int     eofflag;
	int64_t granulepos;
	int64_t sequence;
	vorbis_dsp_state* vd; /* For read-only access of configuration */

	/* local storage to avoid remallocing; it's up to the mapping to
	   structure it */
	void*   localstore;
	int32_t localtop;
	int32_t localalloc;
	int32_t totaluse;
	struct alloc_chain* reap;
} vorbis_block;

/* vorbis_block is a single block of data to be processed as part of
the analysis/synthesis stream; it belongs to a specific logical
bitstream, but is independent from other vorbis_blocks belonging to
that logical bitstream. *************************************************/

struct alloc_chain{
	void* ptr;
	struct alloc_chain* next;
};

/* vorbis_info contains all the setup information specific to the
   specific compression/decompression mode in progress (eg,
   psychoacoustic settings, channel setup, options, codebook
   etc). vorbis_info and substructures are in backends.h.
*********************************************************************/

/* the comments are not part of vorbis_info so that vorbis_info can be
   static storage */
typedef struct vorbis_comment{
	/* unlimited user comment fields.  libvorbis writes 'libvorbis'
	   whatever vendor is set to in encode */
	char**  user_comments;
	int*    comment_lengths;
	int     comments;
	char*   vendor;
} vorbis_comment;

/* libvorbis encodes in two abstraction layers; first we perform DSP
   and produce a packet (see docs/analysis.txt).  The packet is then
   coded into a framed OggSquish bitstream by the second layer (see
   docs/framing.txt).  Decode is the reverse process; we sync/frame
   the bitstream and extract individual packets, then decode the
   packet back into PCM audio.

   The extra framing/packetizing is used in streaming formats, such as
   files.  Over the net (such as with UDP), the framing and
   packetization aren't necessary as they're provided by the transport
   and the streaming layer is not used */

/* Vorbis PRIMITIVES: general ***************************************/

extern int  vorbis_info_init(vorbis_info* vi);
extern void vorbis_info_clear(vorbis_info* vi);
extern int  vorbis_comment_init(vorbis_comment* vc);
extern void vorbis_comment_add(vorbis_comment* vc, const char* comment);
extern void vorbis_comment_clear(vorbis_comment* vc);

extern int  vorbis_block_init(vorbis_dsp_state* v, vorbis_block* vb);
extern int  vorbis_block_clear(vorbis_block* vb);
extern void vorbis_dsp_clear(vorbis_dsp_state* v);

/* Vorbis PRIMITIVES: analysis/DSP layer ****************************/

extern int  vorbis_analysis_init(vorbis_dsp_state* v, vorbis_info* vi);

/* Vorbis PRIMITIVES: synthesis layer *******************************/
extern int  vorbis_synthesis_headerin(vorbis_info* vi,vorbis_comment* vc,
					ogg_packet* op);

extern int  vorbis_synthesis_init(vorbis_dsp_state* v,vorbis_info* vi);
extern int  vorbis_synthesis(vorbis_block* vb,ogg_packet* op);
extern int  vorbis_synthesis_blockin(vorbis_dsp_state* v,vorbis_block* vb);
extern int  vorbis_synthesis_pcmout(vorbis_dsp_state* v,xint*** pcm);
extern int  vorbis_synthesis_read(vorbis_dsp_state* v,int samples);

/* Vorbis ERRORS and return codes ***********************************/

#define OV_FALSE      -1
#define OV_EOF        -2
#define OV_HOLE       -3

#define OV_EMEMORY    -127
#define OV_EREAD      -128
#define OV_EFAULT     -129
#define OV_EIMPL      -130
#define OV_EINVAL     -131
#define OV_ENOTVORBIS -132
#define OV_EBADHEADER -133
#define OV_EVERSION   -134
#define OV_ENOTAUDIO  -135
#define OV_EBADPACKET -136
#define OV_EBADLINK   -137
#define OV_ENOSEEK    -138

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif
