]> git.sesse.net Git - vlc/blob - modules/video_output/hd1000v.cpp
HD1000 video output
[vlc] / modules / video_output / hd1000v.cpp
1 /*****************************************************************************
2  * hd1000v.cpp: HD1000 video output display method
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id: hd1000v.cpp 6961 2004-03-05 17:34:23Z jpsaman $
6  *
7  * Authors: Jean-Paul Saman <jpsaman@wxs.nl>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 extern "C" {
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <string.h>                                            /* strerror() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/vout.h>
34 #include <vlc/intf.h>
35 }
36
37 #include <cascade/graphics/CascadeBitmap.h>
38 #include <cascade/graphics/CascadeScreen.h>
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  Create    ( vlc_object_t * );
44 static void Destroy   ( vlc_object_t * );
45
46 static int  Init      ( vout_thread_t * );
47 static void End       ( vout_thread_t * );
48 static void Display   ( vout_thread_t *, picture_t * );
49
50 static int NewPicture ( vout_thread_t *, picture_t * );
51 static void FreePicture( vout_thread_t *, picture_t * );
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 vlc_module_begin();
57     set_description( _("HD1000 video output") );
58     set_capability( "video output", 10 );
59     add_shortcut( "hd1000v" );
60     set_callbacks( Create, Destroy );
61 vlc_module_end();
62
63 /*****************************************************************************
64  * vout_sys_t: video output method descriptor
65  *****************************************************************************
66  * This structure is part of the video output thread descriptor.
67  * It describes the aa specific properties of an output thread.
68  *****************************************************************************/
69 struct vout_sys_t
70 {
71     uint32_t            i_width;                     /* width of main window */
72     uint32_t            i_height;                   /* height of main window */
73     uint32_t            i_screen_depth;
74     vlc_bool_t          b_double_buffered;
75     
76     uint32_t            u_current; /* Current output resolution. */
77     CascadeScreen      *p_screen;
78 };
79
80 struct picture_sys_t
81 {
82     CascadeSharedMemZone *p_image;
83 };
84
85 /*****************************************************************************
86  * Create: allocates video thread output method
87  *****************************************************************************
88  * This function allocates and initializes a aa vout method.
89  *****************************************************************************/
90 static int Create( vlc_object_t *p_this )
91 {
92     vout_thread_t *p_vout = (vout_thread_t *)p_this;
93     bool b_double_buffered = false;
94     
95     p_vout->p_sys = (struct vout_sys_t*) malloc( sizeof(struct vout_sys_t) );
96     if( p_vout->p_sys == NULL )
97     {
98         msg_Err( p_vout, "out of memory" );
99         return VLC_EGENERIC;
100     }
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->GetCurrentOutputResolution( (u32) p_vout->p_sys->u_current );
122     p_vout->p_sys->p_screen->GetSupportedScreenResolutionAt( (u32) p_vout->p_sys->u_current,
123             (u32) p_vout->p_sys->i_width,
124             (u32) p_vout->p_sys->i_height,
125             (u8) p_vout->p_sys->i_screen_depth,
126             b_double_buffered);
127     p_vout->p_sys->b_double_buffered = (vlc_bool_t) b_double_buffered;
128     msg_Dbg( p_vout, "index = %u, width = %u, height = %u, depth = %u, double buffered = %d",
129             p_vout->p_sys->u_current,
130             p_vout->p_sys->i_width,
131             p_vout->p_sys->i_height,
132             p_vout->p_sys->i_screen_depth,
133             p_vout->p_sys->b_double_buffered );
134
135     return VLC_SUCCESS;
136 }
137
138 static void Destroy( vlc_object_t *p_this )
139 {
140     vout_thread_t *p_vout = (vout_thread_t *)p_this;
141
142     delete p_vout->p_sys->p_screen;
143     free( p_vout->p_sys );
144 }
145
146 /*****************************************************************************
147  * Init: initialize video thread output method
148  *****************************************************************************/
149 static int Init( vout_thread_t *p_vout )
150 {
151     int i_index;
152     picture_t *p_pic = NULL;
153
154     I_OUTPUTPICTURES = 0;
155
156     p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
157     p_vout->output.i_width = p_vout->p_sys->i_width;
158     p_vout->output.i_height = p_vout->p_sys->i_height;
159     p_vout->output.i_aspect = p_vout->p_sys->i_width
160                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
161
162     /* Only RGBA 32bpp is supported by output device. */
163     switch( p_vout->p_sys->i_screen_depth )
164     {
165         case 8: /* FIXME: set the palette */
166             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
167         case 15:
168             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
169         case 16:
170             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
171         case 24:
172             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
173         case 32:
174             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
175         default:
176             msg_Err( p_vout, "unknown screen depth %i",
177                      p_vout->p_sys->i_screen_depth );
178             return VLC_SUCCESS;
179     }
180
181     /* Find an empty picture slot */
182     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
183     {
184         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
185         {
186             p_pic = p_vout->p_picture + i_index;
187             break;
188         }
189     }
190
191     if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
192     {
193         return -1;
194     }
195
196     /* Allocate the picture */
197     p_pic->p->i_lines = p_vout->p_sys->i_height;
198     p_pic->p->i_pitch = p_vout->p_sys->i_width;
199     p_pic->p->i_pixel_pitch = 1;
200     p_pic->p->i_visible_pitch = p_vout->p_sys->i_width;
201     p_pic->i_planes = 1;
202
203     p_pic->i_status = DESTROYED_PICTURE;
204     p_pic->i_type   = DIRECT_PICTURE;
205
206     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
207     I_OUTPUTPICTURES++;
208
209     return VLC_SUCCESS;
210 }
211
212 /*****************************************************************************
213  * End: terminate video thread output method
214  *****************************************************************************/
215 static void End( vout_thread_t *p_vout )
216 {
217     int i_index;
218
219     /* Free the direct buffers we allocated */
220     for( i_index = I_OUTPUTPICTURES ; i_index ; )
221     {
222         i_index--;
223         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
224     }
225 }
226
227 /*****************************************************************************
228  * NewPicture: Allocate shared memory zone for video output
229  *****************************************************************************/
230 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
231 {
232     CascadeDims p_dims = p_vout->p_sys->p_screen->GetDims();
233
234     p_pic->p_sys = (picture_sys_t *) malloc( sizeof( picture_sys_t ) );
235     if( p_pic->p_sys == NULL )
236     {
237         return -1;
238     }
239
240     /* Fill in picture_t fields */
241     vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
242                       p_vout->output.i_width, p_vout->output.i_height,
243                       p_vout->output.i_aspect );
244
245     p_pic->p_sys->p_image = new CascadeSharedMemZone();
246     if( p_pic->p_sys->p_image == NULL )
247     {
248         free( p_pic->p_sys );
249         return -1;
250     }
251
252     if( p_pic->p_sys->p_image->Open( "vlc_hd1000v", p_vout->output.i_width *
253             p_vout->output.i_height * p_vout->p_sys->i_screen_depth,
254             true ) )
255     {
256         msg_Err( p_vout, "failed to allocate shared memory" );
257         free( p_pic->p_sys );
258         return -1;
259     }
260     
261     p_pic->p->i_lines = p_vout->output.i_height;
262     p_pic->p->p_pixels = (uint8_t*) p_pic->p_sys->p_image->MapLock();
263     p_pic->p->i_pitch = p_vout->p_sys->i_screen_depth;
264     p_pic->p->i_visible_pitch = p_pic->p->i_pixel_pitch
265                                  * p_vout->output.i_width;
266
267     return VLC_SUCCESS;                                 
268 }
269
270 /*****************************************************************************
271  * FreePicture: destroy a picture allocated with NewPicture
272  *****************************************************************************
273  * Destroy SharedMemZoned AND associated data. The picture normally will be
274  * unlocked in the Display() function except when the video output is closed
275  * before the picture is displayed.
276  *****************************************************************************/
277 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
278 {
279     if( p_pic->p_sys->p_image->Unlock() )
280     { /* Just a test to see the effect described above. REMOVE THIS */
281         msg_Err( p_vout, "unlocking shared memory failed, already unlocked" );
282     }
283     
284     if( p_pic->p_sys->p_image->Close() )
285     {
286         msg_Err( p_vout, "closing shared memory failed. Leaking memory of %ul",
287                     p_pic->p_sys->p_image->GetSize() );
288     }
289     
290     delete p_pic->p_sys->p_image;
291     free( p_pic->p_sys );
292 }
293
294 /*****************************************************************************
295  * Display: Map p_image onto the screen
296  *****************************************************************************/
297 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
298 {
299     uint32_t i_width, i_height, i_x, i_y;
300     uint32_t i_offset = 0;
301     
302     vout_PlacePicture( p_vout, p_vout->p_sys->i_width,
303                        p_vout->p_sys->i_height,
304                        &i_x, &i_y, &i_width, &i_height );
305     msg_Dbg( p_vout, "PlacePicture at x_left = %d, y_left = %d, x_bottom = %d, y_bottom = %d",
306                 i_x, i_y, i_width, i_height );
307
308     /* Currently the only pixel format supported is 32bpp RGBA.*/
309     p_vout->p_sys->p_screen->LockScreen();
310     
311     /* Unlock the shared memory region first. */
312     if( p_pic->p_sys->p_image->Unlock() ) 
313     {
314         msg_Err( p_vout, "unlocking shared memory failed. Expect threading problems." );
315     }
316     
317     p_vout->p_sys->p_screen->Blit( CascadePoint( (u32) i_x, (u32) i_y ), /* Place bitmap at */
318             (*p_pic->p_sys->p_image)   ,                                      /* Image data */
319             (u32) i_offset,                                   /* Offset in SharedMemoryZone */
320             (u32) i_width,                                           /* Source bitmap width */
321             (u32) i_height,                                         /* Source bitmap height */
322             (u32) p_vout->p_sys->i_screen_depth,                      /* Source pixel depth */
323             CascadeRect( (u32) i_x, (u32) i_y, (u32) i_width, (u32) i_height ) );
324             
325     p_vout->p_sys->p_screen->UnlockScreen();
326 }