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