1 /*****************************************************************************
2 * mga.c : Matrox Graphic Array plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: mga.c,v 1.19 2002/07/20 18:01:43 sam Exp $
7 * Authors: Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
8 * Samuel Hocevar <sam@zoy.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <unistd.h> /* close() */
30 #include <stdlib.h> /* free() */
31 #include <string.h> /* strerror() */
32 #include <fcntl.h> /* open() */
33 #include <sys/ioctl.h> /* ioctl() */
34 #include <sys/mman.h> /* PROT_WRITE */
40 #include <sys/types.h> /* typedef ushort */
43 /*****************************************************************************
44 * Capabilities defined in the other files.
45 *****************************************************************************/
46 static void vout_getfunctions( function_list_t * p_function_list );
48 static int vout_Create ( vout_thread_t * );
49 static int vout_Init ( vout_thread_t * );
50 static void vout_End ( vout_thread_t * );
51 static void vout_Destroy ( vout_thread_t * );
52 static int vout_Manage ( vout_thread_t * );
53 static void vout_Render ( vout_thread_t *, picture_t * );
54 static void vout_Display ( vout_thread_t *, picture_t * );
56 static int NewPicture ( vout_thread_t *, picture_t * );
58 /*****************************************************************************
59 * Building configuration tree
60 *****************************************************************************/
65 SET_DESCRIPTION( _("Matrox Graphic Array video module") )
66 ADD_CAPABILITY( VOUT, 10 )
70 vout_getfunctions( &p_module->p_functions->vout );
73 MODULE_DEACTIVATE_START
74 MODULE_DEACTIVATE_STOP
76 /*****************************************************************************
77 * vout_sys_t: video output MGA method descriptor
78 *****************************************************************************
79 * This structure is part of the video output thread descriptor.
80 * It describes the MGA specific properties of an output thread.
81 *****************************************************************************/
82 #ifndef __LINUX_MGAVID_H
83 # define __LINUX_MGAVID_H
85 # define MGA_VID_CONFIG _IOR('J', 1, mga_vid_config_t)
86 # define MGA_VID_ON _IO ('J', 2)
87 # define MGA_VID_OFF _IO ('J', 3)
88 # define MGA_VID_FSEL _IOR('J', 4, int)
89 # define MGA_G200 0x1234
90 # define MGA_G400 0x5678
92 # define MGA_VID_FORMAT_YV12 0x32315659
93 # define MGA_VID_FORMAT_IYUV (('I'<<24)|('Y'<<16)|('U'<<8)|'V')
94 # define MGA_VID_FORMAT_I420 (('I'<<24)|('4'<<16)|('2'<<8)|'0')
95 # define MGA_VID_FORMAT_YUY2 (('Y'<<24)|('U'<<16)|('Y'<<8)|'2')
96 # define MGA_VID_FORMAT_UYVY (('U'<<24)|('Y'<<16)|('V'<<8)|'Y')
98 # define MGA_VID_VERSION 0x0201
100 # define MGA_NUM_FRAMES 1
102 typedef struct mga_vid_config_t
125 mga_vid_config_t mga;
135 #define CEIL32(x) (((x)+31)&~31)
137 /*****************************************************************************
138 * Functions exported as capabilities. They are declared as static so that
139 * we don't pollute the namespace too much.
140 *****************************************************************************/
141 static void vout_getfunctions( function_list_t * p_function_list )
143 p_function_list->functions.vout.pf_create = vout_Create;
144 p_function_list->functions.vout.pf_init = vout_Init;
145 p_function_list->functions.vout.pf_end = vout_End;
146 p_function_list->functions.vout.pf_destroy = vout_Destroy;
147 p_function_list->functions.vout.pf_manage = vout_Manage;
148 p_function_list->functions.vout.pf_render = vout_Render;
149 p_function_list->functions.vout.pf_display = vout_Display;
152 /*****************************************************************************
153 * vout_Create: allocates dummy video thread output method
154 *****************************************************************************
155 * This function allocates and initializes a dummy vout method.
156 *****************************************************************************/
157 static int vout_Create( vout_thread_t *p_vout )
159 /* Allocate structure */
160 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
161 if( p_vout->p_sys == NULL )
163 msg_Err( p_vout, "out of memory" );
167 p_vout->p_sys->i_fd = open( "/dev/mga_vid", O_RDWR );
168 if( p_vout->p_sys->i_fd == -1 )
170 msg_Err( p_vout, "cannot open MGA driver /dev/mga_vid" );
171 free( p_vout->p_sys );
178 /*****************************************************************************
179 * vout_Init: initialize dummy video thread output method
180 *****************************************************************************/
181 static int vout_Init( vout_thread_t *p_vout )
186 I_OUTPUTPICTURES = 0;
188 /* create the MGA output */
189 p_vout->output.i_width = p_vout->render.i_width;
190 p_vout->output.i_height = p_vout->render.i_height;
191 p_vout->output.i_aspect = p_vout->render.i_aspect;
193 /* Set coordinates and aspect ratio */
194 p_vout->p_sys->mga.src_width = CEIL32(p_vout->output.i_width);
195 p_vout->p_sys->mga.src_height = p_vout->output.i_height;
196 vout_PlacePicture( p_vout, 1024, 768,
197 &p_vout->p_sys->mga.x_org, &p_vout->p_sys->mga.y_org,
198 &p_vout->p_sys->mga.dest_width,
199 &p_vout->p_sys->mga.dest_height );
201 /* Initialize a video buffer */
202 p_vout->p_sys->mga.colkey_on = 0;
203 p_vout->p_sys->mga.num_frames = MGA_NUM_FRAMES;
204 p_vout->p_sys->mga.frame_size = CEIL32(p_vout->output.i_width)
205 * p_vout->output.i_height * 2;
206 p_vout->p_sys->mga.version = MGA_VID_VERSION;
208 /* Assume we only do YMGA for the moment. XXX: mga_vid calls this
209 * YV12, but it's actually some strange format with packed UV. */
210 p_vout->output.i_chroma = FOURCC_YMGA;
211 p_vout->p_sys->mga.format = MGA_VID_FORMAT_YV12;
213 if( ioctl(p_vout->p_sys->i_fd, MGA_VID_CONFIG, &p_vout->p_sys->mga) )
215 msg_Err( p_vout, "MGA config ioctl failed" );
219 if( p_vout->p_sys->mga.card_type == MGA_G200 )
221 msg_Dbg( p_vout, "detected MGA G200 (%d MB Ram)",
222 p_vout->p_sys->mga.ram_size );
226 msg_Dbg( p_vout, "detected MGA G400/G450 (%d MB Ram)",
227 p_vout->p_sys->mga.ram_size );
230 p_vout->p_sys->p_video = mmap( 0, p_vout->p_sys->mga.frame_size
232 PROT_WRITE, MAP_SHARED,
233 p_vout->p_sys->i_fd, 0 );
235 /* Try to initialize up to MGA_NUM_FRAMES direct buffers */
236 while( I_OUTPUTPICTURES < MGA_NUM_FRAMES )
240 /* Find an empty picture slot */
241 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
243 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
245 p_pic = p_vout->p_picture + i_index;
250 /* Allocate the picture */
251 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
256 p_pic->i_status = DESTROYED_PICTURE;
257 p_pic->i_type = DIRECT_PICTURE;
259 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
264 /* Blank the windows */
265 for( i_index = 0; i_index < I_OUTPUTPICTURES; i_index++ )
267 memset( p_vout->p_sys->p_video
268 + p_vout->p_sys->mga.frame_size * i_index,
269 0x00, p_vout->p_sys->mga.frame_size / 2 );
270 memset( p_vout->p_sys->p_video
271 + p_vout->p_sys->mga.frame_size * ( 2*i_index + 1 ) / 2,
272 0x80, p_vout->p_sys->mga.frame_size / 2 );
275 /* Display the image */
276 ioctl( p_vout->p_sys->i_fd, MGA_VID_ON, 0 );
281 /*****************************************************************************
282 * vout_End: terminate dummy video thread output method
283 *****************************************************************************/
284 static void vout_End( vout_thread_t *p_vout )
288 ioctl( p_vout->p_sys->i_fd, MGA_VID_OFF, 0 );
290 /* Free the output buffers we allocated */
291 for( i_index = I_OUTPUTPICTURES ; i_index ; )
297 /*****************************************************************************
298 * vout_Destroy: destroy dummy video thread output method
299 *****************************************************************************
300 * Terminate an output method created by DummyCreateOutputMethod
301 *****************************************************************************/
302 static void vout_Destroy( vout_thread_t *p_vout )
304 close( p_vout->p_sys->i_fd );
306 free( p_vout->p_sys );
309 /*****************************************************************************
310 * vout_Manage: handle dummy events
311 *****************************************************************************
312 * This function should be called regularly by video output thread. It manages
313 * console events. It returns a non null value on error.
314 *****************************************************************************/
315 static int vout_Manage( vout_thread_t *p_vout )
320 /*****************************************************************************
321 * vout_Render: displays previously calculated output
322 *****************************************************************************/
323 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
325 /* Grmbl, if we have a G200 which mistakenly assumes 4:2:0 planar
326 * has *packed* chroma information, we'll need to do some
327 * vonversion... but vlc does this for us. */
330 /*****************************************************************************
331 * vout_Display: displays previously rendered output
332 *****************************************************************************/
333 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
335 ioctl( p_vout->p_sys->i_fd, MGA_VID_FSEL, &p_pic->p_sys->i_frame );
338 /* Following functions are local */
340 /*****************************************************************************
341 * NewPicture: allocate a picture
342 *****************************************************************************
343 * Returns 0 on success, -1 otherwise
344 *****************************************************************************/
345 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
347 /* We know the chroma, allocate a buffer which will be used
348 * directly by the decoder */
349 p_pic->p_data = p_vout->p_sys->p_video + I_OUTPUTPICTURES
350 * p_vout->p_sys->mga.frame_size;
352 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
354 if( p_pic->p_sys == NULL )
359 p_pic->Y_PIXELS = p_pic->p_data;
360 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
361 p_pic->p[Y_PLANE].i_pitch = CEIL32( p_vout->output.i_width );
362 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
364 if( p_pic->p[Y_PLANE].i_pitch == p_vout->output.i_width )
366 p_pic->p[Y_PLANE].b_margin = 0;
370 /* FIXME: do something here */
371 p_pic->p[Y_PLANE].b_margin = 0;
374 p_pic->U_PIXELS = p_pic->p_data + p_vout->p_sys->mga.frame_size * 2/4;
375 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
376 p_pic->p[U_PLANE].i_pitch = CEIL32( p_vout->output.i_width ) / 2;
377 p_pic->p[U_PLANE].i_pixel_bytes = 1;
378 p_pic->p[U_PLANE].b_margin = 0;
380 p_pic->V_PIXELS = p_pic->p_data + p_vout->p_sys->mga.frame_size * 3/4;
381 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
382 p_pic->p[V_PLANE].i_pitch = CEIL32( p_vout->output.i_width ) / 2;
383 p_pic->p[V_PLANE].i_pixel_bytes = 1;
384 p_pic->p[V_PLANE].b_margin = 0;
386 p_pic->p_sys->i_frame = I_OUTPUTPICTURES;