]> git.sesse.net Git - vlc/blob - plugins/mga/mga.c
* ALL: got rid of *_Probe functions because most of them were duplicates
[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.16 2002/02/15 13:32:53 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 <videolan/vlc.h>
37
38 #ifdef SYS_BSD
39 #include <sys/types.h>                                     /* typedef ushort */
40 #endif
41
42 #include "video.h"
43 #include "video_output.h"
44
45 /*****************************************************************************
46  * Capabilities defined in the other files.
47  *****************************************************************************/
48 static void vout_getfunctions( function_list_t * p_function_list );
49
50 static int  vout_Create    ( vout_thread_t * );
51 static int  vout_Init      ( vout_thread_t * );
52 static void vout_End       ( vout_thread_t * );
53 static void vout_Destroy   ( vout_thread_t * );
54 static int  vout_Manage    ( vout_thread_t * );
55 static void vout_Render    ( vout_thread_t *, picture_t * );
56 static void vout_Display   ( vout_thread_t *, picture_t * );
57
58 static int  NewPicture     ( vout_thread_t *, picture_t * );
59
60 /*****************************************************************************
61  * Building configuration tree
62  *****************************************************************************/
63 MODULE_CONFIG_START
64 MODULE_CONFIG_STOP
65
66 MODULE_INIT_START
67     SET_DESCRIPTION( "Matrox Graphic Array video module" )
68     ADD_CAPABILITY( VOUT, 10 )
69     ADD_SHORTCUT( "mga" )
70 MODULE_INIT_STOP
71
72 MODULE_ACTIVATE_START
73     vout_getfunctions( &p_module->p_functions->vout );
74 MODULE_ACTIVATE_STOP
75
76 MODULE_DEACTIVATE_START
77 MODULE_DEACTIVATE_STOP
78
79 /*****************************************************************************
80  * vout_sys_t: video output MGA method descriptor
81  *****************************************************************************
82  * This structure is part of the video output thread descriptor.
83  * It describes the MGA specific properties of an output thread.
84  *****************************************************************************/
85 #ifndef __LINUX_MGAVID_H
86 #   define __LINUX_MGAVID_H
87
88 #   define MGA_VID_CONFIG _IOR('J', 1, mga_vid_config_t)
89 #   define MGA_VID_ON     _IO ('J', 2)
90 #   define MGA_VID_OFF    _IO ('J', 3)
91 #   define MGA_VID_FSEL   _IOR('J', 4, int)
92 #   define MGA_G200 0x1234
93 #   define MGA_G400 0x5678
94
95 #   define MGA_VID_FORMAT_YV12 0x32315659
96 #   define MGA_VID_FORMAT_IYUV (('I'<<24)|('Y'<<16)|('U'<<8)|'V')
97 #   define MGA_VID_FORMAT_I420 (('I'<<24)|('4'<<16)|('2'<<8)|'0')
98 #   define MGA_VID_FORMAT_YUY2 (('Y'<<24)|('U'<<16)|('Y'<<8)|'2')
99 #   define MGA_VID_FORMAT_UYVY (('U'<<24)|('Y'<<16)|('V'<<8)|'Y')
100
101 #   define MGA_VID_VERSION     0x0201
102
103 #   define MGA_NUM_FRAMES      1
104
105 typedef struct mga_vid_config_s
106 {
107     u16 version;
108     u16 card_type;
109     u32 ram_size;
110     u32 src_width;
111     u32 src_height;
112     u32 dest_width;
113     u32 dest_height;
114     u32 x_org;
115     u32 y_org;
116     u8  colkey_on;
117     u8  colkey_red;
118     u8  colkey_green;
119     u8  colkey_blue;
120     u32 format;
121     u32 frame_size;
122     u32 num_frames;
123 } mga_vid_config_t;
124 #endif
125
126 typedef struct vout_sys_s
127 {
128     mga_vid_config_t    mga;
129     int                 i_fd;
130     byte_t *            p_video;
131
132 } vout_sys_t;
133
134 typedef struct picture_sys_s
135 {
136     int     i_frame;
137
138 } picture_sys_t;
139
140 #define CEIL32(x) (((x)+31)&~31)
141
142 /*****************************************************************************
143  * Functions exported as capabilities. They are declared as static so that
144  * we don't pollute the namespace too much.
145  *****************************************************************************/
146 static void vout_getfunctions( function_list_t * p_function_list )
147 {
148     p_function_list->functions.vout.pf_create     = vout_Create;
149     p_function_list->functions.vout.pf_init       = vout_Init;
150     p_function_list->functions.vout.pf_end        = vout_End;
151     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
152     p_function_list->functions.vout.pf_manage     = vout_Manage;
153     p_function_list->functions.vout.pf_render     = vout_Render;
154     p_function_list->functions.vout.pf_display    = vout_Display;
155 }
156
157 /*****************************************************************************
158  * vout_Create: allocates dummy video thread output method
159  *****************************************************************************
160  * This function allocates and initializes a dummy vout method.
161  *****************************************************************************/
162 static int vout_Create( vout_thread_t *p_vout )
163 {
164     /* Allocate structure */
165     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
166     if( p_vout->p_sys == NULL )
167     {
168         intf_ErrMsg("vout error: %s", strerror(ENOMEM) );
169         return( 1 );
170     }
171
172     p_vout->p_sys->i_fd = open( "/dev/mga_vid", O_RDWR );
173     if( p_vout->p_sys->i_fd == -1 )
174     {
175         intf_ErrMsg( "vout error: can't open MGA driver /dev/mga_vid" );
176         free( p_vout->p_sys );
177         return( 1 );
178     }
179
180     return( 0 );
181 }
182
183 /*****************************************************************************
184  * vout_Init: initialize dummy video thread output method
185  *****************************************************************************/
186 static int vout_Init( vout_thread_t *p_vout )
187 {
188     int i_index;
189     picture_t *p_pic;
190
191     I_OUTPUTPICTURES = 0;
192
193     /* create the MGA output */
194     p_vout->output.i_width = p_vout->render.i_width;
195     p_vout->output.i_height = p_vout->render.i_height;
196     p_vout->output.i_aspect = p_vout->render.i_aspect;
197
198     /* Set coordinates and aspect ratio */
199     p_vout->p_sys->mga.src_width = CEIL32(p_vout->output.i_width);
200     p_vout->p_sys->mga.src_height = p_vout->output.i_height;
201     vout_PlacePicture( p_vout, 1024, 768,
202                        &p_vout->p_sys->mga.x_org, &p_vout->p_sys->mga.y_org,
203                        &p_vout->p_sys->mga.dest_width,
204                        &p_vout->p_sys->mga.dest_height );
205
206     /* Initialize a video buffer */
207     p_vout->p_sys->mga.colkey_on = 0;
208     p_vout->p_sys->mga.num_frames = MGA_NUM_FRAMES;
209     p_vout->p_sys->mga.frame_size = CEIL32(p_vout->output.i_width)
210                                      * p_vout->output.i_height * 2;
211     p_vout->p_sys->mga.version = MGA_VID_VERSION;
212
213     /* Assume we only do YMGA for the moment. XXX: mga_vid calls this
214      * YV12, but it's actually some strange format with packed UV. */
215     p_vout->output.i_chroma = FOURCC_YMGA;
216     p_vout->p_sys->mga.format = MGA_VID_FORMAT_YV12;
217     
218     if( ioctl(p_vout->p_sys->i_fd, MGA_VID_CONFIG, &p_vout->p_sys->mga) )
219     {
220         intf_ErrMsg( "vout error: MGA config ioctl failed" );
221         return -1;
222     }
223
224     if( p_vout->p_sys->mga.card_type == MGA_G200 )
225     {
226         intf_WarnMsg( 3, "vout info: detected MGA G200 (%d MB Ram)",
227                          p_vout->p_sys->mga.ram_size );
228     }
229     else
230     {
231         intf_WarnMsg( 3, "vout info: detected MGA G400/G450 (%d MB Ram)",
232                          p_vout->p_sys->mga.ram_size );
233     }
234
235     p_vout->p_sys->p_video = mmap( 0, p_vout->p_sys->mga.frame_size
236                                        * MGA_NUM_FRAMES,
237                                    PROT_WRITE, MAP_SHARED,
238                                    p_vout->p_sys->i_fd, 0 );
239
240     /* Try to initialize up to MGA_NUM_FRAMES direct buffers */
241     while( I_OUTPUTPICTURES < MGA_NUM_FRAMES )
242     {
243         p_pic = NULL;
244
245         /* Find an empty picture slot */
246         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
247         {
248             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
249             {
250                 p_pic = p_vout->p_picture + i_index;
251                 break;
252             }
253         }
254
255         /* Allocate the picture */
256         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
257         {
258             break;
259         }
260
261         p_pic->i_status = DESTROYED_PICTURE;
262         p_pic->i_type   = DIRECT_PICTURE;
263
264         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
265
266         I_OUTPUTPICTURES++;
267     }
268
269     /* Blank the windows */
270     for( i_index = 0; i_index < I_OUTPUTPICTURES; i_index++ )
271     {
272         memset( p_vout->p_sys->p_video
273                  + p_vout->p_sys->mga.frame_size * i_index,
274                 0x00, p_vout->p_sys->mga.frame_size / 2 );
275         memset( p_vout->p_sys->p_video
276                  + p_vout->p_sys->mga.frame_size * ( 2*i_index + 1 ) / 2,
277                 0x80, p_vout->p_sys->mga.frame_size / 2 );
278     }
279
280     /* Display the image */
281     ioctl( p_vout->p_sys->i_fd, MGA_VID_ON, 0 );
282
283     return( 0 );
284 }
285
286 /*****************************************************************************
287  * vout_End: terminate dummy video thread output method
288  *****************************************************************************/
289 static void vout_End( vout_thread_t *p_vout )
290 {
291     int i_index;
292
293     ioctl( p_vout->p_sys->i_fd, MGA_VID_OFF, 0 );
294
295     /* Free the output buffers we allocated */
296     for( i_index = I_OUTPUTPICTURES ; i_index ; )
297     {
298         i_index--;
299     }
300 }
301
302 /*****************************************************************************
303  * vout_Destroy: destroy dummy video thread output method
304  *****************************************************************************
305  * Terminate an output method created by DummyCreateOutputMethod
306  *****************************************************************************/
307 static void vout_Destroy( vout_thread_t *p_vout )
308 {
309     close( p_vout->p_sys->i_fd );
310
311     free( p_vout->p_sys );
312 }
313
314 /*****************************************************************************
315  * vout_Manage: handle dummy events
316  *****************************************************************************
317  * This function should be called regularly by video output thread. It manages
318  * console events. It returns a non null value on error.
319  *****************************************************************************/
320 static int vout_Manage( vout_thread_t *p_vout )
321 {
322     return( 0 );
323 }
324
325 /*****************************************************************************
326  * vout_Render: displays previously calculated output
327  *****************************************************************************/
328 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
329 {
330     /* Grmbl, if we have a G200 which mistakenly assumes 4:2:0 planar
331      * has *packed* chroma information, we'll need to do some
332      * vonversion... but vlc does this for us. */
333 }
334
335 /*****************************************************************************
336  * vout_Display: displays previously rendered output
337  *****************************************************************************/
338 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
339 {
340     ioctl( p_vout->p_sys->i_fd, MGA_VID_FSEL, &p_pic->p_sys->i_frame );
341 }
342
343 /* Following functions are local */
344
345 /*****************************************************************************
346  * NewPicture: allocate a picture
347  *****************************************************************************
348  * Returns 0 on success, -1 otherwise
349  *****************************************************************************/
350 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
351 {
352     /* We know the chroma, allocate a buffer which will be used
353      * directly by the decoder */
354     p_pic->p_data = p_vout->p_sys->p_video + I_OUTPUTPICTURES
355                                               * p_vout->p_sys->mga.frame_size;
356
357     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
358
359     if( p_pic->p_sys == NULL )
360     {
361         return -1;
362     }
363
364     p_pic->Y_PIXELS = p_pic->p_data;
365     p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
366     p_pic->p[Y_PLANE].i_pitch = CEIL32( p_vout->output.i_width );
367     p_pic->p[Y_PLANE].i_pixel_bytes = 1;
368
369     if( p_pic->p[Y_PLANE].i_pitch == p_vout->output.i_width )
370     {
371         p_pic->p[Y_PLANE].b_margin = 0;
372     }
373     else
374     {
375         /* FIXME: do something here */
376         p_pic->p[Y_PLANE].b_margin = 0;
377     }
378
379     p_pic->U_PIXELS = p_pic->p_data + p_vout->p_sys->mga.frame_size * 2/4;
380     p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
381     p_pic->p[U_PLANE].i_pitch = CEIL32( p_vout->output.i_width ) / 2;
382     p_pic->p[U_PLANE].i_pixel_bytes = 1;
383     p_pic->p[U_PLANE].b_margin = 0;
384
385     p_pic->V_PIXELS = p_pic->p_data + p_vout->p_sys->mga.frame_size * 3/4;
386     p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
387     p_pic->p[V_PLANE].i_pitch = CEIL32( p_vout->output.i_width ) / 2;
388     p_pic->p[V_PLANE].i_pixel_bytes = 1;
389     p_pic->p[V_PLANE].b_margin = 0;
390
391     p_pic->p_sys->i_frame = I_OUTPUTPICTURES;
392
393     p_pic->i_planes = 3;
394
395     return 0;
396 }
397