/*
 * Video driver for PXA27x(Bulverde) LCD Controller (bvdd)
 * by AGAWA Koji <i (AT) atty (DOT) jp>
 * (C) 2004
 */
/* English in this source code is written by machine translation.
   Meaning also not leading, permitting. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/fcntl.h>

#include "config.h"
#include "video_out.h"
#include "video_out_internal.h"
#include "sub.h"
#include "aspect.h"
#include "mp_msg.h"

#include "/home/atty/zaurus/bvdd/bvdd.h"

#define UNUSED(v) ((void)(v))

static vo_info_t info = {
    "PXA27x(Bulverde) LCD Controller",
    "bvdd",
    "AGAWA Koji <i (AT) atty (DOT) jp>",
    "for Sharp Linux Zaurus SL-C3000"
};

LIBVO_EXTERN(bvdd);

// ----------------------------------------------------------------
#define MAX_FRAMES 3
typedef struct vidix_yuv_s
{
	unsigned y,u,v;
}vidix_yuv_t;

typedef struct vidix_rect_s
{
	unsigned x,y,w,h;	/* in pixels */
	vidix_yuv_t pitch;	/* line-align in bytes */
}vidix_rect_t;

typedef struct w100_yuv_planes_s {
	uint8_t *y;
	uint8_t *u;
	uint8_t *v;
} yuv_planes_t;

typedef struct yuv_plane_offsets_s {
	unsigned int y;
	unsigned int u;
	unsigned int v;
} yuv_plane_offsets_t;

typedef struct w100_priv_s {
    uint32_t format;

    /* w100 info */
    int vram_size[2];		/*  */
    void *vram_addr[2];		/* address */
    yuv_planes_t frame_addrs[MAX_FRAMES];
    yuv_planes_t frame_offsets[MAX_FRAMES];
    int is_graphic_window_enabled;
    int eq_brightness;		/* for mplayer */
    int display_brightness;	/* for w100 */

    /* overlay info */
    uint16_t overlay_handle;
    int overlay_pos_x;
    int overlay_pos_y;
    int overlay_expand_h;
    int overlay_expand_v;
    int overlay_pitch_y;
    int overlay_pitch_u;
    int overlay_pitch_v;
} w100_priv_t;

static int st_bvdd_fd;
static uint32_t st_format;
static int st_src_width;
static int st_src_height;
static int st_current_frame;	/* current frame to display */
static yuv_planes_t st_frame_addrs[MAX_FRAMES];
static yuv_plane_offsets_t st_frame_offsets[MAX_FRAMES];
static vidix_yuv_t dstrides;
static bvdd_frame_t st_bvdd_frame;
static int st_overlay_pos_x;
static int st_overlay_pos_y;
static int st_overlay_pitch_y;
static int st_overlay_pitch_u;
static int st_overlay_pitch_v;

static void draw_alpha(int x0,int y0, int w,int h,
		       unsigned char* src, unsigned char *srca, int stride)
{
#if 1
    uint8_t *psrc, *psrca, *pdst;
    pdst = st_frame_addrs[st_current_frame].y;
    pdst += (x0 * dstrides.y) + (dstrides.y - 1 - y0);
    psrc = src;
    psrca = srca;
    while (h--) {
	int j;
	for (j = 0; j < w; ++j) {
	    if (psrca[j])
		pdst[j * dstrides.y] =
		    ((pdst[j * dstrides.y] * psrca[j]) >> 8) + psrc[j];
	}
	psrc += stride;
	psrca += stride;
	pdst -= 1;
    }
#else
    uint32_t apitch, bespitch;
    void *lvo_mem;
    lvo_mem = st_frame_addrs[st_current_frame].y;
    apitch = st_overlay_pitch_y - 1;
    switch (st_format) {
    case IMGFMT_YV12:
    case IMGFMT_IYUV:
    case IMGFMT_I420:
    case IMGFMT_YVU9:
    case IMGFMT_IF09:
    case IMGFMT_Y8:
    case IMGFMT_Y800:
	bespitch = (st_src_width + apitch) & (~apitch);
	vo_draw_alpha_yv12(w,h,src,srca,stride,lvo_mem+bespitch*y0+x0,bespitch);
	break;
#if 0
    case IMGFMT_YUY2:
	bespitch = (priv->src_width*2 + apitch) & (~apitch);
	vo_draw_alpha_yuy2(w,h,src,srca,stride,lvo_mem+bespitch*y0+2*x0,bespitch);
	break;
    case IMGFMT_UYVY:
	bespitch = (priv->src_width*2 + apitch) & (~apitch);
	vo_draw_alpha_yuy2(w,h,src,srca,stride,lvo_mem+bespitch*y0+2*x0+1,bespitch);
	break;
    case IMGFMT_RGB32:
    case IMGFMT_BGR32:
	bespitch = (priv->src_width*4 + apitch) & (~apitch);
	vo_draw_alpha_rgb32(w,h,src,srca,stride,lvo_mem+y0*bespitch+4*x0,bespitch);
	break;
    case IMGFMT_RGB24:
    case IMGFMT_BGR24:
	bespitch = (priv->src_width*3 + apitch) & (~apitch);
	vo_draw_alpha_rgb24(w,h,src,srca,stride,lvo_mem+y0*bespitch+3*x0,bespitch);
	break;
    case IMGFMT_RGB16:
    case IMGFMT_BGR16:
	bespitch = (priv->src_width*2 + apitch) & (~apitch);
	vo_draw_alpha_rgb16(w,h,src,srca,stride,lvo_mem+y0*bespitch+2*x0,bespitch);
	break;
    case IMGFMT_RGB15:
    case IMGFMT_BGR15:
	bespitch = (priv->src_width*2 + apitch) & (~apitch);
	vo_draw_alpha_rgb15(w,h,src,srca,stride,lvo_mem+y0*bespitch+2*x0,bespitch);
	break;
#endif
    default:
	return;
    }
#endif
}

/*
RL_XQ_480x640_1500_128.avi
image: 0x401f1e40 0x4024e3c0 0x4023b7c0 stride: 0x1e0 0xf0 0xf0 w:480 h: 16 x:  0 y: 16
image: 0x401f3c40 0x4024eb40 0x4023bf40 stride: 0x1e0 0xf0 0xf0 w:480 h: 16 x:  0 y: 32
 */
static uint32_t bvdd_draw_slice_420(uint8_t *image[], int stride[],
				     int w, int h, int x, int y)
{
#if 1
    uint8_t *src;
    uint8_t *dest;
    int i;

    mp_msg(MSGT_VO, MSGL_V, "vo_bvdd: bvdd_draw_slice_420: frame=%d\n", st_current_frame);
    /* Plane Y */
    dest = st_frame_addrs[st_current_frame].y;
    dest += dstrides.y * y + x;
    src = image[0];
#if 0
    __asm__ __volatile__ (
			  " \n\t"
			 "1: \n\t"
			  "mov r0, %[w] \n\t"
			 "2: \n\t"
			  "wldrd wr0, [ %[src] ] \n\t"
			  "pld [%[src], #32] \n\t"
			  "wldrd wr1, [ %[src], #8] \n\t"
			  "subs r0, r0, #1 \n\t"
			  "wldrd wr2, [ %[src], #16] \n\t"
			  "wldrd wr3, [ %[src], #24] \n\t"
			  "add %[src], %[src], #32 \n\t"
			  "wstrd wr0, [ %[dest]] \n\t"
			  "wstrd wr1, [ %[dest], #8] \n\t"
			  "wstrd wr2, [ %[dest], #16] \n\t"
			  "wstrd wr3, [ %[dest], #24] \n\t"
			  "add %[dest], %[dest], #32 \n\t"
			  "bne 2b \n\t"
			  "add %[src], %[src], %[srcdiff] \n\t"
			  "add %[dest], %[dest], %[destdiff] \n\t"
			  "subs %[h], %[h], #1 \n\t"
			  "bne 1b \n\t"
			  " \n\t"
			  " \n\t"
			  " \n\t"
			  " \n\t"
			  :
			  : [src]"r"(src), [dest]"r"(dest), [h]"r"(h), [w]"r"(w / 32),
			  [srcdiff]"r"(stride[0] - ((w >> 5) << 5)), [destdiff]"r"(dstrides.y - ((w >> 5) << 5))
			  : "memory", "r0");
#else
    for (i = 0; i < h; ++i) {
	memcpy(dest, src, w);
	src += stride[0];
	dest += dstrides.y;
    }
#endif

    /* Plane V */
    dest = st_frame_addrs[st_current_frame].u;
    dest += (dstrides.y / 2) * (y / 2) + x;
    src = image[1];
    for (i = 0; i < h / 2; ++i) {
	memcpy(dest, src, w / 2);
	src += stride[1];
	dest += dstrides.y / 2;
    }

    /* Plane U */
    dest = st_frame_addrs[st_current_frame].v;
    dest += (dstrides.y / 2) * (y / 2) + x;
/*     dest += dstrides.u * y / 4 + x; */
    src = image[2];
    for (i = 0; i < h / 2; ++i) {
	memcpy(dest, src, w / 2);
	src += stride[2];
	dest += dstrides.y / 2;
    }

#endif
    return 0;
}

#if 0
static uint32_t bvdd_draw_slice_420(uint8_t *image[], int stride[],
				     int w, int h, int x, int y)
{
    uint8_t *src;
    uint8_t *dest;
    int i;

    /* Plane Y */
    dest = st_frame_addrs[st_current_frame].y;
    dest += dstrides.y * y + x;
    src = image[0];
    for (i = 0; i < h; ++i) {
	memcpy(dest, src, w);
	src += stride[0];
	dest += dstrides.y;
    }

    /* Plane V */
    dest = st_frame_addrs[st_current_frame].u;
    dest += dstrides.v * y / 4 + x;
    src = image[1];
    for (i = 0; i < h / 2; ++i) {
	memcpy(dest, src, w / 2);
	src += stride[1];
	dest += dstrides.v / 2;
    }

    /* Plane U */
    dest = st_frame_addrs[st_current_frame].v;
    dest += dstrides.u * y / 4 + x;
    src = image[2];
    for (i = 0; i < h / 2; ++i) {
	memcpy(dest, src, w / 2);
	src += stride[2];
	dest += dstrides.u / 2;
    }

    return 0;
}
#endif

static uint32_t bvdd_draw_slice_packed(uint8_t *image[], int stride[],
					int w, int h, int x, int y)
{
#if 0
    uint8_t *src;
    uint8_t *dest;
    int i;

    dest = st_w100_mem + vidix_play.offsets[st_next_frame] + vidix_play.offset.y;
    dest += dstrides.y * y + x;
    src = image[0];
    for (i = 0; i < h; ++i) {
	memcpy(dest, src, w * st_image_bpp);
	src += stride[0];
	dest += dstrides.y;
    }
#endif
    return 0;
}

static uint32_t bvdd_get_image(mp_image_t *mpi)
{
#if 0
    mp_msg(MSGT_VO, MSGL_V, "vo_w100: w100_get_image called.\n");

    if (mpi->type == MP_IMGTYPE_STATIC && st_num_frames > 1)
	return VO_FALSE;
    if (mpi->flags & MP_IMGFLAG_READABLE)
	return VO_FALSE; /* slow video ram */
    if (((mpi->stride[0] == dstrides.y &&
	  (!(mpi->flags & MP_IMGFLAG_PLANAR) ||
	   (mpi->stride[1] == dstrides.u && mpi->stride[2]==dstrides.v)))
	 || (mpi->flags & (MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_ACCEPT_WIDTH))) &&
	(!(vidix_play.flags & VID_PLAY_INTERLEAVED_UV))) {
	if (mpi->flags & MP_IMGFLAG_ACCEPT_WIDTH) {
	    // check if only width is enough to represent strides:
	    if (mpi->flags & MP_IMGFLAG_PLANAR) {
		if ((dstrides.y >> 1) != dstrides.v || dstrides.v != dstrides.u)
		    return VO_FALSE;
	    } else {
		if (dstrides.y % (mpi->bpp / 8))
		    return VO_FALSE;
	    }
	}
	mpi->planes[0] = st_w100_mem + vidix_play.offsets[st_next_frame]
	    + vidix_play.offset.y;
	mpi->width = mpi->stride[0] = dstrides.y;
	if (mpi->flags & MP_IMGFLAG_PLANAR) {
	    mpi->planes[1] = st_w100_mem + vidix_play.offsets[st_next_frame]
		+ vidix_play.offset.v;
	    mpi->stride[1] = dstrides.v >> mpi->chroma_x_shift;
	    mpi->planes[2] = st_w100_mem + vidix_play.offsets[st_next_frame]
		+ vidix_play.offset.u;
	    mpi->stride[2] = dstrides.u >> mpi->chroma_x_shift;
	} else
	    mpi->width /= mpi->bpp / 8;
	mpi->flags |= MP_IMGFLAG_DIRECT;
	return VO_TRUE;
    }
#endif
    return VO_FALSE;
}

// ---------------------------------------------------------------- interfaces
/*
 * Preinitializes driver (real INITIALIZATION)
 *   arg - currently it's vo_subdevice
 *   returns: zero on successful initialization, non-zero on error.
 */
static uint32_t preinit(const char *vo_subdevice)
{
    UNUSED(vo_subdevice);
    const char *dev = "/home/zaurus/.dev_bvdd";

    mp_msg(MSGT_VO, MSGL_DBG2, "vo_bvdd: preinit() was called\n");

    st_bvdd_fd = open(dev, O_RDWR); /* TODO: check flags */
    if (st_bvdd_fd < 0) {
	mp_msg(MSGT_VO, MSGL_FATAL, "vo_bvdd: couldn't open %s\n", dev);
	return -1;
    }

    ioctl(st_bvdd_fd, BVDD_IOCTL_SET_IWMMXT_STATE, 1);

    return 0;
}

static unsigned int read_cccr()
{
    FILE *fp;
    char buf[11];
    unsigned int cccr;

    fp = fopen("/proc/zaurus/CCCR", "r");
    if (!fp)
	return 0;

    if(fread(buf, 1, 10, fp) != 10) {
	fclose(fp);
	return 0;
    }
    fclose(fp);

    buf[10] = '\0';
    sscanf(buf, "0x%x", &cccr);
    printf("******** %p\n", cccr);

    return cccr;
}

static unsigned int write_cccr(unsigned int cccr)
{
    FILE *fp;
    char buf[9];

    fp = fopen("/proc/zaurus/CCCR", "w");
    if (!fp)
	return 0;

    snprintf(buf, 9, "%08x", cccr);
    printf("write_cccr <%s>\n", buf);
    fputs(buf, fp);
    fclose(fp);

    return 1;
}

/*
 * Initialize (means CONFIGURE) the display driver.
 * params:
 *   src_width,srcheight: image source size
 *   dst_width,dst_height: size of the requested window size, just a hint
 *   fullscreen: flag, 0=windowd 1=fullscreen, just a hint
 *   title: window title, if available
 *   format: fourcc of pixel format
 * returns : zero on successful initialization, non-zero on error.
 */
static uint32_t config(uint32_t src_width, uint32_t src_height,
		       uint32_t dst_width, uint32_t dst_height, uint32_t flags,
		       char *title, uint32_t format)
{
    int fs = flags & VOFLAG_FULLSCREEN;
    int vm = flags & VOFLAG_MODESWITCHING;
    int zoom = flags & VOFLAG_SWSCALE;
    int y_pitch, uv_pitch;
    int x_res = 480, y_res = 640;
    uint32_t apitch;
    int vram_size;
    int i;

    mp_msg(MSGT_VO, MSGL_DBG2, "vo_bvdd: config() was called\n");
    mp_msg(MSGT_VO, MSGL_V, "vo_bvdd: src_width:%d, src_height:%d, dst_width:%d, dst_height:%d\n",
	   src_width, src_height, dst_width, dst_height);

    if (!query_format(format)) {
	printf("vo_bvdd: unsupported fourcc for this w100 driver: %x (%s)\n",
	       format, vo_format_name(format));
	return -1;
    }
    st_format = format;

    // フルスクリーン
    aspect_save_orig(src_width, src_height);
    aspect_save_prescale(dst_width, dst_height);
    aspect_save_screenres(x_res, y_res);

    // 表示領域をセンタリング
    st_overlay_pos_x = (x_res - dst_width) / 2;
    st_overlay_pos_y = (y_res - dst_height) / 2;

    // Hardware scaling
    geometry(&st_overlay_pos_x, &st_overlay_pos_y,
	     &dst_width, &dst_height, x_res, y_res);
/*     aspect(&dst_width, &dst_height, A_NOZOOM); */
    mp_msg(MSGT_VO, MSGL_V, "vo_w100: overlay pos(%d, %d)\n",
	   st_overlay_pos_x, st_overlay_pos_y);
    mp_msg(MSGT_VO, MSGL_V, "vo_w100: src size(%dx%d), dst size(%dx%d)\n",
	   src_width, src_height, dst_width, dst_height);

    st_src_width = src_width;
    st_src_height = src_height;

    switch (format) {
    case IMGFMT_YV12:
    case IMGFMT_IYUV:
    case IMGFMT_I420:
    case IMGFMT_YVU9:
    case IMGFMT_IF09:
    case IMGFMT_Y8:
    case IMGFMT_Y800:
	y_pitch = (src_width + 15) & ~15;
	uv_pitch = y_pitch / 2;
	break;
    default:
	return -1;
    }

    vram_size = (y_pitch * src_height) + (uv_pitch * src_height);
    if (vo_doublebuffering) {
	vram_size *= 3;
    }
    mp_msg(MSGT_VO, MSGL_INFO, "vo_bvdd: BVDD_IOCTL_SETUP_VRAM %d\n", vram_size);
    if (ioctl(st_bvdd_fd, BVDD_IOCTL_SETUP_VRAM, vram_size) < 0) {
	mp_msg(MSGT_VO, MSGL_FATAL, "vo_bvdd: BVDD_IOCTL_SETUP_VRAM error\n");
	return -1;
    }
    {
	uint8_t *mapped;
	mapped = mmap(0, vram_size, PROT_READ|PROT_WRITE, MAP_SHARED, st_bvdd_fd, BVDD_VRAM_OFFSET_USER);
	if (mapped == MAP_FAILED) {
	    mp_msg(MSGT_VO, MSGL_FATAL, "vo_bvdd: mmap failed.\n");
	    return -1;
	}
	st_frame_addrs[0].y = mapped;
	st_frame_addrs[0].u = st_frame_addrs[0].y + (y_pitch * src_height);
	st_frame_addrs[0].v = st_frame_addrs[0].u + (uv_pitch * (src_height / 2));
	st_frame_offsets[0].y = BVDD_VRAM_OFFSET_USER;
	st_frame_offsets[0].u = st_frame_offsets[0].y + (y_pitch * src_height);
	st_frame_offsets[0].v = st_frame_offsets[0].u + (uv_pitch * (src_height / 2));
	if (vo_doublebuffering) {
	    st_frame_addrs[1].y = st_frame_addrs[0].v + (uv_pitch * (src_height / 2));
	    st_frame_addrs[1].u = st_frame_addrs[1].y + (y_pitch * src_height);
	    st_frame_addrs[1].v = st_frame_addrs[1].u + (uv_pitch * (src_height / 2));
	    st_frame_offsets[1].y = st_frame_offsets[0].v + (uv_pitch * (src_height / 2));
	    st_frame_offsets[1].u = st_frame_offsets[1].y + (y_pitch * src_height);
	    st_frame_offsets[1].v = st_frame_offsets[1].u + (uv_pitch * (src_height / 2));
	    st_frame_addrs[2].y = st_frame_addrs[1].v + (uv_pitch * (src_height / 2));
	    st_frame_addrs[2].u = st_frame_addrs[2].y + (y_pitch * src_height);
	    st_frame_addrs[2].v = st_frame_addrs[2].u + (uv_pitch * (src_height / 2));
	    st_frame_offsets[2].y = st_frame_offsets[1].v + (uv_pitch * (src_height / 2));
	    st_frame_offsets[2].u = st_frame_offsets[2].y + (y_pitch * src_height);
	    st_frame_offsets[2].v = st_frame_offsets[2].u + (uv_pitch * (src_height / 2));
	}
    }

    /* select first frame */
    st_current_frame = vo_doublebuffering ? 1 : 0;

    st_bvdd_frame.x = st_overlay_pos_x;
    st_bvdd_frame.y = st_overlay_pos_y;
    st_bvdd_frame.width = dst_width;
    st_bvdd_frame.height = dst_height;
    st_bvdd_frame.format = BVDD_FORMAT_YUV420;
    st_bvdd_frame.offset = st_frame_offsets[st_current_frame].y;
    st_bvdd_frame.u_offset = st_frame_offsets[st_current_frame].u;
    st_bvdd_frame.v_offset = st_frame_offsets[st_current_frame].v;

    {
	unsigned int cccr_orig;
	cccr_orig = read_cccr();
	if (cccr_orig != 0 && cccr_orig != 0x2000210)
	    write_cccr(0x2000210);

	ioctl(st_bvdd_fd, BVDD_IOCTL_SETUP_OVERLAY2_FRAME, &st_bvdd_frame);
	if (cccr_orig != 0 && cccr_orig != 0x2000210)
	    write_cccr(cccr_orig);
    }

    /* clear every frame */
/*     memset(priv->vram_addr[INTERNAL_VRAM], 0, priv->vram_size[INTERNAL_VRAM]); */
/*     memset(priv->vram_addr[EXTERNAL_VRAM] + 640 * 480 * 2, 0, */
/* 	   priv->vram_size[EXTERNAL_VRAM] - 640 * 480 * 2); */

    switch (format) {
    case IMGFMT_YV12:
    case IMGFMT_I420:
    case IMGFMT_IYUV:
    case IMGFMT_YVU9:
    case IMGFMT_IF09:
    case IMGFMT_Y800:
    case IMGFMT_Y8:
	dstrides.y = y_pitch;
	dstrides.v = uv_pitch;
	dstrides.u = uv_pitch;
	break;
    case IMGFMT_RGB32:
    case IMGFMT_BGR32:
	apitch = st_overlay_pitch_y - 1;
	dstrides.y = (src_width * 4 + apitch) & ~apitch;
	dstrides.u = dstrides.v = 0;
/* 	st_image_bpp = 4; */
	break;
    case IMGFMT_RGB24:
    case IMGFMT_BGR24:
	apitch = st_overlay_pitch_y - 1;
	dstrides.y = (src_width * 3 + apitch) & ~apitch;
	dstrides.u = dstrides.v = 0;
/* 	st_image_bpp = 3; */
	break;
    default:
	apitch = st_overlay_pitch_y - 1;
	dstrides.y = (src_width * 2 + apitch) & ~apitch;
	dstrides.u = dstrides.v = 0;
/* 	st_image_bpp = 2; */
	break;
    }

    if (format == IMGFMT_YV12 || format == IMGFMT_I420 || format == IMGFMT_IYUV)
	video_out_bvdd.draw_slice = bvdd_draw_slice_420;
    /* 	else if (format == IMGFMT_YVU9 || format == IMGFMT_IF09) */
    /* 	    vo_server->draw_slice = w100_draw_slice_410; */
    else
	video_out_bvdd.draw_slice = bvdd_draw_slice_packed;

    return 0;
}

/*
 * Control interface
 */
static uint32_t control(uint32_t request, void *data, ...)
{
    switch (request) {
    case VOCTRL_GET_IMAGE:
	return bvdd_get_image(data);
    case VOCTRL_QUERY_FORMAT:
	return query_format(*((uint32_t *)data));
    }

    return VO_NOTIMPL;
}

/*
 * Display a new RGB/BGR frame of the video to the screen.
 * params:
 *   src[0] - pointer to the image
 */
uint32_t draw_frame(uint8_t *src[])
{
    mp_msg(MSGT_VO, MSGL_V, "vo_bvdd: dummy draw_frame() was called\n");
    return -1;
}

/*
 * Draw a planar YUV slice to the buffer:
 * params:
 *   src[3] = source image planes (Y,U,V)
 *   stride[3] = source image planes line widths (in bytes)
 *   w,h = width*height of area to be copied (in Y pixels)
 *   x,y = position at the destination image (in Y pixels)
 */
uint32_t draw_slice(uint8_t *src[], int stride[], int w,int h, int x,int y)
{
    mp_msg(MSGT_VO, MSGL_DBG2, "vo_bvdd: dummy draw_slice() was called\n");
    return -1;
}


/*
 * Draws OSD to the screen buffer
 */
static void draw_osd(void)
{
/*     mp_msg(MSGT_VO, MSGL_V, "vo_bvdd: draw_osd() was called\n"); */
    mp_msg(MSGT_VO, MSGL_V, "vo_bvdd: draw_osd: frame=%d\n", st_current_frame);
    vo_draw_text(st_src_height, st_src_width, draw_alpha);
}

/*
 * Blit/Flip buffer to the screen. Must be called after each frame!
 */
void flip_page(void)
{
/*     mp_msg(MSGT_VO, MSGL_V, "vo_bvdd: flip_page() was called\n"); */
    mp_msg(MSGT_VO, MSGL_V, "vo_bvdd: flip_page: frame=%d\n", st_current_frame);
    if (vo_doublebuffering) {
	st_bvdd_frame.offset = st_frame_offsets[st_current_frame].y;
	st_bvdd_frame.u_offset = st_frame_offsets[st_current_frame].u;
	st_bvdd_frame.v_offset = st_frame_offsets[st_current_frame].v;
	ioctl(st_bvdd_fd, BVDD_IOCTL_BRANCH_OVERLAY2_FRAME, &st_bvdd_frame);
	st_current_frame = (st_current_frame + 1) % 3;
    }
}

/*
 * This func is called after every frames to handle keyboard and
 * other events. It's called in PAUSE mode too!
 */
extern int g_sigcont;
void check_events(void)
{
#if 0
    w100_priv_t *priv = &st_w100_priv;

    if (g_sigcont) {
	mp_msg(MSGT_VO, MSGL_INFO, "vo_bvdd: SIGCONT recived.\n");

	/* Immediately after resuming, because kernel modifies the register, it
	   waits for that. */
	usleep(1000 * 500);

	/* re-attach */
#if 0
	/* Hmm... With respect to of context is necessary, but really it
	   fails. It does not release and also there is no problem. */
	if (!AtiCore_ReleaseOverlay(priv->overlay_handle)) {
	    mp_msg(MSGT_VO, MSGL_FATAL,
		   "vo_w100: AtiCore_ReleaseOverlay failed.\n");
	    exit_player(NULL);
	}
#endif
	if (!AtiCore_ProcessDetach()) {
	    mp_msg(MSGT_VO, MSGL_FATAL,
		   "vo_w100: AtiCore_ProcessDetach failed.\n");
	    exit_player(NULL);
	}
	if (!AtiCore_ProcessAttach()) {
	    mp_msg(MSGT_VO, MSGL_FATAL,
		   "vo_w100: AtiCore_ProcessAttach failed.\n");
	    exit_player(NULL);
	}

	/* re-setup */
	if (!w100_setup(priv))
	    exit_player(NULL);

	g_sigcont = 0;
    }
#endif
}

/*
 * Closes driver. Should restore the original state of the system.
 */
static void uninit(void)
{
    mp_msg(MSGT_VO, MSGL_V, "vo_bvdd: uninit() was called\n");

    ioctl(st_bvdd_fd, BVDD_IOCTL_SET_IWMMXT_STATE, 0);
    close(st_bvdd_fd);
}


// ----------------------------------------------------------------
static uint32_t query_format(uint32_t format)
{
    mp_msg(MSGT_VO, MSGL_V, "vo_bvdd: query_format was called: %x (%s)\n",
	   format, vo_format_name(format));

    if (IMGFMT_IS_RGB(format)) {
	/* RGB/BGR Formats */
	// TODO
	return 0;

	switch (IMGFMT_RGB_DEPTH(format)) {
	case 16:
	    return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_ACCEPT_STRIDE;
	    break;
	}
    } else {
	/* Planar YUV Formats */
	switch (format) {
	case IMGFMT_YV12:
	case IMGFMT_IYUV:
	case IMGFMT_I420:
	case IMGFMT_YVU9:
	case IMGFMT_IF09:
	case IMGFMT_Y8:
	case IMGFMT_Y800:
	    return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_ACCEPT_STRIDE;
	    break;
	}
    }

    return 0;
}
