1 /*****************************************************************************
2 * video_output.c : video output thread
4 *****************************************************************************
5 * This module describes the programming interface for video output threads.
6 * It includes functions allowing to open a new thread, send pictures to a
7 * thread, and destroy a previously oppenned video output thread.
8 *****************************************************************************/
10 /*****************************************************************************
12 *****************************************************************************/
21 #include "vlc_thread.h"
23 #include "video_output.h"
24 #include "video_text.h"
25 #include "video_sys.h"
26 #include "video_yuv.h"
30 /*****************************************************************************
32 *****************************************************************************/
33 static int BinaryLog ( u32 i );
34 static void MaskToShift ( int *pi_left, int *pi_right, u32 i_mask );
35 static int InitThread ( vout_thread_t *p_vout );
36 static void RunThread ( vout_thread_t *p_vout );
37 static void ErrorThread ( vout_thread_t *p_vout );
38 static void EndThread ( vout_thread_t *p_vout );
39 static void DestroyThread ( vout_thread_t *p_vout, int i_status );
40 static void Print ( vout_thread_t *p_vout, int i_x, int i_y,
41 int i_h_align, int i_v_align,
42 unsigned char *psz_text );
43 static void SetBufferArea ( vout_thread_t *p_vout, int i_x, int i_y,
45 static void SetBufferPicture ( vout_thread_t *p_vout, picture_t *p_pic );
46 static void RenderPicture ( vout_thread_t *p_vout, picture_t *p_pic );
47 static void RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
48 static void RenderSubPicture ( vout_thread_t *p_vout,
49 subpicture_t *p_subpic );
50 static void RenderInterface ( vout_thread_t *p_vout );
51 static int RenderIdle ( vout_thread_t *p_vout );
52 static void RenderInfo ( vout_thread_t *p_vout );
53 static int Manage ( vout_thread_t *p_vout );
54 static int Align ( vout_thread_t *p_vout, int *pi_x,
55 int *pi_y, int i_width, int i_height,
56 int i_h_align, int i_v_align );
57 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
58 u16 *green, u16 *blue, u16 *transp );
60 /*****************************************************************************
61 * vout_CreateThread: creates a new video output thread
62 *****************************************************************************
63 * This function creates a new video output thread, and returns a pointer
64 * to its description. On error, it returns NULL.
65 * If pi_status is NULL, then the function will block until the thread is ready.
66 * If not, it will be updated using one of the THREAD_* constants.
67 *****************************************************************************/
68 vout_thread_t * vout_CreateThread ( char *psz_display, int i_root_window,
69 int i_width, int i_height, int *pi_status, int i_method )
71 vout_thread_t * p_vout; /* thread descriptor */
72 int i_status; /* thread status */
73 int i_index; /* index for array initialization */
75 /* Allocate descriptor */
77 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
80 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
84 /* Sets method-specific functions */
87 case VOUT_DUMMY_METHOD:
88 p_vout->p_sys_create = vout_DummySysCreate;
89 p_vout->p_sys_init = vout_DummySysInit;
90 p_vout->p_sys_end = vout_DummySysEnd;
91 p_vout->p_sys_destroy = vout_DummySysDestroy;
92 p_vout->p_sys_manage = vout_DummySysManage;
93 p_vout->p_sys_display = vout_DummySysDisplay;
97 p_vout->p_sys_create = vout_X11SysCreate;
98 p_vout->p_sys_init = vout_X11SysInit;
99 p_vout->p_sys_end = vout_X11SysEnd;
100 p_vout->p_sys_destroy = vout_X11SysDestroy;
101 p_vout->p_sys_manage = vout_X11SysManage;
102 p_vout->p_sys_display = vout_X11SysDisplay;
107 p_vout->p_sys_create = vout_FBSysCreate;
108 p_vout->p_sys_init = vout_FBSysInit;
109 p_vout->p_sys_end = vout_FBSysEnd;
110 p_vout->p_sys_destroy = vout_FBSysDestroy;
111 p_vout->p_sys_manage = vout_FBSysManage;
112 p_vout->p_sys_display = vout_FBSysDisplay;
116 case VOUT_GLIDE_METHOD:
117 p_vout->p_sys_create = vout_GlideSysCreate;
118 p_vout->p_sys_init = vout_GlideSysInit;
119 p_vout->p_sys_end = vout_GlideSysEnd;
120 p_vout->p_sys_destroy = vout_GlideSysDestroy;
121 p_vout->p_sys_manage = vout_GlideSysManage;
122 p_vout->p_sys_display = vout_GlideSysDisplay;
126 case VOUT_DGA_METHOD:
127 p_vout->p_sys_create = vout_DGASysCreate;
128 p_vout->p_sys_init = vout_DGASysInit;
129 p_vout->p_sys_end = vout_DGASysEnd;
130 p_vout->p_sys_destroy = vout_DGASysDestroy;
131 p_vout->p_sys_manage = vout_DGASysManage;
132 p_vout->p_sys_display = vout_DGASysDisplay;
136 case VOUT_GGI_METHOD:
137 p_vout->p_sys_create = vout_GGISysCreate;
138 p_vout->p_sys_init = vout_GGISysInit;
139 p_vout->p_sys_end = vout_GGISysEnd;
140 p_vout->p_sys_destroy = vout_GGISysDestroy;
141 p_vout->p_sys_manage = vout_GGISysManage;
142 p_vout->p_sys_display = vout_GGISysDisplay;
146 case VOUT_BEOS_METHOD:
147 p_vout->p_sys_create = vout_BSysCreate;
148 p_vout->p_sys_init = vout_BSysInit;
149 p_vout->p_sys_end = vout_BSysEnd;
150 p_vout->p_sys_destroy = vout_BSysDestroy;
151 p_vout->p_sys_manage = vout_BSysManage;
152 p_vout->p_sys_display = vout_BSysDisplay;
156 intf_ErrMsg( "error: video output method not available\n" );
161 /* Initialize thread properties - thread id and locks will be initialized
165 p_vout->b_active = 0;
166 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
167 *p_vout->pi_status = THREAD_CREATE;
169 /* Initialize some fields used by the system-dependant method - these fields will
170 * probably be modified by the method, and are only preferences */
171 p_vout->i_changes = 0;
172 p_vout->i_width = i_width;
173 p_vout->i_height = i_height;
174 p_vout->i_bytes_per_line = i_width * 2;
175 p_vout->i_screen_depth = 15;
176 p_vout->i_bytes_per_pixel = 2;
177 p_vout->f_gamma = VOUT_GAMMA;
179 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
181 p_vout->b_interface = 0;
184 p_vout->p_set_palette = SetPalette;
186 intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
187 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
188 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
190 /* Initialize idle screen */
191 p_vout->last_display_date = mdate();
192 p_vout->last_display_date = 0;
193 p_vout->last_idle_date = 0;
196 /* Initialize statistics fields */
197 p_vout->render_time = 0;
198 p_vout->c_fps_samples = 0;
201 /* Initialize buffer index */
202 p_vout->i_buffer_index = 0;
204 /* Initialize pictures and subpictures - translation tables and functions
205 * will be initialized later in InitThread */
206 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
208 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
209 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
210 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
211 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
214 /* Create and initialize system-dependant method - this function issues its
215 * own error messages */
216 if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
221 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
222 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
223 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
224 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
226 /* Calculate shifts from system-updated masks */
227 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
228 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
229 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
231 /* Set some useful colors */
232 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
233 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
234 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
235 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
237 /* Load fonts - fonts must be initialized after the system method since
238 * they may be dependant on screen depth and other thread properties */
239 p_vout->p_default_font = vout_LoadFont( VOUT_DEFAULT_FONT );
240 if( p_vout->p_default_font == NULL )
242 p_vout->p_sys_destroy( p_vout );
246 p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
247 if( p_vout->p_large_font == NULL )
249 vout_UnloadFont( p_vout->p_default_font );
250 p_vout->p_sys_destroy( p_vout );
255 /* Create thread and set locks */
256 vlc_mutex_init( &p_vout->picture_lock );
257 vlc_mutex_init( &p_vout->subpicture_lock );
258 vlc_mutex_init( &p_vout->change_lock );
259 vlc_mutex_lock( &p_vout->change_lock );
260 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
262 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
263 vout_UnloadFont( p_vout->p_default_font );
264 vout_UnloadFont( p_vout->p_large_font );
265 p_vout->p_sys_destroy( p_vout );
270 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
271 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
273 /* If status is NULL, wait until the thread is created */
274 if( pi_status == NULL )
278 msleep( THREAD_SLEEP );
279 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
280 && (i_status != THREAD_FATAL) );
281 if( i_status != THREAD_READY )
289 /*****************************************************************************
290 * vout_DestroyThread: destroys a previously created thread
291 *****************************************************************************
292 * Destroy a terminated thread.
293 * The function will request a destruction of the specified thread. If pi_error
294 * is NULL, it will return once the thread is destroyed. Else, it will be
295 * update using one of the THREAD_* constants.
296 *****************************************************************************/
297 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
299 int i_status; /* thread status */
303 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
304 *p_vout->pi_status = THREAD_DESTROY;
306 /* Request thread destruction */
309 /* If status is NULL, wait until thread has been destroyed */
310 if( pi_status == NULL )
314 msleep( THREAD_SLEEP );
315 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
316 && (i_status != THREAD_FATAL) );
320 /*****************************************************************************
321 * vout_DisplaySubPicture: display a subpicture unit
322 *****************************************************************************
323 * Remove the reservation flag of an subpicture, which will cause it to be ready
324 * for display. The picture does not need to be locked, since it is ignored by
325 * the output thread if is reserved.
326 *****************************************************************************/
327 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
330 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
331 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
335 /* Check if status is valid */
336 if( p_subpic->i_status != RESERVED_SUBPICTURE )
338 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
339 p_subpic->i_status );
343 /* Remove reservation flag */
344 p_subpic->i_status = READY_SUBPICTURE;
347 /* Send subpicture informations */
348 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
349 p_subpic, p_subpic->i_type,
350 mstrtime( psz_begin_date, p_subpic->begin_date ),
351 mstrtime( psz_end_date, p_subpic->end_date ) );
355 /*****************************************************************************
356 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
357 *****************************************************************************
358 * This function create a reserved subpicture in the video output heap.
359 * A null pointer is returned if the function fails. This method provides an
360 * already allocated zone of memory in the spu data fields. It needs locking
361 * since several pictures can be created by several producers threads.
362 *****************************************************************************/
363 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
366 int i_subpic; /* subpicture index */
367 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
368 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
371 vlc_mutex_lock( &p_vout->subpicture_lock );
374 * Look for an empty place
376 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
378 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
380 /* Subpicture is marked for destruction, but is still allocated */
381 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
382 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
384 /* Memory size do match or is smaller : memory will not be reallocated,
385 * and function can end immediately - this is the best possible case,
386 * since no memory allocation needs to be done */
387 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
389 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
390 &p_vout->p_subpicture[i_subpic] );
392 vlc_mutex_unlock( &p_vout->subpicture_lock );
393 return( &p_vout->p_subpicture[i_subpic] );
395 else if( p_destroyed_subpic == NULL )
397 /* Memory size do not match, but subpicture index will be kept in
398 * case no other place are left */
399 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
402 else if( (p_free_subpic == NULL) &&
403 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
405 /* Subpicture is empty and ready for allocation */
406 p_free_subpic = &p_vout->p_subpicture[i_subpic];
410 /* If no free subpicture is available, use a destroyed subpicture */
411 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
413 /* No free subpicture or matching destroyed subpicture has been found, but
414 * a destroyed subpicture is still avalaible */
415 free( p_destroyed_subpic->p_data );
416 p_free_subpic = p_destroyed_subpic;
422 if( p_free_subpic != NULL )
424 /* Allocate memory */
427 case TEXT_SUBPICTURE: /* text subpicture */
428 p_free_subpic->p_data = malloc( i_size + 1 );
432 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
433 p_free_subpic->p_data = NULL;
438 if( p_free_subpic->p_data != NULL )
439 { /* Copy subpicture informations, set some default values */
440 p_free_subpic->i_type = i_type;
441 p_free_subpic->i_status = RESERVED_SUBPICTURE;
442 p_free_subpic->i_size = i_size;
443 p_free_subpic->i_x = 0;
444 p_free_subpic->i_y = 0;
445 p_free_subpic->i_width = 0;
446 p_free_subpic->i_height = 0;
447 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
448 p_free_subpic->i_vertical_align = CENTER_RALIGN;
452 /* Memory allocation failed : set subpicture as empty */
453 p_free_subpic->i_type = EMPTY_SUBPICTURE;
454 p_free_subpic->i_status = FREE_SUBPICTURE;
455 p_free_subpic = NULL;
456 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
460 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
462 vlc_mutex_unlock( &p_vout->subpicture_lock );
463 return( p_free_subpic );
466 /* No free or destroyed subpicture could be found */
467 intf_DbgMsg( "warning: heap is full\n" );
468 vlc_mutex_unlock( &p_vout->subpicture_lock );
472 /*****************************************************************************
473 * vout_DestroySubPicture: remove a subpicture from the heap
474 *****************************************************************************
475 * This function frees a previously reserved subpicture.
476 * It is meant to be used when the construction of a picture aborted.
477 * This function does not need locking since reserved subpictures are ignored
478 * by the output thread.
479 *****************************************************************************/
480 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
483 /* Check if status is valid */
484 if( p_subpic->i_status != RESERVED_SUBPICTURE )
486 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
487 p_subpic, p_subpic->i_status );
491 p_subpic->i_status = DESTROYED_SUBPICTURE;
494 intf_DbgMsg("subpicture %p\n", p_subpic);
498 /*****************************************************************************
499 * vout_DisplayPicture: display a picture
500 *****************************************************************************
501 * Remove the reservation flag of a picture, which will cause it to be ready for
502 * display. The picture won't be displayed until vout_DatePicture has been
504 *****************************************************************************/
505 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
507 vlc_mutex_lock( &p_vout->picture_lock );
508 switch( p_pic->i_status )
510 case RESERVED_PICTURE:
511 p_pic->i_status = RESERVED_DISP_PICTURE;
513 case RESERVED_DATED_PICTURE:
514 p_pic->i_status = READY_PICTURE;
518 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
524 intf_DbgMsg("picture %p\n", p_pic);
526 vlc_mutex_unlock( &p_vout->picture_lock );
529 /*****************************************************************************
530 * vout_DatePicture: date a picture
531 *****************************************************************************
532 * Remove the reservation flag of a picture, which will cause it to be ready for
533 * display. The picture won't be displayed until vout_DisplayPicture has been
535 *****************************************************************************/
536 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
539 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
542 vlc_mutex_lock( &p_vout->picture_lock );
544 switch( p_pic->i_status )
546 case RESERVED_PICTURE:
547 p_pic->i_status = RESERVED_DATED_PICTURE;
549 case RESERVED_DISP_PICTURE:
550 p_pic->i_status = READY_PICTURE;
554 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
560 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
562 vlc_mutex_unlock( &p_vout->picture_lock );
565 /*****************************************************************************
566 * vout_CreatePicture: allocate a picture in the video output heap.
567 *****************************************************************************
568 * This function create a reserved image in the video output heap.
569 * A null pointer is returned if the function fails. This method provides an
570 * already allocated zone of memory in the picture data fields. It needs locking
571 * since several pictures can be created by several producers threads.
572 *****************************************************************************/
573 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
574 int i_width, int i_height )
576 int i_picture; /* picture index */
577 int i_chroma_width = 0; /* chroma width */
578 picture_t * p_free_picture = NULL; /* first free picture */
579 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
582 vlc_mutex_lock( &p_vout->picture_lock );
585 * Look for an empty place
587 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
589 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
591 /* Picture is marked for destruction, but is still allocated - note
592 * that if width and type are the same for two pictures, chroma_width
593 * should also be the same */
594 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
595 (p_vout->p_picture[i_picture].i_height == i_height) &&
596 (p_vout->p_picture[i_picture].i_width == i_width) )
598 /* Memory size do match : memory will not be reallocated, and function
599 * can end immediately - this is the best possible case, since no
600 * memory allocation needs to be done */
601 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
603 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
604 &p_vout->p_picture[i_picture] );
606 vlc_mutex_unlock( &p_vout->picture_lock );
607 return( &p_vout->p_picture[i_picture] );
609 else if( p_destroyed_picture == NULL )
611 /* Memory size do not match, but picture index will be kept in
612 * case no other place are left */
613 p_destroyed_picture = &p_vout->p_picture[i_picture];
616 else if( (p_free_picture == NULL) &&
617 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
619 /* Picture is empty and ready for allocation */
620 p_free_picture = &p_vout->p_picture[i_picture];
624 /* If no free picture is available, use a destroyed picture */
625 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
627 /* No free picture or matching destroyed picture has been found, but
628 * a destroyed picture is still avalaible */
629 free( p_destroyed_picture->p_data );
630 p_free_picture = p_destroyed_picture;
636 if( p_free_picture != NULL )
638 /* Allocate memory */
641 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
642 i_chroma_width = i_width / 2;
643 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
644 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
645 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
646 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
648 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
649 i_chroma_width = i_width / 2;
650 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
651 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
652 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
653 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
655 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
656 i_chroma_width = i_width;
657 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
658 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
659 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
660 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
664 intf_DbgMsg("error: unknown picture type %d\n", i_type );
665 p_free_picture->p_data = NULL;
670 if( p_free_picture->p_data != NULL )
672 /* Copy picture informations, set some default values */
673 p_free_picture->i_type = i_type;
674 p_free_picture->i_status = RESERVED_PICTURE;
675 p_free_picture->i_matrix_coefficients = 1;
676 p_free_picture->i_width = i_width;
677 p_free_picture->i_height = i_height;
678 p_free_picture->i_chroma_width = i_chroma_width;
679 p_free_picture->i_display_horizontal_offset = 0;
680 p_free_picture->i_display_vertical_offset = 0;
681 p_free_picture->i_display_width = i_width;
682 p_free_picture->i_display_height = i_height;
683 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
684 p_free_picture->i_refcount = 0;
688 /* Memory allocation failed : set picture as empty */
689 p_free_picture->i_type = EMPTY_PICTURE;
690 p_free_picture->i_status = FREE_PICTURE;
691 p_free_picture = NULL;
692 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
696 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
698 vlc_mutex_unlock( &p_vout->picture_lock );
699 return( p_free_picture );
702 /* No free or destroyed picture could be found */
703 intf_DbgMsg( "warning: heap is full\n" );
704 vlc_mutex_unlock( &p_vout->picture_lock );
708 /*****************************************************************************
709 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
710 *****************************************************************************
711 * This function frees a previously reserved picture or a permanent
712 * picture. It is meant to be used when the construction of a picture aborted.
713 * Note that the picture will be destroyed even if it is linked !
714 * This function does not need locking since reserved pictures are ignored by
716 *****************************************************************************/
717 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
720 /* Check if picture status is valid */
721 if( (p_pic->i_status != RESERVED_PICTURE) &&
722 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
723 (p_pic->i_status != RESERVED_DISP_PICTURE) )
725 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
729 p_pic->i_status = DESTROYED_PICTURE;
732 intf_DbgMsg("picture %p\n", p_pic);
736 /*****************************************************************************
737 * vout_LinkPicture: increment reference counter of a picture
738 *****************************************************************************
739 * This function increment the reference counter of a picture in the video
740 * heap. It needs a lock since several producer threads can access the picture.
741 *****************************************************************************/
742 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
744 vlc_mutex_lock( &p_vout->picture_lock );
748 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
751 vlc_mutex_unlock( &p_vout->picture_lock );
754 /*****************************************************************************
755 * vout_UnlinkPicture: decrement reference counter of a picture
756 *****************************************************************************
757 * This function decrement the reference counter of a picture in the video heap.
758 *****************************************************************************/
759 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
761 vlc_mutex_lock( &p_vout->picture_lock );
765 if( p_pic->i_refcount < 0 )
767 intf_DbgMsg("error: refcount < 0\n");
768 p_pic->i_refcount = 0;
772 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
774 p_pic->i_status = DESTROYED_PICTURE;
778 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
781 vlc_mutex_unlock( &p_vout->picture_lock );
784 /*****************************************************************************
785 * vout_SetBuffers: set buffers adresses
786 *****************************************************************************
787 * This function is called by system drivers to set buffers video memory
789 *****************************************************************************/
790 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
792 /* No picture previously */
793 p_vout->p_buffer[0].i_pic_x = 0;
794 p_vout->p_buffer[0].i_pic_y = 0;
795 p_vout->p_buffer[0].i_pic_width = 0;
796 p_vout->p_buffer[0].i_pic_height = 0;
797 p_vout->p_buffer[1].i_pic_x = 0;
798 p_vout->p_buffer[1].i_pic_y = 0;
799 p_vout->p_buffer[1].i_pic_width = 0;
800 p_vout->p_buffer[1].i_pic_height = 0;
802 /* The first area covers all the screen */
803 p_vout->p_buffer[0].i_areas = 1;
804 p_vout->p_buffer[0].pi_area_begin[0] = 0;
805 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
806 p_vout->p_buffer[1].i_areas = 1;
807 p_vout->p_buffer[1].pi_area_begin[0] = 0;
808 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
811 p_vout->p_buffer[0].p_data = p_buf1;
812 p_vout->p_buffer[1].p_data = p_buf2;
815 /*****************************************************************************
816 * vout_Pixel2RGB: return red, green and blue from pixel value
817 *****************************************************************************
818 * Return color values, in 0-255 range, of the decomposition of a pixel. This
819 * is a slow routine and should only be used for initialization phase.
820 *****************************************************************************/
821 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
823 *pi_red = i_pixel & p_vout->i_red_mask;
824 *pi_green = i_pixel & p_vout->i_green_mask;
825 *pi_blue = i_pixel & p_vout->i_blue_mask;
828 /* following functions are local */
830 /*****************************************************************************
831 * BinaryLog: computes the base 2 log of a binary value
832 *****************************************************************************
833 * This functions is used by MaskToShift, to get a bit index from a binary
835 *****************************************************************************/
836 static int BinaryLog(u32 i)
861 if (i != ((u32)1 << i_log))
863 intf_ErrMsg("internal error: binary log overflow\n");
869 /*****************************************************************************
870 * MaskToShift: transform a color mask into right and left shifts
871 *****************************************************************************
872 * This function is used for obtaining color shifts from masks.
873 *****************************************************************************/
874 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
876 u32 i_low, i_high; /* lower hand higher bits of the mask */
879 i_low = i_mask & (- i_mask); /* lower bit of the mask */
880 i_high = i_mask + i_low; /* higher bit of the mask */
882 /* Transform bits into an index */
883 i_low = BinaryLog (i_low);
884 i_high = BinaryLog (i_high);
886 /* Update pointers and return */
888 *pi_right = (8 - i_high + i_low);
891 /*****************************************************************************
892 * InitThread: initialize video output thread
893 *****************************************************************************
894 * This function is called from RunThread and performs the second step of the
895 * initialization. It returns 0 on success. Note that the thread's flag are not
896 * modified inside this function.
897 *****************************************************************************/
898 static int InitThread( vout_thread_t *p_vout )
902 *p_vout->pi_status = THREAD_START;
904 /* Initialize output method - this function issues its own error messages */
905 if( p_vout->p_sys_init( p_vout ) )
910 /* Initialize convertion tables and functions */
911 if( vout_InitYUV( p_vout ) )
913 intf_ErrMsg("error: can't allocate YUV translation tables\n");
917 /* Mark thread as running and return */
918 p_vout->b_active = 1;
919 *p_vout->pi_status = THREAD_READY;
920 intf_DbgMsg("thread ready\n");
924 /*****************************************************************************
925 * RunThread: video output thread
926 *****************************************************************************
927 * Video output thread. This function does only returns when the thread is
928 * terminated. It handles the pictures arriving in the video heap and the
929 * display device events.
930 *****************************************************************************/
931 static void RunThread( vout_thread_t *p_vout)
933 int i_index; /* index in heap */
934 mtime_t current_date; /* current date */
935 mtime_t display_date; /* display date */
936 boolean_t b_display; /* display flag */
937 picture_t * p_pic; /* picture pointer */
938 subpicture_t * p_subpic; /* subpicture pointer */
943 p_vout->b_error = InitThread( p_vout );
944 if( p_vout->b_error )
946 DestroyThread( p_vout, THREAD_ERROR );
952 * Main loop - it is not executed if an error occured during
955 while( (!p_vout->b_die) && (!p_vout->b_error) )
957 /* Initialize loop variables */
961 current_date = mdate();
964 * Find the picture to display - this operation does not need lock,
965 * since only READY_PICTUREs are handled
967 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
969 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
971 (p_vout->p_picture[i_index].date < display_date) ) )
973 p_pic = &p_vout->p_picture[i_index];
974 display_date = p_pic->date;
981 /* Computes FPS rate */
982 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
984 if( display_date < current_date )
986 /* Picture is late: it will be destroyed and the thread will sleep and
987 * go to next picture */
988 vlc_mutex_lock( &p_vout->picture_lock );
989 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
990 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
991 vlc_mutex_unlock( &p_vout->picture_lock );
995 else if( display_date > current_date + VOUT_DISPLAY_DELAY )
997 /* A picture is ready to be rendered, but its rendering date is
998 * far from the current one so the thread will perform an empty loop
999 * as if no picture were found. The picture state is unchanged */
1006 * Find the subpicture to display - this operation does not need lock, since
1007 * only READY_SUBPICTURES are handled. If no picture has been selected,
1008 * display_date will depend on the subpicture
1013 * Perform rendering, sleep and display rendered picture
1015 if( p_pic ) /* picture and perhaps subpicture */
1017 b_display = p_vout->b_active;
1018 p_vout->last_display_date = display_date;
1022 /* Set picture dimensions and clear buffer */
1023 SetBufferPicture( p_vout, p_pic );
1025 /* Render picture and informations */
1026 RenderPicture( p_vout, p_pic );
1027 if( p_vout->b_info )
1029 RenderPictureInfo( p_vout, p_pic );
1030 RenderInfo( p_vout );
1034 /* Remove picture from heap */
1035 vlc_mutex_lock( &p_vout->picture_lock );
1036 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
1037 vlc_mutex_unlock( &p_vout->picture_lock );
1039 /* Render interface and subpicture */
1040 if( b_display && p_vout->b_interface )
1042 RenderInterface( p_vout );
1048 RenderSubPicture( p_vout, p_subpic );
1051 /* Remove subpicture from heap */
1052 vlc_mutex_lock( &p_vout->subpicture_lock );
1053 p_subpic->i_status = DESTROYED_SUBPICTURE;
1054 vlc_mutex_unlock( &p_vout->subpicture_lock );
1058 else if( p_subpic ) /* subpicture alone */
1060 b_display = p_vout->b_active;
1061 p_vout->last_display_date = display_date;
1066 SetBufferPicture( p_vout, NULL );
1068 /* Render informations, interface and subpicture */
1069 if( p_vout->b_info )
1071 RenderInfo( p_vout );
1073 if( p_vout->b_interface )
1075 RenderInterface( p_vout );
1077 RenderSubPicture( p_vout, p_subpic );
1080 /* Remove subpicture from heap */
1081 vlc_mutex_lock( &p_vout->subpicture_lock );
1082 p_subpic->i_status = DESTROYED_SUBPICTURE;
1083 vlc_mutex_unlock( &p_vout->subpicture_lock );
1085 else if( p_vout->b_active ) /* idle or interface screen alone */
1087 if( p_vout->b_interface && 0 /* && ?? intf_change */ )
1089 /* Interface has changed, so a new rendering is required - force
1090 * it by setting last idle date to 0 */
1091 p_vout->last_idle_date = 0;
1094 /* Render idle screen and update idle date, then render interface if
1096 b_display = RenderIdle( p_vout );
1099 p_vout->last_idle_date = current_date;
1100 if( p_vout->b_interface )
1102 RenderInterface( p_vout );
1113 * Sleep, wake up and display rendered picture
1117 /* Store render time */
1118 p_vout->render_time = mdate() - current_date;
1121 /* Give back change lock */
1122 vlc_mutex_unlock( &p_vout->change_lock );
1124 /* Sleep a while or until a given date */
1125 if( display_date != 0 )
1127 mwait( display_date );
1131 msleep( VOUT_IDLE_SLEEP );
1134 /* On awakening, take back lock and send immediately picture to display,
1135 * then swap buffers */
1136 vlc_mutex_lock( &p_vout->change_lock );
1138 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1139 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1141 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1143 p_vout->p_sys_display( p_vout );
1144 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1148 * Check events and manage thread
1150 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1152 /* A fatal error occured, and the thread must terminate immediately,
1153 * without displaying anything - setting b_error to 1 cause the
1154 * immediate end of the main while() loop. */
1155 p_vout->b_error = 1;
1160 * Error loop - wait until the thread destruction is requested
1162 if( p_vout->b_error )
1164 ErrorThread( p_vout );
1168 EndThread( p_vout );
1169 DestroyThread( p_vout, THREAD_OVER );
1170 intf_DbgMsg( "thread end\n" );
1173 /*****************************************************************************
1174 * ErrorThread: RunThread() error loop
1175 *****************************************************************************
1176 * This function is called when an error occured during thread main's loop. The
1177 * thread can still receive feed, but must be ready to terminate as soon as
1179 *****************************************************************************/
1180 static void ErrorThread( vout_thread_t *p_vout )
1182 /* Wait until a `die' order */
1184 while( !p_vout->b_die )
1187 msleep( VOUT_IDLE_SLEEP );
1191 /*****************************************************************************
1192 * EndThread: thread destruction
1193 *****************************************************************************
1194 * This function is called when the thread ends after a sucessfull
1195 * initialization. It frees all ressources allocated by InitThread.
1196 *****************************************************************************/
1197 static void EndThread( vout_thread_t *p_vout )
1199 int i_index; /* index in heap */
1203 *p_vout->pi_status = THREAD_END;
1205 /* Destroy all remaining pictures and subpictures */
1206 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1208 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1210 free( p_vout->p_picture[i_index].p_data );
1212 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1214 free( p_vout->p_subpicture[i_index].p_data );
1218 /* Destroy translation tables */
1219 vout_EndYUV( p_vout );
1220 p_vout->p_sys_end( p_vout );
1223 /*****************************************************************************
1224 * DestroyThread: thread destruction
1225 *****************************************************************************
1226 * This function is called when the thread ends. It frees all ressources
1227 * allocated by CreateThread. Status is available at this stage.
1228 *****************************************************************************/
1229 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1231 int *pi_status; /* status adress */
1233 /* Store status adress */
1235 pi_status = p_vout->pi_status;
1237 /* Destroy thread structures allocated by Create and InitThread */
1238 vout_UnloadFont( p_vout->p_default_font );
1239 vout_UnloadFont( p_vout->p_large_font );
1240 p_vout->p_sys_destroy( p_vout );
1242 *pi_status = i_status;
1245 /*****************************************************************************
1246 * Print: print simple text on a picture
1247 *****************************************************************************
1248 * This function will print a simple text on the picture. It is designed to
1249 * print debugging or general informations.
1250 *****************************************************************************/
1251 void Print( vout_thread_t *p_vout, int i_x, int i_y, int i_h_align, int i_v_align, unsigned char *psz_text )
1253 int i_text_height; /* total text height */
1254 int i_text_width; /* total text width */
1256 /* Update upper left coordinates according to alignment */
1257 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1258 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1260 /* Set area and print text */
1261 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1262 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1263 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1264 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1265 p_vout->i_white_pixel, 0, 0,
1270 /*****************************************************************************
1271 * SetBufferArea: activate an area in current buffer
1272 *****************************************************************************
1273 * This function is called when something is rendered on the current buffer.
1274 * It set the area as active and prepare it to be cleared on next rendering.
1275 * Pay attention to the fact that in this functions, i_h is in fact the end y
1276 * coordinate of the new area.
1277 *****************************************************************************/
1278 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1280 vout_buffer_t * p_buffer; /* current buffer */
1281 int i_area_begin, i_area_end; /* area vertical extension */
1282 int i_area, i_area_copy; /* area index */
1283 int i_area_shift; /* shift distance for areas */
1285 /* Choose buffer and modify h to end of area position */
1286 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1290 * Remove part of the area which is inside the picture - this is done
1291 * by calling again SetBufferArea with the correct areas dimensions.
1293 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1295 i_area_begin = p_buffer->i_pic_y;
1296 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1298 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1299 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1300 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1302 /* Keep the stripe above the picture, if any */
1303 if( i_y < i_area_begin )
1305 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1307 /* Keep the stripe below the picture, if any */
1308 if( i_h > i_area_end )
1310 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1316 /* Skip some extensions until interesting areas */
1318 (i_area < p_buffer->i_areas) &&
1319 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1325 if( i_area == p_buffer->i_areas )
1327 /* New area is below all existing ones: just add it at the end of the
1328 * array, if possible - else, append it to the last one */
1329 if( i_area < VOUT_MAX_AREAS )
1331 p_buffer->pi_area_begin[i_area] = i_y;
1332 p_buffer->pi_area_end[i_area] = i_h;
1333 p_buffer->i_areas++;
1338 intf_DbgMsg("areas overflow\n");
1340 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1345 i_area_begin = p_buffer->pi_area_begin[i_area];
1346 i_area_end = p_buffer->pi_area_end[i_area];
1348 if( i_y < i_area_begin )
1350 if( i_h >= i_area_begin - 1 )
1352 /* Extend area above */
1353 p_buffer->pi_area_begin[i_area] = i_y;
1357 /* Create a new area above : merge last area if overflow, then
1358 * move all old areas down */
1359 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1362 intf_DbgMsg("areas overflow\n");
1364 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1368 p_buffer->i_areas++;
1370 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1372 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1373 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1375 p_buffer->pi_area_begin[i_area] = i_y;
1376 p_buffer->pi_area_end[i_area] = i_h;
1380 if( i_h > i_area_end )
1382 /* Find further areas which can be merged with the new one */
1383 for( i_area_copy = i_area + 1;
1384 (i_area_copy < p_buffer->i_areas) &&
1385 (p_buffer->pi_area_begin[i_area] <= i_h);
1392 if( i_area_copy != i_area )
1394 /* Merge with last possible areas */
1395 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1397 /* Shift lower areas upward */
1398 i_area_shift = i_area_copy - i_area;
1399 p_buffer->i_areas -= i_area_shift;
1400 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1402 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1403 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1408 /* Extend area below */
1409 p_buffer->pi_area_end[i_area] = i_h;
1415 /*****************************************************************************
1416 * SetBufferPicture: clear buffer and set picture area
1417 *****************************************************************************
1418 * This function is called before any rendering. It clears the current
1419 * rendering buffer and set the new picture area. If the picture pointer is
1420 * NULL, then no picture area is defined. Floating operations are avoided since
1421 * some MMX calculations may follow.
1422 *****************************************************************************/
1423 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1425 vout_buffer_t * p_buffer; /* current buffer */
1426 int i_pic_x, i_pic_y; /* picture position */
1427 int i_pic_width, i_pic_height; /* picture dimensions */
1428 int i_old_pic_y, i_old_pic_height; /* old picture area */
1429 int i_vout_width, i_vout_height; /* display dimensions */
1430 int i_area; /* area index */
1431 int i_data_index; /* area data index */
1432 int i_data_size; /* area data size, in 256 bytes blocs */
1433 u64 * p_data; /* area data, for clearing */
1434 byte_t * p_data8; /* area data, for clearing (slow) */
1436 /* Choose buffer and set display dimensions */
1437 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1438 i_vout_width = p_vout->i_width;
1439 i_vout_height = p_vout->i_height;
1442 * Computes new picture size
1446 /* Try horizontal scaling first - width must be a mutiple of 16 */
1447 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1448 i_vout_width : p_pic->i_width) & ~0xf;
1449 switch( p_pic->i_aspect_ratio )
1451 case AR_3_4_PICTURE:
1452 i_pic_height = i_pic_width * 3 / 4;
1454 case AR_16_9_PICTURE:
1455 i_pic_height = i_pic_width * 9 / 16;
1457 case AR_221_1_PICTURE:
1458 i_pic_height = i_pic_width * 100 / 221;
1460 case AR_SQUARE_PICTURE:
1462 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1466 /* If picture dimensions using horizontal scaling are too large, use
1467 * vertical scaling. Since width must be a multiple of 16, height is
1468 * adjusted again after. */
1469 if( i_pic_height > i_vout_height )
1471 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1472 i_vout_height : p_pic->i_height;
1473 switch( p_pic->i_aspect_ratio )
1475 case AR_3_4_PICTURE:
1476 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1477 i_pic_height = i_pic_width * 3 / 4;
1479 case AR_16_9_PICTURE:
1480 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1481 i_pic_height = i_pic_width * 9 / 16;
1483 case AR_221_1_PICTURE:
1484 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1485 i_pic_height = i_pic_width * 100 / 221;
1487 case AR_SQUARE_PICTURE:
1489 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1490 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1495 /* Set picture position */
1496 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1497 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1501 /* No picture: size is 0 */
1509 * Set new picture size - if is is smaller than the previous one, clear
1510 * around it. Since picture are centered, only their size is tested.
1512 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1514 i_old_pic_y = p_buffer->i_pic_y;
1515 i_old_pic_height = p_buffer->i_pic_height;
1516 p_buffer->i_pic_x = i_pic_x;
1517 p_buffer->i_pic_y = i_pic_y;
1518 p_buffer->i_pic_width = i_pic_width;
1519 p_buffer->i_pic_height = i_pic_height;
1520 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1524 p_buffer->i_pic_x = i_pic_x;
1525 p_buffer->i_pic_y = i_pic_y;
1526 p_buffer->i_pic_width = i_pic_width;
1527 p_buffer->i_pic_height = i_pic_height;
1533 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1536 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1537 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1539 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1540 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1541 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1543 /* Clear 256 bytes block */
1544 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1545 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1546 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1547 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1548 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1549 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1550 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1551 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1553 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1555 /* Clear remaining 16 bytes blocks */
1556 *p_data++ = 0; *p_data++ = 0;
1558 p_data8 = (byte_t *)p_data;
1559 for( i_data_index = i_data_size % 16; i_data_index--; )
1561 /* Clear remaining bytes */
1569 p_buffer->i_areas = 0;
1572 /*****************************************************************************
1573 * RenderPicture: render a picture
1574 *****************************************************************************
1575 * This function convert a picture from a video heap to a pixel-encoded image
1576 * and copy it to the current rendering buffer. No lock is required, since the
1577 * rendered picture has been determined as existant, and will only be destroyed
1578 * by the vout thread later.
1579 *****************************************************************************/
1580 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1583 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1584 mtime_t render_time; /* picture rendering time */
1586 vout_buffer_t * p_buffer; /* rendering buffer */
1587 byte_t * p_pic_data; /* convertion destination */
1589 /* Get and set rendering informations */
1590 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1591 p_pic_data = p_buffer->p_data +
1592 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1593 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1595 render_time = mdate();
1599 * Choose appropriate rendering function and render picture
1601 switch( p_pic->i_type )
1603 case YUV_420_PICTURE:
1604 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1605 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1606 p_pic->i_width, p_pic->i_height,
1607 p_buffer->i_pic_width, p_buffer->i_pic_height,
1608 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1609 p_pic->i_matrix_coefficients );
1611 case YUV_422_PICTURE:
1612 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1613 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1614 p_pic->i_width, p_pic->i_height,
1615 p_buffer->i_pic_width, p_buffer->i_pic_height,
1616 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1617 p_pic->i_matrix_coefficients );
1619 case YUV_444_PICTURE:
1620 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1621 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1622 p_pic->i_width, p_pic->i_height,
1623 p_buffer->i_pic_width, p_buffer->i_pic_height,
1624 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1625 p_pic->i_matrix_coefficients );
1629 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1635 /* Print picture date and rendering time */
1636 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1637 p_vout->i_buffer_index, (long) (mdate() - render_time),
1638 mstrtime( psz_date, p_pic->date ));
1642 /*****************************************************************************
1643 * RenderPictureInfo: print additionnal informations on a picture
1644 *****************************************************************************
1645 * This function will print informations such as fps and other picture
1646 * dependant informations.
1647 *****************************************************************************/
1648 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1650 #if defined(STATS) || defined(DEBUG)
1651 char psz_buffer[256]; /* string buffer */
1656 * Print FPS rate in upper right corner
1658 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1660 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1661 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1662 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1663 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1667 * Print frames count and loop time in upper left corner
1669 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1670 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1671 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1676 * Print picture information in lower right corner
1678 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1679 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1680 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1681 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1682 p_pic->i_width, p_pic->i_height,
1683 p_pic->i_display_width, p_pic->i_display_height,
1684 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1685 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1686 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1687 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1688 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1689 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1690 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1691 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1692 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1693 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1697 /*****************************************************************************
1698 * RenderIdle: render idle picture
1699 *****************************************************************************
1700 * This function will print something on the screen. It will return 0 if
1701 * nothing has been rendered, or 1 if something has been changed on the screen.
1702 * Note that if you absolutely want something to be printed, you will have
1703 * to force it by setting the last idle date to 0.
1704 * Unlike other rendering functions, this one calls the SetBufferPicture
1705 * function when needed.
1706 *****************************************************************************/
1707 static int RenderIdle( vout_thread_t *p_vout )
1709 int i_x = 0, i_y = 0; /* text position */
1710 int i_width, i_height; /* text size */
1711 mtime_t current_date; /* current date */
1712 const char *psz_text = "waiting for stream ..."; /* text to display */
1715 current_date = mdate();
1716 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1717 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1719 SetBufferPicture( p_vout, NULL );
1720 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1721 &i_width, &i_height );
1722 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1724 vout_Print( p_vout->p_large_font,
1725 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1726 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1727 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1728 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1729 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1730 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1737 /*****************************************************************************
1738 * RenderInfo: render additionnal informations
1739 *****************************************************************************
1740 * This function render informations which do not depend of the current picture
1742 *****************************************************************************/
1743 static void RenderInfo( vout_thread_t *p_vout )
1746 char psz_buffer[256]; /* string buffer */
1747 int i_ready_pic = 0; /* ready pictures */
1748 int i_reserved_pic = 0; /* reserved pictures */
1749 int i_picture; /* picture index */
1754 * Print thread state in lower left corner
1756 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1758 switch( p_vout->p_picture[i_picture].i_status )
1760 case RESERVED_PICTURE:
1761 case RESERVED_DATED_PICTURE:
1762 case RESERVED_DISP_PICTURE:
1770 sprintf( psz_buffer, "pic: %d/%d/%d",
1771 i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1772 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1776 /*****************************************************************************
1777 * RenderSubPicture: render a subpicture
1778 *****************************************************************************
1779 * This function render a sub picture unit.
1780 *****************************************************************************/
1781 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1783 p_vout_font_t p_font; /* text font */
1784 int i_width, i_height; /* subpicture dimensions */
1786 switch( p_subpic->i_type )
1788 case TEXT_SUBPICTURE: /* single line text */
1789 /* Select default font if not specified */
1790 p_font = p_subpic->type.text.p_font;
1791 if( p_font == NULL )
1793 p_font = p_vout->p_default_font;
1796 /* Computes text size (width and height fields are ignored) and print it */
1797 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1798 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1799 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1801 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1802 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1803 p_subpic->i_y * p_vout->i_bytes_per_line,
1804 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1805 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1806 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1808 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1814 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1819 /*****************************************************************************
1820 * RenderInterface: render the interface
1821 *****************************************************************************
1822 * This function render the interface, if any.
1823 *****************************************************************************/
1824 static void RenderInterface( vout_thread_t *p_vout )
1826 int i_height, i_text_height; /* total and text height */
1827 int i_width_1, i_width_2; /* text width */
1828 int i_byte; /* byte index */
1829 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1830 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1833 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1834 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1835 i_height += i_text_height;
1837 /* Render background */
1838 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1839 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1843 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1846 /* Render text, if not larger than screen */
1847 if( i_width_1 < p_vout->i_width )
1849 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1850 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1851 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1852 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1853 OUTLINED_TEXT, psz_text_1 );
1855 if( i_width_2 < p_vout->i_width )
1857 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1858 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1859 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1860 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1861 OUTLINED_TEXT, psz_text_2 );
1864 /* Activate modified area */
1865 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1868 /*****************************************************************************
1869 * Manage: manage thread
1870 *****************************************************************************
1871 * This function will handle changes in thread configuration.
1872 *****************************************************************************/
1873 static int Manage( vout_thread_t *p_vout )
1876 if( p_vout->i_changes )
1878 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1879 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1883 /* On gamma or grayscale change, rebuild tables */
1884 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1887 if( vout_ResetYUV( p_vout ) )
1889 intf_ErrMsg("error: can't rebuild convertion tables\n");
1894 /* Clear changes flags which does not need management or have been
1896 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1897 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
1898 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
1900 /* Detect unauthorized changes */
1901 if( p_vout->i_changes )
1903 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
1904 * function, it means they should not be authorized */
1905 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
1912 /*****************************************************************************
1913 * Align: align a subpicture in the screen
1914 *****************************************************************************
1915 * This function is used for rendering text or subpictures. It returns non 0
1916 * it the final aera is not fully included in display area. Return coordinates
1918 *****************************************************************************/
1919 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
1920 int i_width, int i_height, int i_h_align, int i_v_align )
1922 /* Align horizontally */
1926 *pi_x -= i_width / 2;
1929 *pi_x += (p_vout->i_width - i_width) / 2;
1935 *pi_x += p_vout->i_width - i_width;
1939 /* Align vertically */
1943 *pi_y -= i_height / 2;
1946 *pi_y += (p_vout->i_height - i_height) / 2;
1952 *pi_y += p_vout->i_height - i_height;
1954 case SUBTITLE_RALIGN:
1955 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
1956 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
1960 /* Return non 0 if clipping failed */
1961 return( (*pi_x < 0) || (*pi_y < 0) ||
1962 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
1965 /*****************************************************************************
1966 * SetPalette: sets an 8 bpp palette
1967 *****************************************************************************
1968 * This function is just a prototype that does nothing. Architectures that
1969 * support palette allocation should override it.
1970 *****************************************************************************/
1971 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
1972 u16 *green, u16 *blue, u16 *transp )
1974 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );