#ifndef AVIDEO_H
#define AVIDEO_H  1

/* (C) ARCHOS - Hardware and software patent pending */

/* AVIDEO flags added to BitMap->Flags */
#define	AVIDEO12	0x80	/* AVIDEO12 style BitMap->Flags */
#define	AVIDEO24	0x40	/* AVIDEO24 style BitMap->Flags */
#define  AVERSION 3     /* current AVideo release */

/* commands recognized by AVideo server */
#define  AVLOAD  		1  /* load AVideo 1-bit to 24-bit ILBM pictures into Amiga memory */
#define	AVDISPLAY  	2  /* transfer image from Amiga memory to frame buffer */
#define	AVSHOW    	3  /* load image from file into frame buffer and display */
#define	AVFBUFFER  	4  /* show AVideo frame buffer only */
#define	AVTRANSPAR  5  /* set Amiga View transparency mode on/off */
#define	AVCLEAR  	6  /* clear frame buffer */
#define	AVRESET		7	/* reset AVideo card: Amiga display mode */
#define	AVFADE		8	/* fade frame buffer image in or out */
#define	AVRECORD		9	/* record current view into frame buffer */
#define	AVLOADVIEWS	10	/* load given views[] array into frame buffer */
#define	AVSWITCH		12	/* select AVideo24's active 12-bit buffer */
#define	AVLACE		13	/* switch interlace mode */
#define	AVQUIT		11	/* dump AVideo server */

#define AVIDEOPORT "avideo"  /* AVideo server's port name, if any */
#define AVPREFS "s:avideo.prefs" /* AVideo preferences file */

#define AVSAVEMEM 10000	/* leave this amount of CHIP or FAST memory free in any case */

struct AVideoMsg { /* message structure to converse directly with AVideo server */
	struct Message  msg;
   WORD   argc;         /* number of argument in command line: if zero don't use command line mode */
   char   **argv;       /* parsed command line to AVIDEO server (command line mode) */
	WORD   cmd;          /* command to be executed by AVIDEO server (not command line mode ) */
	WORD   mode;         /* command mode or parameter (not command line mode ) */
	WORD   error;        /* error code returned by server */
   char   *errMsg;      /* error message line returned by server */
	struct BitMap  *bm;  /* 12/24-bit bitmap to be displayed by server (not command line mode ) */
	UWORD  vpModes ;     /* ViewPort modes of the picture to be displayed  */
	UWORD  *colorTable ; /* ColorMap of the picture to be displayed for standard bitmaps  */
	char   *name;        /* name of picture to be loaded if any (AVLOAD, AVSHOW, ...) */
   struct View *views;  /* array of 3 views to be loaded into frame buffer (AVLOADVIEWS) */
   struct View *endView;  /* views to be loaded after loading view array into frame buffer */
   LONG   sup;          /* general purpose extension */
};

/*** 'avideo.library' functions are listed bellow with their prototypes ***/

/* AVideo server functions */

/* AVOpen -- main routine to set up AVideo driver.
 * Return 1 or 2 if interrupt servers cannot be installed.
 */
UWORD AVOpen( void );

/* AVClose -- close Avideo driver
 */
UWORD AVClose( void );

/* AVLoadBuf -- load ILBM 1 to 24-bit bitmaps into AVideo frame buffer.
 * 'name' is the file name and 'bm' a partially (or fully loaded)
 * 1 to 12-bit bitmap for AVIDEO12 and a 1 to 24-bit bitmap for AVIDEO24.
 * This routine deals with standard Amiga Bitmaps and Extended AVideo Bitmaps
 * defined as follows :
 *    - bm->pad = number of planes loaded into CHIP memory;
 *    - bm->Flags = AVIDEO12 or AVIDEO24 flag;
 *    - bm->Planes[7] = &planes[0];
 * 'planes' is the array of plane pointers:
 *    - AVIDEO12 : PLANEPTR *planes[12+4];
 *    - AVIDEO24 : PLANEPTR *planes[24+4];
 *
 * Il all planes do not fit in CHIP memory, those that do not fit are loaded
 * into FAST and four extra planes, planes[12] to planes[16] for AVideo12
 * and planes[24] to planes[28] for AVideo24 must be provided for buffering
 * purposes.
 *
 * For AVideo24 only, the allocation of CHIP planes should be made in the
 * order stated below for 12-bit images and 15 through 24-bit images:
 *
 * static UWORD planeLst24[24+4] = {
 *       6, 23,15,7,  13,5, 22,14, 20,12, 4,21,  2, 19,11,3,  9, 1, 18,10, 16,8, 0, 17, 24,25,26,27 };
 * 24-b  R1 B0 G0 R0  G2 R2 B1 G1  B3 G3 R3 B2   R5 B4 G4 R4  G6 R6 B5 G5  B7 G7 R7 B6  X1 X2 X3 X4
 *
 * static UWORD planeLst12[24+4] = {
 *       2, 11,7, 3,  5, 1, 10,6,  8, 4, 0, 9,   12,13,14,15, 16,17,18,19, 20,21,22,23, 24,25,26,27 };
 * 12-b  R1 B0 G0 R0  G2 R2 B1 G1  B3 G3 R3 B2
 *
 * 'bm->pad' indicates the number of planes actually loaded in CHIP memory of the
 * plane array pointed to by 'bm->Planes[7]' which must contains
 * at least the 4 planes loaded in CHIP memory.
 *
 * 'vpModes' are the image ViewPort modes.
 *
 *'colorMap' is the image color map; it must always be supplied and
 * initialized as a grey scale map for 12-bit and more (see AVSetViews()).
 * If all planes are not in CHIP memory, AVideo frame buffer is filled
 * in 4 planes at a time. If all planes are supplied (12 planes for AVideo12
 * and 12 to 24 planes for AVideo24), then the image is loaded into AVideo
 * buffer in one go.
 * For 12-bit and beyond 'colMap' contains a grey scale color map initialized
 * as follows:
 *	   for (i=0;  i<16;  i++)
 *	      colMap[i] = (i<<8) | (i<<4) | i;
 * For standard Amiga images (depth <= 5), 'colMap' is the actual color map.
 *
 * For AVIDEO12 board, bm->Flags must be set equal to AVIDEO12.
 * For AVIDEO24 board, bm->Flags must be set equal to AVIDEO24.
 * For 12-bit bitmaps, bm->Planes[7] must point to an array of
 * 12+4 plane pointers. AVIDEO12 accepts only 12-bit bitmaps (R4,G4,B4)
 * but note that AVLoadPict() loads images of any depth beyond 12 (i.e.
 * 15,18,21 and 24-bit).
 * For 24-bit bitmaps with AVIDEO24, bm->Planes[7] must point
 * to an array of (24+4)=28 plane pointers. The first 12 or 24 planes
 * are preferably loaded into FAST memory and contain the image data;
 * the remaining extra 4 planes are used as a temporary buffer to feed
 * the AVideo frame buffer, they need not reside in CHIP memory.
 */
UWORD AVLoadFBuf( char *name, struct BitMap *bm, WORD  fade, UWORD vpModes, UWORD *colorTab);

/* AVFrameBuf -- display AVideo frame buffer
 * AVIDEO12 : if (mode == -1) returns to Amiga mode else Display frame buffer only.
 * AVIDEO24 :  Display AVideo frame buffer only.
 * If mode is 0, display first 12-bit Frame Buffer;
 * else if mode is 1, display alternate 12-bit Frame Buffer;
 * else if mode is 2, display 24-bit Frame Buffer;
 *	else if mode equals -1, reset display in Amiga mode (Amiga
 *		image only);
 *	else (default) display current AVideo buffer with no
 *		transparency.
 */
UWORD AVFrameBuf( WORD mode );

/* AVTranspar -- make Amiga graphics transparent.
 * AVIDEO12 : if mode == -1 , Amiga imagery only;
 *            else Amiga imagery is made transparent?
 * AVIDEO24 : 
 *	If mode equals  -1, transparency is turned off (Amiga
 *	   image only);
 *	else if mode equals 0, transparency is set to Frame Buffer 0;
 *	else if mode equals 1, transparency is set to Frame Buffer 1;
 *	else if mode equals 1, transparency is set to entire 
 *		24-bit Frame Buffer;
 *	else (default), transparency is set to current Frame Buffer.
 */
UWORD AVTranspar( WORD speed );

/* AVLoadViews -- load an array of three Views, 'views' into
 * the AVideo frame buffer. The views are instantly loaded and
 * displayed. This function is well suited for animation
 * especially with AVideo24 using two 12-bit buffers (double buffering)
 * and should be used by any program which need refresh
 * the AVideo frame buffer regularly with no delay.
 * On AVideo24, this function loads one 12-bit buffer while
 * the other is being displayed. For non-interlaced views,
 * loading requires 3/50th secs and 6/50th for interlaced views.
 * 'views' to be loaded are normally prepared with AVSetViews().
 * 'endView' is the view to be loaded after loading the 'views' array
 * into the buffer. This parameter is taken into account by AVIDEO24
 * only : if 'endView' is given, once the frame buffer has been loaded,
 * 'endView' is eventually loaded and transparency is on, if not,
 * the frame buffer remains solely visible and transparency is turned off.
 * To escape from this latter situation, one has to reload a view
 * and explicitly turn on transparency mode again. With AVideo24 and
 * 'endView' set to NULL, double buffering animation may proceed at the
 * rate stated above and the Amiga view with transparency must be
 * restored when the animation is over.
 * To restore current Amiga View, set endView = GfxBase->ActiView;
 */
UWORD  AVLoadViews( struct View *views, struct View *endView );

/* AVReset -- reset AVideo standard settings.
 * Amiga graphics are not tranparent. Doesn't clear AVideo frame buffer.
 */
UWORD AVReset( void );

/* AVClear -- feed in an overscan laced black screen to clear
 * the Avideo frame buffer.
 */
UWORD AVClear( void );

/* AVError-- given an error number returned by an AVideo
 * library function, fill in the corresponding error
 * message into the supplied string buffer 'buf'.
 */
UWORD AVError( UWORD error,  char *buf );

/* AVFade -- if (speed==0 || speed==1) instant fade out.
 * 			 if (speed==-1) instant fade in.
 *           if (speed>1) fade out in 'speed' steps of 1/50th sec.
 *           if (speed<-1) fade in  in 'speed' steps of 1/50th sec.
 */
UWORD AVFade( WORD speed );

/* AVRecord -- record current view into frame buffer
 * if (bank == 0x1) record RED bank.
 * if (bank == 0x2) record GREEN bank.
 * if (bank == 0x3) record BLUE bank.
 * if (bank == 0x7) record all three banks.
 * Presently implemented on AVideo12 only.
 */
UWORD AVRecord( WORD  bank );

/* AVSwitch -- Switch or select which 12-bit buffer to display (AVideo24 only).
 * if (buff==-1) switch active buffer.
 * if (buff==0 || buff==1) select given buffer.
 */
UWORD AVSwitch(WORD buff);

/* AVSetViews -- AVideo12: Given a 12-bit AVideo bitmap with all 12 planes
 * stored in the plane array, 'planes', pointed to by 'loadBm->Planes[7]',
 * will set up an array of three Amiga Views, 'views', to be
 * loaded into AVideo frame buffer using AVLoadViews().
 * 'vpModes' contains the proper ViewPort modes.
 * 'colMap' is the picture color map; for 12-bit and beyond
 * 'colMap' contains a grey scale color map initialized as follows:
 *	   for (i=0;  i<16;  i++)
 *	      colMap[i] = (i<<8) | (i<<4) | i;
 *
 * Things are trickier for AVideo24. First, the dimension of the array, 'views',
 * must be 6 (AVLoadViews() actually can set up 6 views with 4 planes, 6*4 = 24 planes).
 * Second, the planes array 'planes' (planes = loadBm->Planes[7]) must be presented
 * with the order if planes given by the array, 'planeLst[]' bellow, to be loaded
 * later in the correct order by AVLoadViews(). All planes should reside in CHIP memory.
 * Third, to set only 3 views out of 6, as needed by AVLoadViews(), planes[3*4] up to
 * planes[6*4] should be set to zero: AVSetViews() stops building up views as soon as
 * a NULL plane is encountered.
 *
 * AVideo24 plane order for 12-bit loading :
 * static UWORD planeLst12[24+4] = { 2, 11,7, 3,   5, 1, 10,6,   8, 4, 0, 9, };
 *                     12-bit order  R1 B0 G0 R0   G2 R2 B1 G1   B3 G3 R3 B2
 */
UWORD AVSetViews( struct BitMap *loadBm, UWORD vpModes, UWORD *colorTab, struct View  *views );

/* AVResetViews -- clear 'views' array after use (views[3] for AVideo12 and
 * Views[6] for AVideo24). NULL views are tolerated.
 * Free all copper lists and ColorMaps allocated by AVSetViews().
 */
UWORD AVResetViews( struct View *views );

/*** AVideo IFF ILMBM load functions ***/

/* AVLoadPict -- load an AVIDEO 12-bit or an IFF 24-bit image into
 * given bitmap, bm.
 * 'name' is the picture ILBM file name.
 * Partial loading of bitmap planes is possible if
 * 'planeMask' bits are set for planes NOT to be loaded.
 * If the image is 12-bit and beyond (15,18,21 and 24-bit),
 * the bitmap planes are stored in an array pointed to
 * by bm->Planes[7].
 * With AVIDEO12, 12 planes at most are loaded. With AVIDEO24, images up to
 * 12-bit (inclusive) are completely loaded, only 12 planes are loaded for
 * deeper images but the loader should behave well for standard deep ILBM images.
 */
UWORD AVLoadPict( char *name, struct BitMap *bm,  LONG planeMask );

/* AVPicModes -- return loaded picture view modes
 */
UWORD AVPicModes( VOID );

/* AVPicColors -- return loaded picture color table
 */
UWORD *AVPicColors( VOID );

/* AVFreeBm -- free allocated bitmap planes : non null planes are freed.
 * CAUTION: this routine also attempts to free the bitmap planes
 * stored in the plane array pointed to by bm->Planes[].
 * CAUTION : with AVideo12, the plane array dimension is (12+4).
 * CAUTION : with AVideo24, the plane array dimension is (24+4).
 */
UWORD AVFreeBm( struct BitMap *bm );

/* AVType -- return AVIDEO12 flag (0x80) for 12-bit version of
 * 'avideo.library' and AVIDEO24 flag (0x40) for 24-bit version
 */
UWORD AVType( void );


/* AVLace -- set/unset interlace mode
 *	Set interlaced mode on a non interlaced Amiga view.
 *	The command AVlace may be used to force a non-interlced
 *	view to be interlaced (AVLace 1) and to recover the non-
 *	interlaced state (AVLace 0). An interlaced Amiga view
 *	(e.g. the Workbench) cannot be set non interlaced.
 * AVLace clears the buffer when turned on. 
 */
UWORD  AVLace( BOOL on );

#endif

