]> git.sesse.net Git - vlc/blob - plugins/mga/mga.c
da28acfaf00b85c5b2898ebc9f5112d32499af32
[vlc] / plugins / mga / mga.c
1 /*****************************************************************************
2  * mga.c : Matrox Graphic Array plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: mga.c,v 1.20 2002/07/23 00:39:17 sam Exp $
6  *
7  * Authors: Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
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 */
35
36 #include <vlc/vlc.h>
37 #include <vlc/vout.h>
38
39 #ifdef SYS_BSD
40 #include <sys/types.h>                                     /* typedef ushort */
41 #endif
42
43 /*****************************************************************************
44  * Capabilities defined in the other files.
45  *****************************************************************************/
46 static void vout_getfunctions( function_list_t * p_function_list );
47
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 * );
55
56 static int  NewPicture     ( vout_thread_t *, picture_t * );
57
58 /*****************************************************************************
59  * Building configuration tree
60  *****************************************************************************/
61 MODULE_CONFIG_START
62 MODULE_CONFIG_STOP
63
64 MODULE_INIT_START
65     SET_DESCRIPTION( _("Matrox Graphic Array video module") )
66     ADD_CAPABILITY( VOUT, 10 )
67 MODULE_INIT_STOP
68
69 MODULE_ACTIVATE_START
70     vout_getfunctions( &p_module->p_functions->vout );
71 MODULE_ACTIVATE_STOP
72
73 MODULE_DEACTIVATE_START
74 MODULE_DEACTIVATE_STOP
75
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
84
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
91
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')
97
98 #   define MGA_VID_VERSION     0x0201
99
100 #   define MGA_NUM_FRAMES      1
101
102 typedef struct mga_vid_config_t
103 {
104     u16 version;
105     u16 card_type;
106     u32 ram_size;
107     u32 src_width;
108     u32 src_height;
109     u32 dest_width;
110     u32 dest_height;
111     u32 x_org;
112     u32 y_org;
113     u8  colkey_on;
114     u8  colkey_red;
115     u8  colkey_green;
116     u8  colkey_blue;
117     u32 format;
118     u32 frame_size;
119     u32 num_frames;
120 } mga_vid_config_t;
121 #endif
122
123 struct vout_sys_t
124 {
125     mga_vid_config_t    mga;
126     int                 i_fd;
127     byte_t *            p_video;
128 };
129
130 struct picture_sys_t
131 {
132     int     i_frame;
133 };
134
135 #define CEIL32(x) (((x)+31)&~31)
136
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 )
142 {
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;
150 }
151
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 )
158 {
159     /* Allocate structure */
160     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
161     if( p_vout->p_sys == NULL )
162     {
163         msg_Err( p_vout, "out of memory" );
164         return( 1 );
165     }
166
167     p_vout->p_sys->i_fd = open( "/dev/mga_vid", O_RDWR );
168     if( p_vout->p_sys->i_fd == -1 )
169     {
170         msg_Err( p_vout, "cannot open MGA driver /dev/mga_vid" );
171         free( p_vout->p_sys );
172         return( 1 );
173     }
174
175     return( 0 );
176 }
177
178 /*****************************************************************************
179  * vout_Init: initialize dummy video thread output method
180  *****************************************************************************/
181 static int vout_Init( vout_thread_t *p_vout )
182 {
183     int i_index;
184     picture_t *p_pic;
185
186     I_OUTPUTPICTURES = 0;
187
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;
192
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 );
200
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;
207
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 = VLC_FOURCC('Y','M','G','A');
211     p_vout->p_sys->mga.format = MGA_VID_FORMAT_YV12;
212     
213     if( ioctl(p_vout->p_sys->i_fd, MGA_VID_CONFIG, &p_vout->p_sys->mga) )
214     {
215         msg_Err( p_vout, "MGA config ioctl failed" );
216         return -1;
217     }
218
219     if( p_vout->p_sys->mga.card_type == MGA_G200 )
220     {
221         msg_Dbg( p_vout, "detected MGA G200 (%d MB Ram)",
222                          p_vout->p_sys->mga.ram_size );
223     }
224     else
225     {
226         msg_Dbg( p_vout, "detected MGA G400/G450 (%d MB Ram)",
227                          p_vout->p_sys->mga.ram_size );
228     }
229
230     p_vout->p_sys->p_video = mmap( 0, p_vout->p_sys->mga.frame_size
231                                        * MGA_NUM_FRAMES,
232                                    PROT_WRITE, MAP_SHARED,
233                                    p_vout->p_sys->i_fd, 0 );
234
235     /* Try to initialize up to MGA_NUM_FRAMES direct buffers */
236     while( I_OUTPUTPICTURES < MGA_NUM_FRAMES )
237     {
238         p_pic = NULL;
239
240         /* Find an empty picture slot */
241         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
242         {
243             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
244             {
245                 p_pic = p_vout->p_picture + i_index;
246                 break;
247             }
248         }
249
250         /* Allocate the picture */
251         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
252         {
253             break;
254         }
255
256         p_pic->i_status = DESTROYED_PICTURE;
257         p_pic->i_type   = DIRECT_PICTURE;
258
259         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
260
261         I_OUTPUTPICTURES++;
262     }
263
264     /* Blank the windows */
265     for( i_index = 0; i_index < I_OUTPUTPICTURES; i_index++ )
266     {
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 );
273     }
274
275     /* Display the image */
276     ioctl( p_vout->p_sys->i_fd, MGA_VID_ON, 0 );
277
278     return( 0 );
279 }
280
281 /*****************************************************************************
282  * vout_End: terminate dummy video thread output method
283  *****************************************************************************/
284 static void vout_End( vout_thread_t *p_vout )
285 {
286     int i_index;
287
288     ioctl( p_vout->p_sys->i_fd, MGA_VID_OFF, 0 );
289
290     /* Free the output buffers we allocated */
291     for( i_index = I_OUTPUTPICTURES ; i_index ; )
292     {
293         i_index--;
294     }
295 }
296
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 )
303 {
304     close( p_vout->p_sys->i_fd );
305
306     free( p_vout->p_sys );
307 }
308
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 )
316 {
317     return( 0 );
318 }
319
320 /*****************************************************************************
321  * vout_Render: displays previously calculated output
322  *****************************************************************************/
323 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
324 {
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. */
328 }
329
330 /*****************************************************************************
331  * vout_Display: displays previously rendered output
332  *****************************************************************************/
333 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
334 {
335     ioctl( p_vout->p_sys->i_fd, MGA_VID_FSEL, &p_pic->p_sys->i_frame );
336 }
337
338 /* Following functions are local */
339
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 )
346 {
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;
351
352     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
353
354     if( p_pic->p_sys == NULL )
355     {
356         return -1;
357     }
358
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_pitch = 1;
363     p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
364
365     p_pic->U_PIXELS = p_pic->p_data + p_vout->p_sys->mga.frame_size * 2/4;
366     p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
367     p_pic->p[U_PLANE].i_pitch = CEIL32( p_vout->output.i_width ) / 2;
368     p_pic->p[U_PLANE].i_pixel_pitch = 1;
369     p_pic->p[U_PLANE].i_visible_pitch = p_pic->p[U_PLANE].i_pitch;
370
371     p_pic->V_PIXELS = p_pic->p_data + p_vout->p_sys->mga.frame_size * 3/4;
372     p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
373     p_pic->p[V_PLANE].i_pitch = CEIL32( p_vout->output.i_width ) / 2;
374     p_pic->p[V_PLANE].i_pixel_pitch = 1;
375     p_pic->p[V_PLANE].i_visible_pitch = p_pic->p[V_PLANE].i_pitch;
376
377     p_pic->p_sys->i_frame = I_OUTPUTPICTURES;
378
379     p_pic->i_planes = 3;
380
381     return 0;
382 }
383