Comment out the correct #endif directive.
[mplayer/greg.git] / libvo / vo_xvr100.c
blobff92018ad9ef097041272f002e5be58fa32a9957
1 /*
2 * SUN XVR-100 (ATI Radeon 7000) VO driver for SPARC Solaris(at least)
4 * Copyright (C) 2000-2004 Robin Kay <komadori [at] gekkou [dot] co [dot] uk>
5 * Copyright (C) 2004 Jake Goerzen
6 * Copyright (C) 2007 Denes Balatoni
8 * written for xine by
9 * Robin Kay <komadori [at] gekkou [dot] co [dot] uk>
11 * Sun XVR-100 framebuffer graciously donated by Jake Goerzen.
13 * Ported to mplayer by Denes Balatoni
14 * Contains portions from the mga and tdfix_vid vo drivers
16 * no double-buffering, as it would slow down playback (waiting for vertical retraces)
17 * FIXME: only YV12 supported for now
19 * This file is part of MPlayer.
21 * MPlayer is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
26 * MPlayer is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with MPlayer; if not, write to the Free Software
33 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <sys/fbio.h>
42 #include <sys/visual_io.h>
43 #include <strings.h>
44 #include <sys/mman.h>
46 #include "config.h"
47 #include "video_out.h"
48 #include "video_out_internal.h"
49 #include "aspect.h"
50 #include "geometry.h"
51 #include "fastmemcpy.h"
52 #include "sub.h"
53 #include "mp_msg.h"
56 static const vo_info_t info = {
57 "sun xvr-100",
58 "xvr100",
59 "Denes Balatoni",
63 const LIBVO_EXTERN(xvr100)
65 #define PFB_VRAM_MMAPBASE 0x08000000
66 #define PFB_VRAM_MMAPLEN 0x04000000 /* depends on memory size */
67 #define PFB_REGS_MMAPBASE 0x10000000
68 #define PFB_REGS_MMAPLEN 0x00040000
70 #define PFB_CLOCK_CNTL_INDEX 0x002
71 #define PFB_CLOCK_CNTL_DATA 0x003
73 #define PFB_MC_FB_LOCATION 0x052
75 #define PFB_OV0_Y_X_START 0x100
76 #define PFB_OV0_Y_X_END 0x101
77 #define PFB_OV0_REG_LOAD_CNTL 0x104
78 #define PFB_OV0_REG_LOAD_LOCK 0x00000001
79 #define PFB_OV0_REG_LOAD_LOCK_READBACK 0x00000008
80 #define PFB_OV0_SCALE_CNTL 0x108
81 #define PFB_OV0_SCALE_EN 0x417f0000/*417f0000*/
82 #define PFB_OV0_SCALE_YUV12 0x00000A00
83 #define PFB_OV0_SCALE_VYUY422 0x00000B00
84 #define PFB_OV0_V_INC 0x109
85 #define PFB_OV0_P1_V_ACCUM_INIT 0x10A
86 #define PFB_OV0_P23_V_ACCUM_INIT 0x10B
87 #define PFB_OV0_P1_BLANK_LINES_AT_TOP 0x10C
88 #define PFB_OV0_P23_BLANK_LINES_AT_TOP 0x10D
89 #define PFB_OV0_BASE_ADDR 0x10F
90 #define PFB_OV0_BUF0_BASE_ADRS 0x110
91 #define PFB_OV0_BUF1_BASE_ADRS 0x111
92 #define PFB_OV0_BUF2_BASE_ADRS 0x112
93 #define PFB_OV0_BUF3_BASE_ADRS 0x113
94 #define PFB_OV0_BUF4_BASE_ADRS 0x114
95 #define PFB_OV0_BUF5_BASE_ADRS 0x115
96 #define PFB_OV0_VID_BUF_PITCH0_VALUE 0x118
97 #define PFB_OV0_VID_BUF_PITCH1_VALUE 0x119
98 #define PFB_OV0_AUTO_FLIP_CNTL 0x11C
99 #define PFB_OV0_AUTO_FLIP_BUF0 0x00000200
100 #define PFB_OV0_AUTO_FLIP_BUF3 0x00000243
101 #define PFB_OV0_DEINTERLACE_PATTERN 0x11D
102 #define PFB_OV0_H_INC 0x120
103 #define PFB_OV0_STEP_BY 0x121
104 #define PFB_OV0_P1_H_ACCUM_INIT 0x122
105 #define PFB_OV0_P23_H_ACCUM_INIT 0x123
106 #define PFB_OV0_P1_X_START_END 0x125
107 #define PFB_OV0_P2_X_START_END 0x126
108 #define PFB_OV0_P3_X_START_END 0x127
109 #define PFB_OV0_FILTER_CNTL 0x128
110 #define PFB_OV0_FILTER_EN 0x0000000f
111 #define PFB_OV0_GRPH_KEY_CLR_LOW 0x13B
112 #define PFB_OV0_GRPH_KEY_CLR_HIGH 0x13C
113 #define PFB_OV0_KEY_CNTL 0x13D
114 #define PFB_OV0_KEY_EN 0x00000121
116 #define PFB_DISP_MERGE_CNTL 0x358
117 #define PFB_DISP_MERGE_EN 0xffff0000
121 static char pfb_devname[]="/dev/fbs/pfb0";
122 static int pfb_devfd;
123 static uint8_t *pfb_vbase;
124 static volatile uint32_t *pfb_vregs;
125 static int pfb_buffer[3];
126 static int pfb_stride[3];
127 static int pfb_srcwidth, pfb_srcheight, pfb_dstwidth, pfb_dstheight;
128 static int pfb_native_format=PFB_OV0_SCALE_YUV12;
129 static int pfb_deinterlace_en=0;
130 static short int pfb_wx0, pfb_wy0, pfb_wx1, pfb_wy1;
131 static int pfb_xres,pfb_yres;
132 static int pfb_free_top;
133 static int pfb_fs;
134 static int pfb_usecolorkey=0;
135 static uint32_t pfb_colorkey;
139 void pfb_overlay_on() {
140 int h_inc, h_step, ecp_div;
142 pfb_vregs[PFB_CLOCK_CNTL_INDEX] = (pfb_vregs[PFB_CLOCK_CNTL_INDEX] & ~0x0000003f) | 0x00000008;
143 ecp_div = (pfb_vregs[PFB_CLOCK_CNTL_DATA] >> 8) & 0x3;
144 h_inc = (pfb_srcwidth << (12 + ecp_div)) / pfb_dstwidth;
145 h_step = 1;
147 while (h_inc > 0x1fff) {
148 h_inc >>= 1;
149 h_step++;
152 pfb_vregs[PFB_OV0_REG_LOAD_CNTL] = PFB_OV0_REG_LOAD_LOCK;
153 while (!(pfb_vregs[PFB_OV0_REG_LOAD_CNTL] & PFB_OV0_REG_LOAD_LOCK_READBACK))
154 usleep(100);
156 pfb_vregs[PFB_DISP_MERGE_CNTL] = PFB_DISP_MERGE_EN;
157 pfb_vregs[PFB_OV0_Y_X_START] = (pfb_wy0 << 16) | pfb_wx0;
158 pfb_vregs[PFB_OV0_Y_X_END] = ((pfb_wy1 - 1) << 16) | (pfb_wx1 - 1);
159 pfb_vregs[PFB_OV0_V_INC] = ((pfb_deinterlace_en ? pfb_srcheight/2 : pfb_srcheight) << 20) / pfb_dstheight;
160 pfb_vregs[PFB_OV0_P1_V_ACCUM_INIT] = 0x00180001;
161 pfb_vregs[PFB_OV0_P23_V_ACCUM_INIT] = 0x00180001;
162 pfb_vregs[PFB_OV0_P1_BLANK_LINES_AT_TOP] = (((pfb_deinterlace_en ? pfb_srcheight/2 : pfb_srcheight) - 1) << 16) | 0xfff;
163 pfb_vregs[PFB_OV0_P23_BLANK_LINES_AT_TOP] = (((pfb_deinterlace_en ? pfb_srcheight/2 : pfb_srcheight) / 2 - 1) << 16) | 0x7ff;
164 pfb_vregs[PFB_OV0_BASE_ADDR] = (pfb_vregs[PFB_MC_FB_LOCATION] & 0xffff) << 16;
165 pfb_vregs[PFB_OV0_VID_BUF_PITCH0_VALUE] = pfb_deinterlace_en ? pfb_stride[0]*2 : pfb_stride[0];
166 pfb_vregs[PFB_OV0_VID_BUF_PITCH1_VALUE] = pfb_deinterlace_en ? pfb_stride[1]*2 : pfb_stride[1];
167 pfb_vregs[PFB_OV0_DEINTERLACE_PATTERN] = 0x000aaaaa;
168 pfb_vregs[PFB_OV0_H_INC] = ((h_inc / 2) << 16) | h_inc;
169 pfb_vregs[PFB_OV0_STEP_BY] = (h_step << 8) | h_step;
170 pfb_vregs[PFB_OV0_P1_H_ACCUM_INIT] = (((0x00005000 + h_inc) << 7) & 0x000f8000) | (((0x00005000 + h_inc) << 15) & 0xf0000000);
171 pfb_vregs[PFB_OV0_P23_H_ACCUM_INIT] = (((0x0000A000 + h_inc) << 6) & 0x000f8000) | (((0x0000A000 + h_inc) << 14) & 0x70000000);
172 pfb_vregs[PFB_OV0_P1_X_START_END] = pfb_srcwidth - 1;
173 pfb_vregs[PFB_OV0_P2_X_START_END] = (pfb_srcwidth / 2) - 1;
174 pfb_vregs[PFB_OV0_P3_X_START_END] = (pfb_srcwidth / 2) - 1;
175 pfb_vregs[PFB_OV0_FILTER_CNTL] = PFB_OV0_FILTER_EN;
177 if (pfb_usecolorkey) {
178 pfb_vregs[PFB_OV0_GRPH_KEY_CLR_LOW] = pfb_colorkey;
179 pfb_vregs[PFB_OV0_GRPH_KEY_CLR_HIGH] = pfb_colorkey | 0xff000000;
180 pfb_vregs[PFB_OV0_KEY_CNTL] = PFB_OV0_KEY_EN;
181 } else {
182 pfb_vregs[PFB_OV0_KEY_CNTL] = 0x010;
185 pfb_vregs[PFB_OV0_SCALE_CNTL] = PFB_OV0_SCALE_EN | pfb_native_format;
187 pfb_vregs[PFB_OV0_REG_LOAD_CNTL] = 0;
189 pfb_vregs[PFB_OV0_BUF0_BASE_ADRS] = pfb_buffer[0];
190 pfb_vregs[PFB_OV0_BUF1_BASE_ADRS] = pfb_buffer[1] | 0x00000001;
191 pfb_vregs[PFB_OV0_BUF2_BASE_ADRS] = pfb_buffer[2] | 0x00000001;
193 pfb_vregs[PFB_OV0_AUTO_FLIP_CNTL] = PFB_OV0_AUTO_FLIP_BUF0;
196 void pfb_overlay_off() {
197 pfb_vregs[PFB_OV0_SCALE_CNTL] = 0;
200 void center_overlay() {
201 if (pfb_xres > pfb_dstwidth) {
202 pfb_wx0 = (pfb_xres - pfb_dstwidth) / 2;
203 pfb_wx1 = pfb_wx0 + pfb_dstwidth;
205 else {
206 pfb_wx0 = 0;
207 pfb_wx1 = pfb_xres;
210 if (pfb_yres > pfb_dstheight) {
211 pfb_wy0 = (pfb_yres - pfb_dstheight) / 2;
212 pfb_wy1 = pfb_wy0 + pfb_dstheight;
214 else {
215 pfb_wy0 = 0;
216 pfb_wy1 = pfb_yres;
220 static int config(uint32_t width, uint32_t height, uint32_t d_width,
221 uint32_t d_height, uint32_t flags, char *title,
222 uint32_t format) {
223 int memsize;
225 pfb_srcwidth=width;
226 pfb_srcheight=height;
228 if (pfb_srcwidth>1536)
229 mp_msg(MSGT_VO, MSGL_WARN, "vo_xvr100: XVR-100 can not handle width greater than 1536 pixels!\n");
231 if (!(flags & VOFLAG_XOVERLAY_SUB_VO)) {
232 aspect_save_orig(width,height);
233 aspect_save_prescale(d_width,d_height);
234 aspect_save_screenres(pfb_xres,pfb_yres);
235 if( flags&VOFLAG_FULLSCREEN) { /* -fs */
236 aspect(&pfb_dstwidth,&pfb_dstheight, A_ZOOM);
237 pfb_fs = 1;
238 } else {
239 aspect(&pfb_dstwidth,&pfb_dstheight, A_NOZOOM);
240 pfb_fs = 0;
242 } else {
243 pfb_dstwidth=d_width;
244 pfb_dstheight=d_height;
247 center_overlay();
249 pfb_stride[0]=(pfb_srcwidth+15) & ~15;
250 pfb_stride[1]=pfb_stride[2]=(((pfb_srcwidth+1)>>1)+15) & ~15;
251 memsize = (pfb_stride[0]*pfb_srcheight+pfb_stride[1]*((pfb_srcheight+1) & ~1));
252 if (memsize > pfb_free_top) {
253 mp_msg(MSGT_VO, MSGL_FATAL, "vo_xvr100: out of VRAM! \n");
254 return 1;
256 pfb_buffer[0] = pfb_free_top - memsize;
257 pfb_buffer[1] = pfb_buffer[0] + pfb_stride[0]*pfb_srcheight;
258 pfb_buffer[2] = pfb_buffer[1] + pfb_stride[1]*((pfb_srcheight+1)>>1);
260 pfb_overlay_on();
262 return 0;
265 static int preinit(const char *arg) {
266 struct vis_identifier ident;
267 struct fbgattr attr;
269 if ((pfb_devfd = open(pfb_devname, O_RDWR)) < 0) {
270 mp_msg(MSGT_VO, MSGL_ERR, "vo_xvr100: Error: can't open framebuffer device '%s'\n", pfb_devname);
271 return 1;
274 if (ioctl(pfb_devfd, VIS_GETIDENTIFIER, &ident) < 0) {
275 mp_msg(MSGT_VO, MSGL_ERR, "vo_xvr100: Error: ioctl failed (VIS_GETIDENTIFIER), bad device (%s)\n", pfb_devname);
276 return 1;
279 if (strcmp("SUNWpfb", ident.name) == 0) {
280 mp_msg(MSGT_VO, MSGL_INFO, "vo_xvr100: SUNWpfb (XVR-100/ATI Radeon 7000) detected \n");
282 else {
283 mp_msg(MSGT_VO, MSGL_ERR, "vo_xvr100: Error: '%s' is not a SUN XVR-100 framebuffer device\n", pfb_devname);
284 return 1;
287 if (ioctl(pfb_devfd, FBIOGATTR, &attr) < 0) {
288 mp_msg(MSGT_VO, MSGL_ERR, "vo_xvr100: Error: ioctl failed (FBIOGATTR)\n");
289 close(pfb_devfd);
290 return 1;
293 pfb_free_top = attr.fbtype.fb_size - 0x2000;
294 pfb_xres = attr.fbtype.fb_width;
295 pfb_yres = attr.fbtype.fb_height;
297 if ((pfb_vbase = mmap(NULL, PFB_VRAM_MMAPLEN, PROT_READ | PROT_WRITE,
298 MAP_SHARED, pfb_devfd, PFB_VRAM_MMAPBASE)) == MAP_FAILED) {
299 mp_msg(MSGT_VO, MSGL_ERR, "vo_xvr100: Error: unable to memory map framebuffer\n");
300 close(pfb_devfd);
301 return 1;
304 if ((pfb_vregs = (uint32_t *)(void *)mmap(NULL, PFB_REGS_MMAPLEN, PROT_READ | PROT_WRITE,
305 MAP_SHARED, pfb_devfd, PFB_REGS_MMAPBASE)) == MAP_FAILED) {
306 mp_msg(MSGT_VO, MSGL_ERR, "vo_xvr100: Error: unable to memory map framebuffer\n");
307 munmap(pfb_vbase, PFB_VRAM_MMAPLEN);
308 close(pfb_devfd);
309 return 1;
312 return 0;
315 static void uninit(void)
317 if (!vo_config_count)
318 return;
320 pfb_overlay_off();
321 munmap(pfb_vbase, PFB_VRAM_MMAPLEN);
322 munmap(pfb_vregs, PFB_REGS_MMAPLEN);
325 static uint32_t pfb_fullscreen() {
326 if (!pfb_fs) {
327 aspect(&pfb_dstwidth,&pfb_dstheight, A_ZOOM);
328 pfb_fs = 1;
329 } else {
330 aspect(&pfb_dstwidth,&pfb_dstheight, A_NOZOOM);
331 pfb_fs = 0;
334 center_overlay();
336 pfb_overlay_on();
338 return VO_TRUE;
341 static void draw_alpha(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
342 vo_draw_alpha_yv12(w,h,src,srca,stride,pfb_vbase+pfb_buffer[0]+pfb_stride[0]*y0+x0,pfb_stride[0]);
345 static void draw_osd(void)
347 vo_draw_text(pfb_srcwidth, pfb_srcheight,draw_alpha);
350 static void check_events(void)
354 static void flip_page(void)
358 static uint32_t get_image(mp_image_t *mpi){
359 if (mpi->flags&MP_IMGFLAG_READABLE) return VO_FALSE;
360 if (!(mpi->flags&MP_IMGFLAG_PLANAR)) return VO_FALSE; // FIXME: impossible for YV12, right?
361 if (!(mpi->flags&MP_IMGFLAG_ACCEPT_STRIDE)) return VO_FALSE;
363 mpi->planes[0]=pfb_vbase + pfb_buffer[0];
364 mpi->planes[1]=pfb_vbase + pfb_buffer[1];
365 mpi->planes[2]=pfb_vbase + pfb_buffer[2];
366 mpi->stride[0]=pfb_stride[0];
367 mpi->stride[1]=mpi->stride[2]=pfb_stride[1];
368 mpi->flags|=MP_IMGFLAG_DIRECT;
370 return VO_TRUE;
373 static int draw_frame(uint8_t *src[])
375 mp_msg(MSGT_VO,MSGL_WARN,"!!! vo_xvr100::draw_frame() called !!!\n");
376 return 0;
380 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
382 mem2agpcpy_pic(pfb_vbase + pfb_buffer[0] + pfb_stride[0] * y + x, src[0], w, h, pfb_stride[0], stride[0]);
383 w>>=1; h>>=1; x>>=1; y>>=1;
384 mem2agpcpy_pic(pfb_vbase + pfb_buffer[1] + pfb_stride[1] * y + x, src[1], w, h, pfb_stride[1], stride[1]);
385 mem2agpcpy_pic(pfb_vbase + pfb_buffer[2] + pfb_stride[1] * y + x, src[2], w, h, pfb_stride[1], stride[2]);
387 return 0;
391 static uint32_t draw_image(mp_image_t *mpi){
392 // if -dr or -slices then do nothing:
393 if (mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK)) return VO_TRUE;
395 draw_slice(mpi->planes,mpi->stride,mpi->w,mpi->h,0,0);
397 return VO_TRUE;
400 static uint32_t pfb_set_colorkey(mp_colorkey_t* colork) {
401 pfb_colorkey = colork->x11;
402 pfb_usecolorkey = 1;
404 pfb_overlay_on();
406 return VO_TRUE;
409 static uint32_t pfb_set_window(mp_win_t* w) {
410 pfb_dstwidth = w->w;
411 pfb_dstheight = w->h;
412 pfb_wx0 = w->x;
413 pfb_wy0 = w->y;
414 pfb_wx1 = w->x + pfb_dstwidth;
415 pfb_wy1 = w->y + pfb_dstheight;
417 pfb_overlay_on();
419 return VO_TRUE;
422 static int query_format(uint32_t format)
424 switch(format){
425 case IMGFMT_YV12:
426 case IMGFMT_I420:
427 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW |VFCAP_HWSCALE_UP|VFCAP_HWSCALE_DOWN|VFCAP_ACCEPT_STRIDE;
429 return 0;
432 static int control(uint32_t request, void *data, ...)
434 switch (request) {
435 case VOCTRL_GET_IMAGE:
436 return get_image(data);
437 case VOCTRL_QUERY_FORMAT:
438 return query_format(*((uint32_t*)data));
439 case VOCTRL_DRAW_IMAGE:
440 return draw_image(data);
441 case VOCTRL_FULLSCREEN:
442 return pfb_fullscreen();
443 case VOCTRL_XOVERLAY_SUPPORT:
444 return VO_TRUE;
445 case VOCTRL_XOVERLAY_SET_COLORKEY:
446 return pfb_set_colorkey(data);
447 case VOCTRL_XOVERLAY_SET_WIN:
448 return pfb_set_window(data);
451 return VO_NOTIMPL;