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