]> git.sesse.net Git - vlc/blob - modules/video_output/mga.c
Fix compilation error with SDL under 1.2.10
[vlc] / modules / video_output / mga.c
1 /*****************************************************************************
2  * mga.c : Matrox Graphic Array plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <unistd.h>                                               /* close() */
30 #include <fcntl.h>                                                 /* open() */
31 #include <sys/ioctl.h>                                            /* ioctl() */
32 #include <sys/mman.h>                                          /* PROT_WRITE */
33
34 #include <vlc/vlc.h>
35 #include <vlc_vout.h>
36
37 #ifdef SYS_BSD
38 #include <sys/types.h>                                     /* typedef ushort */
39 #endif
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  Create    ( vlc_object_t * );
45 static void Destroy   ( vlc_object_t * );
46
47 static int  Init      ( vout_thread_t * );
48 static void End       ( vout_thread_t * );
49 static void Display   ( vout_thread_t *, picture_t * );
50
51 static int  NewPicture     ( vout_thread_t *, picture_t * );
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 vlc_module_begin();
57     set_description( _("Matrox Graphic Array video output") );
58     set_capability( "video output", 10 );
59     set_callbacks( Create, Destroy );
60 vlc_module_end();
61
62 /*****************************************************************************
63  * vout_sys_t: video output MGA method descriptor
64  *****************************************************************************
65  * This structure is part of the video output thread descriptor.
66  * It describes the MGA specific properties of an output thread.
67  *****************************************************************************/
68 #ifndef __LINUX_MGAVID_H
69 #   define __LINUX_MGAVID_H
70
71 #   define MGA_VID_CONFIG _IOR('J', 1, mga_vid_config_t)
72 #   define MGA_VID_ON     _IO ('J', 2)
73 #   define MGA_VID_OFF    _IO ('J', 3)
74 #   define MGA_VID_FSEL   _IOR('J', 4, int)
75 #   define MGA_G200 0x1234
76 #   define MGA_G400 0x5678
77
78 #   define MGA_VID_FORMAT_YV12 0x32315659
79 #   define MGA_VID_FORMAT_IYUV (('I'<<24)|('Y'<<16)|('U'<<8)|'V')
80 #   define MGA_VID_FORMAT_I420 (('I'<<24)|('4'<<16)|('2'<<8)|'0')
81 #   define MGA_VID_FORMAT_YUY2 (('Y'<<24)|('U'<<16)|('Y'<<8)|'2')
82 #   define MGA_VID_FORMAT_UYVY (('U'<<24)|('Y'<<16)|('V'<<8)|'Y')
83
84 #   define MGA_VID_VERSION     0x0201
85
86 #   define MGA_NUM_FRAMES      1
87
88 typedef struct mga_vid_config_t
89 {
90     uint16_t version;
91     uint16_t card_type;
92     uint32_t ram_size;
93     uint32_t src_width;
94     uint32_t src_height;
95     uint32_t dest_width;
96     uint32_t dest_height;
97     uint32_t x_org;
98     uint32_t y_org;
99     uint8_t  colkey_on;
100     uint8_t  colkey_red;
101     uint8_t  colkey_green;
102     uint8_t  colkey_blue;
103     uint32_t format;
104     uint32_t frame_size;
105     uint32_t num_frames;
106 } mga_vid_config_t;
107 #endif
108
109 struct vout_sys_t
110 {
111     mga_vid_config_t    mga;
112     int                 i_fd;
113     byte_t *            p_video;
114 };
115
116 struct picture_sys_t
117 {
118     int     i_frame;
119 };
120
121 #define CEIL32(x) (((x)+31)&~31)
122
123 /*****************************************************************************
124  * Create: allocates dummy video thread output method
125  *****************************************************************************
126  * This function allocates and initializes a dummy vout method.
127  *****************************************************************************/
128 static int Create( vlc_object_t *p_this )
129 {
130     vout_thread_t *p_vout = (vout_thread_t *)p_this;
131
132     /* Allocate structure */
133     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
134     if( p_vout->p_sys == NULL )
135     {
136         msg_Err( p_vout, "out of memory" );
137         return( 1 );
138     }
139
140     p_vout->p_sys->i_fd = open( "/dev/mga_vid", O_RDWR );
141     if( p_vout->p_sys->i_fd == -1 )
142     {
143         msg_Err( p_vout, "cannot open MGA driver /dev/mga_vid" );
144         free( p_vout->p_sys );
145         return( 1 );
146     }
147
148     p_vout->pf_init = Init;
149     p_vout->pf_end = End;
150     p_vout->pf_manage = NULL;
151     p_vout->pf_render = NULL;
152     p_vout->pf_display = Display;
153
154     return( 0 );
155 }
156
157 /*****************************************************************************
158  * Init: initialize dummy video thread output method
159  *****************************************************************************/
160 static int Init( vout_thread_t *p_vout )
161 {
162     int i_index;
163     picture_t *p_pic;
164
165     I_OUTPUTPICTURES = 0;
166
167     /* create the MGA output */
168     p_vout->output.i_width = p_vout->render.i_width;
169     p_vout->output.i_height = p_vout->render.i_height;
170     p_vout->output.i_aspect = p_vout->render.i_aspect;
171
172     /* Set coordinates and aspect ratio */
173     p_vout->p_sys->mga.src_width = CEIL32(p_vout->output.i_width);
174     p_vout->p_sys->mga.src_height = p_vout->output.i_height;
175     vout_PlacePicture( p_vout, 1024, 768,
176                        &p_vout->p_sys->mga.x_org, &p_vout->p_sys->mga.y_org,
177                        &p_vout->p_sys->mga.dest_width,
178                        &p_vout->p_sys->mga.dest_height );
179
180     /* Initialize a video buffer */
181     p_vout->p_sys->mga.colkey_on = 0;
182     p_vout->p_sys->mga.num_frames = MGA_NUM_FRAMES;
183     p_vout->p_sys->mga.frame_size = CEIL32(p_vout->output.i_width)
184                                      * p_vout->output.i_height * 2;
185     p_vout->p_sys->mga.version = MGA_VID_VERSION;
186
187     /* Assume we only do YMGA for the moment. XXX: mga_vid calls this
188      * YV12, but it's actually some strange format with packed UV. */
189     p_vout->output.i_chroma = VLC_FOURCC('Y','M','G','A');
190     p_vout->p_sys->mga.format = MGA_VID_FORMAT_YV12;
191
192     if( ioctl(p_vout->p_sys->i_fd, MGA_VID_CONFIG, &p_vout->p_sys->mga) )
193     {
194         msg_Err( p_vout, "MGA config ioctl failed" );
195         return -1;
196     }
197
198     if( p_vout->p_sys->mga.card_type == MGA_G200 )
199     {
200         msg_Dbg( p_vout, "detected MGA G200 (%d MB Ram)",
201                          p_vout->p_sys->mga.ram_size );
202     }
203     else
204     {
205         msg_Dbg( p_vout, "detected MGA G400/G450 (%d MB Ram)",
206                          p_vout->p_sys->mga.ram_size );
207     }
208
209     p_vout->p_sys->p_video = mmap( 0, p_vout->p_sys->mga.frame_size
210                                        * MGA_NUM_FRAMES,
211                                    PROT_WRITE, MAP_SHARED,
212                                    p_vout->p_sys->i_fd, 0 );
213
214     /* Try to initialize up to MGA_NUM_FRAMES direct buffers */
215     while( I_OUTPUTPICTURES < MGA_NUM_FRAMES )
216     {
217         p_pic = NULL;
218
219         /* Find an empty picture slot */
220         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
221         {
222             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
223             {
224                 p_pic = p_vout->p_picture + i_index;
225                 break;
226             }
227         }
228
229         /* Allocate the picture */
230         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
231         {
232             break;
233         }
234
235         p_pic->i_status = DESTROYED_PICTURE;
236         p_pic->i_type   = DIRECT_PICTURE;
237
238         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
239
240         I_OUTPUTPICTURES++;
241     }
242
243     /* Blank the windows */
244     for( i_index = 0; i_index < I_OUTPUTPICTURES; i_index++ )
245     {
246         memset( p_vout->p_sys->p_video
247                  + p_vout->p_sys->mga.frame_size * i_index,
248                 0x00, p_vout->p_sys->mga.frame_size / 2 );
249         memset( p_vout->p_sys->p_video
250                  + p_vout->p_sys->mga.frame_size * ( 2*i_index + 1 ) / 2,
251                 0x80, p_vout->p_sys->mga.frame_size / 2 );
252     }
253
254     /* Display the image */
255     ioctl( p_vout->p_sys->i_fd, MGA_VID_ON, 0 );
256
257     return( 0 );
258 }
259
260 /*****************************************************************************
261  * End: terminate dummy video thread output method
262  *****************************************************************************/
263 static void End( vout_thread_t *p_vout )
264 {
265     int i_index;
266
267     ioctl( p_vout->p_sys->i_fd, MGA_VID_OFF, 0 );
268
269     /* Free the output buffers we allocated */
270     for( i_index = I_OUTPUTPICTURES ; i_index ; )
271     {
272         i_index--;
273     }
274 }
275
276 /*****************************************************************************
277  * Destroy: destroy dummy video thread output method
278  *****************************************************************************
279  * Terminate an output method created by DummyCreateOutputMethod
280  *****************************************************************************/
281 static void Destroy( vlc_object_t *p_this )
282 {
283     vout_thread_t *p_vout = (vout_thread_t *)p_this;
284     close( p_vout->p_sys->i_fd );
285     free( p_vout->p_sys );
286 }
287
288 /*****************************************************************************
289  * Display: displays previously rendered output
290  *****************************************************************************/
291 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
292 {
293     ioctl( p_vout->p_sys->i_fd, MGA_VID_FSEL, &p_pic->p_sys->i_frame );
294 }
295
296 /* Following functions are local */
297
298 /*****************************************************************************
299  * NewPicture: allocate a picture
300  *****************************************************************************
301  * Returns 0 on success, -1 otherwise
302  *****************************************************************************/
303 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
304 {
305     /* We know the chroma, allocate a buffer which will be used
306      * directly by the decoder */
307     p_pic->p_data = p_vout->p_sys->p_video + I_OUTPUTPICTURES
308                                               * p_vout->p_sys->mga.frame_size;
309
310     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
311
312     if( p_pic->p_sys == NULL )
313     {
314         return -1;
315     }
316
317     p_pic->Y_PIXELS = p_pic->p_data;
318     p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
319     p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
320     p_pic->p[Y_PLANE].i_pitch = CEIL32( p_vout->output.i_width );
321     p_pic->p[Y_PLANE].i_pixel_pitch = 1;
322     p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width;
323
324     p_pic->U_PIXELS = p_pic->p_data + p_vout->p_sys->mga.frame_size * 2/4;
325     p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
326     p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
327     p_pic->p[U_PLANE].i_pitch = CEIL32( p_vout->output.i_width ) / 2;
328     p_pic->p[U_PLANE].i_pixel_pitch = 1;
329     p_pic->p[U_PLANE].i_visible_pitch = p_pic->p[U_PLANE].i_pitch;
330
331     p_pic->V_PIXELS = p_pic->p_data + p_vout->p_sys->mga.frame_size * 3/4;
332     p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
333     p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
334     p_pic->p[V_PLANE].i_pitch = CEIL32( p_vout->output.i_width ) / 2;
335     p_pic->p[V_PLANE].i_pixel_pitch = 1;
336     p_pic->p[V_PLANE].i_visible_pitch = p_pic->p[V_PLANE].i_pitch;
337
338     p_pic->p_sys->i_frame = I_OUTPUTPICTURES;
339
340     p_pic->i_planes = 3;
341
342     return 0;
343 }
344