Fix a segfault (buffer overflow for win32 only).
[vlc.git] / modules / video_output / hd1000v.cpp
1 /*****************************************************************************
2  * hd1000v.cpp: HD1000 video output display method
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 extern "C" {
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 <vlc_playlist.h>
38 }
39
40 #include <cascade/graphics/CascadeBitmap.h>
41 #include <cascade/graphics/CascadeScreen.h>
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int  Create    ( vlc_object_t * );
47 static void Destroy   ( vlc_object_t * );
48
49 static int  Init      ( vout_thread_t * );
50 static void End       ( vout_thread_t * );
51 static void Display   ( vout_thread_t *, picture_t * );
52
53 static int NewPicture ( vout_thread_t *, picture_t * );
54 static void FreePicture( vout_thread_t *, picture_t * );
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 vlc_module_begin ()
60     set_description( N_("HD1000 video output") )
61     set_capability( "video output", 100 )
62     add_shortcut( "hd1000v" )
63     set_callbacks( Create, Destroy )
64 vlc_module_end ()
65
66 /*****************************************************************************
67  * vout_sys_t: video output method descriptor
68  *****************************************************************************
69  * This structure is part of the video output thread descriptor.
70  * It describes the aa specific properties of an output thread.
71  *****************************************************************************/
72 struct vout_sys_t
73 {
74     uint32_t            i_width;                     /* width of main window */
75     uint32_t            i_height;                   /* height of main window */
76     uint32_t            i_screen_depth;
77     bool          b_double_buffered;
78  
79     uint32_t            u_current; /* Current output resolution. */
80     CascadeScreen      *p_screen;
81 };
82
83 struct picture_sys_t
84 {
85     CascadeSharedMemZone *p_image;
86 };
87
88 /*****************************************************************************
89  * Create: allocates video thread output method
90  *****************************************************************************
91  * This function allocates and initializes a aa vout method.
92  *****************************************************************************/
93 static int Create( vlc_object_t *p_this )
94 {
95     vout_thread_t *p_vout = (vout_thread_t *)p_this;
96     bool b_double_buffered = false;
97  
98     p_vout->p_sys = (struct vout_sys_t*) malloc( sizeof(struct vout_sys_t) );
99     if( p_vout->p_sys == NULL )
100         return VLC_ENOMEM;
101
102     /* Allocate a screen for VLC vout. */
103     p_vout->p_sys->p_screen = new CascadeScreen();
104     if( p_vout->p_sys->p_screen == NULL )
105     {
106         msg_Err( p_vout, "unable to allocate screen" );
107         free( p_vout->p_sys );
108         return VLC_EGENERIC;
109     }
110
111     p_vout->pf_init = Init;
112     p_vout->pf_end = End;
113     p_vout->pf_manage = NULL;
114     p_vout->pf_render = NULL;
115     p_vout->pf_display = Display;
116
117     /* Get current screen resolution */
118     msg_Dbg( p_vout, "number of screen resolutions supported %u",
119       p_vout->p_sys->p_screen->GetNumScreenResolutionsSupported() );
120  
121     p_vout->p_sys->p_screen->GetCurrentScreenResolution( (u32) p_vout->p_sys->u_current );
122     p_vout->p_sys->p_screen->SetScreenResolution( (u32) p_vout->p_sys->u_current );
123
124 #if 1
125     msg_Dbg( p_vout, "available screen resolutions:" );
126     for (u32 i=0; i<p_vout->p_sys->p_screen->GetNumScreenResolutionsSupported(); i++)
127     {
128         u32 i_width=0;
129     u32 i_height=0;
130     u8 i_screen_depth=0;
131     bool b_buffered;
132     
133         p_vout->p_sys->p_screen->GetSupportedScreenResolutionAt( i,
134             i_width, i_height, i_screen_depth, b_buffered);
135         msg_Dbg( p_vout, "  screen index = %u, width = %u, height = %u, depth = %u, double buffered = %s",
136             i, i_width, i_height, i_screen_depth, (b_buffered ? "yes" : "no") );
137     }
138 #endif
139  
140     p_vout->p_sys->p_screen->GetSupportedScreenResolutionAt( (u32) p_vout->p_sys->u_current,
141             (u32) p_vout->p_sys->i_width,
142             (u32) p_vout->p_sys->i_height,
143             (u8) p_vout->p_sys->i_screen_depth,
144             b_double_buffered );
145     p_vout->p_sys->b_double_buffered = (bool) b_double_buffered;
146     msg_Dbg( p_vout, "using screen index = %u, width = %u, height = %u, depth = %u, double buffered = %d",
147             p_vout->p_sys->u_current, /* Current screen. */
148             p_vout->p_sys->i_width,
149             p_vout->p_sys->i_height,
150             p_vout->p_sys->i_screen_depth,
151             p_vout->p_sys->b_double_buffered );
152  
153     return VLC_SUCCESS;
154 }
155
156 static void Destroy( vlc_object_t *p_this )
157 {
158     vout_thread_t *p_vout = (vout_thread_t *)p_this;
159
160     delete p_vout->p_sys->p_screen;
161     free( p_vout->p_sys );
162 }
163
164 /*****************************************************************************
165  * Init: initialize video thread output method
166  *****************************************************************************/
167 static int Init( vout_thread_t *p_vout )
168 {
169     int i_index;
170     picture_t *p_pic = NULL;
171
172     I_OUTPUTPICTURES = 0;
173
174     p_vout->output.i_chroma = VLC_CODEC_RGB8;
175     p_vout->output.i_width = p_vout->p_sys->i_width;
176     p_vout->output.i_height = p_vout->p_sys->i_height;
177     p_vout->output.i_aspect = p_vout->p_sys->i_width
178                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
179
180     /* Only RGBA 32bpp is supported by output device. */
181     switch( p_vout->p_sys->i_screen_depth )
182     {
183         case 8: /* FIXME: set the palette */
184             p_vout->output.i_chroma = VLC_CODEC_RGB8; break;
185         case 15:
186             p_vout->output.i_chroma = VLC_CODEC_RGB15; break;
187         case 16:
188             p_vout->output.i_chroma = VLC_CODEC_RGB16; break;
189         case 24:
190             p_vout->output.i_chroma = VLC_CODEC_RGB24; break;
191         case 32:
192             p_vout->output.i_chroma = VLC_CODEC_RGB32; break;
193         default:
194             msg_Err( p_vout, "unknown screen depth %i",
195                      p_vout->p_sys->i_screen_depth );
196             return VLC_SUCCESS;
197     }
198
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     if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
210     {
211         return -1;
212     }
213
214     /* Allocate the picture */
215     p_pic->p->i_lines = p_vout->p_sys->i_height;
216     p_pic->p->i_visible_lines = p_vout->p_sys->i_height;
217     p_pic->p->i_pitch = p_vout->p_sys->i_width;
218     p_pic->p->i_pixel_pitch = 1;
219     p_pic->p->i_visible_pitch = p_vout->p_sys->i_width;
220     p_pic->i_planes = 1;
221
222     p_pic->i_status = DESTROYED_PICTURE;
223     p_pic->i_type   = DIRECT_PICTURE;
224
225     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
226     I_OUTPUTPICTURES++;
227
228     return VLC_SUCCESS;
229 }
230
231 /*****************************************************************************
232  * End: terminate video thread output method
233  *****************************************************************************/
234 static void End( vout_thread_t *p_vout )
235 {
236     int i_index;
237
238     /* Free the direct buffers we allocated */
239     for( i_index = I_OUTPUTPICTURES ; i_index ; )
240     {
241         i_index--;
242         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
243     }
244 }
245
246 /*****************************************************************************
247  * NewPicture: Allocate shared memory zone for video output
248  *****************************************************************************/
249 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
250 {
251     CascadeDims p_dims = p_vout->p_sys->p_screen->GetDims();
252
253     p_pic->p_sys = (picture_sys_t *) malloc( sizeof( picture_sys_t ) );
254     if( p_pic->p_sys == NULL )
255     {
256         return -1;
257     }
258
259     /* Fill in picture_t fields */
260     picture_Setup( p_pic, p_vout->output.i_chroma,
261                    p_vout->output.i_width, p_vout->output.i_height,
262                    p_vout->output.i_aspect );
263
264     p_pic->p_sys->p_image = new CascadeSharedMemZone();
265     if( p_pic->p_sys->p_image == NULL )
266     {
267         free( p_pic->p_sys );
268         return -1;
269     }
270
271     if( p_pic->p_sys->p_image->Open( "vlc_hd1000v", p_vout->output.i_width *
272             p_vout->output.i_height * p_vout->p_sys->i_screen_depth,
273             true ) )
274     {
275         msg_Err( p_vout, "failed to allocate shared memory" );
276         free( p_pic->p_sys );
277         return -1;
278     }
279  
280     p_pic->p->i_lines = p_vout->output.i_height;
281     p_pic->p->i_visible_lines = p_vout->output.i_height;
282     p_pic->p->p_pixels = (uint8_t*) p_pic->p_sys->p_image->MapLock();
283     p_pic->p->i_pitch = p_vout->p_sys->i_screen_depth;
284     p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
285                                  * p_vout->output.i_width;
286
287     return VLC_SUCCESS;
288 }
289
290 /*****************************************************************************
291  * FreePicture: destroy a picture allocated with NewPicture
292  *****************************************************************************
293  * Destroy SharedMemZoned AND associated data. The picture normally will be
294  * unlocked in the Display() function except when the video output is closed
295  * before the picture is displayed.
296  *****************************************************************************/
297 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
298 {
299     if( p_pic->p_sys->p_image->Unlock() )
300     { /* Just a test to see the effect described above. REMOVE THIS */
301         msg_Err( p_vout, "unlocking shared memory failed, already unlocked" );
302     }
303  
304     if( p_pic->p_sys->p_image->Close() )
305     {
306         msg_Err( p_vout, "closing shared memory failed. Leaking memory of %ul",
307                     p_pic->p_sys->p_image->GetSize() );
308     }
309  
310     delete p_pic->p_sys->p_image;
311     free( p_pic->p_sys );
312 }
313
314 /*****************************************************************************
315  * Display: Map p_image onto the screen
316  *****************************************************************************/
317 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
318 {
319     uint32_t i_width, i_height, i_x, i_y;
320     uint32_t i_offset = 0;
321  
322     vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
323                        p_vout->p_sys->i_height,
324                        &i_x, &i_y, &i_width, &i_height );
325     msg_Dbg( p_vout, "PlacePicture at x_left = %d, y_left = %d, x_bottom = %d, y_bottom = %d",
326                 i_x, i_y, i_width, i_height );
327
328     /* Currently the only pixel format supported is 32bpp RGBA.*/
329     p_vout->p_sys->p_screen->LockScreen();
330  
331     /* Unlock the shared memory region first. */
332     if( p_pic->p_sys->p_image->Unlock() )
333     {
334         msg_Err( p_vout, "unlocking shared memory failed. Expect threading problems." );
335     }
336  
337     p_vout->p_sys->p_screen->Blit( CascadePoint( (u32) i_x, (u32) i_y ), /* Place bitmap at */
338             (*p_pic->p_sys->p_image)   ,                                      /* Image data */
339             (u32) i_offset,                                   /* Offset in SharedMemoryZone */
340             (u32) i_width,                                           /* Source bitmap width */
341             (u32) i_height,                                         /* Source bitmap height */
342             (u32) p_vout->p_sys->i_screen_depth,                      /* Source pixel depth */
343             CascadeRect( (u32) i_x, (u32) i_y, (u32) i_width, (u32) i_height ) );
344  
345     p_vout->p_sys->p_screen->UnlockScreen();
346 }