/********************************************************************
 *                                                                  *
 * 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-2002             *
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
 *                                                                  *
 ********************************************************************

 function: toplevel libogg include
 last mod: $Id: ogg.h,v 1.19 2002/09/15 23:48:02 xiphmont Exp $

 ********************************************************************/
#ifndef _OGG_H
#define _OGG_H

#ifdef __cplusplus
extern "C" {
#endif

#include "os_types.h"

typedef struct {
	int     endbit;

	byte*   buffer;
	byte*   ptr;
	byte*   storage; // points at end of buffer
} oggpack_buffer;

/* ogg_page is used to encapsulate the data in one Ogg bitstream page *****/

typedef struct {
	byte*   header;
	int32_t header_len;
	byte*   body;
	int32_t body_len;
} ogg_page;

/* ogg_stream_state contains the current encode/decode state of a logical
   Ogg bitstream **********************************************************/

typedef struct {
	byte*   body_data;      /* bytes from packet bodies */
	int32_t body_storage;   /* storage elements allocated */
	int32_t body_fill;      /* elements stored; fill mark */
	int32_t body_returned;  /* elements of fill returned */

	int*    lacing_vals;    /* The values that will go to the segment table */
	int64_t*    granule_vals; /* granulepos values for headers. Not compact
	                             this way, but it is simple coupled to the
	                             lacing fifo */
	int32_t lacing_storage;
	int32_t lacing_fill;
	int32_t lacing_packet;
	int32_t lacing_returned;

	byte    header[282];    /* working space for header encode */
	int     header_fill;

	int32_t e_o_s;          /* set when we have buffered the last packet in
	                           the logical bitstream */
	int32_t b_o_s;          /* set after we've written the initial page
	                           of a logical bitstream */
	int32_t serialno;
	int32_t pageno;
	int64_t packetno;       /* sequence number for decode; the framing
	                           knows where there's a hole in the data,
	                           but we need coupling so that the codec
	                           (which is in a seperate abstraction
	                           layer) also knows about the gap */
	int64_t granulepos;
} ogg_stream_state;

/* ogg_packet is used to encapsulate the data and metadata belonging
   to a single raw Ogg/Vorbis packet *************************************/

typedef struct {
	byte*   packet;
	int32_t bytes;
	int32_t b_o_s;
	int32_t e_o_s;

	int64_t granulepos;

	int64_t packetno;       /* sequence number for decode; the framing
	                           knows where there's a hole in the data,
	                           but we need coupling so that the codec
	                           (which is in a seperate abstraction
	                           layer) also knows about the gap */
} ogg_packet;

typedef struct {
	byte*   data;
	int32_t storage;
	int32_t fill;
	int32_t returned;

	int32_t unsynced;
	int32_t headerbytes;
	int32_t bodybytes;
} ogg_sync_state;

/* Ogg BITSTREAM PRIMITIVES: bitstream ************************/

extern void oggpack_writeinit(oggpack_buffer* b);
extern void oggpack_reset(oggpack_buffer* b);
extern void oggpack_writeclear(oggpack_buffer* b);
extern void oggpack_readinit(oggpack_buffer* b, byte* buf, int32_t bytes);
extern void oggpack_write(oggpack_buffer* b,uint32_t value,int bits);
extern int32_t oggpack_look(oggpack_buffer* b,int bits);
extern int32_t oggpack_look_huff(oggpack_buffer* b,int bits);
extern int32_t oggpack_look1(oggpack_buffer* b);
extern void oggpack_adv(oggpack_buffer* b,int bits);
extern int  oggpack_adv_huff(oggpack_buffer* b,int bits);
extern void oggpack_adv1(oggpack_buffer* b);
extern int32_t oggpack_read(oggpack_buffer* b,int bits);
extern int32_t oggpack_read1(oggpack_buffer* b);
extern int32_t oggpack_bytes(oggpack_buffer* b);
extern int32_t oggpack_bits(oggpack_buffer* b);
extern byte* oggpack_get_buffer(oggpack_buffer* b);

/* Ogg BITSTREAM PRIMITIVES: encoding **************************/

extern int ogg_stream_packetin(ogg_stream_state* os, ogg_packet* op);
extern int ogg_stream_pageout(ogg_stream_state* os, ogg_page* og);
extern int ogg_stream_flush(ogg_stream_state* os, ogg_page* og);

/* Ogg BITSTREAM PRIMITIVES: decoding **************************/

extern int ogg_sync_init(ogg_sync_state* oy);
extern int ogg_sync_clear(ogg_sync_state* oy);
extern int ogg_sync_reset(ogg_sync_state* oy);
extern int ogg_sync_destroy(ogg_sync_state* oy);

extern byte* ogg_sync_buffer(ogg_sync_state* oy, int32_t size);
extern int ogg_sync_wrote(ogg_sync_state* oy, int32_t bytes);
extern int32_t ogg_sync_pageseek(ogg_sync_state* oy,ogg_page* og);
extern int ogg_sync_pageout(ogg_sync_state* oy, ogg_page* og);
extern int ogg_stream_pagein(ogg_stream_state* os, ogg_page* og);
extern int ogg_stream_packetout(ogg_stream_state* os,ogg_packet* op);
extern int ogg_stream_packetpeek(ogg_stream_state* os,ogg_packet* op);

/* Ogg BITSTREAM PRIMITIVES: general ***************************/

extern int ogg_stream_init(ogg_stream_state* os,int serialno);
extern int ogg_stream_clear(ogg_stream_state* os);
extern int ogg_stream_reset(ogg_stream_state* os);
extern int ogg_stream_reset_serialno(ogg_stream_state* os,int serialno);
extern int ogg_stream_destroy(ogg_stream_state* os);
extern int ogg_stream_eos(ogg_stream_state* os);

extern void ogg_page_checksum_set(ogg_page* og);

extern int ogg_page_version(const ogg_page* og);
extern int ogg_page_continued(const ogg_page* og);
extern int ogg_page_bos(const ogg_page* og);
extern int ogg_page_eos(const ogg_page* og);
extern int64_t ogg_page_granulepos(const ogg_page* og);
extern int ogg_page_serialno(const ogg_page* og);
extern int32_t ogg_page_pageno(const ogg_page* og);
extern int ogg_page_packets(const ogg_page* og);

extern void ogg_packet_clear(ogg_packet* op);

#ifdef __cplusplus
}
#endif

#endif  /* _OGG_H */
