/*
 * libacm - Interplay ACM audio decoder.
 *
 * Copyright (C) 2004  Marko Kreen
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#ifndef __LIBACM_H
#define __LIBACM_H

#define LIBACM_VERSION "0.9"

#define ACM_ID		0x032897
#define ACM_BPS		2

#define ACM_OK			 0
#define ACM_ERR_OTHER		-1
#define ACM_ERR_OPEN		-2
#define ACM_ERR_NOT_ACM		-3
#define ACM_ERR_READ_ERR	-4
#define ACM_ERR_BADFMT		-5

typedef struct ACMInfo {
	int channels;
	int rate;
	int acm_id;
	int acm_version;
	int acm_level;
	int acm_cols;		/* 1 << acm_level */
	int acm_rows;
} ACMInfo;

typedef struct {
	int (*read_func)(void *ptr, int size, int n, void *datasrc);
	int (*seek_func)(void *datasrc, int offset, int whence);
	int (*close_func)(void *datasrc);
	int (*tell_func)(void *datasrc);
	int (*write_func)(void *ptr, int size, int n, void *datadst);
} acm_io_callbacks;

struct ACMEncData;

struct ACMStream {
	ACMInfo info;
	int total_values;

	/* acm data stream */
	void *io_arg;
	acm_io_callbacks io;
	int data_len;

	/* acm stream buffer */
	unsigned char *buf;
	int buf_max, buf_size, buf_pos, bit_avail;
	unsigned bit_data;

	/* block lengths (in samples) */
	int block_len;
	int wrapbuf_len;
	/* buffers */
	int *block;
	int *wrapbuf;
	int *ampbuf;
	int *midbuf;			/* pointer into ampbuf */
	/* result */
	int block_ready;
	int stream_pos;			/* in words. absolute */
	int block_pos;			/* in words, relative */
	struct ACMEncData *enc_data;	/* encoding private data */
	int (*finish_encoding)(void *acm);
};
typedef struct ACMStream ACMStream;

/* decode.c */
int acm_open_decoder(ACMStream **res, void *io_arg, acm_io_callbacks io);
int acm_read(ACMStream *acm, char *buf, int nbytes,
		int bigendianp, int wordlen, int sgned);
void acm_close(ACMStream *acm);

/* util.c */
int acm_open_file(ACMStream **acm, const char *filename);
const ACMInfo *acm_info(ACMStream *acm);
int acm_seekable(ACMStream *acm);
int acm_bitrate(ACMStream *acm);
int acm_raw_total(ACMStream *acm);
int acm_raw_tell(ACMStream *acm);
int acm_pcm_total(ACMStream *acm);
int acm_pcm_tell(ACMStream *acm);
int acm_time_total(ACMStream *acm);
int acm_time_tell(ACMStream *acm);
int acm_read_loop(ACMStream *acm, char *dst, int len,
		int bigendianp, int wordlen, int sgned);
int acm_seek_pcm(ACMStream *acm, int pcm_pos);
int acm_seek_time(ACMStream *acm, int pos_ms);
const char *acm_strerror(int err);

/* encode.c */
int acm_open_encoder(ACMStream **res,
		int rate, int channels,
		int acm_level, int acm_rows,
		void *io_arg, acm_io_callbacks io);
int acm_encode(ACMStream *acm, unsigned char *src, int nbytes);

#endif

