1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VideoLAN
5 * $Id: picture.c 10081 2005-03-01 15:33:51Z dionoea $
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
8 * Christophe Massiot <massiot@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* free() */
30 #include <string.h> /* strerror() */
35 #include "vlc_image.h"
39 /*****************************************************************************
41 *****************************************************************************/
44 picture_vout_t *p_picture_vout;
47 int i_picture_pos; /* picture position in p_picture_vout */
48 image_handler_t *p_image; /* filters for resizing */
50 image_handler_t *p_image2;
52 int i_height, i_width;
56 /* Delay after which the picture is blanked out if there hasn't been any
58 #define BLANK_DELAY I64C(1000000)
60 typedef void (* pf_release_t)( picture_t * );
61 static void ReleasePicture( picture_t *p_pic )
65 if ( p_pic->i_refcount <= 0 )
67 if ( p_pic->p_sys != NULL )
69 pf_release_t pf_release = (pf_release_t)p_pic->p_sys;
75 if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
76 if( p_pic ) free( p_pic );
81 /*****************************************************************************
83 *****************************************************************************/
84 static int Open ( vlc_object_t * );
85 static void Close ( vlc_object_t * );
86 static int Init ( vout_thread_t * );
87 static void End ( vout_thread_t * );
88 static int Manage ( vout_thread_t * );
89 static void Display ( vout_thread_t *, picture_t * );
91 /*****************************************************************************
93 *****************************************************************************/
94 #define ID_TEXT N_("ID")
95 #define ID_LONGTEXT N_( \
96 "Specify an identifier string for this subpicture" )
98 #define WIDTH_TEXT N_("Video width")
99 #define WIDTH_LONGTEXT N_( \
100 "Allows you to specify the output video width." )
101 #define HEIGHT_TEXT N_("Video height")
102 #define HEIGHT_LONGTEXT N_( \
103 "Allows you to specify the output video height." )
106 set_shortname( _( "Picture" ) );
107 set_description(_("VLC internal picture video output") );
108 set_capability( "video output", 0 );
110 add_string( "picture-id", "Id", NULL, ID_TEXT, ID_LONGTEXT, VLC_FALSE );
111 add_integer( "picture-width", 0, NULL, WIDTH_TEXT,
112 WIDTH_LONGTEXT, VLC_TRUE );
113 add_integer( "picture-height", 0, NULL, HEIGHT_TEXT,
114 HEIGHT_LONGTEXT, VLC_TRUE );
116 set_callbacks( Open, Close );
118 var_Create( p_module->p_libvlc, "picture-lock", VLC_VAR_MUTEX );
121 /*****************************************************************************
122 * Open : allocate video thread output method
123 *****************************************************************************/
124 static int Open( vlc_object_t *p_this )
126 vout_thread_t *p_vout = (vout_thread_t *)p_this;
128 libvlc_t *p_libvlc = p_vout->p_libvlc;
129 picture_vout_t *p_picture_vout = NULL;
130 picture_vout_e_t *p_pic;
131 vlc_value_t val, lockval;
133 p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
136 msg_Err( p_vout, "out of memory" );
140 var_Get( p_libvlc, "picture-lock", &lockval );
141 p_sys->p_lock = lockval.p_address;
142 vlc_mutex_lock( p_sys->p_lock );
144 p_sys->i_picture_pos = -1;
145 if( var_Get( p_libvlc, "p_picture_vout", &val ) != VLC_SUCCESS )
147 p_picture_vout = malloc( sizeof( struct picture_vout_t ) );
148 if( p_picture_vout == NULL )
150 msg_Err( p_vout, "out of memory" );
154 var_Create( p_libvlc, "p_picture_vout", VLC_VAR_ADDRESS );
155 val.p_address = p_picture_vout;
156 var_Set( p_libvlc, "p_picture_vout", val );
158 p_picture_vout->i_picture_num = 0;
159 p_picture_vout->p_pic = NULL;
164 p_picture_vout = val.p_address;
165 for ( i = 0; i < p_picture_vout->i_picture_num; i++ )
167 if ( p_picture_vout->p_pic[i].i_status == PICTURE_VOUT_E_AVAILABLE )
171 if ( i != p_picture_vout->i_picture_num )
172 p_sys->i_picture_pos = i;
175 p_sys->p_picture_vout = p_picture_vout;
177 if ( p_sys->i_picture_pos == -1 )
179 p_picture_vout->p_pic = realloc( p_picture_vout->p_pic,
180 (p_picture_vout->i_picture_num + 1)
181 * sizeof(picture_vout_e_t) );
182 p_sys->i_picture_pos = p_picture_vout->i_picture_num;
183 p_picture_vout->i_picture_num++;
186 p_pic = &p_picture_vout->p_pic[p_sys->i_picture_pos];
187 p_pic->p_picture = NULL;
188 p_pic->i_status = PICTURE_VOUT_E_OCCUPIED;
190 var_Create( p_vout, "picture-id", VLC_VAR_STRING );
191 var_Change( p_vout, "picture-id", VLC_VAR_INHERITVALUE, &val, NULL );
192 p_pic->psz_id = val.psz_string;
194 vlc_mutex_unlock( p_sys->p_lock );
196 var_Create( p_vout, "picture-height", VLC_VAR_INTEGER );
197 var_Change( p_vout, "picture-height", VLC_VAR_INHERITVALUE, &val, NULL );
198 p_sys->i_height = val.i_int;
200 var_Create( p_vout, "picture-width", VLC_VAR_INTEGER );
201 var_Change( p_vout, "picture-width", VLC_VAR_INHERITVALUE, &val, NULL );
202 p_sys->i_width = val.i_int;
204 if ( p_sys->i_height || p_sys->i_width )
206 p_sys->p_image = image_HandlerCreate( p_vout );
208 p_sys->p_image2 = image_HandlerCreate( p_vout );
212 p_sys->i_last_pic = 0;
214 p_vout->pf_init = Init;
215 p_vout->pf_end = End;
216 p_vout->pf_manage = Manage;
217 p_vout->pf_render = NULL;
218 p_vout->pf_display = Display;
224 /*****************************************************************************
226 *****************************************************************************/
227 static int Init( vout_thread_t *p_vout )
232 I_OUTPUTPICTURES = 0;
234 p_vout->output.i_chroma = p_vout->render.i_chroma;
235 p_vout->output.i_width = p_vout->render.i_width;
236 p_vout->output.i_height = p_vout->render.i_height;
237 p_vout->output.i_aspect = p_vout->render.i_aspect;
243 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
245 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
247 p_pic = p_vout->p_picture + i_index;
257 vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic,
258 p_vout->output.i_chroma,
259 p_vout->output.i_width, p_vout->output.i_height,
260 p_vout->output.i_aspect );
262 if( p_pic->i_planes == 0 )
267 p_pic->i_status = DESTROYED_PICTURE;
268 p_pic->i_type = DIRECT_PICTURE;
270 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
274 //return VLC_SUCCESS;
279 /*****************************************************************************
281 *****************************************************************************/
282 static void End( vout_thread_t *p_vout )
287 /*****************************************************************************
289 *****************************************************************************/
290 static void Close( vlc_object_t *p_this )
292 vout_thread_t *p_vout = (vout_thread_t *)p_this;
293 vout_sys_t *p_sys = p_vout->p_sys;
294 picture_vout_t *p_picture_vout = p_sys->p_picture_vout;
295 picture_vout_e_t *p_pic;
296 vlc_bool_t b_last_picture = VLC_TRUE;
299 vlc_mutex_lock( p_sys->p_lock );
300 p_pic = &p_picture_vout->p_pic[p_sys->i_picture_pos];
302 if( p_pic->p_picture )
304 p_pic->p_picture->pf_release( p_pic->p_picture );
305 p_pic->p_picture = NULL;
307 p_pic->i_status = PICTURE_VOUT_E_AVAILABLE;
309 free( p_pic->psz_id );
311 for( i = 0; i < p_picture_vout->i_picture_num; i ++)
313 if( p_picture_vout->p_pic[i].i_status == PICTURE_VOUT_E_OCCUPIED )
315 b_last_picture = VLC_FALSE;
322 free( p_picture_vout->p_pic );
323 free( p_picture_vout );
324 var_Destroy( p_this->p_libvlc, "p_picture_vout" );
327 vlc_mutex_unlock( p_sys->p_lock );
329 if ( p_sys->i_height || p_sys->i_width )
331 image_HandlerDelete( p_sys->p_image );
333 image_HandlerDelete( p_sys->p_image2 );
340 /*****************************************************************************
341 * PushPicture : push a picture in the p_picture_vout structure
342 *****************************************************************************/
343 static void PushPicture( vout_thread_t *p_vout, picture_t *p_picture )
345 vout_sys_t *p_sys = p_vout->p_sys;
346 picture_vout_t *p_picture_vout = p_sys->p_picture_vout;
347 picture_vout_e_t *p_pic;
349 vlc_mutex_lock( p_sys->p_lock );
350 p_pic = &p_picture_vout->p_pic[p_sys->i_picture_pos];
352 if( p_pic->p_picture != NULL )
354 p_pic->p_picture->pf_release( p_pic->p_picture );
356 p_pic->p_picture = p_picture;
358 vlc_mutex_unlock( p_sys->p_lock );
361 /*****************************************************************************
363 *****************************************************************************/
364 static int Manage( vout_thread_t *p_vout )
366 vout_sys_t *p_sys = p_vout->p_sys;
368 if ( mdate() - p_sys->i_last_pic > BLANK_DELAY )
372 picture_t *p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
375 if ( p_sys->i_height || p_sys->i_width )
377 vout_AllocatePicture( p_vout, p_new_pic,
378 VLC_FOURCC('Y','U','V','A'),
379 p_sys->i_width, p_sys->i_height,
380 p_vout->render.i_aspect );
384 vout_AllocatePicture( p_vout, p_new_pic, p_vout->render.i_chroma,
385 p_vout->render.i_width,
386 p_vout->render.i_height,
387 p_vout->render.i_aspect );
390 p_new_pic->i_refcount++;
391 p_new_pic->i_status = DESTROYED_PICTURE;
392 p_new_pic->i_type = DIRECT_PICTURE;
393 p_new_pic->pf_release = ReleasePicture;
395 for ( i = 0; i < p_pic->i_planes; i++ )
397 /* This assumes planar YUV format */
398 p_vout->p_vlc->pf_memset( p_pic->p[i].p_pixels, i ? 0x80 : 0,
400 * p_pic->p[i].i_pitch );
403 PushPicture( p_vout, p_new_pic );
405 PushPicture( p_vout, NULL );
407 p_sys->i_last_pic = INT64_MAX;
413 /*****************************************************************************
415 *****************************************************************************/
416 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
418 vout_sys_t *p_sys = p_vout->p_sys;
419 picture_t *p_new_pic;
421 if ( p_sys->i_height || p_sys->i_width )
423 video_format_t fmt_out = {0}, fmt_in = {0};
425 vide_format_t fmt_middle = {0};
426 picture_t *p_new_pic2;
429 fmt_in.i_chroma = p_vout->render.i_chroma;
430 fmt_in.i_width = p_vout->render.i_width;
431 fmt_in.i_height = p_vout->render.i_height;
434 fmt_middle.i_chroma = p_vout->render.i_chroma;
435 fmt_middle.i_width = p_vout->p_sys->i_width;
436 fmt_middle.i_height = p_vout->p_sys->i_height;
437 fmt_middle.i_visible_width = fmt_middle.i_width;
438 fmt_middle.i_visible_height = fmt_middle.i_height;
440 p_new_pic2 = image_Convert( p_vout->p_sys->p_image2,
441 p_pic, &fmt_in, &fmt_middle );
442 if ( p_new_pic2 == NULL )
444 msg_Err( p_vout, "image resizing failed %dx%d->%dx%d %4.4s",
445 p_vout->render.i_width, p_vout->render.i_height,
446 fmt_middle.i_width, fmt_middle.i_height,
447 (char *)&p_vout->render.i_chroma);
452 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
453 fmt_out.i_width = p_sys->i_width;
454 fmt_out.i_height = p_sys->i_height;
455 fmt_out.i_visible_width = fmt_out.i_width;
456 fmt_out.i_visible_height = fmt_out.i_height;
459 p_new_pic = image_Convert( p_vout->p_sys->p_image,
460 p_new_pic2, &fmt_middle, &fmt_out );
461 p_new_pic2->pf_release( p_new_pic2 );
463 p_new_pic = image_Convert( p_vout->p_sys->p_image,
464 p_pic, &fmt_in, &fmt_out );
466 if ( p_new_pic == NULL )
468 msg_Err( p_vout, "image conversion failed" );
474 p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
475 vout_AllocatePicture( p_vout, p_new_pic, p_pic->format.i_chroma,
476 p_pic->format.i_width, p_pic->format.i_height,
477 p_vout->render.i_aspect );
479 vout_CopyPicture( p_vout, p_new_pic, p_pic );
482 p_new_pic->i_refcount = 1;
483 p_new_pic->i_status = DESTROYED_PICTURE;
484 p_new_pic->i_type = DIRECT_PICTURE;
485 p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
486 p_new_pic->pf_release = ReleasePicture;
488 PushPicture( p_vout, p_new_pic );
489 p_sys->i_last_pic = p_pic->date;