Fix crash during early exit
[mplayer.git] / libvo / vo_tdfxfb.c
blobfb825c37f3de4bcc41065ad723f0386a07b3c705
1 /* Copyright (C) Mark Zealey, 2002, <mark@zealos.org>. Released under the terms
2 * and conditions of the GPL.
4 * 30/03/02: An almost total rewrite, added DR support and support for modes
5 * other than 16bpp. Fixed the crash when playing multiple files
6 * 07/04/02: Fixed DR support, added YUY2 support, fixed OSD stuff.
7 * 08/04/02: Fixed a wierd sound corruption problem caused by some optomizations
8 * I made.
9 * 09/04/02: Fixed a problem with changing the variables passed to draw_slice().
10 * Fixed DR support for YV12 et al. Added BGR support. Removed lots of dud code.
11 * 10/04/02: Changed the memcpy functions to mem2agpcpy.. should be a tad
12 * faster.
13 * 11/04/02: Added a compile option so you can watch the film with the console
14 * as the background, or not.
15 * 13/04/02: Fix rough OSD stuff by rendering it straight onto the output
16 * buffer. Added double-buffering. Supports hardware zoom/reduce zoom modes.
17 * 13/04/02: Misc cleanups of the code.
18 * 22/10/02: Added geometry support to it
20 * Hints and tricks:
21 * - Use -dr to get direct rendering
22 * - Use -vf yuy2 to get yuy2 rendering, *MUCH* faster than yv12
23 * - To get a black background and nice smooth OSD, use -double
24 * - To get the console as a background, but with scaled OSD, use -nodouble
25 * - The driver supports both scaling and shrinking the image using the -x and
26 * -y options on the mplayer commandline. Also repositioning via the -geometry
27 * option.
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/ioctl.h>
36 #include <fcntl.h>
37 #include <sys/mman.h>
38 #include <linux/fb.h>
40 #include "config.h"
41 #include "mp_msg.h"
42 #include "help_mp.h"
43 #include "fastmemcpy.h"
44 #include "video_out.h"
45 #include "video_out_internal.h"
46 #include "drivers/3dfx.h"
47 #include "aspect.h"
48 #include "sub.h"
50 static const vo_info_t info =
52 "3Dfx Banshee/Voodoo3/Voodoo5",
53 "tdfxfb",
54 "Mark Zealey <mark@zealos.org>",
58 const LIBVO_EXTERN(tdfxfb)
60 /* Some registers on the card */
61 #define S2S_STRECH_BLT 2 // BLT + Strech
62 #define S2S_IMMED (1 << 8) // Do it immediatly
63 #define S2S_ROP (0xCC << 24) // ???
65 /* Stepping between the different YUV plane registers */
66 #define YUV_STRIDE 1024
67 struct YUV_plane {
68 char Y[0x0100000];
69 char U[0x0100000];
70 char V[0x0100000];
73 static int fd = -1;
74 static struct fb_fix_screeninfo fb_finfo;
75 static struct fb_var_screeninfo fb_vinfo;
76 static uint32_t in_width, in_height, in_format, in_depth, in_voodoo_format,
77 screenwidth, screenheight, screendepth, vidwidth, vidheight, vidx, vidy,
78 vid_voodoo_format, *vidpage, *hidpage, *inpage, vidpageoffset,
79 hidpageoffset, inpageoffset, *memBase0 = NULL, *memBase1 = NULL, r_width, r_height;
80 static volatile voodoo_io_reg *reg_IO;
81 static volatile voodoo_2d_reg *reg_2d;
82 static voodoo_yuv_reg *reg_YUV;
83 static struct YUV_plane *YUV;
84 static void (*alpha_func)(), (*alpha_func_double)();
86 static int preinit(const char *arg)
88 char *name;
90 if(arg)
91 name = (char*)arg;
92 else if(!(name = getenv("FRAMEBUFFER")))
93 name = "/dev/fb0";
95 if((fd = open(name, O_RDWR)) == -1) {
96 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_TDFXFB_CantOpen, name, strerror(errno));
97 return -1;
100 if(ioctl(fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
101 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_TDFXFB_ProblemWithFbitgetFscreenInfo,
102 strerror(errno));
103 close(fd);
104 fd = -1;
105 return -1;
108 if(ioctl(fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
109 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_TDFXFB_ProblemWithFbitgetVscreenInfo,
110 strerror(errno));
111 close(fd);
112 fd = -1;
113 return -1;
116 /* BANSHEE means any of the series aparently */
117 if (fb_finfo.accel != FB_ACCEL_3DFX_BANSHEE) {
118 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_TDFXFB_ThisDriverOnlySupports);
119 close(fd);
120 fd = -1;
121 return -1;
124 // Check the depth now as config() musn't fail
125 switch(fb_vinfo.bits_per_pixel) {
126 case 16:
127 case 24:
128 case 32:
129 break; // Ok
130 default:
131 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_TDFXFB_OutputIsNotSupported, fb_vinfo.bits_per_pixel);
132 close(fd);
133 fd = -1;
134 return -1;
137 /* Open up a window to the hardware */
138 memBase1 = mmap(0, fb_finfo.smem_len, PROT_READ | PROT_WRITE,
139 MAP_SHARED, fd, 0);
140 memBase0 = mmap(0, fb_finfo.mmio_len, PROT_READ | PROT_WRITE,
141 MAP_SHARED, fd, fb_finfo.smem_len);
143 if((long)memBase0 == -1 || (long)memBase1 == -1) {
144 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_TDFXFB_CouldntMapMemoryAreas, strerror(errno));
145 if((long)memBase0 != -1)
146 munmap(memBase0, fb_finfo.smem_len);
147 if((long)memBase1 != -1)
148 munmap(memBase1, fb_finfo.smem_len);
149 memBase0 = memBase1 = NULL;
150 return -1;
153 /* Set up global pointers to the voodoo's regs */
154 reg_IO = (void *)memBase0 + VOODOO_IO_REG_OFFSET;
155 reg_2d = (void *)memBase0 + VOODOO_2D_REG_OFFSET;
156 reg_YUV = (void *)memBase0 + VOODOO_YUV_REG_OFFSET;
157 YUV = (void *)memBase0 + VOODOO_YUV_PLANE_OFFSET;
159 return 0;
162 static void uninit(void)
164 if(reg_IO) {
165 /* Restore the screen (Linux lives at 0) */
166 reg_IO->vidDesktopStartAddr = 0;
167 reg_IO = NULL;
170 /* And close our mess */
171 if(memBase1) {
172 munmap(memBase1, fb_finfo.smem_len);
173 memBase1 = NULL;
176 if(memBase0) {
177 munmap(memBase0, fb_finfo.mmio_len);
178 memBase0 = NULL;
181 if(fd != -1) {
182 close(fd);
183 fd = -1;
187 static void clear_screen()
189 /* There needs to be some sort of delay here or else things seriously
190 * screw up. Causes the image to not be the right size on screen if
191 * this isn't like this. A printf before the memset call also seems to
192 * work, but this made more sense since it actually checks the status of
193 * the card.
195 if(vo_doublebuffering) {
196 /* first wait for the card to be ready, do not try to write
197 * every time - alex */
198 do {} while((reg_IO->status & 0x1f) < 1);
199 memset(vidpage, 0, screenwidth * screenheight * screendepth);
200 memset(hidpage, 0, screenwidth * screenheight * screendepth);
204 /* Setup output screen dimensions etc */
205 static void setup_screen(uint32_t full)
207 aspect(&vidwidth, &vidheight, full ? A_ZOOM : A_NOZOOM);
209 geometry(&vidx, &vidy, &vidwidth, &vidheight, screenwidth, screenheight);
210 vo_fs = full;
211 clear_screen();
214 static int config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height,
215 uint32_t flags, char *title, uint32_t format)
217 screenwidth = fb_vinfo.xres;
218 screenheight = fb_vinfo.yres;
219 aspect_save_screenres(fb_vinfo.xres,fb_vinfo.yres);
221 in_width = width;
222 in_height = height;
223 in_format = format;
224 aspect_save_orig(width,height);
226 r_width = d_width;
227 r_height = d_height;
228 aspect_save_prescale(d_width,d_height);
230 /* Setup the screen for rendering to */
231 switch(fb_vinfo.bits_per_pixel) {
232 case 16:
233 screendepth = 2;
234 vid_voodoo_format = VOODOO_BLT_FORMAT_16;
235 alpha_func_double = vo_draw_alpha_rgb16;
236 break;
238 case 24:
239 screendepth = 3;
240 vid_voodoo_format = VOODOO_BLT_FORMAT_24;
241 alpha_func_double = vo_draw_alpha_rgb24;
242 break;
244 case 32:
245 screendepth = 4;
246 vid_voodoo_format = VOODOO_BLT_FORMAT_32;
247 alpha_func_double = vo_draw_alpha_rgb32;
248 break;
250 default:
251 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_LIBVO_TDFXFB_BppOutputIsNotSupported, fb_vinfo.bits_per_pixel);
252 return -1;
255 vid_voodoo_format |= screenwidth * screendepth;
257 /* Some defaults here */
258 in_voodoo_format = VOODOO_BLT_FORMAT_YUYV;
259 in_depth = 2;
260 alpha_func = vo_draw_alpha_yuy2;
262 switch(in_format) {
263 case IMGFMT_YV12:
264 case IMGFMT_I420:
265 case IMGFMT_IYUV:
266 case IMGFMT_YUY2:
267 break;
268 case IMGFMT_UYVY:
269 in_voodoo_format = VOODOO_BLT_FORMAT_UYVY;
270 break;
271 case IMGFMT_BGR16:
272 in_voodoo_format = VOODOO_BLT_FORMAT_16;
273 alpha_func = vo_draw_alpha_rgb16;
274 break;
276 case IMGFMT_BGR24:
277 in_depth = 3;
278 in_voodoo_format = VOODOO_BLT_FORMAT_24;
279 alpha_func = vo_draw_alpha_rgb24;
280 break;
282 case IMGFMT_BGR32:
283 in_depth = 4;
284 in_voodoo_format = VOODOO_BLT_FORMAT_32;
285 alpha_func = vo_draw_alpha_rgb32;
286 break;
288 default:
289 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_TDFXFB_SomethingIsWrongWithControl);
290 return -1;
293 in_voodoo_format |= in_width * in_depth;
295 /* Linux lives in the first frame */
296 if(vo_doublebuffering) {
297 vidpageoffset = screenwidth * screenheight * screendepth;
298 hidpageoffset = vidpageoffset + screenwidth * screenheight * screendepth;
299 } else
300 vidpageoffset = hidpageoffset = 0; /* Console background */
303 inpageoffset = hidpageoffset + screenwidth * screenheight * screendepth;
305 if(inpageoffset + in_width * in_depth * in_height > fb_finfo.smem_len) {
306 mp_msg(MSGT_VO,MSGL_ERR, MSGTR_LIBVO_TDFXFB_NotEnoughVideoMemoryToPlay);
307 return -1;
310 vidpage = (void *)memBase1 + (unsigned long)vidpageoffset;
311 hidpage = (void *)memBase1 + (unsigned long)hidpageoffset;
312 inpage = (void *)memBase1 + (unsigned long)inpageoffset;
314 setup_screen(flags & VOFLAG_FULLSCREEN);
316 memset(inpage, 0, in_width * in_height * in_depth);
318 mp_msg(MSGT_VO,MSGL_INFO, MSGTR_LIBVO_TDFXFB_ScreenIs,
319 screenwidth, screenheight, screendepth * 8,
320 in_width, in_height, in_depth * 8,
321 d_width, d_height);
323 return 0;
326 /* Double-buffering draw_alpha */
327 static void draw_alpha_double(int x, int y, int w, int h, unsigned char *src,
328 unsigned char *srca, int stride)
330 char *dst = (char *)vidpage + ((y + vidy) * screenwidth + x + vidx) * screendepth;
331 alpha_func_double(w, h, src, srca, stride, dst, screenwidth * screendepth);
334 /* Single-buffering draw_alpha */
335 static void draw_alpha(int x, int y, int w, int h, unsigned char *src,
336 unsigned char *srca, int stride)
338 char *dst = (char *)inpage + (y * in_width + x) * in_depth;
339 alpha_func(w, h, src, srca, stride, dst, in_width * in_depth);
342 static void draw_osd(void)
344 if(!vo_doublebuffering)
345 vo_draw_text(in_width, in_height, draw_alpha);
348 /* Render onto the screen */
349 static void flip_page(void)
351 voodoo_2d_reg regs = *reg_2d; /* Copy the regs */
352 int i = 0;
354 if(vo_doublebuffering) {
355 /* Flip to an offscreen buffer for rendering */
356 uint32_t t = vidpageoffset;
357 void *j = vidpage;
359 vidpage = hidpage;
360 hidpage = j;
361 vidpageoffset = hidpageoffset;
362 hidpageoffset = t;
365 reg_2d->commandExtra = 0;
366 reg_2d->clip0Min = 0;
367 reg_2d->clip0Max = 0xffffffff;
369 reg_2d->srcBaseAddr = inpageoffset;
370 reg_2d->srcXY = 0;
371 reg_2d->srcFormat = in_voodoo_format;
372 reg_2d->srcSize = XYREG(in_width, in_height);
374 reg_2d->dstBaseAddr = vidpageoffset;
375 reg_2d->dstXY = XYREG(vidx, vidy);
376 reg_2d->dstFormat = vid_voodoo_format;
377 reg_2d->dstSize = XYREG(vidwidth, vidheight);
378 reg_2d->command = S2S_STRECH_BLT | S2S_IMMED | S2S_ROP;
380 /* Wait for the command to finish (If we don't do this, we get wierd
381 * sound corruption... */
382 while((reg_IO->status & 0x1f) < 1)
383 /* Wait */;
385 *((volatile uint32_t *)((uint32_t *)reg_IO + COMMAND_3D)) = COMMAND_3D_NOP;
387 while(i < 3)
388 if(!(reg_IO->status & STATUS_BUSY))
389 i++;
391 /* Restore the old regs now */
392 reg_2d->commandExtra = regs.commandExtra;
393 reg_2d->clip0Min = regs.clip0Min;
394 reg_2d->clip0Max = regs.clip0Max;
396 reg_2d->srcBaseAddr = regs.srcBaseAddr;
397 reg_2d->srcXY = regs.srcXY;
398 reg_2d->srcFormat = regs.srcFormat;
399 reg_2d->srcSize = regs.srcSize;
401 reg_2d->dstBaseAddr = regs.dstBaseAddr;
402 reg_2d->dstXY = regs.dstXY;
403 reg_2d->dstFormat = regs.dstFormat;
404 reg_2d->dstSize = regs.dstSize;
406 reg_2d->command = 0;
408 /* Render any text onto this buffer */
409 if(vo_doublebuffering)
410 vo_draw_text(vidwidth, vidheight, draw_alpha_double);
412 /* And flip to the new buffer! */
413 reg_IO->vidDesktopStartAddr = vidpageoffset;
416 static int draw_frame(uint8_t *src[])
418 mem2agpcpy(inpage, src[0], in_width * in_depth * in_height);
419 return 0;
422 static int draw_slice(uint8_t *i[], int s[], int w, int h, int x, int y)
424 /* We want to render to the YUV to the input page + the location
425 * of the stripes we're doing */
426 reg_YUV->yuvBaseAddr = inpageoffset + in_width * in_depth * y + x;
427 reg_YUV->yuvStride = in_width * in_depth;
429 /* Put the YUV channels into the voodoos internal combiner unit
430 * thingie */
431 mem2agpcpy_pic(YUV->Y, i[0], s[0], h , YUV_STRIDE, s[0]);
432 mem2agpcpy_pic(YUV->U, i[1], s[1], h / 2, YUV_STRIDE, s[1]);
433 mem2agpcpy_pic(YUV->V, i[2], s[2], h / 2, YUV_STRIDE, s[2]);
434 return 0;
437 /* Attempt to start doing DR */
438 static uint32_t get_image(mp_image_t *mpi)
441 if(mpi->flags & MP_IMGFLAG_READABLE)
442 return VO_FALSE;
443 if(mpi->type == MP_IMGTYPE_STATIC && vo_doublebuffering)
444 return VO_FALSE;
445 if(mpi->type > MP_IMGTYPE_TEMP)
446 return VO_FALSE; // TODO ??
448 switch(in_format) {
449 case IMGFMT_YUY2:
450 case IMGFMT_BGR16:
451 case IMGFMT_BGR24:
452 case IMGFMT_BGR32:
453 case IMGFMT_UYVY:
454 mpi->planes[0] = (char *)inpage;
455 mpi->stride[0] = in_width * in_depth;
456 break;
458 case IMGFMT_YV12:
459 case IMGFMT_I420:
460 case IMGFMT_IYUV:
461 if(!(mpi->flags & MP_IMGFLAG_ACCEPT_STRIDE) && mpi->w != YUV_STRIDE)
462 return VO_FALSE;
463 mpi->planes[0] = YUV->Y;
464 mpi->planes[1] = YUV->U;
465 mpi->planes[2] = YUV->V;
466 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = YUV_STRIDE;
467 reg_YUV->yuvBaseAddr = inpageoffset;
468 reg_YUV->yuvStride = in_width * in_depth;
469 break;
471 default:
472 return VO_FALSE;
475 mpi->width = in_width;
476 mpi->flags |= MP_IMGFLAG_DIRECT;
478 return VO_TRUE;
481 static int control(uint32_t request, void *data)
483 switch(request) {
484 case VOCTRL_GET_IMAGE:
485 return get_image(data);
487 case VOCTRL_QUERY_FORMAT:
488 switch(*((uint32_t*)data)) {
489 case IMGFMT_YV12:
490 case IMGFMT_I420:
491 case IMGFMT_IYUV:
492 case IMGFMT_YUY2:
493 case IMGFMT_UYVY:
494 case IMGFMT_BGR16:
495 case IMGFMT_BGR24:
496 case IMGFMT_BGR32:
497 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW |
498 VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
501 return 0; /* Not supported */
503 case VOCTRL_FULLSCREEN:
504 setup_screen(!vo_fs);
505 return 0;
508 return VO_NOTIMPL;
511 /* Dummy funcs */
512 static void check_events(void) {}