1 /*****************************************************************************
2 * directfb.c: DirectFB video output display method
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
6 * Authors: Iuri Diniz <iuri@digizap.com.br>
8 * This code is based in sdl.c and fb.c, thanks for VideoLAN team.
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* free() */
30 #include <string.h> /* strerror() */
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Create ( vlc_object_t * );
40 static void Destroy ( vlc_object_t * );
42 static int Init ( vout_thread_t * );
43 static void End ( vout_thread_t * );
44 static int Manage ( vout_thread_t * );
45 static void Display ( vout_thread_t *, picture_t * );
47 static int OpenDisplay ( vout_thread_t * );
48 static void CloseDisplay ( vout_thread_t * );
52 IDirectFB *p_directfb;
53 IDirectFBSurface *p_primary;
54 DFBSurfacePixelFormat p_pixel_format;
62 /*****************************************************************************
64 *****************************************************************************/
66 set_shortname( "DirectFB" );
67 set_category( CAT_VIDEO );
68 set_subcategory( SUBCAT_VIDEO_VOUT );
69 set_description( _("DirectFB video output http://www.directfb.org/") );
70 set_capability( "video output", 60 );
71 add_shortcut( "directfb" );
72 set_callbacks( Create, Destroy );
76 static int Create( vlc_object_t *p_this )
78 vout_thread_t *p_vout = (vout_thread_t *)p_this;
79 vout_sys_t *p_sys = NULL;
80 p_vout->pf_init = Init;
82 p_vout->pf_manage = Manage;
83 p_vout->pf_render = NULL;
84 p_vout->pf_display = Display;
86 /* Allocate structure */
87 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
90 msg_Err( p_vout, "out of memory" );
94 p_sys->p_directfb = NULL;
95 p_sys->p_primary = NULL;
96 p_sys->p_pixels = NULL;
101 if( DirectFBInit(NULL,NULL) != DFB_OK )
103 msg_Err(p_vout, "Cannot init DirectFB");
107 if( OpenDisplay( p_vout ) )
109 msg_Err(p_vout, "Cannot create primary surface");
117 static int Init( vout_thread_t *p_vout )
119 vout_sys_t *p_sys = p_vout->p_sys;
120 IDirectFBSurface *p_primary = (IDirectFBSurface *) p_vout->p_sys->p_primary;
121 byte_t* p_pixels = NULL;
122 picture_t *p_pic = NULL;
123 int i_rlength, i_glength, i_blength;
124 int i_roffset, i_goffset, i_boffset;
129 I_OUTPUTPICTURES = 0;
131 switch( p_sys->p_pixel_format )
134 /* 8 bit RGB (1 byte, red 3@5, green 3@2, blue 2@0) */
135 /* i_pixel_pitch = 1; */
142 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
146 /* 16 bit RGB (2 byte, red 5@11, green 6@5, blue 5@0) */
147 /* i_pixel_pitch = 2; */
154 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
158 /* 24 bit RGB (3 byte, red 8@16, green 8@8, blue 8@0) */
159 /* i_pixel_pitch = 3; */
166 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
170 /* 24 bit RGB (4 byte, nothing@24, red 8@16, green 8@8, blue 8@0) */
171 /* i_pixel_pitch = 4; */
178 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
182 msg_Err( p_vout, "unknown screen depth %i",
183 p_sys->p_pixel_format );
186 /* Set the RGB masks */
187 p_vout->output.i_rmask = ( (1 << i_rlength) - 1 ) << i_roffset;
188 p_vout->output.i_gmask = ( (1 << i_glength) - 1 ) << i_goffset;
189 p_vout->output.i_bmask = ( (1 << i_blength) - 1 ) << i_boffset;
191 /* Width and height */
192 p_vout->output.i_width = p_sys->i_width;
193 p_vout->output.i_height = p_sys->i_height;
196 p_vout->output.i_aspect = (p_sys->i_width * VOUT_ASPECT_FACTOR) /
199 /* Try to initialize 1 buffer */
200 /* Find an empty picture slot */
201 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
203 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
205 p_pic = p_vout->p_picture + i_index;
210 /* Allocate the picture */
215 if( p_primary->Lock( p_primary, DSLF_READ, (void **) &p_pixels,
216 &i_line_pitch) != DFB_OK )
219 /* allocate p_pixels */
220 i_size = i_line_pitch * p_sys->i_height;
221 p_sys->p_pixels = malloc( sizeof(byte_t) * i_size );
222 if( p_sys->p_pixels == NULL )
224 p_primary->Unlock(p_primary);
229 memcpy( p_sys->p_pixels, p_pixels, i_size );
230 if( p_primary->Unlock(p_primary) != DFB_OK )
235 p_pic->p->p_pixels = p_sys->p_pixels;
236 p_pic->p->i_pixel_pitch = i_line_pitch / p_sys->i_width;
237 p_pic->p->i_lines = p_sys->i_height;
238 p_pic->p->i_visible_lines = p_sys->i_height;
239 p_pic->p->i_pitch = i_line_pitch;
240 p_pic->p->i_visible_pitch = i_line_pitch;
242 p_pic->i_status = DESTROYED_PICTURE;
243 p_pic->i_type = DIRECT_PICTURE;
245 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
252 static void End( vout_thread_t *p_vout )
254 vout_sys_t *p_sys = p_vout->p_sys;
256 if( p_sys->p_pixels )
257 free( p_sys->p_pixels );
260 static void Destroy( vlc_object_t *p_this )
262 vout_thread_t *p_vout = (vout_thread_t *)p_this;
263 vout_sys_t *p_sys = p_vout->p_sys;
265 CloseDisplay( p_vout );
266 if( p_sys ) free( p_sys );
270 static int Manage( vout_thread_t *p_vout )
275 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
277 vout_sys_t *p_sys = p_vout->p_sys;
278 IDirectFBSurface *p_primary = (IDirectFBSurface *) p_sys->p_primary;
279 byte_t* p_pixels = NULL;
284 if( p_primary->Lock( p_primary, DSLF_WRITE,
286 &i_line_pitch) == DFB_OK )
288 i_size = i_line_pitch * p_vout->p_sys->i_height;
291 memcpy( p_pixels, p_pic->p->p_pixels, i_size);
292 if( p_primary->Unlock(p_primary) == DFB_OK )
294 p_primary->Flip(p_primary, NULL, 0);
299 static int OpenDisplay( vout_thread_t *p_vout )
301 vout_sys_t *p_sys = p_vout->p_sys;
302 IDirectFB *p_directfb = NULL;
303 IDirectFBSurface *p_primary = NULL;
304 DFBSurfaceDescription dsc;
306 /*dsc.flags = DSDESC_CAPS | DSDESC_HEIGHT | DSDESC_WIDTH;*/
307 dsc.flags = DSDESC_CAPS;
308 dsc.caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
311 /*dsc.height = 240;*/
312 if( DirectFBCreate( &p_directfb ) != DFB_OK )
315 p_sys->p_directfb = p_directfb;
319 if( p_directfb->CreateSurface( p_directfb, &dsc, &p_primary ) )
322 p_sys->p_primary = p_primary;
326 p_primary->GetSize( p_primary, &p_sys->i_width,
328 p_primary->GetPixelFormat( p_primary, &p_sys->p_pixel_format );
329 p_primary->FillRectangle( p_primary, 0, 0, p_sys->i_width,
331 p_primary->Flip( p_primary, NULL, 0 );
336 static void CloseDisplay( vout_thread_t *p_vout )
338 vout_sys_t *p_sys = p_vout->p_sys;
339 IDirectFB *p_directfb = p_sys->p_directfb;
340 IDirectFBSurface *p_primary = p_sys->p_primary;
343 p_primary->Release( p_primary );
346 p_directfb->Release( p_directfb );