1 /*****************************************************************************
2 * mga.c : Matrox Graphic Array plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: mga.c,v 1.15 2002/01/07 02:12:29 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 */
36 #include <videolan/vlc.h>
39 #include <sys/types.h> /* typedef ushort */
43 #include "video_output.h"
45 /*****************************************************************************
46 * Capabilities defined in the other files.
47 *****************************************************************************/
48 static void vout_getfunctions( function_list_t * p_function_list );
50 static int vout_Probe ( probedata_t *p_data );
51 static int vout_Create ( vout_thread_t * );
52 static int vout_Init ( vout_thread_t * );
53 static void vout_End ( vout_thread_t * );
54 static void vout_Destroy ( vout_thread_t * );
55 static int vout_Manage ( vout_thread_t * );
56 static void vout_Render ( vout_thread_t *, picture_t * );
57 static void vout_Display ( vout_thread_t *, picture_t * );
59 static int NewPicture ( vout_thread_t *, picture_t * );
61 /*****************************************************************************
62 * Building configuration tree
63 *****************************************************************************/
68 SET_DESCRIPTION( "Matrox Graphic Array video module" )
69 ADD_CAPABILITY( VOUT, 10 )
74 vout_getfunctions( &p_module->p_functions->vout );
77 MODULE_DEACTIVATE_START
78 MODULE_DEACTIVATE_STOP
80 /*****************************************************************************
81 * vout_sys_t: video output MGA method descriptor
82 *****************************************************************************
83 * This structure is part of the video output thread descriptor.
84 * It describes the MGA specific properties of an output thread.
85 *****************************************************************************/
86 #ifndef __LINUX_MGAVID_H
87 # define __LINUX_MGAVID_H
89 # define MGA_VID_CONFIG _IOR('J', 1, mga_vid_config_t)
90 # define MGA_VID_ON _IO ('J', 2)
91 # define MGA_VID_OFF _IO ('J', 3)
92 # define MGA_VID_FSEL _IOR('J', 4, int)
93 # define MGA_G200 0x1234
94 # define MGA_G400 0x5678
96 # define MGA_VID_FORMAT_YV12 0x32315659
97 # define MGA_VID_FORMAT_IYUV (('I'<<24)|('Y'<<16)|('U'<<8)|'V')
98 # define MGA_VID_FORMAT_I420 (('I'<<24)|('4'<<16)|('2'<<8)|'0')
99 # define MGA_VID_FORMAT_YUY2 (('Y'<<24)|('U'<<16)|('Y'<<8)|'2')
100 # define MGA_VID_FORMAT_UYVY (('U'<<24)|('Y'<<16)|('V'<<8)|'Y')
102 # define MGA_VID_VERSION 0x0201
104 # define MGA_NUM_FRAMES 1
106 typedef struct mga_vid_config_s
127 typedef struct vout_sys_s
129 mga_vid_config_t mga;
135 typedef struct picture_sys_s
141 #define CEIL32(x) (((x)+31)&~31)
143 /*****************************************************************************
144 * Functions exported as capabilities. They are declared as static so that
145 * we don't pollute the namespace too much.
146 *****************************************************************************/
147 static void vout_getfunctions( function_list_t * p_function_list )
149 p_function_list->pf_probe = vout_Probe;
150 p_function_list->functions.vout.pf_create = vout_Create;
151 p_function_list->functions.vout.pf_init = vout_Init;
152 p_function_list->functions.vout.pf_end = vout_End;
153 p_function_list->functions.vout.pf_destroy = vout_Destroy;
154 p_function_list->functions.vout.pf_manage = vout_Manage;
155 p_function_list->functions.vout.pf_render = vout_Render;
156 p_function_list->functions.vout.pf_display = vout_Display;
159 /*****************************************************************************
160 * intf_Probe: return a score
161 *****************************************************************************/
162 static int vout_Probe( probedata_t *p_data )
166 i_fd = open( "/dev/mga_vid", O_RDWR );
177 /*****************************************************************************
178 * vout_Create: allocates dummy video thread output method
179 *****************************************************************************
180 * This function allocates and initializes a dummy vout method.
181 *****************************************************************************/
182 static int vout_Create( vout_thread_t *p_vout )
184 /* Allocate structure */
185 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
186 if( p_vout->p_sys == NULL )
188 intf_ErrMsg("vout error: %s", strerror(ENOMEM) );
192 p_vout->p_sys->i_fd = open( "/dev/mga_vid", O_RDWR );
193 if( p_vout->p_sys->i_fd == -1 )
195 intf_ErrMsg( "vout error: can't open MGA driver /dev/mga_vid" );
196 free( p_vout->p_sys );
203 /*****************************************************************************
204 * vout_Init: initialize dummy video thread output method
205 *****************************************************************************/
206 static int vout_Init( vout_thread_t *p_vout )
211 I_OUTPUTPICTURES = 0;
213 /* create the MGA output */
214 p_vout->output.i_width = p_vout->render.i_width;
215 p_vout->output.i_height = p_vout->render.i_height;
216 p_vout->output.i_aspect = p_vout->render.i_aspect;
218 /* Set coordinates and aspect ratio */
219 p_vout->p_sys->mga.src_width = CEIL32(p_vout->output.i_width);
220 p_vout->p_sys->mga.src_height = p_vout->output.i_height;
221 vout_PlacePicture( p_vout, 1024, 768,
222 &p_vout->p_sys->mga.x_org, &p_vout->p_sys->mga.y_org,
223 &p_vout->p_sys->mga.dest_width,
224 &p_vout->p_sys->mga.dest_height );
226 /* Initialize a video buffer */
227 p_vout->p_sys->mga.colkey_on = 0;
228 p_vout->p_sys->mga.num_frames = MGA_NUM_FRAMES;
229 p_vout->p_sys->mga.frame_size = CEIL32(p_vout->output.i_width)
230 * p_vout->output.i_height * 2;
231 p_vout->p_sys->mga.version = MGA_VID_VERSION;
233 /* Assume we only do YMGA for the moment. XXX: mga_vid calls this
234 * YV12, but it's actually some strange format with packed UV. */
235 p_vout->output.i_chroma = FOURCC_YMGA;
236 p_vout->p_sys->mga.format = MGA_VID_FORMAT_YV12;
238 if( ioctl(p_vout->p_sys->i_fd, MGA_VID_CONFIG, &p_vout->p_sys->mga) )
240 intf_ErrMsg( "vout error: MGA config ioctl failed" );
244 if( p_vout->p_sys->mga.card_type == MGA_G200 )
246 intf_WarnMsg( 3, "vout info: detected MGA G200 (%d MB Ram)",
247 p_vout->p_sys->mga.ram_size );
251 intf_WarnMsg( 3, "vout info: detected MGA G400/G450 (%d MB Ram)",
252 p_vout->p_sys->mga.ram_size );
255 p_vout->p_sys->p_video = mmap( 0, p_vout->p_sys->mga.frame_size
257 PROT_WRITE, MAP_SHARED,
258 p_vout->p_sys->i_fd, 0 );
260 /* Try to initialize up to MGA_NUM_FRAMES direct buffers */
261 while( I_OUTPUTPICTURES < MGA_NUM_FRAMES )
265 /* Find an empty picture slot */
266 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
268 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
270 p_pic = p_vout->p_picture + i_index;
275 /* Allocate the picture */
276 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
281 p_pic->i_status = DESTROYED_PICTURE;
282 p_pic->i_type = DIRECT_PICTURE;
284 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
289 /* Blank the windows */
290 for( i_index = 0; i_index < I_OUTPUTPICTURES; i_index++ )
292 memset( p_vout->p_sys->p_video
293 + p_vout->p_sys->mga.frame_size * i_index,
294 0x00, p_vout->p_sys->mga.frame_size / 2 );
295 memset( p_vout->p_sys->p_video
296 + p_vout->p_sys->mga.frame_size * ( 2*i_index + 1 ) / 2,
297 0x80, p_vout->p_sys->mga.frame_size / 2 );
300 /* Display the image */
301 ioctl( p_vout->p_sys->i_fd, MGA_VID_ON, 0 );
306 /*****************************************************************************
307 * vout_End: terminate dummy video thread output method
308 *****************************************************************************/
309 static void vout_End( vout_thread_t *p_vout )
313 ioctl( p_vout->p_sys->i_fd, MGA_VID_OFF, 0 );
315 /* Free the output buffers we allocated */
316 for( i_index = I_OUTPUTPICTURES ; i_index ; )
322 /*****************************************************************************
323 * vout_Destroy: destroy dummy video thread output method
324 *****************************************************************************
325 * Terminate an output method created by DummyCreateOutputMethod
326 *****************************************************************************/
327 static void vout_Destroy( vout_thread_t *p_vout )
329 close( p_vout->p_sys->i_fd );
331 free( p_vout->p_sys );
334 /*****************************************************************************
335 * vout_Manage: handle dummy events
336 *****************************************************************************
337 * This function should be called regularly by video output thread. It manages
338 * console events. It returns a non null value on error.
339 *****************************************************************************/
340 static int vout_Manage( vout_thread_t *p_vout )
345 /*****************************************************************************
346 * vout_Render: displays previously calculated output
347 *****************************************************************************/
348 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
350 /* Grmbl, if we have a G200 which mistakenly assumes 4:2:0 planar
351 * has *packed* chroma information, we'll need to do some
352 * vonversion... but vlc does this for us. */
355 /*****************************************************************************
356 * vout_Display: displays previously rendered output
357 *****************************************************************************/
358 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
360 ioctl( p_vout->p_sys->i_fd, MGA_VID_FSEL, &p_pic->p_sys->i_frame );
363 /* Following functions are local */
365 /*****************************************************************************
366 * NewPicture: allocate a picture
367 *****************************************************************************
368 * Returns 0 on success, -1 otherwise
369 *****************************************************************************/
370 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
372 /* We know the chroma, allocate a buffer which will be used
373 * directly by the decoder */
374 p_pic->p_data = p_vout->p_sys->p_video + I_OUTPUTPICTURES
375 * p_vout->p_sys->mga.frame_size;
377 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
379 if( p_pic->p_sys == NULL )
384 p_pic->Y_PIXELS = p_pic->p_data;
385 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
386 p_pic->p[Y_PLANE].i_pitch = CEIL32( p_vout->output.i_width );
387 p_pic->p[Y_PLANE].i_pixel_bytes = 1;
389 if( p_pic->p[Y_PLANE].i_pitch == p_vout->output.i_width )
391 p_pic->p[Y_PLANE].b_margin = 0;
395 /* FIXME: do something here */
396 p_pic->p[Y_PLANE].b_margin = 0;
399 p_pic->U_PIXELS = p_pic->p_data + p_vout->p_sys->mga.frame_size * 2/4;
400 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
401 p_pic->p[U_PLANE].i_pitch = CEIL32( p_vout->output.i_width ) / 2;
402 p_pic->p[U_PLANE].i_pixel_bytes = 1;
403 p_pic->p[U_PLANE].b_margin = 0;
405 p_pic->V_PIXELS = p_pic->p_data + p_vout->p_sys->mga.frame_size * 3/4;
406 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
407 p_pic->p[V_PLANE].i_pitch = CEIL32( p_vout->output.i_width ) / 2;
408 p_pic->p[V_PLANE].i_pixel_bytes = 1;
409 p_pic->p[V_PLANE].b_margin = 0;
411 p_pic->p_sys->i_frame = I_OUTPUTPICTURES;