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