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