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 )
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 /* Initialize thread properties - thread id and locks will be initialized
89 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
90 *p_vout->pi_status = THREAD_CREATE;
92 /* Initialize some fields used by the system-dependant method - these fields will
93 * probably be modified by the method, and are only preferences */
94 p_vout->i_changes = 0;
95 p_vout->i_width = i_width;
96 p_vout->i_height = i_height;
97 p_vout->i_bytes_per_line = i_width * 2;
98 p_vout->i_screen_depth = 15;
99 p_vout->i_bytes_per_pixel = 2;
100 p_vout->f_gamma = VOUT_GAMMA;
102 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
104 p_vout->b_interface = 0;
107 p_vout->p_set_palette = SetPalette;
109 intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
110 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
111 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
113 /* Initialize idle screen */
114 p_vout->last_display_date = mdate();
115 p_vout->last_display_date = 0;
116 p_vout->last_idle_date = 0;
119 /* Initialize statistics fields */
120 p_vout->render_time = 0;
121 p_vout->c_fps_samples = 0;
124 /* Initialize buffer index */
125 p_vout->i_buffer_index = 0;
127 /* Initialize pictures and subpictures - translation tables and functions
128 * will be initialized later in InitThread */
129 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
131 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
132 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
133 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
134 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
137 /* Create and initialize system-dependant method - this function issues its
138 * own error messages */
139 if( vout_SysCreate( p_vout, psz_display, i_root_window ) )
144 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
145 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
146 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
147 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
149 /* Calculate shifts from system-updated masks */
150 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
151 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
152 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
154 /* Set some useful colors */
155 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
156 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
157 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
158 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
160 /* Load fonts - fonts must be initialized after the systme method since
161 * they may be dependant of screen depth and other thread properties */
162 p_vout->p_default_font = vout_LoadFont( VOUT_DEFAULT_FONT );
163 if( p_vout->p_default_font == NULL )
165 vout_SysDestroy( p_vout );
169 p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
170 if( p_vout->p_large_font == NULL )
172 vout_UnloadFont( p_vout->p_default_font );
173 vout_SysDestroy( p_vout );
178 /* Create thread and set locks */
179 vlc_mutex_init( &p_vout->picture_lock );
180 vlc_mutex_init( &p_vout->subpicture_lock );
181 vlc_mutex_init( &p_vout->change_lock );
182 vlc_mutex_lock( &p_vout->change_lock );
183 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
185 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
186 vout_UnloadFont( p_vout->p_default_font );
187 vout_UnloadFont( p_vout->p_large_font );
188 vout_SysDestroy( p_vout );
193 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
194 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
196 /* If status is NULL, wait until the thread is created */
197 if( pi_status == NULL )
201 msleep( THREAD_SLEEP );
202 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
203 && (i_status != THREAD_FATAL) );
204 if( i_status != THREAD_READY )
212 /*****************************************************************************
213 * vout_DestroyThread: destroys a previously created thread
214 *****************************************************************************
215 * Destroy a terminated thread.
216 * The function will request a destruction of the specified thread. If pi_error
217 * is NULL, it will return once the thread is destroyed. Else, it will be
218 * update using one of the THREAD_* constants.
219 *****************************************************************************/
220 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
222 int i_status; /* thread status */
226 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
227 *p_vout->pi_status = THREAD_DESTROY;
229 /* Request thread destruction */
232 /* If status is NULL, wait until thread has been destroyed */
233 if( pi_status == NULL )
237 msleep( THREAD_SLEEP );
238 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
239 && (i_status != THREAD_FATAL) );
243 /*****************************************************************************
244 * vout_DisplaySubPicture: display a subpicture unit
245 *****************************************************************************
246 * Remove the reservation flag of an subpicture, which will cause it to be ready
247 * for display. The picture does not need to be locked, since it is ignored by
248 * the output thread if is reserved.
249 *****************************************************************************/
250 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
253 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
254 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
258 /* Check if status is valid */
259 if( p_subpic->i_status != RESERVED_SUBPICTURE )
261 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
262 p_subpic->i_status );
266 /* Remove reservation flag */
267 p_subpic->i_status = READY_SUBPICTURE;
270 /* Send subpicture informations */
271 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
272 p_subpic, p_subpic->i_type,
273 mstrtime( psz_begin_date, p_subpic->begin_date ),
274 mstrtime( psz_end_date, p_subpic->end_date ) );
278 /*****************************************************************************
279 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
280 *****************************************************************************
281 * This function create a reserved subpicture in the video output heap.
282 * A null pointer is returned if the function fails. This method provides an
283 * already allocated zone of memory in the spu data fields. It needs locking
284 * since several pictures can be created by several producers threads.
285 *****************************************************************************/
286 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
289 int i_subpic; /* subpicture index */
290 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
291 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
294 vlc_mutex_lock( &p_vout->subpicture_lock );
297 * Look for an empty place
299 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
301 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
303 /* Subpicture is marked for destruction, but is still allocated */
304 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
305 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
307 /* Memory size do match or is smaller : memory will not be reallocated,
308 * and function can end immediately - this is the best possible case,
309 * since no memory allocation needs to be done */
310 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
312 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
313 &p_vout->p_subpicture[i_subpic] );
315 vlc_mutex_unlock( &p_vout->subpicture_lock );
316 return( &p_vout->p_subpicture[i_subpic] );
318 else if( p_destroyed_subpic == NULL )
320 /* Memory size do not match, but subpicture index will be kept in
321 * case no other place are left */
322 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
325 else if( (p_free_subpic == NULL) &&
326 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
328 /* Subpicture is empty and ready for allocation */
329 p_free_subpic = &p_vout->p_subpicture[i_subpic];
333 /* If no free subpicture is available, use a destroyed subpicture */
334 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
336 /* No free subpicture or matching destroyed subpicture has been found, but
337 * a destroyed subpicture is still avalaible */
338 free( p_destroyed_subpic->p_data );
339 p_free_subpic = p_destroyed_subpic;
345 if( p_free_subpic != NULL )
347 /* Allocate memory */
350 case TEXT_SUBPICTURE: /* text subpicture */
351 p_free_subpic->p_data = malloc( i_size + 1 );
355 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
356 p_free_subpic->p_data = NULL;
361 if( p_free_subpic->p_data != NULL )
362 { /* Copy subpicture informations, set some default values */
363 p_free_subpic->i_type = i_type;
364 p_free_subpic->i_status = RESERVED_SUBPICTURE;
365 p_free_subpic->i_size = i_size;
366 p_free_subpic->i_x = 0;
367 p_free_subpic->i_y = 0;
368 p_free_subpic->i_width = 0;
369 p_free_subpic->i_height = 0;
370 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
371 p_free_subpic->i_vertical_align = CENTER_RALIGN;
375 /* Memory allocation failed : set subpicture as empty */
376 p_free_subpic->i_type = EMPTY_SUBPICTURE;
377 p_free_subpic->i_status = FREE_SUBPICTURE;
378 p_free_subpic = NULL;
379 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
383 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
385 vlc_mutex_unlock( &p_vout->subpicture_lock );
386 return( p_free_subpic );
389 /* No free or destroyed subpicture could be found */
390 intf_DbgMsg( "warning: heap is full\n" );
391 vlc_mutex_unlock( &p_vout->subpicture_lock );
395 /*****************************************************************************
396 * vout_DestroySubPicture: remove a subpicture from the heap
397 *****************************************************************************
398 * This function frees a previously reserved subpicture.
399 * It is meant to be used when the construction of a picture aborted.
400 * This function does not need locking since reserved subpictures are ignored
401 * by the output thread.
402 *****************************************************************************/
403 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
406 /* Check if status is valid */
407 if( p_subpic->i_status != RESERVED_SUBPICTURE )
409 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
410 p_subpic, p_subpic->i_status );
414 p_subpic->i_status = DESTROYED_SUBPICTURE;
417 intf_DbgMsg("subpicture %p\n", p_subpic);
421 /*****************************************************************************
422 * vout_DisplayPicture: display a picture
423 *****************************************************************************
424 * Remove the reservation flag of a picture, which will cause it to be ready for
425 * display. The picture won't be displayed until vout_DatePicture has been
427 *****************************************************************************/
428 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
430 vlc_mutex_lock( &p_vout->picture_lock );
431 switch( p_pic->i_status )
433 case RESERVED_PICTURE:
434 p_pic->i_status = RESERVED_DISP_PICTURE;
436 case RESERVED_DATED_PICTURE:
437 p_pic->i_status = READY_PICTURE;
441 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
447 intf_DbgMsg("picture %p\n", p_pic);
449 vlc_mutex_unlock( &p_vout->picture_lock );
452 /*****************************************************************************
453 * vout_DatePicture: date a picture
454 *****************************************************************************
455 * Remove the reservation flag of a picture, which will cause it to be ready for
456 * display. The picture won't be displayed until vout_DisplayPicture has been
458 *****************************************************************************/
459 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
462 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
465 vlc_mutex_lock( &p_vout->picture_lock );
467 switch( p_pic->i_status )
469 case RESERVED_PICTURE:
470 p_pic->i_status = RESERVED_DATED_PICTURE;
472 case RESERVED_DISP_PICTURE:
473 p_pic->i_status = READY_PICTURE;
477 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
483 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
485 vlc_mutex_unlock( &p_vout->picture_lock );
488 /*****************************************************************************
489 * vout_CreatePicture: allocate a picture in the video output heap.
490 *****************************************************************************
491 * This function create a reserved image in the video output heap.
492 * A null pointer is returned if the function fails. This method provides an
493 * already allocated zone of memory in the picture data fields. It needs locking
494 * since several pictures can be created by several producers threads.
495 *****************************************************************************/
496 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
497 int i_width, int i_height )
499 int i_picture; /* picture index */
500 int i_chroma_width = 0; /* chroma width */
501 picture_t * p_free_picture = NULL; /* first free picture */
502 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
505 vlc_mutex_lock( &p_vout->picture_lock );
508 * Look for an empty place
510 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
512 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
514 /* Picture is marked for destruction, but is still allocated - note
515 * that if width and type are the same for two pictures, chroma_width
516 * should also be the same */
517 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
518 (p_vout->p_picture[i_picture].i_height == i_height) &&
519 (p_vout->p_picture[i_picture].i_width == i_width) )
521 /* Memory size do match : memory will not be reallocated, and function
522 * can end immediately - this is the best possible case, since no
523 * memory allocation needs to be done */
524 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
526 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
527 &p_vout->p_picture[i_picture] );
529 vlc_mutex_unlock( &p_vout->picture_lock );
530 return( &p_vout->p_picture[i_picture] );
532 else if( p_destroyed_picture == NULL )
534 /* Memory size do not match, but picture index will be kept in
535 * case no other place are left */
536 p_destroyed_picture = &p_vout->p_picture[i_picture];
539 else if( (p_free_picture == NULL) &&
540 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
542 /* Picture is empty and ready for allocation */
543 p_free_picture = &p_vout->p_picture[i_picture];
547 /* If no free picture is available, use a destroyed picture */
548 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
550 /* No free picture or matching destroyed picture has been found, but
551 * a destroyed picture is still avalaible */
552 free( p_destroyed_picture->p_data );
553 p_free_picture = p_destroyed_picture;
559 if( p_free_picture != NULL )
561 /* Allocate memory */
564 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
565 i_chroma_width = i_width / 2;
566 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
567 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
568 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
569 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
571 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
572 i_chroma_width = i_width / 2;
573 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
574 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
575 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
576 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
578 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
579 i_chroma_width = i_width;
580 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
581 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
582 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
583 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
587 intf_DbgMsg("error: unknown picture type %d\n", i_type );
588 p_free_picture->p_data = NULL;
593 if( p_free_picture->p_data != NULL )
595 /* Copy picture informations, set some default values */
596 p_free_picture->i_type = i_type;
597 p_free_picture->i_status = RESERVED_PICTURE;
598 p_free_picture->i_matrix_coefficients = 1;
599 p_free_picture->i_width = i_width;
600 p_free_picture->i_height = i_height;
601 p_free_picture->i_chroma_width = i_chroma_width;
602 p_free_picture->i_display_horizontal_offset = 0;
603 p_free_picture->i_display_vertical_offset = 0;
604 p_free_picture->i_display_width = i_width;
605 p_free_picture->i_display_height = i_height;
606 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
607 p_free_picture->i_refcount = 0;
611 /* Memory allocation failed : set picture as empty */
612 p_free_picture->i_type = EMPTY_PICTURE;
613 p_free_picture->i_status = FREE_PICTURE;
614 p_free_picture = NULL;
615 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
619 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
621 vlc_mutex_unlock( &p_vout->picture_lock );
622 return( p_free_picture );
625 /* No free or destroyed picture could be found */
626 intf_DbgMsg( "warning: heap is full\n" );
627 vlc_mutex_unlock( &p_vout->picture_lock );
631 /*****************************************************************************
632 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
633 *****************************************************************************
634 * This function frees a previously reserved picture or a permanent
635 * picture. It is meant to be used when the construction of a picture aborted.
636 * Note that the picture will be destroyed even if it is linked !
637 * This function does not need locking since reserved pictures are ignored by
639 *****************************************************************************/
640 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
643 /* Check if picture status is valid */
644 if( (p_pic->i_status != RESERVED_PICTURE) &&
645 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
646 (p_pic->i_status != RESERVED_DISP_PICTURE) )
648 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
652 p_pic->i_status = DESTROYED_PICTURE;
655 intf_DbgMsg("picture %p\n", p_pic);
659 /*****************************************************************************
660 * vout_LinkPicture: increment reference counter of a picture
661 *****************************************************************************
662 * This function increment the reference counter of a picture in the video
663 * heap. It needs a lock since several producer threads can access the picture.
664 *****************************************************************************/
665 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
667 vlc_mutex_lock( &p_vout->picture_lock );
671 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
674 vlc_mutex_unlock( &p_vout->picture_lock );
677 /*****************************************************************************
678 * vout_UnlinkPicture: decrement reference counter of a picture
679 *****************************************************************************
680 * This function decrement the reference counter of a picture in the video heap.
681 *****************************************************************************/
682 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
684 vlc_mutex_lock( &p_vout->picture_lock );
688 if( p_pic->i_refcount < 0 )
690 intf_DbgMsg("error: refcount < 0\n");
691 p_pic->i_refcount = 0;
695 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
697 p_pic->i_status = DESTROYED_PICTURE;
701 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
704 vlc_mutex_unlock( &p_vout->picture_lock );
707 /*****************************************************************************
708 * vout_SetBuffers: set buffers adresses
709 *****************************************************************************
710 * This function is called by system drivers to set buffers video memory
712 *****************************************************************************/
713 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
715 /* No picture previously */
716 p_vout->p_buffer[0].i_pic_x = 0;
717 p_vout->p_buffer[0].i_pic_y = 0;
718 p_vout->p_buffer[0].i_pic_width = 0;
719 p_vout->p_buffer[0].i_pic_height = 0;
720 p_vout->p_buffer[1].i_pic_x = 0;
721 p_vout->p_buffer[1].i_pic_y = 0;
722 p_vout->p_buffer[1].i_pic_width = 0;
723 p_vout->p_buffer[1].i_pic_height = 0;
725 /* The first area covers all the screen */
726 p_vout->p_buffer[0].i_areas = 1;
727 p_vout->p_buffer[0].pi_area_begin[0] = 0;
728 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
729 p_vout->p_buffer[1].i_areas = 1;
730 p_vout->p_buffer[1].pi_area_begin[0] = 0;
731 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
734 p_vout->p_buffer[0].p_data = p_buf1;
735 p_vout->p_buffer[1].p_data = p_buf2;
738 /*****************************************************************************
739 * vout_Pixel2RGB: return red, green and blue from pixel value
740 *****************************************************************************
741 * Return color values, in 0-255 range, of the decomposition of a pixel. This
742 * is a slow routine and should only be used for initialization phase.
743 *****************************************************************************/
744 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
746 *pi_red = i_pixel & p_vout->i_red_mask;
747 *pi_green = i_pixel & p_vout->i_green_mask;
748 *pi_blue = i_pixel & p_vout->i_blue_mask;
751 /* following functions are local */
753 /*****************************************************************************
754 * BinaryLog: computes the base 2 log of a binary value
755 *****************************************************************************
756 * This functions is used by MaskToShift, to get a bit index from a binary
758 *****************************************************************************/
759 static int BinaryLog(u32 i)
784 if (i != ((u32)1 << i_log))
786 intf_ErrMsg("internal error: binary log overflow\n");
792 /*****************************************************************************
793 * MaskToShift: transform a color mask into right and left shifts
794 *****************************************************************************
795 * This function is used for obtaining color shifts from masks.
796 *****************************************************************************/
797 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
799 u32 i_low, i_high; /* lower hand higher bits of the mask */
802 i_low = i_mask & (- i_mask); /* lower bit of the mask */
803 i_high = i_mask + i_low; /* higher bit of the mask */
805 /* Transform bits into an index */
806 i_low = BinaryLog (i_low);
807 i_high = BinaryLog (i_high);
809 /* Update pointers and return */
811 *pi_right = (8 - i_high + i_low);
814 /*****************************************************************************
815 * InitThread: initialize video output thread
816 *****************************************************************************
817 * This function is called from RunThread and performs the second step of the
818 * initialization. It returns 0 on success. Note that the thread's flag are not
819 * modified inside this function.
820 *****************************************************************************/
821 static int InitThread( vout_thread_t *p_vout )
825 *p_vout->pi_status = THREAD_START;
827 /* Initialize output method - this function issues its own error messages */
828 if( vout_SysInit( p_vout ) )
833 /* Initialize convertion tables and functions */
834 if( vout_InitYUV( p_vout ) )
836 intf_ErrMsg("error: can't allocate YUV translation tables\n");
840 /* Mark thread as running and return */
841 p_vout->b_active = 1;
842 *p_vout->pi_status = THREAD_READY;
843 intf_DbgMsg("thread ready\n");
847 /*****************************************************************************
848 * RunThread: video output thread
849 *****************************************************************************
850 * Video output thread. This function does only returns when the thread is
851 * terminated. It handles the pictures arriving in the video heap and the
852 * display device events.
853 *****************************************************************************/
854 static void RunThread( vout_thread_t *p_vout)
856 int i_index; /* index in heap */
857 mtime_t current_date; /* current date */
858 mtime_t display_date; /* display date */
859 boolean_t b_display; /* display flag */
860 picture_t * p_pic; /* picture pointer */
861 subpicture_t * p_subpic; /* subpicture pointer */
866 p_vout->b_error = InitThread( p_vout );
867 if( p_vout->b_error )
869 DestroyThread( p_vout, THREAD_ERROR );
875 * Main loop - it is not executed if an error occured during
878 while( (!p_vout->b_die) && (!p_vout->b_error) )
880 /* Initialize loop variables */
884 current_date = mdate();
887 * Find the picture to display - this operation does not need lock,
888 * since only READY_PICTUREs are handled
890 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
892 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
894 (p_vout->p_picture[i_index].date < display_date) ) )
896 p_pic = &p_vout->p_picture[i_index];
897 display_date = p_pic->date;
904 /* Computes FPS rate */
905 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
907 if( display_date < current_date )
909 /* Picture is late: it will be destroyed and the thread will sleep and
910 * go to next picture */
911 vlc_mutex_lock( &p_vout->picture_lock );
912 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
913 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
914 vlc_mutex_unlock( &p_vout->picture_lock );
918 else if( display_date > current_date + VOUT_DISPLAY_DELAY )
920 /* A picture is ready to be rendered, but its rendering date is
921 * far from the current one so the thread will perform an empty loop
922 * as if no picture were found. The picture state is unchanged */
929 * Find the subpicture to display - this operation does not need lock, since
930 * only READY_SUBPICTURES are handled. If no picture has been selected,
931 * display_date will depend on the subpicture
936 * Perform rendering, sleep and display rendered picture
938 if( p_pic ) /* picture and perhaps subpicture */
940 b_display = p_vout->b_active;
941 p_vout->last_display_date = display_date;
945 /* Set picture dimensions and clear buffer */
946 SetBufferPicture( p_vout, p_pic );
948 /* Render picture and informations */
949 RenderPicture( p_vout, p_pic );
952 RenderPictureInfo( p_vout, p_pic );
953 RenderInfo( p_vout );
957 /* Remove picture from heap */
958 vlc_mutex_lock( &p_vout->picture_lock );
959 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
960 vlc_mutex_unlock( &p_vout->picture_lock );
962 /* Render interface and subpicture */
963 if( b_display && p_vout->b_interface )
965 RenderInterface( p_vout );
971 RenderSubPicture( p_vout, p_subpic );
974 /* Remove subpicture from heap */
975 vlc_mutex_lock( &p_vout->subpicture_lock );
976 p_subpic->i_status = DESTROYED_SUBPICTURE;
977 vlc_mutex_unlock( &p_vout->subpicture_lock );
981 else if( p_subpic ) /* subpicture alone */
983 b_display = p_vout->b_active;
984 p_vout->last_display_date = display_date;
989 SetBufferPicture( p_vout, NULL );
991 /* Render informations, interface and subpicture */
994 RenderInfo( p_vout );
996 if( p_vout->b_interface )
998 RenderInterface( p_vout );
1000 RenderSubPicture( p_vout, p_subpic );
1003 /* Remove subpicture from heap */
1004 vlc_mutex_lock( &p_vout->subpicture_lock );
1005 p_subpic->i_status = DESTROYED_SUBPICTURE;
1006 vlc_mutex_unlock( &p_vout->subpicture_lock );
1008 else if( p_vout->b_active ) /* idle or interface screen alone */
1010 if( p_vout->b_interface && 0 /* && ?? intf_change */ )
1012 /* Interface has changed, so a new rendering is required - force
1013 * it by setting last idle date to 0 */
1014 p_vout->last_idle_date = 0;
1017 /* Render idle screen and update idle date, then render interface if
1019 b_display = RenderIdle( p_vout );
1022 p_vout->last_idle_date = current_date;
1023 if( p_vout->b_interface )
1025 RenderInterface( p_vout );
1036 * Sleep, wake up and display rendered picture
1040 /* Store render time */
1041 p_vout->render_time = mdate() - current_date;
1044 /* Give back change lock */
1045 vlc_mutex_unlock( &p_vout->change_lock );
1047 /* Sleep a while or until a given date */
1048 if( display_date != 0 )
1050 mwait( display_date );
1054 msleep( VOUT_IDLE_SLEEP );
1057 /* On awakening, take back lock and send immediately picture to display,
1058 * then swap buffers */
1059 vlc_mutex_lock( &p_vout->change_lock );
1061 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1062 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1064 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1066 vout_SysDisplay( p_vout );
1067 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1071 * Check events and manage thread
1073 if( vout_SysManage( p_vout ) | Manage( p_vout ) )
1075 /* A fatal error occured, and the thread must terminate immediately,
1076 * without displaying anything - setting b_error to 1 cause the
1077 * immediate end of the main while() loop. */
1078 p_vout->b_error = 1;
1083 * Error loop - wait until the thread destruction is requested
1085 if( p_vout->b_error )
1087 ErrorThread( p_vout );
1091 EndThread( p_vout );
1092 DestroyThread( p_vout, THREAD_OVER );
1093 intf_DbgMsg( "thread end\n" );
1096 /*****************************************************************************
1097 * ErrorThread: RunThread() error loop
1098 *****************************************************************************
1099 * This function is called when an error occured during thread main's loop. The
1100 * thread can still receive feed, but must be ready to terminate as soon as
1102 *****************************************************************************/
1103 static void ErrorThread( vout_thread_t *p_vout )
1105 /* Wait until a `die' order */
1107 while( !p_vout->b_die )
1110 msleep( VOUT_IDLE_SLEEP );
1114 /*****************************************************************************
1115 * EndThread: thread destruction
1116 *****************************************************************************
1117 * This function is called when the thread ends after a sucessfull
1118 * initialization. It frees all ressources allocated by InitThread.
1119 *****************************************************************************/
1120 static void EndThread( vout_thread_t *p_vout )
1122 int i_index; /* index in heap */
1126 *p_vout->pi_status = THREAD_END;
1128 /* Destroy all remaining pictures and subpictures */
1129 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1131 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1133 free( p_vout->p_picture[i_index].p_data );
1135 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1137 free( p_vout->p_subpicture[i_index].p_data );
1141 /* Destroy translation tables */
1142 vout_EndYUV( p_vout );
1143 vout_SysEnd( p_vout );
1146 /*****************************************************************************
1147 * DestroyThread: thread destruction
1148 *****************************************************************************
1149 * This function is called when the thread ends. It frees all ressources
1150 * allocated by CreateThread. Status is available at this stage.
1151 *****************************************************************************/
1152 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1154 int *pi_status; /* status adress */
1156 /* Store status adress */
1158 pi_status = p_vout->pi_status;
1160 /* Destroy thread structures allocated by Create and InitThread */
1161 vout_UnloadFont( p_vout->p_default_font );
1162 vout_UnloadFont( p_vout->p_large_font );
1163 vout_SysDestroy( p_vout );
1165 *pi_status = i_status;
1168 /*****************************************************************************
1169 * Print: print simple text on a picture
1170 *****************************************************************************
1171 * This function will print a simple text on the picture. It is designed to
1172 * print debugging or general informations.
1173 *****************************************************************************/
1174 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 )
1176 int i_text_height; /* total text height */
1177 int i_text_width; /* total text width */
1179 /* Update upper left coordinates according to alignment */
1180 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1181 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1183 /* Set area and print text */
1184 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1185 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1186 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1187 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1188 p_vout->i_white_pixel, 0, 0,
1193 /*****************************************************************************
1194 * SetBufferArea: activate an area in current buffer
1195 *****************************************************************************
1196 * This function is called when something is rendered on the current buffer.
1197 * It set the area as active and prepare it to be cleared on next rendering.
1198 * Pay attention to the fact that in this functions, i_h is in fact the end y
1199 * coordinate of the new area.
1200 *****************************************************************************/
1201 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1203 vout_buffer_t * p_buffer; /* current buffer */
1204 int i_area_begin, i_area_end; /* area vertical extension */
1205 int i_area, i_area_copy; /* area index */
1206 int i_area_shift; /* shift distance for areas */
1208 /* Choose buffer and modify h to end of area position */
1209 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1213 * Remove part of the area which is inside the picture - this is done
1214 * by calling again SetBufferArea with the correct areas dimensions.
1216 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1218 i_area_begin = p_buffer->i_pic_y;
1219 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1221 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1222 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1223 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1225 /* Keep the stripe above the picture, if any */
1226 if( i_y < i_area_begin )
1228 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1230 /* Keep the stripe below the picture, if any */
1231 if( i_h > i_area_end )
1233 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1239 /* Skip some extensions until interesting areas */
1241 (i_area < p_buffer->i_areas) &&
1242 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1248 if( i_area == p_buffer->i_areas )
1250 /* New area is below all existing ones: just add it at the end of the
1251 * array, if possible - else, append it to the last one */
1252 if( i_area < VOUT_MAX_AREAS )
1254 p_buffer->pi_area_begin[i_area] = i_y;
1255 p_buffer->pi_area_end[i_area] = i_h;
1256 p_buffer->i_areas++;
1261 intf_DbgMsg("areas overflow\n");
1263 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1268 i_area_begin = p_buffer->pi_area_begin[i_area];
1269 i_area_end = p_buffer->pi_area_end[i_area];
1271 if( i_y < i_area_begin )
1273 if( i_h >= i_area_begin - 1 )
1275 /* Extend area above */
1276 p_buffer->pi_area_begin[i_area] = i_y;
1280 /* Create a new area above : merge last area if overflow, then
1281 * move all old areas down */
1282 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1285 intf_DbgMsg("areas overflow\n");
1287 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1291 p_buffer->i_areas++;
1293 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1295 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1296 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1298 p_buffer->pi_area_begin[i_area] = i_y;
1299 p_buffer->pi_area_end[i_area] = i_h;
1303 if( i_h > i_area_end )
1305 /* Find further areas which can be merged with the new one */
1306 for( i_area_copy = i_area + 1;
1307 (i_area_copy < p_buffer->i_areas) &&
1308 (p_buffer->pi_area_begin[i_area] <= i_h);
1315 if( i_area_copy != i_area )
1317 /* Merge with last possible areas */
1318 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1320 /* Shift lower areas upward */
1321 i_area_shift = i_area_copy - i_area;
1322 p_buffer->i_areas -= i_area_shift;
1323 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1325 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1326 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1331 /* Extend area below */
1332 p_buffer->pi_area_end[i_area] = i_h;
1338 /*****************************************************************************
1339 * SetBufferPicture: clear buffer and set picture area
1340 *****************************************************************************
1341 * This function is called before any rendering. It clears the current
1342 * rendering buffer and set the new picture area. If the picture pointer is
1343 * NULL, then no picture area is defined. Floating operations are avoided since
1344 * some MMX calculations may follow.
1345 *****************************************************************************/
1346 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1348 vout_buffer_t * p_buffer; /* current buffer */
1349 int i_pic_x, i_pic_y; /* picture position */
1350 int i_pic_width, i_pic_height; /* picture dimensions */
1351 int i_old_pic_y, i_old_pic_height; /* old picture area */
1352 int i_vout_width, i_vout_height; /* display dimensions */
1353 int i_area; /* area index */
1354 int i_data_index; /* area data index */
1355 int i_data_size; /* area data size, in 256 bytes blocs */
1356 u64 * p_data; /* area data, for clearing */
1357 byte_t * p_data8; /* area data, for clearing (slow) */
1359 /* Choose buffer and set display dimensions */
1360 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1361 i_vout_width = p_vout->i_width;
1362 i_vout_height = p_vout->i_height;
1365 * Computes new picture size
1369 /* Try horizontal scaling first - width must be a mutiple of 16 */
1370 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1371 i_vout_width : p_pic->i_width) & ~0xf;
1372 switch( p_pic->i_aspect_ratio )
1374 case AR_3_4_PICTURE:
1375 i_pic_height = i_pic_width * 3 / 4;
1377 case AR_16_9_PICTURE:
1378 i_pic_height = i_pic_width * 9 / 16;
1380 case AR_221_1_PICTURE:
1381 i_pic_height = i_pic_width * 100 / 221;
1383 case AR_SQUARE_PICTURE:
1385 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1389 /* If picture dimensions using horizontal scaling are too large, use
1390 * vertical scaling. Since width must be a multiple of 16, height is
1391 * adjusted again after. */
1392 if( i_pic_height > i_vout_height )
1394 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1395 i_vout_height : p_pic->i_height;
1396 switch( p_pic->i_aspect_ratio )
1398 case AR_3_4_PICTURE:
1399 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1400 i_pic_height = i_pic_width * 3 / 4;
1402 case AR_16_9_PICTURE:
1403 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1404 i_pic_height = i_pic_width * 9 / 16;
1406 case AR_221_1_PICTURE:
1407 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1408 i_pic_height = i_pic_width * 100 / 221;
1410 case AR_SQUARE_PICTURE:
1412 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1413 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1418 /* Set picture position */
1419 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1420 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1424 /* No picture: size is 0 */
1432 * Set new picture size - if is is smaller than the previous one, clear
1433 * around it. Since picture are centered, only their size is tested.
1435 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1437 i_old_pic_y = p_buffer->i_pic_y;
1438 i_old_pic_height = p_buffer->i_pic_height;
1439 p_buffer->i_pic_x = i_pic_x;
1440 p_buffer->i_pic_y = i_pic_y;
1441 p_buffer->i_pic_width = i_pic_width;
1442 p_buffer->i_pic_height = i_pic_height;
1443 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1447 p_buffer->i_pic_x = i_pic_x;
1448 p_buffer->i_pic_y = i_pic_y;
1449 p_buffer->i_pic_width = i_pic_width;
1450 p_buffer->i_pic_height = i_pic_height;
1456 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1459 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1460 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1462 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1463 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1464 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1466 /* Clear 256 bytes block */
1467 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1468 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1469 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1470 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1471 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1472 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1473 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1474 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1476 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1478 /* Clear remaining 16 bytes blocks */
1479 *p_data++ = 0; *p_data++ = 0;
1481 p_data8 = (byte_t *)p_data;
1482 for( i_data_index = i_data_size % 16; i_data_index--; )
1484 /* Clear remaining bytes */
1492 p_buffer->i_areas = 0;
1495 /*****************************************************************************
1496 * RenderPicture: render a picture
1497 *****************************************************************************
1498 * This function convert a picture from a video heap to a pixel-encoded image
1499 * and copy it to the current rendering buffer. No lock is required, since the
1500 * rendered picture has been determined as existant, and will only be destroyed
1501 * by the vout thread later.
1502 *****************************************************************************/
1503 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1506 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1507 mtime_t render_time; /* picture rendering time */
1509 vout_buffer_t * p_buffer; /* rendering buffer */
1510 byte_t * p_pic_data; /* convertion destination */
1512 /* Get and set rendering informations */
1513 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1514 p_pic_data = p_buffer->p_data +
1515 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1516 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1518 render_time = mdate();
1522 * Choose appropriate rendering function and render picture
1524 switch( p_pic->i_type )
1526 case YUV_420_PICTURE:
1527 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1528 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1529 p_pic->i_width, p_pic->i_height,
1530 p_buffer->i_pic_width, p_buffer->i_pic_height,
1531 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1532 p_pic->i_matrix_coefficients );
1534 case YUV_422_PICTURE:
1535 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1536 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1537 p_pic->i_width, p_pic->i_height,
1538 p_buffer->i_pic_width, p_buffer->i_pic_height,
1539 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1540 p_pic->i_matrix_coefficients );
1542 case YUV_444_PICTURE:
1543 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1544 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1545 p_pic->i_width, p_pic->i_height,
1546 p_buffer->i_pic_width, p_buffer->i_pic_height,
1547 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1548 p_pic->i_matrix_coefficients );
1552 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1558 /* Print picture date and rendering time */
1559 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1560 p_vout->i_buffer_index, (long) (mdate() - render_time),
1561 mstrtime( psz_date, p_pic->date ));
1565 /*****************************************************************************
1566 * RenderPictureInfo: print additionnal informations on a picture
1567 *****************************************************************************
1568 * This function will print informations such as fps and other picture
1569 * dependant informations.
1570 *****************************************************************************/
1571 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1573 #if defined(STATS) || defined(DEBUG)
1574 char psz_buffer[256]; /* string buffer */
1579 * Print FPS rate in upper right corner
1581 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1583 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1584 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1585 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1586 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1590 * Print frames count and loop time in upper left corner
1592 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1593 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1594 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1599 * Print picture information in lower right corner
1601 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1602 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1603 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1604 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1605 p_pic->i_width, p_pic->i_height,
1606 p_pic->i_display_width, p_pic->i_display_height,
1607 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1608 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1609 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1610 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1611 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1612 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1613 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1614 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1615 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1616 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1620 /*****************************************************************************
1621 * RenderIdle: render idle picture
1622 *****************************************************************************
1623 * This function will print something on the screen. It will return 0 if
1624 * nothing has been rendered, or 1 if something has been changed on the screen.
1625 * Note that if you absolutely want something to be printed, you will have
1626 * to force it by setting the last idle date to 0.
1627 * Unlike other rendering functions, this one calls the SetBufferPicture
1628 * function when needed.
1629 *****************************************************************************/
1630 static int RenderIdle( vout_thread_t *p_vout )
1632 int i_x = 0, i_y = 0; /* text position */
1633 int i_width, i_height; /* text size */
1634 mtime_t current_date; /* current date */
1635 const char *psz_text = "waiting for stream ..."; /* text to display */
1638 current_date = mdate();
1639 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1640 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1642 SetBufferPicture( p_vout, NULL );
1643 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1644 &i_width, &i_height );
1645 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1647 vout_Print( p_vout->p_large_font,
1648 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1649 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1650 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1651 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1652 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1653 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1660 /*****************************************************************************
1661 * RenderInfo: render additionnal informations
1662 *****************************************************************************
1663 * This function render informations which do not depend of the current picture
1665 *****************************************************************************/
1666 static void RenderInfo( vout_thread_t *p_vout )
1669 char psz_buffer[256]; /* string buffer */
1670 int i_ready_pic = 0; /* ready pictures */
1671 int i_reserved_pic = 0; /* reserved pictures */
1672 int i_picture; /* picture index */
1677 * Print thread state in lower left corner
1679 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1681 switch( p_vout->p_picture[i_picture].i_status )
1683 case RESERVED_PICTURE:
1684 case RESERVED_DATED_PICTURE:
1685 case RESERVED_DISP_PICTURE:
1693 sprintf( psz_buffer, "pic: %d/%d/%d",
1694 i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1695 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1699 /*****************************************************************************
1700 * RenderSubPicture: render a subpicture
1701 *****************************************************************************
1702 * This function render a sub picture unit.
1703 *****************************************************************************/
1704 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1706 p_vout_font_t p_font; /* text font */
1707 int i_width, i_height; /* subpicture dimensions */
1709 switch( p_subpic->i_type )
1711 case TEXT_SUBPICTURE: /* single line text */
1712 /* Select default font if not specified */
1713 p_font = p_subpic->type.text.p_font;
1714 if( p_font == NULL )
1716 p_font = p_vout->p_default_font;
1719 /* Computes text size (width and height fields are ignored) and print it */
1720 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1721 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1722 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1724 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1725 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1726 p_subpic->i_y * p_vout->i_bytes_per_line,
1727 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1728 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1729 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1731 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1737 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1742 /*****************************************************************************
1743 * RenderInterface: render the interface
1744 *****************************************************************************
1745 * This function render the interface, if any.
1746 *****************************************************************************/
1747 static void RenderInterface( vout_thread_t *p_vout )
1749 int i_height, i_text_height; /* total and text height */
1750 int i_width_1, i_width_2; /* text width */
1751 int i_byte; /* byte index */
1752 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1753 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1756 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1757 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1758 i_height += i_text_height;
1760 /* Render background */
1761 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1762 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1766 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1769 /* Render text, if not larger than screen */
1770 if( i_width_1 < p_vout->i_width )
1772 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1773 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1774 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1775 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1776 OUTLINED_TEXT, psz_text_1 );
1778 if( i_width_2 < p_vout->i_width )
1780 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1781 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1782 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1783 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1784 OUTLINED_TEXT, psz_text_2 );
1787 /* Activate modified area */
1788 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1791 /*****************************************************************************
1792 * Manage: manage thread
1793 *****************************************************************************
1794 * This function will handle changes in thread configuration.
1795 *****************************************************************************/
1796 static int Manage( vout_thread_t *p_vout )
1799 if( p_vout->i_changes )
1801 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1802 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1806 /* On gamma or grayscale change, rebuild tables */
1807 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1810 if( vout_ResetYUV( p_vout ) )
1812 intf_ErrMsg("error: can't rebuild convertion tables\n");
1817 /* Clear changes flags which does not need management or have been
1819 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1820 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
1821 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
1823 /* Detect unauthorized changes */
1824 if( p_vout->i_changes )
1826 /* Some changes were not acknowledged by vout_SysManage or this function,
1827 * it means they should not be authorized */
1828 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
1835 /*****************************************************************************
1836 * Align: align a subpicture in the screen
1837 *****************************************************************************
1838 * This function is used for rendering text or subpictures. It returns non 0
1839 * it the final aera is not fully included in display area. Return coordinates
1841 *****************************************************************************/
1842 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
1843 int i_width, int i_height, int i_h_align, int i_v_align )
1845 /* Align horizontally */
1849 *pi_x -= i_width / 2;
1852 *pi_x += (p_vout->i_width - i_width) / 2;
1858 *pi_x += p_vout->i_width - i_width;
1862 /* Align vertically */
1866 *pi_y -= i_height / 2;
1869 *pi_y += (p_vout->i_height - i_height) / 2;
1875 *pi_y += p_vout->i_height - i_height;
1877 case SUBTITLE_RALIGN:
1878 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
1879 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
1883 /* Return non 0 if clipping failed */
1884 return( (*pi_x < 0) || (*pi_y < 0) ||
1885 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
1888 /*****************************************************************************
1889 * SetPalette: sets an 8 bpp palette
1890 *****************************************************************************
1891 * This function is just a prototype that does nothing. Architectures that
1892 * support palette allocation should override it.
1893 *****************************************************************************/
1894 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
1895 u16 *green, u16 *blue, u16 *transp )
1897 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );