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