]> git.sesse.net Git - vlc/blob - modules/video_output/directfb.c
Remove stdlib.h
[vlc] / modules / video_output / directfb.c
1 /*****************************************************************************
2  * directfb.c: DirectFB video output display method
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * Authors: Iuri Diniz <iuri@digizap.com.br>
7  *
8  * This code is based in sdl.c and fb.c, thanks for VideoLAN team.
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 <string.h>                                            /* strerror() */
30
31 #include <vlc/vlc.h>
32 #include <vlc_vout.h>
33 #include <directfb.h>
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Create    ( vlc_object_t * );
39 static void Destroy   ( vlc_object_t * );
40
41 static int  Init      ( vout_thread_t * );
42 static void End       ( vout_thread_t * );
43 static int  Manage    ( vout_thread_t * );
44 static void Display   ( vout_thread_t *, picture_t * );
45
46 static int  OpenDisplay    ( vout_thread_t * );
47 static void CloseDisplay   ( vout_thread_t * );
48
49 struct vout_sys_t
50 {
51     IDirectFB *p_directfb;
52     IDirectFBSurface *p_primary;
53     DFBSurfacePixelFormat p_pixel_format;
54
55     int i_width;
56     int i_height;
57
58     byte_t* p_pixels;
59 };
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64 vlc_module_begin();
65     set_shortname( "DirectFB" );
66     set_category( CAT_VIDEO );
67     set_subcategory( SUBCAT_VIDEO_VOUT );
68     set_description( _("DirectFB video output http://www.directfb.org/") );
69     set_capability( "video output", 60 );
70     add_shortcut( "directfb" );
71     set_callbacks( Create, Destroy );
72 vlc_module_end();
73
74
75 static int Create( vlc_object_t *p_this ) 
76 {
77     vout_thread_t *p_vout = (vout_thread_t *)p_this;
78     vout_sys_t *p_sys = NULL;
79     p_vout->pf_init = Init;
80     p_vout->pf_end = End;
81     p_vout->pf_manage = Manage;
82     p_vout->pf_render = NULL;
83     p_vout->pf_display = Display;
84
85     /* Allocate structure */
86     p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
87     if( !p_sys )
88     {
89         msg_Err( p_vout, "out of memory" );
90         return VLC_ENOMEM;
91     }
92
93     p_sys->p_directfb = NULL;
94     p_sys->p_primary = NULL;
95     p_sys->p_pixels = NULL;
96     p_sys->i_width = 0;
97     p_sys->i_height = 0;
98
99     /* Init DirectFB */
100     if( DirectFBInit(NULL,NULL) != DFB_OK )
101     {
102         msg_Err(p_vout, "Cannot init DirectFB");
103         return VLC_EGENERIC;
104     }
105
106     if( OpenDisplay( p_vout ) )
107     {
108         msg_Err(p_vout, "Cannot create primary surface");
109         free( p_sys );
110         return VLC_EGENERIC;
111     }
112     return VLC_SUCCESS;
113 }
114
115
116 static int Init( vout_thread_t *p_vout )
117 {
118     vout_sys_t *p_sys = p_vout->p_sys;
119     IDirectFBSurface *p_primary = (IDirectFBSurface *) p_vout->p_sys->p_primary;
120     byte_t* p_pixels = NULL;
121     picture_t *p_pic = NULL;
122     int i_rlength, i_glength, i_blength;
123     int i_roffset, i_goffset, i_boffset;
124     int i_line_pitch;
125     int i_size;
126     int i_index;
127
128     I_OUTPUTPICTURES = 0;
129
130     switch( p_sys->p_pixel_format )
131     {
132         case DSPF_RGB332: 
133             /* 8 bit RGB (1 byte, red 3@5, green 3@2, blue 2@0) */
134             /* i_pixel_pitch = 1; */
135             i_rlength = 3;
136             i_roffset = 5;
137             i_glength = 3;
138             i_goffset = 2;
139             i_blength = 2;
140             i_boffset = 0;
141             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
142             break;
143
144         case DSPF_RGB16:
145             /* 16 bit RGB (2 byte, red 5@11, green 6@5, blue 5@0) */
146             /* i_pixel_pitch = 2; */
147             i_rlength = 5;
148             i_roffset = 11;
149             i_glength = 6;
150             i_goffset = 5;
151             i_blength = 5;
152             i_boffset = 0;
153             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
154             break;
155
156         case DSPF_RGB24:
157             /* 24 bit RGB (3 byte, red 8@16, green 8@8, blue 8@0) */
158             /* i_pixel_pitch = 3; */
159             i_rlength = 8;
160             i_roffset = 16;
161             i_glength = 8;
162             i_goffset = 8;
163             i_blength = 8;
164             i_boffset = 0;
165             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
166             break;
167
168         case DSPF_RGB32:
169             /* 24 bit RGB (4 byte, nothing@24, red 8@16, green 8@8, blue 8@0) */
170             /* i_pixel_pitch = 4; */
171             i_rlength = 8;
172             i_roffset = 16;
173             i_glength = 8;
174             i_goffset = 8;
175             i_blength = 8;
176             i_boffset = 0;
177             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
178             break;
179
180         default:
181             msg_Err( p_vout, "unknown screen depth %i",
182                      p_sys->p_pixel_format );
183             return VLC_EGENERIC;
184     }
185     /* Set the RGB masks */
186     p_vout->output.i_rmask = ( (1 << i_rlength) - 1 ) << i_roffset;
187     p_vout->output.i_gmask = ( (1 << i_glength) - 1 ) << i_goffset;
188     p_vout->output.i_bmask = ( (1 << i_blength) - 1 ) << i_boffset;
189
190     /* Width and height */
191     p_vout->output.i_width  = p_sys->i_width;
192     p_vout->output.i_height = p_sys->i_height;
193
194     /* The aspect */
195     p_vout->output.i_aspect = (p_sys->i_width * VOUT_ASPECT_FACTOR) /
196                                p_sys->i_height;
197
198     /* Try to initialize 1 buffer */
199     /* Find an empty picture slot */
200     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
201     {
202         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
203         {
204             p_pic = p_vout->p_picture + i_index;
205             break;
206         }
207     }
208
209     /* Allocate the picture */
210     if( !p_pic )
211         return VLC_EGENERIC;
212
213     /* get the pixels */
214     if( p_primary->Lock( p_primary, DSLF_READ, (void **) &p_pixels,
215                          &i_line_pitch) != DFB_OK )
216         return VLC_EGENERIC;
217
218     /* allocate p_pixels */
219     i_size = i_line_pitch * p_sys->i_height;
220     p_sys->p_pixels = malloc( sizeof(byte_t) * i_size );
221     if( p_sys->p_pixels == NULL )
222     {
223         p_primary->Unlock(p_primary);
224         return VLC_ENOMEM;
225     }
226
227     /* copy pixels */
228     memcpy( p_sys->p_pixels,  p_pixels, i_size );
229     if( p_primary->Unlock(p_primary) != DFB_OK )
230     {
231         return VLC_EGENERIC;
232     }
233
234     p_pic->p->p_pixels = p_sys->p_pixels;
235     p_pic->p->i_pixel_pitch = i_line_pitch / p_sys->i_width;
236     p_pic->p->i_lines = p_sys->i_height;
237     p_pic->p->i_visible_lines = p_sys->i_height;
238     p_pic->p->i_pitch = i_line_pitch;
239     p_pic->p->i_visible_pitch = i_line_pitch;
240     p_pic->i_planes = 1;
241     p_pic->i_status = DESTROYED_PICTURE;
242     p_pic->i_type   = DIRECT_PICTURE;
243
244     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
245
246     I_OUTPUTPICTURES++;
247
248     return VLC_SUCCESS;
249 }
250
251 static void End( vout_thread_t *p_vout )
252 {
253     vout_sys_t *p_sys = p_vout->p_sys;
254
255     if( p_sys->p_pixels )
256         free( p_sys->p_pixels );
257 }
258
259 static void Destroy( vlc_object_t *p_this )
260 {
261     vout_thread_t *p_vout = (vout_thread_t *)p_this;
262     vout_sys_t *p_sys = p_vout->p_sys;
263
264     CloseDisplay( p_vout );
265     if( p_sys ) free( p_sys );
266     p_sys = NULL;
267 }
268
269 static int Manage( vout_thread_t *p_vout )
270 {
271     return VLC_SUCCESS;
272 }
273
274 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
275 {
276     vout_sys_t *p_sys = p_vout->p_sys;
277     IDirectFBSurface *p_primary = (IDirectFBSurface *) p_sys->p_primary;
278     byte_t* p_pixels = NULL;
279     int i_size;
280     int i_line_pitch;
281
282     /* get the pixels */
283     if( p_primary->Lock( p_primary, DSLF_WRITE,
284                          (void **) &p_pixels,
285                          &i_line_pitch) == DFB_OK )
286     {
287         i_size = i_line_pitch * p_vout->p_sys->i_height;
288
289         /* copy pixels */
290         memcpy( p_pixels, p_pic->p->p_pixels, i_size);
291         if( p_primary->Unlock(p_primary) == DFB_OK )
292         {
293             p_primary->Flip(p_primary, NULL, 0);
294         }
295     }
296 }
297
298 static int OpenDisplay( vout_thread_t *p_vout )
299 {
300     vout_sys_t *p_sys = p_vout->p_sys;
301     IDirectFB *p_directfb = NULL;
302     IDirectFBSurface *p_primary = NULL;
303     DFBSurfaceDescription dsc;
304
305     /*dsc.flags = DSDESC_CAPS | DSDESC_HEIGHT | DSDESC_WIDTH;*/
306     dsc.flags = DSDESC_CAPS;
307     dsc.caps  = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
308
309     /*dsc.width = 352;*/
310     /*dsc.height = 240;*/
311     if( DirectFBCreate( &p_directfb ) != DFB_OK )
312         return VLC_EGENERIC;
313
314     p_sys->p_directfb = p_directfb;
315     if( !p_directfb )
316         return VLC_EGENERIC;
317
318     if( p_directfb->CreateSurface( p_directfb, &dsc, &p_primary ) )
319         return VLC_EGENERIC;
320
321     p_sys->p_primary = p_primary;
322     if( !p_primary )
323         return VLC_EGENERIC;
324
325     p_primary->GetSize( p_primary, &p_sys->i_width,
326                         &p_sys->i_height );
327     p_primary->GetPixelFormat( p_primary, &p_sys->p_pixel_format );
328     p_primary->FillRectangle( p_primary, 0, 0, p_sys->i_width,
329                               p_sys->i_height );
330     p_primary->Flip( p_primary, NULL, 0 );
331
332     return VLC_SUCCESS;
333 }
334
335 static void CloseDisplay( vout_thread_t *p_vout )
336 {
337     vout_sys_t *p_sys = p_vout->p_sys;
338     IDirectFB *p_directfb = p_sys->p_directfb;
339     IDirectFBSurface *p_primary = p_sys->p_primary;
340
341     if( p_primary )
342         p_primary->Release( p_primary );
343
344     if( p_directfb )
345         p_directfb->Release( p_directfb );
346 }