]> git.sesse.net Git - vlc/blob - modules/video_output/hd1000v.cpp
Include vlc_plugin.h as needed
[vlc] / 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/vlc.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( _("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     {
101         msg_Err( p_vout, "out of memory" );
102         return VLC_EGENERIC;
103     }
104
105     /* Allocate a screen for VLC vout. */
106     p_vout->p_sys->p_screen = new CascadeScreen();
107     if( p_vout->p_sys->p_screen == NULL )
108     {
109         msg_Err( p_vout, "unable to allocate screen" );
110         free( p_vout->p_sys );
111         return VLC_EGENERIC;
112     }
113
114     p_vout->pf_init = Init;
115     p_vout->pf_end = End;
116     p_vout->pf_manage = NULL;
117     p_vout->pf_render = NULL;
118     p_vout->pf_display = Display;
119
120     /* Get current screen resolution */
121     msg_Dbg( p_vout, "number of screen resolutions supported %u",
122       p_vout->p_sys->p_screen->GetNumScreenResolutionsSupported() );
123  
124     p_vout->p_sys->p_screen->GetCurrentScreenResolution( (u32) p_vout->p_sys->u_current );
125     p_vout->p_sys->p_screen->SetScreenResolution( (u32) p_vout->p_sys->u_current );
126
127 #if 1
128     msg_Dbg( p_vout, "available screen resolutions:" );
129     for (u32 i=0; i<p_vout->p_sys->p_screen->GetNumScreenResolutionsSupported(); i++)
130     {
131         u32 i_width=0;
132     u32 i_height=0;
133     u8 i_screen_depth=0;
134     bool b_buffered;
135     
136         p_vout->p_sys->p_screen->GetSupportedScreenResolutionAt( i,
137             i_width, i_height, i_screen_depth, b_buffered);
138         msg_Dbg( p_vout, "  screen index = %u, width = %u, height = %u, depth = %u, double buffered = %s",
139             i, i_width, i_height, i_screen_depth, (b_buffered ? "yes" : "no") );
140     }
141 #endif
142  
143     p_vout->p_sys->p_screen->GetSupportedScreenResolutionAt( (u32) p_vout->p_sys->u_current,
144             (u32) p_vout->p_sys->i_width,
145             (u32) p_vout->p_sys->i_height,
146             (u8) p_vout->p_sys->i_screen_depth,
147             b_double_buffered );
148     p_vout->p_sys->b_double_buffered = (bool) b_double_buffered;
149     msg_Dbg( p_vout, "using screen index = %u, width = %u, height = %u, depth = %u, double buffered = %d",
150             p_vout->p_sys->u_current, /* Current screen. */
151             p_vout->p_sys->i_width,
152             p_vout->p_sys->i_height,
153             p_vout->p_sys->i_screen_depth,
154             p_vout->p_sys->b_double_buffered );
155  
156     return VLC_SUCCESS;
157 }
158
159 static void Destroy( vlc_object_t *p_this )
160 {
161     vout_thread_t *p_vout = (vout_thread_t *)p_this;
162
163     delete p_vout->p_sys->p_screen;
164     free( p_vout->p_sys );
165 }
166
167 /*****************************************************************************
168  * Init: initialize video thread output method
169  *****************************************************************************/
170 static int Init( vout_thread_t *p_vout )
171 {
172     int i_index;
173     picture_t *p_pic = NULL;
174
175     I_OUTPUTPICTURES = 0;
176
177     p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
178     p_vout->output.i_width = p_vout->p_sys->i_width;
179     p_vout->output.i_height = p_vout->p_sys->i_height;
180     p_vout->output.i_aspect = p_vout->p_sys->i_width
181                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
182
183     /* Only RGBA 32bpp is supported by output device. */
184     switch( p_vout->p_sys->i_screen_depth )
185     {
186         case 8: /* FIXME: set the palette */
187             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
188         case 15:
189             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
190         case 16:
191             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
192         case 24:
193             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
194         case 32:
195             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
196         default:
197             msg_Err( p_vout, "unknown screen depth %i",
198                      p_vout->p_sys->i_screen_depth );
199             return VLC_SUCCESS;
200     }
201
202     /* Find an empty picture slot */
203     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
204     {
205         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
206         {
207             p_pic = p_vout->p_picture + i_index;
208             break;
209         }
210     }
211
212     if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
213     {
214         return -1;
215     }
216
217     /* Allocate the picture */
218     p_pic->p->i_lines = p_vout->p_sys->i_height;
219     p_pic->p->i_visible_lines = p_vout->p_sys->i_height;
220     p_pic->p->i_pitch = p_vout->p_sys->i_width;
221     p_pic->p->i_pixel_pitch = 1;
222     p_pic->p->i_visible_pitch = p_vout->p_sys->i_width;
223     p_pic->i_planes = 1;
224
225     p_pic->i_status = DESTROYED_PICTURE;
226     p_pic->i_type   = DIRECT_PICTURE;
227
228     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
229     I_OUTPUTPICTURES++;
230
231     return VLC_SUCCESS;
232 }
233
234 /*****************************************************************************
235  * End: terminate video thread output method
236  *****************************************************************************/
237 static void End( vout_thread_t *p_vout )
238 {
239     int i_index;
240
241     /* Free the direct buffers we allocated */
242     for( i_index = I_OUTPUTPICTURES ; i_index ; )
243     {
244         i_index--;
245         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
246     }
247 }
248
249 /*****************************************************************************
250  * NewPicture: Allocate shared memory zone for video output
251  *****************************************************************************/
252 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
253 {
254     CascadeDims p_dims = p_vout->p_sys->p_screen->GetDims();
255
256     p_pic->p_sys = (picture_sys_t *) malloc( sizeof( picture_sys_t ) );
257     if( p_pic->p_sys == NULL )
258     {
259         return -1;
260     }
261
262     /* Fill in picture_t fields */
263     vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
264                       p_vout->output.i_width, p_vout->output.i_height,
265                       p_vout->output.i_aspect );
266
267     p_pic->p_sys->p_image = new CascadeSharedMemZone();
268     if( p_pic->p_sys->p_image == NULL )
269     {
270         free( p_pic->p_sys );
271         return -1;
272     }
273
274     if( p_pic->p_sys->p_image->Open( "vlc_hd1000v", p_vout->output.i_width *
275             p_vout->output.i_height * p_vout->p_sys->i_screen_depth,
276             true ) )
277     {
278         msg_Err( p_vout, "failed to allocate shared memory" );
279         free( p_pic->p_sys );
280         return -1;
281     }
282  
283     p_pic->p->i_lines = p_vout->output.i_height;
284     p_pic->p->i_visible_lines = p_vout->output.i_height;
285     p_pic->p->p_pixels = (uint8_t*) p_pic->p_sys->p_image->MapLock();
286     p_pic->p->i_pitch = p_vout->p_sys->i_screen_depth;
287     p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
288                                  * p_vout->output.i_width;
289
290     return VLC_SUCCESS;
291 }
292
293 /*****************************************************************************
294  * FreePicture: destroy a picture allocated with NewPicture
295  *****************************************************************************
296  * Destroy SharedMemZoned AND associated data. The picture normally will be
297  * unlocked in the Display() function except when the video output is closed
298  * before the picture is displayed.
299  *****************************************************************************/
300 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
301 {
302     if( p_pic->p_sys->p_image->Unlock() )
303     { /* Just a test to see the effect described above. REMOVE THIS */
304         msg_Err( p_vout, "unlocking shared memory failed, already unlocked" );
305     }
306  
307     if( p_pic->p_sys->p_image->Close() )
308     {
309         msg_Err( p_vout, "closing shared memory failed. Leaking memory of %ul",
310                     p_pic->p_sys->p_image->GetSize() );
311     }
312  
313     delete p_pic->p_sys->p_image;
314     free( p_pic->p_sys );
315 }
316
317 /*****************************************************************************
318  * Display: Map p_image onto the screen
319  *****************************************************************************/
320 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
321 {
322     uint32_t i_width, i_height, i_x, i_y;
323     uint32_t i_offset = 0;
324  
325     vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
326                        p_vout->p_sys->i_height,
327                        &i_x, &i_y, &i_width, &i_height );
328     msg_Dbg( p_vout, "PlacePicture at x_left = %d, y_left = %d, x_bottom = %d, y_bottom = %d",
329                 i_x, i_y, i_width, i_height );
330
331     /* Currently the only pixel format supported is 32bpp RGBA.*/
332     p_vout->p_sys->p_screen->LockScreen();
333  
334     /* Unlock the shared memory region first. */
335     if( p_pic->p_sys->p_image->Unlock() )
336     {
337         msg_Err( p_vout, "unlocking shared memory failed. Expect threading problems." );
338     }
339  
340     p_vout->p_sys->p_screen->Blit( CascadePoint( (u32) i_x, (u32) i_y ), /* Place bitmap at */
341             (*p_pic->p_sys->p_image)   ,                                      /* Image data */
342             (u32) i_offset,                                   /* Offset in SharedMemoryZone */
343             (u32) i_width,                                           /* Source bitmap width */
344             (u32) i_height,                                         /* Source bitmap height */
345             (u32) p_vout->p_sys->i_screen_depth,                      /* Source pixel depth */
346             CascadeRect( (u32) i_x, (u32) i_y, (u32) i_width, (u32) i_height ) );
347  
348     p_vout->p_sys->p_screen->UnlockScreen();
349 }