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
7 * - Use -dr to get direct rendering
8 * - Use -vf yuy2 to get yuy2 rendering, *MUCH* faster than yv12
16 #include <sys/ioctl.h>
22 #ifdef HAVE_SYS_MMAN_H
26 #include "fastmemcpy.h"
27 #include "video_out.h"
28 #include "video_out_internal.h"
32 static vo_info_t info
=
34 "S3 Virge over fbdev",
36 "Mark Sanderson <mmp@kiora.ath.cx>",
42 typedef struct vga_type
{
47 static vga_t
*v
= NULL
;
49 static struct fb_fix_screeninfo fb_finfo
;
50 static struct fb_var_screeninfo fb_vinfo
;
51 static uint32_t in_width
, in_height
, in_format
, in_depth
, in_s3_format
,
52 screenwidth
, screenheight
, screendepth
, screenstride
,
53 vidwidth
, vidheight
, vidx
, vidy
, page
, offset
, sreg
;
54 static char *inpage
, *inpage0
, *smem
= NULL
;
55 static void (*alpha_func
)();
57 static void clear_screen();
59 /* streams registers */
60 #define PSTREAM_CONTROL_REG 0x8180
61 #define COL_CHROMA_KEY_CONTROL_REG 0x8184
62 #define SSTREAM_CONTROL_REG 0x8190
63 #define CHROMA_KEY_UPPER_BOUND_REG 0x8194
64 #define SSTREAM_STRETCH_REG 0x8198
65 #define BLEND_CONTROL_REG 0x81A0
66 #define PSTREAM_FBADDR0_REG 0x81C0
67 #define PSTREAM_FBADDR1_REG 0x81C4
68 #define PSTREAM_STRIDE_REG 0x81C8
69 #define DOUBLE_BUFFER_REG 0x81CC
70 #define SSTREAM_FBADDR0_REG 0x81D0
71 #define SSTREAM_FBADDR1_REG 0x81D4
72 #define SSTREAM_STRIDE_REG 0x81D8
73 #define OPAQUE_OVERLAY_CONTROL_REG 0x81DC
74 #define K1_VSCALE_REG 0x81E0
75 #define K2_VSCALE_REG 0x81E4
76 #define DDA_VERT_REG 0x81E8
77 #define STREAMS_FIFO_REG 0x81EC
78 #define PSTREAM_START_REG 0x81F0
79 #define PSTREAM_WINDOW_SIZE_REG 0x81F4
80 #define SSTREAM_START_REG 0x81F8
81 #define SSTREAM_WINDOW_SIZE_REG 0x81FC
83 #define S3_MEMBASE sreg
84 #define S3_NEWMMIO_REGBASE 0x1000000 /* 16MB */
85 #define S3_NEWMMIO_REGSIZE 0x10000 /* 64KB */
86 #define S3V_MMIO_REGSIZE 0x8000 /* 32KB */
87 #define S3_NEWMMIO_VGABASE (S3_NEWMMIO_REGBASE + 0x8000)
89 #define OUTREG(mmreg, value) *(unsigned int *)(&v->mmio[mmreg]) = value
91 int readcrtc(int reg
) {
96 void writecrtc(int reg
, int value
) {
101 // enable S3 registers
108 v
= malloc(sizeof(vga_t
));
110 if (ioperm(0x3d4, 2, 1) == 0) {
111 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
);
116 if (v
->mmio
!= MAP_FAILED
) {
117 v
->cr38
= readcrtc(0x38);
118 v
->cr39
= readcrtc(0x39);
119 v
->cr53
= readcrtc(0x53);
120 writecrtc(0x38, 0x48);
121 writecrtc(0x39, 0xa5);
122 writecrtc(0x53, 0x08);
135 writecrtc(0x53, v
->cr53
);
136 writecrtc(0x39, v
->cr39
);
137 writecrtc(0x38, v
->cr38
);
139 munmap(v
->mmio
, S3_NEWMMIO_REGSIZE
);
145 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
) {
146 int tmp
, pitch
, start
, src_wc
, src_hc
, bpp
;
148 if (format
== 0 || format
== 7)
150 else if (format
== 6)
155 src_wc
= src_w
- crop
* 2;
156 src_hc
= src_h
- crop
* 2;
159 // video card memory layout:
160 // 0-n: visible screen memory, n = width * height * bytes per pixel
161 // n-m: scaler source memory, n is aligned to a page boundary
162 // m+: scaler source memory for multiple buffers
164 // offset is the first aligned byte after the screen memory, where the scaler input buffer is
165 tmp
= (yres
* line_length
+ 4095) & ~4095;
168 // start is the top left viewable scaler input pixel
169 start
= offset
+ crop
* pitch
+ crop
* bpp
;
171 OUTREG(COL_CHROMA_KEY_CONTROL_REG
, 0x47000000);
172 OUTREG(CHROMA_KEY_UPPER_BOUND_REG
, 0x0);
173 OUTREG(BLEND_CONTROL_REG
, 0x00000020);
174 OUTREG(DOUBLE_BUFFER_REG
, 0x0); /* Choose fbaddr0 as stream source. */
175 OUTREG(OPAQUE_OVERLAY_CONTROL_REG
, 0x0);
177 OUTREG(PSTREAM_CONTROL_REG
, 0x06000000);
178 OUTREG(PSTREAM_FBADDR0_REG
, 0x0);
179 OUTREG(PSTREAM_FBADDR1_REG
, 0x0);
180 OUTREG(PSTREAM_STRIDE_REG
, line_length
);
181 OUTREG(PSTREAM_START_REG
, 0x00010001);
182 OUTREG(PSTREAM_WINDOW_SIZE_REG
, 0x00010001);
183 //OUTREG(SSTREAM_WINDOW_SIZE_REG, ( ((xres-1) << 16) | yres) & 0x7ff07ff);
189 /* format 1=YCbCr-16 2=YUV-16 3=BGR15 4=YUV-16/32(mixed 2/4byte stride) 5=BGR16 6=BGR24 0,7=BGR32 */
190 /* 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. */
191 OUTREG(SSTREAM_CONTROL_REG
, tmp
<< 28 | (format
<< 24) |
192 ((((src_wc
-1)<<1)-(dst_w
-1)) & 0xfff));
193 OUTREG(SSTREAM_STRETCH_REG
,
194 ((src_wc
- 1) & 0x7ff) | (((src_wc
- dst_w
-1) & 0x7ff) << 16));
195 OUTREG(SSTREAM_FBADDR0_REG
, start
& 0x3fffff );
196 OUTREG(SSTREAM_STRIDE_REG
, pitch
& 0xfff );
197 OUTREG(SSTREAM_START_REG
, ((dst_x
+ 1) << 16) | (dst_y
+ 1));
198 OUTREG(SSTREAM_WINDOW_SIZE_REG
, ( ((dst_w
-1) << 16) | (dst_h
) ) & 0x7ff07ff);
199 OUTREG(K1_VSCALE_REG
, src_hc
- 1 );
200 OUTREG(K2_VSCALE_REG
, (src_hc
- dst_h
) & 0x7ff );
201 /* 0xc000 = bw & vert interp */
202 /* 0x8000 = no bw save */
203 OUTREG(DDA_VERT_REG
, (((~dst_h
)-1) & 0xfff ) | 0xc000);
204 writecrtc(0x92, (((pitch
+ 7) / 8) >> 8) | 0x80);
205 writecrtc(0x93, (pitch
+ 7) / 8);
207 writecrtc(0x67, readcrtc(0x67) | 0x4);
213 writecrtc(0x67, readcrtc(0x67) & ~0xc);
214 memset(v
->mmio
+ 0x8180, 0, 0x80);
215 OUTREG(0x81b8, 0x900);
216 OUTREG(0x81bc, 0x900);
217 OUTREG(0x81c8, 0x900);
218 OUTREG(0x81cc, 0x900);
220 OUTREG(0x81f8, 0x07ff07ff);
221 OUTREG(0x81fc, 0x00010001);
226 static int preinit(const char *arg
)
232 else if(!(name
= getenv("FRAMEBUFFER")))
235 if((fd
= open(name
, O_RDWR
)) == -1) {
236 mp_msg(MSGT_VO
, MSGL_FATAL
, "s3fb: can't open %s: %s\n", name
, strerror(errno
));
240 if(ioctl(fd
, FBIOGET_FSCREENINFO
, &fb_finfo
)) {
241 mp_msg(MSGT_VO
, MSGL_FATAL
, "s3fb: problem with FBITGET_FSCREENINFO ioctl: %s\n",
248 if(ioctl(fd
, FBIOGET_VSCREENINFO
, &fb_vinfo
)) {
249 mp_msg(MSGT_VO
, MSGL_FATAL
, "s3fb: problem with FBITGET_VSCREENINFO ioctl: %s\n",
256 // Check the depth now as config() musn't fail
257 switch(fb_vinfo
.bits_per_pixel
) {
263 mp_msg(MSGT_VO
, MSGL_FATAL
, "s3fb: %d bpp output is not supported\n", fb_vinfo
.bits_per_pixel
);
269 /* Open up a window to the hardware */
270 smem
= mmap(0, fb_finfo
.smem_len
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
271 sreg
= fb_finfo
.smem_start
;
273 if(smem
== (void *)-1) {
274 mp_msg(MSGT_VO
, MSGL_FATAL
, "s3fb: Couldn't map memory areas: %s\n", strerror(errno
));
282 mp_msg(MSGT_VO
, MSGL_FATAL
, "s3fb: Couldn't map S3 registers: %s\n", strerror(errno
));
291 /* And close our mess */
292 static void uninit(void)
301 munmap(smem
, fb_finfo
.smem_len
);
313 static void clear_screen()
318 memset(smem
, 0, screenheight
* screenstride
);
320 if (in_format
== IMGFMT_YUY2
) {
324 ptr
= (unsigned short *)inpage0
;
325 n
= in_width
* in_height
;
326 if (vo_doublebuffering
)
332 n
= in_depth
* in_width
* in_height
;
333 if (vo_doublebuffering
)
335 memset(inpage0
, 0, n
);
340 /* Setup output screen dimensions etc */
341 static void setup_screen(uint32_t full
)
345 aspect(&vidwidth
, &vidheight
, full
? A_ZOOM
: A_NOZOOM
);
348 vidx
= (screenwidth
- vidwidth
) / 2;
349 vidy
= (screenheight
- vidheight
) / 2;
351 geometry(&vidx
, &vidy
, &vidwidth
, &vidheight
, screenwidth
, screenheight
);
354 inpageoffset
= yuv_on(in_s3_format
, in_width
, in_height
, vidx
, vidy
, vidwidth
, vidheight
, 0, screenwidth
, screenheight
, screenstride
, 0);
355 inpage0
= smem
+ inpageoffset
;
357 mp_msg(MSGT_VO
, MSGL_INFO
, "s3fb: output is at %dx%d +%dx%d\n", vidx
, vidy
, vidwidth
, vidheight
);
362 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
, uint32_t d_height
,
363 uint32_t flags
, char *title
, uint32_t format
)
365 screenwidth
= fb_vinfo
.xres
;
366 screenheight
= fb_vinfo
.yres
;
367 screenstride
= fb_finfo
.line_length
;
368 aspect_save_screenres(fb_vinfo
.xres
,fb_vinfo
.yres
);
373 aspect_save_orig(width
,height
);
375 aspect_save_prescale(d_width
,d_height
);
377 /* Setup the screen for rendering to */
378 screendepth
= fb_vinfo
.bits_per_pixel
/ 8;
385 alpha_func
= vo_draw_alpha_yuy2
;
391 alpha_func
= vo_draw_alpha_rgb16
;
397 alpha_func
= vo_draw_alpha_rgb16
;
403 alpha_func
= vo_draw_alpha_rgb24
;
409 alpha_func
= vo_draw_alpha_rgb32
;
413 mp_msg(MSGT_VO
, MSGL_FATAL
, "s3fb: Eik! Something's wrong with control().\n");
417 offset
= in_width
* in_depth
* in_height
;
418 if (vo_doublebuffering
)
423 if(screenheight
* screenstride
+ page
+ offset
> fb_finfo
.smem_len
) {
424 mp_msg(MSGT_VO
, MSGL_FATAL
, "s3fb: Not enough video memory to play this movie. Try at a lower resolution\n");
428 setup_screen(flags
& VOFLAG_FULLSCREEN
);
429 if (vo_doublebuffering
)
430 inpage
= inpage0
+ page
;
432 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",
433 screenwidth
, screenheight
, screendepth
* 8,
434 in_width
, in_height
, in_depth
* 8,
440 static void draw_alpha(int x
, int y
, int w
, int h
, unsigned char *src
,
441 unsigned char *srca
, int stride
)
443 char *dst
= inpage
+ (y
* in_width
+ x
) * in_depth
;
444 alpha_func(w
, h
, src
, srca
, stride
, dst
, in_width
* in_depth
);
447 static void draw_osd(void)
449 if (!vo_doublebuffering
)
450 vo_draw_text(in_width
, in_height
, draw_alpha
);
453 /* Render onto the screen */
454 static void flip_page(void)
456 if(vo_doublebuffering
) {
457 vo_draw_text(in_width
, in_height
, draw_alpha
);
458 yuv_on(in_s3_format
, in_width
, in_height
, vidx
, vidy
, vidwidth
, vidheight
, 0, screenwidth
, screenheight
, screenstride
, page
);
460 inpage
= inpage0
+ page
;
464 static int draw_frame(uint8_t *src
[])
466 mem2agpcpy(inpage
, src
[0], in_width
* in_depth
* in_height
);
470 static int draw_slice(uint8_t *i
[], int s
[], int w
, int h
, int x
, int y
)
475 /* Attempt to start doing DR */
476 static uint32_t get_image(mp_image_t
*mpi
)
479 if(mpi
->flags
& MP_IMGFLAG_READABLE
)
481 if(mpi
->type
== MP_IMGTYPE_STATIC
&& vo_doublebuffering
)
483 if(mpi
->type
> MP_IMGTYPE_TEMP
)
484 return VO_FALSE
; // TODO ??
492 mpi
->planes
[0] = inpage
;
493 mpi
->stride
[0] = in_width
* in_depth
;
500 mpi
->width
= in_width
;
501 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
506 static int control(uint32_t request
, void *data
, ...)
509 case VOCTRL_GET_IMAGE
:
510 return get_image(data
);
512 case VOCTRL_QUERY_FORMAT
:
513 switch(*((uint32_t*)data
)) {
519 return VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
|
520 VFCAP_OSD
| VFCAP_HWSCALE_UP
| VFCAP_HWSCALE_DOWN
;
523 return 0; /* Not supported */
525 case VOCTRL_FULLSCREEN
:
526 setup_screen(!vo_fs
);
534 static void check_events(void) {}