video: default to colorspace/range from libavcodec
[mplayer.git] / libvo / vo_tdfxfb.c
blob6e14f600b70a3ad1ae8dac2f81b4a38973efdd36
1 /*
2 * copyright (C) 2002 Mark Zealey <mark@zealos.org>
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * 30/03/02: An almost total rewrite, added DR support and support for modes
23 * other than 16bpp. Fixed the crash when playing multiple files
24 * 07/04/02: Fixed DR support, added YUY2 support, fixed OSD stuff.
25 * 08/04/02: Fixed a wierd sound corruption problem caused by some optomizations
26 * I made.
27 * 09/04/02: Fixed a problem with changing the variables passed to draw_slice().
28 * Fixed DR support for YV12 et al. Added BGR support. Removed lots of dud code.
29 * 10/04/02: Changed the memcpy functions to mem2agpcpy.. should be a tad
30 * faster.
31 * 11/04/02: Added a compile option so you can watch the film with the console
32 * as the background, or not.
33 * 13/04/02: Fix rough OSD stuff by rendering it straight onto the output
34 * buffer. Added double-buffering. Supports hardware zoom/reduce zoom modes.
35 * 13/04/02: Misc cleanups of the code.
36 * 22/10/02: Added geometry support to it
38 * Hints and tricks:
39 * - Use -dr to get direct rendering
40 * - Use -vf yuy2 to get yuy2 rendering, *MUCH* faster than yv12
41 * - To get a black background and nice smooth OSD, use -double
42 * - To get the console as a background, but with scaled OSD, use -nodouble
43 * - The driver supports both scaling and shrinking the image using the -x and
44 * -y options on the mplayer commandline. Also repositioning via the -geometry
45 * option.
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <errno.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <sys/ioctl.h>
54 #include <fcntl.h>
55 #include <sys/mman.h>
56 #include <linux/fb.h>
58 #include "config.h"
59 #include "mp_msg.h"
60 #include "fastmemcpy.h"
61 #include "video_out.h"
62 #include "video_out_internal.h"
63 #include "drivers/3dfx.h"
64 #include "aspect.h"
65 #include "sub/sub.h"
67 static const vo_info_t info =
69 "3Dfx Banshee/Voodoo3/Voodoo5",
70 "tdfxfb",
71 "Mark Zealey <mark@zealos.org>",
75 const LIBVO_EXTERN(tdfxfb)
77 /* Some registers on the card */
78 #define S2S_STRECH_BLT 2 // BLT + Strech
79 #define S2S_IMMED (1 << 8) // Do it immediatly
80 #define S2S_ROP (0xCC << 24) // ???
82 /* Stepping between the different YUV plane registers */
83 #define YUV_STRIDE 1024
84 struct YUV_plane {
85 char Y[0x0100000];
86 char U[0x0100000];
87 char V[0x0100000];
90 static int fd = -1;
91 static struct fb_fix_screeninfo fb_finfo;
92 static struct fb_var_screeninfo fb_vinfo;
93 static uint32_t in_width, in_height, in_format, in_depth, in_voodoo_format,
94 screenwidth, screenheight, screendepth, vidwidth, vidheight, vidx, vidy,
95 vid_voodoo_format, *vidpage, *hidpage, *inpage, vidpageoffset,
96 hidpageoffset, inpageoffset, *memBase0 = NULL, *memBase1 = NULL, r_width, r_height;
97 static volatile voodoo_io_reg *reg_IO;
98 static volatile voodoo_2d_reg *reg_2d;
99 static voodoo_yuv_reg *reg_YUV;
100 static struct YUV_plane *YUV;
101 static void (*alpha_func)(), (*alpha_func_double)();
103 static int preinit(const char *arg)
105 char *name;
107 if(arg)
108 name = (char*)arg;
109 else if(!(name = getenv("FRAMEBUFFER")))
110 name = "/dev/fb0";
112 if((fd = open(name, O_RDWR)) == -1) {
113 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_TDFXFB] Can't open %s: %s.\n", name, strerror(errno));
114 return -1;
117 if(ioctl(fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
118 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_TDFXFB] Problem with FBITGET_FSCREENINFO ioctl: %s.\n",
119 strerror(errno));
120 close(fd);
121 fd = -1;
122 return -1;
125 if(ioctl(fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
126 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_TDFXFB] Problem with FBITGET_VSCREENINFO ioctl: %s.\n",
127 strerror(errno));
128 close(fd);
129 fd = -1;
130 return -1;
133 /* BANSHEE means any of the series aparently */
134 if (fb_finfo.accel != FB_ACCEL_3DFX_BANSHEE) {
135 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_TDFXFB] This driver only supports the 3Dfx Banshee, Voodoo3 and Voodoo 5.\n");
136 close(fd);
137 fd = -1;
138 return -1;
141 // Check the depth now as config() musn't fail
142 switch(fb_vinfo.bits_per_pixel) {
143 case 16:
144 case 24:
145 case 32:
146 break; // Ok
147 default:
148 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_TDFXFB] %d bpp output is not supported.\n", fb_vinfo.bits_per_pixel);
149 close(fd);
150 fd = -1;
151 return -1;
154 /* Open up a window to the hardware */
155 memBase1 = mmap(0, fb_finfo.smem_len, PROT_READ | PROT_WRITE,
156 MAP_SHARED, fd, 0);
157 memBase0 = mmap(0, fb_finfo.mmio_len, PROT_READ | PROT_WRITE,
158 MAP_SHARED, fd, fb_finfo.smem_len);
160 if((long)memBase0 == -1 || (long)memBase1 == -1) {
161 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_TDFXFB] Couldn't map memory areas: %s.\n", strerror(errno));
162 if((long)memBase0 != -1)
163 munmap(memBase0, fb_finfo.smem_len);
164 if((long)memBase1 != -1)
165 munmap(memBase1, fb_finfo.smem_len);
166 memBase0 = memBase1 = NULL;
167 return -1;
170 /* Set up global pointers to the voodoo's regs */
171 reg_IO = (void *)memBase0 + VOODOO_IO_REG_OFFSET;
172 reg_2d = (void *)memBase0 + VOODOO_2D_REG_OFFSET;
173 reg_YUV = (void *)memBase0 + VOODOO_YUV_REG_OFFSET;
174 YUV = (void *)memBase0 + VOODOO_YUV_PLANE_OFFSET;
176 return 0;
179 static void uninit(void)
181 if(reg_IO) {
182 /* Restore the screen (Linux lives at 0) */
183 reg_IO->vidDesktopStartAddr = 0;
184 reg_IO = NULL;
187 /* And close our mess */
188 if(memBase1) {
189 munmap(memBase1, fb_finfo.smem_len);
190 memBase1 = NULL;
193 if(memBase0) {
194 munmap(memBase0, fb_finfo.mmio_len);
195 memBase0 = NULL;
198 if(fd != -1) {
199 close(fd);
200 fd = -1;
204 static void clear_screen(void)
206 /* There needs to be some sort of delay here or else things seriously
207 * screw up. Causes the image to not be the right size on screen if
208 * this isn't like this. A printf before the memset call also seems to
209 * work, but this made more sense since it actually checks the status of
210 * the card.
212 if(vo_doublebuffering) {
213 /* first wait for the card to be ready, do not try to write
214 * every time - alex */
215 do {} while((reg_IO->status & 0x1f) < 1);
216 memset(vidpage, 0, screenwidth * screenheight * screendepth);
217 memset(hidpage, 0, screenwidth * screenheight * screendepth);
221 /* Setup output screen dimensions etc */
222 static void setup_screen(uint32_t full)
224 aspect(&vidwidth, &vidheight, full ? A_ZOOM : A_NOZOOM);
226 geometry(&vidx, &vidy, &vidwidth, &vidheight, screenwidth, screenheight);
227 vo_fs = full;
228 clear_screen();
231 static int config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height,
232 uint32_t flags, char *title, uint32_t format)
234 screenwidth = fb_vinfo.xres;
235 screenheight = fb_vinfo.yres;
236 aspect_save_screenres(fb_vinfo.xres,fb_vinfo.yres);
238 in_width = width;
239 in_height = height;
240 in_format = format;
241 aspect_save_orig(width,height);
243 r_width = d_width;
244 r_height = d_height;
245 aspect_save_prescale(d_width,d_height);
247 /* Setup the screen for rendering to */
248 switch(fb_vinfo.bits_per_pixel) {
249 case 16:
250 screendepth = 2;
251 vid_voodoo_format = VOODOO_BLT_FORMAT_16;
252 alpha_func_double = vo_draw_alpha_rgb16;
253 break;
255 case 24:
256 screendepth = 3;
257 vid_voodoo_format = VOODOO_BLT_FORMAT_24;
258 alpha_func_double = vo_draw_alpha_rgb24;
259 break;
261 case 32:
262 screendepth = 4;
263 vid_voodoo_format = VOODOO_BLT_FORMAT_32;
264 alpha_func_double = vo_draw_alpha_rgb32;
265 break;
267 default:
268 mp_tmsg(MSGT_VO, MSGL_ERR, "[VO_TDFXFB] %d bpp output is not supported (This should never have happened).\n", fb_vinfo.bits_per_pixel);
269 return -1;
272 vid_voodoo_format |= screenwidth * screendepth;
274 /* Some defaults here */
275 in_voodoo_format = VOODOO_BLT_FORMAT_YUYV;
276 in_depth = 2;
277 alpha_func = vo_draw_alpha_yuy2;
279 switch(in_format) {
280 case IMGFMT_YV12:
281 case IMGFMT_I420:
282 case IMGFMT_IYUV:
283 case IMGFMT_YUY2:
284 break;
285 case IMGFMT_UYVY:
286 in_voodoo_format = VOODOO_BLT_FORMAT_UYVY;
287 break;
288 case IMGFMT_BGR16:
289 in_voodoo_format = VOODOO_BLT_FORMAT_16;
290 alpha_func = vo_draw_alpha_rgb16;
291 break;
293 case IMGFMT_BGR24:
294 in_depth = 3;
295 in_voodoo_format = VOODOO_BLT_FORMAT_24;
296 alpha_func = vo_draw_alpha_rgb24;
297 break;
299 case IMGFMT_BGR32:
300 in_depth = 4;
301 in_voodoo_format = VOODOO_BLT_FORMAT_32;
302 alpha_func = vo_draw_alpha_rgb32;
303 break;
305 default:
306 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_TDFXFB] Eik! Something's wrong with control().\n");
307 return -1;
310 in_voodoo_format |= in_width * in_depth;
312 /* Linux lives in the first frame */
313 if(vo_doublebuffering) {
314 vidpageoffset = screenwidth * screenheight * screendepth;
315 hidpageoffset = vidpageoffset + screenwidth * screenheight * screendepth;
316 } else
317 vidpageoffset = hidpageoffset = 0; /* Console background */
320 inpageoffset = hidpageoffset + screenwidth * screenheight * screendepth;
322 if(inpageoffset + in_width * in_depth * in_height > fb_finfo.smem_len) {
323 mp_tmsg(MSGT_VO,MSGL_ERR, "[VO_TDFXFB] Not enough video memory to play this movie. Try at a lower resolution.\n");
324 return -1;
327 vidpage = (void *)memBase1 + (unsigned long)vidpageoffset;
328 hidpage = (void *)memBase1 + (unsigned long)hidpageoffset;
329 inpage = (void *)memBase1 + (unsigned long)inpageoffset;
331 setup_screen(flags & VOFLAG_FULLSCREEN);
333 memset(inpage, 0, in_width * in_height * in_depth);
335 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_TDFXFB] Screen is %dx%d at %d bpp, in is %dx%d at %d bpp, norm is %dx%d.\n",
336 screenwidth, screenheight, screendepth * 8,
337 in_width, in_height, in_depth * 8,
338 d_width, d_height);
340 return 0;
343 /* Double-buffering draw_alpha */
344 static void draw_alpha_double(int x, int y, int w, int h, unsigned char *src,
345 unsigned char *srca, int stride)
347 char *dst = (char *)vidpage + ((y + vidy) * screenwidth + x + vidx) * screendepth;
348 alpha_func_double(w, h, src, srca, stride, dst, screenwidth * screendepth);
351 /* Single-buffering draw_alpha */
352 static void draw_alpha(int x, int y, int w, int h, unsigned char *src,
353 unsigned char *srca, int stride)
355 char *dst = (char *)inpage + (y * in_width + x) * in_depth;
356 alpha_func(w, h, src, srca, stride, dst, in_width * in_depth);
359 static void draw_osd(void)
361 if(!vo_doublebuffering)
362 vo_draw_text(in_width, in_height, draw_alpha);
365 /* Render onto the screen */
366 static void flip_page(void)
368 voodoo_2d_reg regs = *reg_2d; /* Copy the regs */
369 int i = 0;
371 if(vo_doublebuffering) {
372 /* Flip to an offscreen buffer for rendering */
373 uint32_t t = vidpageoffset;
374 void *j = vidpage;
376 vidpage = hidpage;
377 hidpage = j;
378 vidpageoffset = hidpageoffset;
379 hidpageoffset = t;
382 reg_2d->commandExtra = 0;
383 reg_2d->clip0Min = 0;
384 reg_2d->clip0Max = 0xffffffff;
386 reg_2d->srcBaseAddr = inpageoffset;
387 reg_2d->srcXY = 0;
388 reg_2d->srcFormat = in_voodoo_format;
389 reg_2d->srcSize = XYREG(in_width, in_height);
391 reg_2d->dstBaseAddr = vidpageoffset;
392 reg_2d->dstXY = XYREG(vidx, vidy);
393 reg_2d->dstFormat = vid_voodoo_format;
394 reg_2d->dstSize = XYREG(vidwidth, vidheight);
395 reg_2d->command = S2S_STRECH_BLT | S2S_IMMED | S2S_ROP;
397 /* Wait for the command to finish (If we don't do this, we get wierd
398 * sound corruption... */
399 while((reg_IO->status & 0x1f) < 1)
400 /* Wait */;
402 *((volatile uint32_t *)((uint32_t *)reg_IO + COMMAND_3D)) = COMMAND_3D_NOP;
404 while(i < 3)
405 if(!(reg_IO->status & STATUS_BUSY))
406 i++;
408 /* Restore the old regs now */
409 reg_2d->commandExtra = regs.commandExtra;
410 reg_2d->clip0Min = regs.clip0Min;
411 reg_2d->clip0Max = regs.clip0Max;
413 reg_2d->srcBaseAddr = regs.srcBaseAddr;
414 reg_2d->srcXY = regs.srcXY;
415 reg_2d->srcFormat = regs.srcFormat;
416 reg_2d->srcSize = regs.srcSize;
418 reg_2d->dstBaseAddr = regs.dstBaseAddr;
419 reg_2d->dstXY = regs.dstXY;
420 reg_2d->dstFormat = regs.dstFormat;
421 reg_2d->dstSize = regs.dstSize;
423 reg_2d->command = 0;
425 /* Render any text onto this buffer */
426 if(vo_doublebuffering)
427 vo_draw_text(vidwidth, vidheight, draw_alpha_double);
429 /* And flip to the new buffer! */
430 reg_IO->vidDesktopStartAddr = vidpageoffset;
433 static int draw_frame(uint8_t *src[])
435 mem2agpcpy(inpage, src[0], in_width * in_depth * in_height);
436 return 0;
439 static int draw_slice(uint8_t *i[], int s[], int w, int h, int x, int y)
441 /* We want to render to the YUV to the input page + the location
442 * of the stripes we're doing */
443 reg_YUV->yuvBaseAddr = inpageoffset + in_width * in_depth * y + x;
444 reg_YUV->yuvStride = in_width * in_depth;
446 /* Put the YUV channels into the voodoos internal combiner unit
447 * thingie */
448 mem2agpcpy_pic(YUV->Y, i[0], s[0], h , YUV_STRIDE, s[0]);
449 mem2agpcpy_pic(YUV->U, i[1], s[1], h / 2, YUV_STRIDE, s[1]);
450 mem2agpcpy_pic(YUV->V, i[2], s[2], h / 2, YUV_STRIDE, s[2]);
451 return 0;
454 /* Attempt to start doing DR */
455 static uint32_t get_image(mp_image_t *mpi)
458 if(mpi->flags & MP_IMGFLAG_READABLE)
459 return VO_FALSE;
460 if(mpi->type == MP_IMGTYPE_STATIC && vo_doublebuffering)
461 return VO_FALSE;
462 if(mpi->type > MP_IMGTYPE_TEMP)
463 return VO_FALSE; // TODO ??
465 switch(in_format) {
466 case IMGFMT_YUY2:
467 case IMGFMT_BGR16:
468 case IMGFMT_BGR24:
469 case IMGFMT_BGR32:
470 case IMGFMT_UYVY:
471 mpi->planes[0] = (char *)inpage;
472 mpi->stride[0] = in_width * in_depth;
473 break;
475 case IMGFMT_YV12:
476 case IMGFMT_I420:
477 case IMGFMT_IYUV:
478 if(!(mpi->flags & MP_IMGFLAG_ACCEPT_STRIDE) && mpi->w != YUV_STRIDE)
479 return VO_FALSE;
480 mpi->planes[0] = YUV->Y;
481 mpi->planes[1] = YUV->U;
482 mpi->planes[2] = YUV->V;
483 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = YUV_STRIDE;
484 reg_YUV->yuvBaseAddr = inpageoffset;
485 reg_YUV->yuvStride = in_width * in_depth;
486 break;
488 default:
489 return VO_FALSE;
492 mpi->width = in_width;
493 mpi->flags |= MP_IMGFLAG_DIRECT;
495 return VO_TRUE;
498 static int control(uint32_t request, void *data)
500 switch(request) {
501 case VOCTRL_GET_IMAGE:
502 return get_image(data);
504 case VOCTRL_QUERY_FORMAT:
505 switch(*((uint32_t*)data)) {
506 case IMGFMT_YV12:
507 case IMGFMT_I420:
508 case IMGFMT_IYUV:
509 case IMGFMT_YUY2:
510 case IMGFMT_UYVY:
511 case IMGFMT_BGR16:
512 case IMGFMT_BGR24:
513 case IMGFMT_BGR32:
514 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW |
515 VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
518 return 0; /* Not supported */
520 case VOCTRL_FULLSCREEN:
521 setup_screen(!vo_fs);
522 return 0;
525 return VO_NOTIMPL;
528 /* Dummy funcs */
529 static void check_events(void) {}