typo fixes
[mplayer/greg.git] / libvo / vo_s3fb.c
blob91af64ec29346614d35d15be75b2efa7c0e5ed9b
1 /* Copyright (C) Mark Sanderson, 2006, <mmp@kiora.ath.cx>.
2 * Released under the terms and conditions of the GPL.
4 * 30-Mar-2006 Modified from tdfxfb.c by Mark Zealey
5 *
6 * Hints and tricks:
7 * - Use -dr to get direct rendering
8 * - Use -vf yuy2 to get yuy2 rendering, *MUCH* faster than yv12
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <sys/ioctl.h>
17 #include <fcntl.h>
18 #include <linux/fb.h>
19 #include <sys/io.h>
21 #include "config.h"
22 #ifdef HAVE_SYS_MMAN_H
23 #include <sys/mman.h>
24 #endif
25 #include "mp_msg.h"
26 #include "fastmemcpy.h"
27 #include "video_out.h"
28 #include "video_out_internal.h"
29 #include "aspect.h"
30 #include "sub.h"
32 static vo_info_t info =
34 "S3 Virge over fbdev",
35 "s3fb",
36 "Mark Sanderson <mmp@kiora.ath.cx>",
40 LIBVO_EXTERN(s3fb)
42 static int fd = -1;
43 static struct fb_fix_screeninfo fb_finfo;
44 static struct fb_var_screeninfo fb_vinfo;
45 static uint32_t in_width, in_height, in_format, in_depth, in_s3_format,
46 screenwidth, screenheight, screendepth, screenstride,
47 vidwidth, vidheight, vidx, vidy, page, offset, sreg;
48 static char *inpage, *inpage0, *smem = NULL;
49 static void (*alpha_func)();
51 static void clear_screen();
53 /* streams registers */
54 #define PSTREAM_CONTROL_REG 0x8180
55 #define COL_CHROMA_KEY_CONTROL_REG 0x8184
56 #define SSTREAM_CONTROL_REG 0x8190
57 #define CHROMA_KEY_UPPER_BOUND_REG 0x8194
58 #define SSTREAM_STRETCH_REG 0x8198
59 #define BLEND_CONTROL_REG 0x81A0
60 #define PSTREAM_FBADDR0_REG 0x81C0
61 #define PSTREAM_FBADDR1_REG 0x81C4
62 #define PSTREAM_STRIDE_REG 0x81C8
63 #define DOUBLE_BUFFER_REG 0x81CC
64 #define SSTREAM_FBADDR0_REG 0x81D0
65 #define SSTREAM_FBADDR1_REG 0x81D4
66 #define SSTREAM_STRIDE_REG 0x81D8
67 #define OPAQUE_OVERLAY_CONTROL_REG 0x81DC
68 #define K1_VSCALE_REG 0x81E0
69 #define K2_VSCALE_REG 0x81E4
70 #define DDA_VERT_REG 0x81E8
71 #define STREAMS_FIFO_REG 0x81EC
72 #define PSTREAM_START_REG 0x81F0
73 #define PSTREAM_WINDOW_SIZE_REG 0x81F4
74 #define SSTREAM_START_REG 0x81F8
75 #define SSTREAM_WINDOW_SIZE_REG 0x81FC
77 #define S3_MEMBASE sreg
78 #define S3_NEWMMIO_REGBASE 0x1000000 /* 16MB */
79 #define S3_NEWMMIO_REGSIZE 0x10000 /* 64KB */
80 #define S3V_MMIO_REGSIZE 0x8000 /* 32KB */
81 #define S3_NEWMMIO_VGABASE (S3_NEWMMIO_REGBASE + 0x8000)
83 #define OUTREG(mmreg, value) *(unsigned int *)(&v.mmio[mmreg]) = value
85 typedef struct vga_type {
86 int cr38, cr39, cr53;
87 unsigned char *mmio;
88 } vga_t;
90 int readcrtc(int reg) {
91 outb(reg, 0x3d4);
92 return inb(0x3d5);
95 void writecrtc(int reg, int value) {
96 outb(reg, 0x3d4);
97 outb(value, 0x3d5);
100 int enable(vga_t *v) {
101 int fd;
103 // enable registers
104 if (iopl(3) != 0)
105 return 0;
106 v->cr38 = readcrtc(0x38);
107 v->cr39 = readcrtc(0x39);
108 v->cr53 = readcrtc(0x53);
109 writecrtc(0x38, 0x48);
110 writecrtc(0x39, 0xa5);
111 writecrtc(0x53, 0x08);
112 fd = open("/dev/mem", O_RDWR);
113 v->mmio = mmap(0, S3_NEWMMIO_REGSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
114 S3_MEMBASE + S3_NEWMMIO_REGBASE);
115 close(fd);
116 return 1;
119 void disable(vga_t *v) {
120 writecrtc(0x53, v->cr53);
121 writecrtc(0x39, v->cr39);
122 writecrtc(0x38, v->cr38);
123 iopl(0);
124 munmap(v->mmio, S3_NEWMMIO_REGSIZE);
127 int yuv_on(int format, int src_w, int src_h, int dst_x, int dst_y, int dst_w, int dst_h, int crop, int xres, int yres, int line_length, int offset) {
128 int tmp, pitch, start, src_wc, src_hc, bpp;
129 vga_t v;
131 if (format == 0 || format == 7)
132 bpp = 4;
133 else if (format == 6)
134 bpp = 3;
135 else
136 bpp = 2;
138 src_wc = src_w - crop * 2;
139 src_hc = src_h - crop * 2;
140 pitch = src_w * bpp;
142 // video card memory layout:
143 // 0-n: visable screen memory, n = width * height * bytes per pixel
144 // n-m: scaler source memory, n is aligned to a page boundary
145 // m+: scaler source memory for multiple buffers
147 // offset is the first aligned byte after the screen memory, where the scaler input buffer is
148 tmp = (yres * line_length + 4095) & ~4095;
149 offset += tmp;
151 // start is the top left viewable scaler input pixel
152 start = offset + crop * pitch + crop * bpp;
154 if (!enable(&v))
155 return 0;
157 OUTREG(COL_CHROMA_KEY_CONTROL_REG, 0x47000000);
158 OUTREG(CHROMA_KEY_UPPER_BOUND_REG, 0x0);
159 OUTREG(BLEND_CONTROL_REG, 0x00000020);
160 OUTREG(DOUBLE_BUFFER_REG, 0x0); /* Choose fbaddr0 as stream source. */
161 OUTREG(OPAQUE_OVERLAY_CONTROL_REG, 0x0);
163 OUTREG(PSTREAM_CONTROL_REG, 0x06000000);
164 OUTREG(PSTREAM_FBADDR0_REG, 0x0);
165 OUTREG(PSTREAM_FBADDR1_REG, 0x0);
166 OUTREG(PSTREAM_STRIDE_REG, line_length);
167 OUTREG(PSTREAM_START_REG, 0x00010001);
168 OUTREG(PSTREAM_WINDOW_SIZE_REG, 0x00010001);
169 //OUTREG(SSTREAM_WINDOW_SIZE_REG, ( ((xres-1) << 16) | yres) & 0x7ff07ff);
171 if (dst_w == src_w)
172 tmp = 0;
173 else
174 tmp = 2;
175 /* format 1=YCbCr-16 2=YUV-16 3=BGR15 4=YUV-16/32(mixed 2/4byte stride) 5=BGR16 6=BGR24 0,7=BGR32 */
176 /* The YUV format pixel has a range of value from 0 to 255, while the YCbCr format pixel values are in the range of 16 to 240. */
177 OUTREG(SSTREAM_CONTROL_REG, tmp << 28 | (format << 24) |
178 ((((src_wc-1)<<1)-(dst_w-1)) & 0xfff));
179 OUTREG(SSTREAM_STRETCH_REG,
180 ((src_wc - 1) & 0x7ff) | (((src_wc - dst_w-1) & 0x7ff) << 16));
181 OUTREG(SSTREAM_FBADDR0_REG, start & 0x3fffff );
182 OUTREG(SSTREAM_STRIDE_REG, pitch & 0xfff );
183 OUTREG(SSTREAM_START_REG, ((dst_x + 1) << 16) | (dst_y + 1));
184 OUTREG(SSTREAM_WINDOW_SIZE_REG, ( ((dst_w-1) << 16) | (dst_h ) ) & 0x7ff07ff);
185 OUTREG(K1_VSCALE_REG, src_hc - 1 );
186 OUTREG(K2_VSCALE_REG, (src_hc - dst_h) & 0x7ff );
187 /* 0xc000 = bw & vert interp */
188 /* 0x8000 = no bw save */
189 OUTREG(DDA_VERT_REG, (((~dst_h)-1) & 0xfff ) | 0xc000);
190 writecrtc(0x92, (((pitch + 7) / 8) >> 8) | 0x80);
191 writecrtc(0x93, (pitch + 7) / 8);
193 writecrtc(0x67, readcrtc(0x67) | 0x4);
195 disable(&v);
197 return offset;
200 void yuv_off() {
201 vga_t v;
203 enable(&v);
205 writecrtc(0x67, readcrtc(0x67) & ~0xc);
206 memset(v.mmio + 0x8180, 0, 0x80);
207 OUTREG(0x81b8, 0x900);
208 OUTREG(0x81bc, 0x900);
209 OUTREG(0x81c8, 0x900);
210 OUTREG(0x81cc, 0x900);
211 OUTREG(0x81d8, 0x1);
212 OUTREG(0x81f8, 0x07ff07ff);
213 OUTREG(0x81fc, 0x00010001);
214 writecrtc(0x92, 0);
215 writecrtc(0x93, 0);
216 disable(&v);
219 static int preinit(const char *arg)
221 char *name;
223 if(arg)
224 name = (char*)arg;
225 else if(!(name = getenv("FRAMEBUFFER")))
226 name = "/dev/fb0";
228 if((fd = open(name, O_RDWR)) == -1) {
229 mp_msg(MSGT_VO, MSGL_FATAL, "s3fb: can't open %s: %s\n", name, strerror(errno));
230 return -1;
233 if(ioctl(fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
234 mp_msg(MSGT_VO, MSGL_FATAL, "s3fb: problem with FBITGET_FSCREENINFO ioctl: %s\n",
235 strerror(errno));
236 close(fd);
237 fd = -1;
238 return -1;
241 if(ioctl(fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
242 mp_msg(MSGT_VO, MSGL_FATAL, "s3fb: problem with FBITGET_VSCREENINFO ioctl: %s\n",
243 strerror(errno));
244 close(fd);
245 fd = -1;
246 return -1;
249 // Check the depth now as config() musn't fail
250 switch(fb_vinfo.bits_per_pixel) {
251 case 16:
252 case 24:
253 case 32:
254 break; // Ok
255 default:
256 mp_msg(MSGT_VO, MSGL_FATAL, "s3fb: %d bpp output is not supported\n", fb_vinfo.bits_per_pixel);
257 close(fd);
258 fd = -1;
259 return -1;
262 /* Open up a window to the hardware */
263 smem = mmap(0, fb_finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
264 sreg = fb_finfo.smem_start;
266 if((long)smem == -1) {
267 mp_msg(MSGT_VO, MSGL_FATAL, "s3fb: Couldn't map memory areas: %s\n", strerror(errno));
268 if((long)smem != -1)
269 munmap(smem, fb_finfo.smem_len);
270 smem = NULL;
271 return -1;
274 return 0;
277 static void uninit(void)
279 if (inpage0) {
280 clear_screen();
281 yuv_off();
282 inpage0 = NULL;
285 /* And close our mess */
286 if(smem) {
287 munmap(smem, fb_finfo.smem_len);
288 smem = NULL;
291 if(fd != -1) {
292 close(fd);
293 fd = -1;
297 static void clear_screen()
299 if (inpage0) {
300 int n;
302 memset(smem, 0, screenheight * screenstride);
304 if (in_format == IMGFMT_YUY2) {
305 unsigned short *ptr;
306 int i;
308 ptr = (unsigned short *)inpage0;
309 n = in_width * in_height;
310 if (vo_doublebuffering)
311 n *= 2;
312 for(i=0; i<n; i++)
313 *ptr++ = 0x8000;
315 } else {
316 n = in_depth * in_width * in_height;
317 if (vo_doublebuffering)
318 n *= 2;
319 memset(inpage0, 0, n);
324 /* Setup output screen dimensions etc */
325 static void setup_screen(uint32_t full)
327 int inpageoffset;
329 aspect(&vidwidth, &vidheight, full ? A_ZOOM : A_NOZOOM);
331 // center picture
332 vidx = (screenwidth - vidwidth) / 2;
333 vidy = (screenheight - vidheight) / 2;
335 geometry(&vidx, &vidy, &vidwidth, &vidheight, screenwidth, screenheight);
336 vo_fs = full;
338 inpageoffset = yuv_on(in_s3_format, in_width, in_height, vidx, vidy, vidwidth, vidheight, 0, screenwidth, screenheight, screenstride, 0);
339 inpage0 = smem + inpageoffset;
340 inpage = inpage0;
341 mp_msg(MSGT_VO, MSGL_INFO, "s3fb: output is at %dx%d +%dx%d\n", vidx, vidy, vidwidth, vidheight);
343 clear_screen();
346 static int config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height,
347 uint32_t flags, char *title, uint32_t format)
349 screenwidth = fb_vinfo.xres;
350 screenheight = fb_vinfo.yres;
351 screenstride = fb_finfo.line_length;
352 aspect_save_screenres(fb_vinfo.xres,fb_vinfo.yres);
354 in_width = width;
355 in_height = height;
356 in_format = format;
357 aspect_save_orig(width,height);
359 aspect_save_prescale(d_width,d_height);
361 /* Setup the screen for rendering to */
362 screendepth = fb_vinfo.bits_per_pixel / 8;
364 switch(in_format) {
366 case IMGFMT_YUY2:
367 in_depth = 2;
368 in_s3_format = 1;
369 alpha_func = vo_draw_alpha_yuy2;
370 break;
372 case IMGFMT_BGR15:
373 in_depth = 2;
374 in_s3_format = 3;
375 alpha_func = vo_draw_alpha_rgb16;
376 break;
378 case IMGFMT_BGR16:
379 in_depth = 2;
380 in_s3_format = 5;
381 alpha_func = vo_draw_alpha_rgb16;
382 break;
384 case IMGFMT_BGR24:
385 in_depth = 3;
386 in_s3_format = 6;
387 alpha_func = vo_draw_alpha_rgb24;
388 break;
390 case IMGFMT_BGR32:
391 in_depth = 4;
392 in_s3_format = 7;
393 alpha_func = vo_draw_alpha_rgb32;
394 break;
396 default:
397 mp_msg(MSGT_VO, MSGL_FATAL, "s3fb: Eik! Something's wrong with control().\n");
398 return -1;
401 offset = in_width * in_depth * in_height;
402 if (vo_doublebuffering)
403 page = offset;
404 else
405 page = 0;
407 if(screenheight * screenstride + page + offset > fb_finfo.smem_len) {
408 mp_msg(MSGT_VO, MSGL_FATAL, "s3fb: Not enough video memory to play this movie. Try at a lower resolution\n");
409 return -1;
412 setup_screen(flags & VOFLAG_FULLSCREEN);
413 if (vo_doublebuffering)
414 inpage = inpage0 + page;
416 mp_msg(MSGT_VO, MSGL_INFO, "s3fb: screen is %dx%d at %d bpp, in is %dx%d at %d bpp, norm is %dx%d\n",
417 screenwidth, screenheight, screendepth * 8,
418 in_width, in_height, in_depth * 8,
419 d_width, d_height);
421 return 0;
424 static void draw_alpha(int x, int y, int w, int h, unsigned char *src,
425 unsigned char *srca, int stride)
427 char *dst = inpage + (y * in_width + x) * in_depth;
428 alpha_func(w, h, src, srca, stride, dst, in_width * in_depth);
431 static void draw_osd(void)
433 if (!vo_doublebuffering)
434 vo_draw_text(in_width, in_height, draw_alpha);
437 /* Render onto the screen */
438 static void flip_page(void)
440 if(vo_doublebuffering) {
441 vo_draw_text(in_width, in_height, draw_alpha);
442 yuv_on(in_s3_format, in_width, in_height, vidx, vidy, vidwidth, vidheight, 0, screenwidth, screenheight, screenstride, page);
443 page ^= offset;
444 inpage = inpage0 + page;
448 static int draw_frame(uint8_t *src[])
450 mem2agpcpy(inpage, src[0], in_width * in_depth * in_height);
451 return 0;
454 static int draw_slice(uint8_t *i[], int s[], int w, int h, int x, int y)
456 return 1;
459 /* Attempt to start doing DR */
460 static uint32_t get_image(mp_image_t *mpi)
463 if(mpi->flags & MP_IMGFLAG_READABLE)
464 return VO_FALSE;
465 if(mpi->type == MP_IMGTYPE_STATIC && vo_doublebuffering)
466 return VO_FALSE;
467 if(mpi->type > MP_IMGTYPE_TEMP)
468 return VO_FALSE; // TODO ??
470 switch(in_format) {
471 case IMGFMT_BGR15:
472 case IMGFMT_BGR16:
473 case IMGFMT_BGR24:
474 case IMGFMT_BGR32:
475 case IMGFMT_YUY2:
476 mpi->planes[0] = inpage;
477 mpi->stride[0] = in_width * in_depth;
478 break;
480 default:
481 return VO_FALSE;
484 mpi->width = in_width;
485 mpi->flags |= MP_IMGFLAG_DIRECT;
487 return VO_TRUE;
490 static int control(uint32_t request, void *data, ...)
492 switch(request) {
493 case VOCTRL_GET_IMAGE:
494 return get_image(data);
496 case VOCTRL_QUERY_FORMAT:
497 switch(*((uint32_t*)data)) {
498 case IMGFMT_BGR15:
499 case IMGFMT_BGR16:
500 case IMGFMT_BGR24:
501 case IMGFMT_BGR32:
502 case IMGFMT_YUY2:
503 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW |
504 VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN;
507 return 0; /* Not supported */
509 case VOCTRL_FULLSCREEN:
510 setup_screen(!vo_fs);
511 return 0;
514 return VO_NOTIMPL;
517 /* Dummy funcs */
518 static void check_events(void) {}