[MPlayer-users] New tdfxfb driver

Mark Zealey mark at zealos.org
Sat Mar 30 22:39:01 CET 2002


Ok, I've spent the day working on a new tdfxfb driver. This will let you do
movies in 16, 24 or 32 bpp mode. It allows the video codec to use DRI (Might be
a small bug in there). OSD is slightly fucked, maybe someone wants to have a
look at that.

I'd appreciate any testers for this, reports, good or bad and I'll try to fix
it.

I have sent several bug reports to the maintainer recently, without reply. I
guess I'll be the maintainer of this new version if he doesn't talk to me...

Unfortunatly I'm off on holiday now, so you've got a week to find all the bugs
and maybe even send me patchen!

(File attached as diff would be about twice as big!)

TODO:
- Fix OSD
- Find DRI heissenbug
- Add YUY2 and UYVY support
- Clean up the driver a bit more

-- 

Mark Zealey (aka JALH on irc.openprojects.net: #zealos and many more)
mark at zealos.org; mark at itsolve.co.uk

UL++++>$ G!>(GCM/GCS/GS/GM) dpu? s:-@ a16! C++++>$ P++++>+++++$ L+++>+++++$
!E---? W+++>$ !w--- r++ !t---?@ !X---?  !R- !tv b+ G+++ e>+++++ !h++* r!-- y--
-------------- next part --------------
/* Copyright (C) Mark Zealey, 2002, <mark at zealos.org>
 *
 * 30/03/02: An almost total rewrite, added DRI support and support for modes
 * other than 16bpp.
 *
 * TODO:
 * - Fix OSD (It's position is different in 16 and 32 bit mode)
 * - Add YUV2 and UYVY support
 * - Remove some of the old dud code
 */

/*
 *    vo_tdfxfb.c
 *
 *  Copyright (C) Zeljko Stevanovic 2001, <zsteva at ptt.yu>
 *
 *  Most code rewrited, move from /dev/3dfx to /dev/fb0 (kernel 2.4.?)
 *  add support for YUY2 and BGR16 format, remove all X11 DGA code.
 *  - add support for hardware accelerated OSD (buggy for now).
 *    work on BGR16 and YUY2 (VO_3DFX_METHOD == 2 only)
 *  [oct2001]
 *  - added hardware acceleration for OSD (does not look nice, but is faster)
 *    (for YV12 don't fork.)
 *  - fixed YV12 support for ffdivx, but on my cpu this is sllower of yuv2rgb()
 *    try to uncommenting '#define YV12_CONV_METH'
 *  - fast_memcpy() is sllower of memcpy() (why, i don't know)
 *
 *
 *	Copyright (C) Colin Cross Apr 2000
 *
 *  This file heavily based off of vo_mga.c of Aaron Holtzman's
 *  mpeg2dec
 *
 *  mpeg2dec 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, or (at your option)
 *  any later version.
 *
 *  mpeg2dec 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 GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "config.h"
#include "video_out.h"
#include "video_out_internal.h"
#include "mp_image.h"

LIBVO_EXTERN(tdfxfb)

#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <wchar.h>
#include <signal.h>

#include <linux/fb.h>

extern int verbose;

#include "drivers/3dfx.h"

static vo_info_t vo_info =
{
	"tdfxfb (/dev/fb?)",
	"tdfxfb",
	"Zeljko Stevanovic <zsteva at ptt.yu>",
	""
};

/* Some registers on the card */
#define S2S_STRECH_BLT		2			// BLT + Strech
#define S2S_IMMED		(1 << 8)		// Do it immediatly
#define S2S_ROP			(0xCC << 24)		// ???


/* XXX: Get rid of most of these vars... */
static char *fb_devname;
static int fb_fd;
static struct fb_fix_screeninfo fb_finfo;
static struct fb_var_screeninfo fb_vinfo;

static uint32_t in_width, in_height, in_format, in_depth,
		in_banshee_format, in_banshee_size,
		screenwidth, screenheight, screendepth,
		vidwidth, vidheight, vidx, vidy,
		vid_banshee_xy, vid_banshee_format, vid_banshee_size,
		*vidpage0, *vidpage1, *in_page0, *iobase,
		vidpage0offset, vidpage1offset, in_page0_offset,
		*memBase0, *memBase1;

static voodoo_io_reg *reg_IO;
static voodoo_2d_reg *reg_2d;
static voodoo_yuv_reg *reg_YUV;
static voodoo_yuv_fb *fb_YUV;

static uint32_t preinit(const char *arg)
{
	if (!fb_devname && !(fb_devname = getenv("FRAMEBUFFER")))
		fb_devname = "/dev/fb0";

	if (verbose)
		printf("tdfxfb: fbdev ==> %s\n", fb_devname);

	if ((fb_fd = open(fb_devname, O_RDWR)) == -1) {
		printf("tdfxfb: can't open %s, %s\n", fb_devname, strerror(errno));
		return -1;
	}

	if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
		printf("tdfxfb: problem with FBITGET_FSCREENINFO ioctl: %s\n",
				strerror(errno));
		return -1;
	}

	if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
		printf("tdfxfb: problem with FBITGET_VSCREENINFO ioctl: %s\n",
				strerror(errno));
		return -1;
	}

	if (verbose) {
		printf("fb_finfo:\n");
		printf("  id: %s\n", fb_finfo.id);
		printf("  frame bufer at %x len %x (%d)\n", fb_finfo.smem_start,
				fb_finfo.smem_len, fb_finfo.smem_len);
		printf("  mem io      at %x len %x\n", fb_finfo.mmio_start,
				fb_finfo.mmio_len);

		printf("fb_vinfo:\n");
		printf("  resolution:  %dx%d\n", fb_vinfo.xres, fb_vinfo.yres);
		printf("  virtual res: %dx%d\n", fb_vinfo.xres_virtual,
				fb_vinfo.yres_virtual);
		printf("  virt offset: %dx%d\n", fb_vinfo.xoffset, fb_vinfo.yoffset);
	}

	if (fb_finfo.accel != FB_ACCEL_3DFX_BANSHEE)
		printf("tdfxfb: This driver is only known to support the banshee!\n");

	/* Open up a window to the hardware */
	memBase1 = mmap(0, fb_finfo.smem_len, PROT_READ | PROT_WRITE,
					MAP_SHARED, fb_fd, 0);
	memBase0 = mmap(0, fb_finfo.mmio_len, PROT_READ | PROT_WRITE,
					MAP_SHARED, fb_fd, fb_finfo.smem_len);

	if((long)memBase0 == -1 || (long)memBase1 == -1) {
		printf("Couldn't map 3dfx memory areas: %p, %p, %d\n",
				memBase0, memBase1, errno);
		return -1;
	}

	iobase = (void *)memBase0 + VOODOO_IO_REG_OFFSET;

	// Set up global pointers
	reg_IO  = (void *)memBase0 + VOODOO_IO_REG_OFFSET;
	reg_2d  = (void *)memBase0 + VOODOO_2D_REG_OFFSET;
	reg_YUV = (void *)memBase0 + VOODOO_YUV_REG_OFFSET;
	fb_YUV  = (void *)memBase0 + VOODOO_YUV_PLANE_OFFSET;

	atexit(uninit);

	return 0;
}

static void uninit(void)
{
	/* Restore the screen */
	reg_IO->vidDesktopStartAddr = vidpage0offset;

	/* And close our mess */
/*	munmap(memBase1, fb_finfo.smem_len);		// Causes a seg fault...
	munmap(memBase0, fb_finfo.mmio_len);
*/	close(fb_fd);
}

static uint32_t config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height,
		uint32_t fullscreen, char *title, uint32_t format,const vo_tune_info_t *info)
{
	screenwidth = fb_vinfo.xres;
	screenheight = fb_vinfo.yres;

	in_width = width;
	in_height = height;
	in_format = format;

	/* Setup the screen for rendering to */
	switch(fb_vinfo.bits_per_pixel) {
	case 16:
		screendepth = 2;
		vid_banshee_format = VOODOO_BLT_FORMAT_16;
		break;

	case 24:
		screendepth = 3;
		vid_banshee_format = VOODOO_BLT_FORMAT_24;
		break;

	case 32:
		screendepth = 4;
		vid_banshee_format = VOODOO_BLT_FORMAT_32;
		break;

	default:
		printf("tdfxfb: %d bpp output is not supported\n", fb_vinfo.bits_per_pixel);
		return -1;
	}

	vid_banshee_format |= screenwidth * screendepth;

	/* Arpi says we don't need to support RGB */
	switch(in_format) {
	case IMGFMT_YV12:
	case IMGFMT_I420:
	case IMGFMT_IYUV:
		in_banshee_format = VOODOO_BLT_FORMAT_YUYV;
		in_depth = screendepth;			// XXX: Wierd...
		break;

	default:
		printf("tdfxfb: Eik! Something's wrong with control().\n");
		return -1;
	}

	in_banshee_format |= in_width * in_depth;

	/* XXX: Fix up this */
	if (fullscreen) {
		double exrat;

		if (verbose)
			printf("tdfxfb: fullscreen mode...\n");

		vidwidth = screenwidth;
		vidheight = screenheight;

		exrat = (double)in_width / in_height;
		if(verbose)
			printf("tdfxfb: in_width / in_height = %f\n", exrat);

		if(screenwidth / exrat <= screenheight)
			vidheight = (double)screenwidth / exrat;
		else
			vidwidth = (double)screenheight * exrat;

		vidx = (screenwidth - vidwidth) / 2;
		vidy = (screenheight - vidheight) / 2;

		if(verbose)
			printf("vidwidth = %d, vidheight = %d, vidx = %d, vidy = %d\n",
					vidwidth, vidheight, vidx, vidy);
	} else {
		if (in_width > screenwidth || in_height > screenheight) {
			printf("tdfxfb: your resolution is too small to play the movie...\n");
			return -1;
		} else {
			vidwidth = in_width;
			vidheight = in_height;
		}
	}

	vidpage0offset = 0;
	vidpage1offset = screenwidth * screenheight * screendepth;
	in_page0_offset = vidpage1offset + screenwidth * screenheight * screendepth;

	vidpage0 = (void *)memBase1 + (unsigned long)vidpage0offset;
	vidpage1 = (void *)memBase1 + (unsigned long)vidpage1offset;
	in_page0 = (void *)memBase1 + (unsigned long)in_page0_offset;

	vid_banshee_xy = XYREG(vidx, vidy);
	vid_banshee_size = XYREG(vidwidth, vidheight);

	in_banshee_size = XYREG(in_width, in_height);

/*	memset(vidpage0, 0, screenwidth * screenheight * screendepth);
	memset(vidpage1, 0, screenwidth * screenheight * screendepth);
*/	memset(in_page0, 0, in_width * in_height * in_depth);

	reg_IO->vidDesktopStartAddr = vidpage0offset;

	printf("tdfxfb: screen is %dx%d at %d bpp, in is %dx%d at %d bpp (%p/%p)\n",
			screenwidth, screenheight, screendepth * 8,
			in_width, in_height, in_depth * 8,
			memBase0, memBase1);

	/* fd is not closed. */

	return 0;
}

static const vo_info_t* get_info(void)
{
	return &vo_info;
}

/* Render onto the screen */
static void flip_page(void)
{
	int i = 0;
	voodoo_2d_reg regs = *reg_2d;		/* Copy the regs */

	reg_2d->commandExtra = 0;
	reg_2d->clip0Min = 0;
	reg_2d->clip0Max = 0xffffffff;
	reg_2d->srcBaseAddr = in_page0_offset;
	reg_2d->srcXY = 0;
	reg_2d->srcFormat = in_banshee_format;
	reg_2d->srcSize = in_banshee_size;
	reg_2d->dstBaseAddr = vidpage0offset;
	reg_2d->dstXY = vid_banshee_xy;
	reg_2d->dstFormat = vid_banshee_format;
	reg_2d->dstSize = vid_banshee_size;
	reg_2d->command = S2S_STRECH_BLT | S2S_IMMED | S2S_ROP;

	while((*((volatile uint32_t *)(iobase + STATUS)) & 0x1f) < 1);
	*((volatile uint32_t *)(iobase + COMMAND_3D)) = COMMAND_3D_NOP;

	while(i < 3)
		if(!(*((volatile uint32_t *)(iobase + STATUS)) & STATUS_BUSY))
			i++;

	reg_2d->commandExtra = regs.commandExtra;
	reg_2d->clip0Min = regs.clip0Min;
	reg_2d->clip0Max = regs.clip0Max;

	reg_2d->srcBaseAddr = regs.srcBaseAddr;
	reg_2d->srcXY = regs.srcXY;
	reg_2d->srcFormat = regs.srcFormat;
	reg_2d->srcSize = regs.srcSize;

	reg_2d->dstBaseAddr = regs.dstBaseAddr;
	reg_2d->dstXY = regs.dstXY;
	reg_2d->dstFormat = regs.dstFormat;
	reg_2d->dstSize = regs.dstSize;

	reg_2d->command = 0;
}

/* Only YV12 will call here */
static uint32_t draw_slice(uint8_t *image[], int stride[], int w, int h,
		int x, int y)
{
	void *i_y = image[0], *i_u = image[1], *i_v = image[2];
	uint32_t *Y, *U, *V, j;

	switch(in_format) {
	case IMGFMT_YV12:		/* The normal case */
		Y = fb_YUV->Y;
		U = fb_YUV->U;
		V = fb_YUV->V;
		break;

	case IMGFMT_I420:		/* Same as above but swapped */
	case IMGFMT_IYUV:
		Y = fb_YUV->Y;
		U = fb_YUV->V;
		V = fb_YUV->U;
		break;

	/* No need for a default case, config would have handled it */
	}

	reg_YUV->yuvBaseAddr = in_page0_offset + w * screendepth * y;
	reg_YUV->yuvStride = w * screendepth;

	for (j = 0; j < h >> 1; j++) {
		memcpy(Y, i_y, stride[0]);
		Y += VOODOO_YUV_STRIDE; i_y += stride[0];
		memcpy(Y, i_y, stride[0]);
		Y += VOODOO_YUV_STRIDE; i_y += stride[0];

		memcpy(U, i_u, stride[1]);
		memcpy(V, i_v, stride[2]);
		U += VOODOO_YUV_STRIDE; i_u += stride[1];
		V += VOODOO_YUV_STRIDE; i_v += stride[2];
	}

	return 0;
}

static void draw_alpha(int x, int y, int w, int h, unsigned char *src,
		unsigned char *srca, int stride)
{
	char *dst = (char *)in_page0 + (in_width * y + x);
	vo_draw_alpha_yv12(w, h, src, srca, stride, dst, in_width);
}

static void draw_osd(void)
{
	vo_draw_text(in_width, in_height, draw_alpha);
}

/* Attempt to start doing DRI (Copied mostly from mga_common.c) */
static uint32_t get_image(mp_image_t *mpi)
{
	static int enabled = 0;

	if(enabled == 0) {
		if(mpi->flags & MP_IMGFLAG_READABLE)
			return VO_FALSE;		/* slow video ram */

		if(!(mpi->flags & (MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_ACCEPT_WIDTH)))
			return VO_FALSE;

		printf("tdfxfb: get_image() SUCCESS -> Direct Rendering ENABLED\n");
	}

	enabled = 1;
	mpi->planes[0] = (char *)fb_YUV->Y;
	mpi->planes[1] = (char *)fb_YUV->U;
	mpi->planes[2] = (char *)fb_YUV->V;

	mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = VOODOO_YUV_STRIDE * 4;

	mpi->flags |= MP_IMGFLAG_DIRECT;

	return VO_TRUE;
}

static uint32_t query_format(uint32_t format)
{
	switch(format) {
	case IMGFMT_YV12:
	case IMGFMT_I420:
	case IMGFMT_IYUV:
		return 7;	/* Supported without conversion, supports OSD */
	}

	return 0;		/* Not supported */
}

static uint32_t control(uint32_t request, void *data, ...)
{
	switch (request) {
	case VOCTRL_QUERY_FORMAT:
		return query_format(*((uint32_t*)data));

	case VOCTRL_GET_IMAGE:
		return get_image(data);
	}

	return VO_NOTIMPL;
}

/* Dummy funcs */
static uint32_t draw_frame(uint8_t *src[]) { return 0; }
static void check_events(void) { }


More information about the MPlayer-users mailing list