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 );
58 /******************************************************************************
59 * vout_CreateThread: creates a new video output thread
60 ******************************************************************************
61 * This function creates a new video output thread, and returns a pointer
62 * to its description. On error, it returns NULL.
63 * If pi_status is NULL, then the function will block until the thread is ready.
64 * If not, it will be updated using one of the THREAD_* constants.
65 ******************************************************************************/
66 vout_thread_t * vout_CreateThread ( char *psz_display, int i_root_window,
67 int i_width, int i_height, int *pi_status )
69 vout_thread_t * p_vout; /* thread descriptor */
70 int i_status; /* thread status */
71 int i_index; /* index for array initialization */
73 /* Allocate descriptor */
75 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
78 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
82 /* Initialize thread properties - thread id and locks will be initialized
87 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
88 *p_vout->pi_status = THREAD_CREATE;
90 /* Initialize some fields used by the system-dependant method - these fields will
91 * probably be modified by the method, and are only preferences */
92 p_vout->i_changes = 0;
93 p_vout->i_width = i_width;
94 p_vout->i_height = i_height;
95 p_vout->i_bytes_per_line = i_width * 2;
96 p_vout->i_screen_depth = 15;
97 p_vout->i_bytes_per_pixel = 2;
98 p_vout->f_gamma = VOUT_GAMMA;
100 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
102 p_vout->b_interface = 0;
105 intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
106 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
107 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
109 /* Initialize idle screen */
110 p_vout->last_display_date = mdate();
111 p_vout->last_idle_date = 0;
114 /* Initialize statistics fields */
115 p_vout->render_time = 0;
116 p_vout->c_fps_samples = 0;
119 /* Initialize buffer index */
120 p_vout->i_buffer_index = 0;
122 /* Initialize pictures and subpictures - translation tables and functions
123 * will be initialized later in InitThread */
124 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
126 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
127 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
128 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
129 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
132 /* Create and initialize system-dependant method - this function issues its
133 * own error messages */
134 if( vout_SysCreate( p_vout, psz_display, i_root_window ) )
139 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
140 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
141 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
142 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
144 /* Calculate shifts from system-updated masks */
145 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
146 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
147 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
149 /* Set some usefull colors */
150 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
151 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
152 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
153 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
155 /* Load fonts - fonts must be initialized after the systme method since
156 * they may be dependant of screen depth and other thread properties */
157 p_vout->p_default_font = vout_LoadFont( VOUT_DEFAULT_FONT );
158 if( p_vout->p_default_font == NULL )
160 vout_SysDestroy( p_vout );
164 p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
165 if( p_vout->p_large_font == NULL )
167 vout_UnloadFont( p_vout->p_default_font );
168 vout_SysDestroy( p_vout );
173 /* Create thread and set locks */
174 vlc_mutex_init( &p_vout->picture_lock );
175 vlc_mutex_init( &p_vout->subpicture_lock );
176 vlc_mutex_init( &p_vout->change_lock );
177 vlc_mutex_lock( &p_vout->change_lock );
178 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
180 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
181 vout_UnloadFont( p_vout->p_default_font );
182 vout_UnloadFont( p_vout->p_large_font );
183 vout_SysDestroy( p_vout );
188 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
189 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
191 /* If status is NULL, wait until the thread is created */
192 if( pi_status == NULL )
196 msleep( THREAD_SLEEP );
197 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
198 && (i_status != THREAD_FATAL) );
199 if( i_status != THREAD_READY )
207 /******************************************************************************
208 * vout_DestroyThread: destroys a previously created thread
209 ******************************************************************************
210 * Destroy a terminated thread.
211 * The function will request a destruction of the specified thread. If pi_error
212 * is NULL, it will return once the thread is destroyed. Else, it will be
213 * update using one of the THREAD_* constants.
214 ******************************************************************************/
215 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
217 int i_status; /* thread status */
221 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
222 *p_vout->pi_status = THREAD_DESTROY;
224 /* Request thread destruction */
227 /* If status is NULL, wait until thread has been destroyed */
228 if( pi_status == NULL )
232 msleep( THREAD_SLEEP );
233 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
234 && (i_status != THREAD_FATAL) );
238 /******************************************************************************
239 * vout_DisplaySubPicture: display a subpicture unit
240 ******************************************************************************
241 * Remove the reservation flag of an subpicture, which will cause it to be ready
242 * for display. The picture does not need to be locked, since it is ignored by
243 * the output thread if is reserved.
244 ******************************************************************************/
245 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
248 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
249 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
253 /* Check if status is valid */
254 if( p_subpic->i_status != RESERVED_SUBPICTURE )
256 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
257 p_subpic->i_status );
261 /* Remove reservation flag */
262 p_subpic->i_status = READY_SUBPICTURE;
265 /* Send subpicture informations */
266 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
267 p_subpic, p_subpic->i_type,
268 mstrtime( psz_begin_date, p_subpic->begin_date ),
269 mstrtime( psz_end_date, p_subpic->end_date ) );
273 /******************************************************************************
274 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
275 ******************************************************************************
276 * This function create a reserved subpicture in the video output heap.
277 * A null pointer is returned if the function fails. This method provides an
278 * already allocated zone of memory in the spu data fields. It needs locking
279 * since several pictures can be created by several producers threads.
280 ******************************************************************************/
281 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
284 int i_subpic; /* subpicture index */
285 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
286 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
289 vlc_mutex_lock( &p_vout->subpicture_lock );
292 * Look for an empty place
294 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
296 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
298 /* Subpicture is marked for destruction, but is still allocated */
299 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
300 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
302 /* Memory size do match or is smaller : memory will not be reallocated,
303 * and function can end immediately - this is the best possible case,
304 * since no memory allocation needs to be done */
305 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
307 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
308 &p_vout->p_subpicture[i_subpic] );
310 vlc_mutex_unlock( &p_vout->subpicture_lock );
311 return( &p_vout->p_subpicture[i_subpic] );
313 else if( p_destroyed_subpic == NULL )
315 /* Memory size do not match, but subpicture index will be kept in
316 * case no other place are left */
317 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
320 else if( (p_free_subpic == NULL) &&
321 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
323 /* Subpicture is empty and ready for allocation */
324 p_free_subpic = &p_vout->p_subpicture[i_subpic];
328 /* If no free subpicture is available, use a destroyed subpicture */
329 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
331 /* No free subpicture or matching destroyed subpicture has been found, but
332 * a destroyed subpicture is still avalaible */
333 free( p_destroyed_subpic->p_data );
334 p_free_subpic = p_destroyed_subpic;
340 if( p_free_subpic != NULL )
342 /* Allocate memory */
345 case TEXT_SUBPICTURE: /* text subpicture */
346 p_free_subpic->p_data = malloc( i_size + 1 );
350 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
351 p_free_subpic->p_data = NULL;
356 if( p_free_subpic->p_data != NULL )
357 { /* Copy subpicture informations, set some default values */
358 p_free_subpic->i_type = i_type;
359 p_free_subpic->i_status = RESERVED_SUBPICTURE;
360 p_free_subpic->i_size = i_size;
361 p_free_subpic->i_x = 0;
362 p_free_subpic->i_y = 0;
363 p_free_subpic->i_width = 0;
364 p_free_subpic->i_height = 0;
365 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
366 p_free_subpic->i_vertical_align = CENTER_RALIGN;
370 /* Memory allocation failed : set subpicture as empty */
371 p_free_subpic->i_type = EMPTY_SUBPICTURE;
372 p_free_subpic->i_status = FREE_SUBPICTURE;
373 p_free_subpic = NULL;
374 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
378 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
380 vlc_mutex_unlock( &p_vout->subpicture_lock );
381 return( p_free_subpic );
384 /* No free or destroyed subpicture could be found */
385 intf_DbgMsg( "warning: heap is full\n" );
386 vlc_mutex_unlock( &p_vout->subpicture_lock );
390 /******************************************************************************
391 * vout_DestroySubPicture: remove a subpicture from the heap
392 ******************************************************************************
393 * This function frees a previously reserved subpicture.
394 * It is meant to be used when the construction of a picture aborted.
395 * This function does not need locking since reserved subpictures are ignored
396 * by the output thread.
397 ******************************************************************************/
398 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
401 /* Check if status is valid */
402 if( p_subpic->i_status != RESERVED_SUBPICTURE )
404 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
405 p_subpic, p_subpic->i_status );
409 p_subpic->i_status = DESTROYED_SUBPICTURE;
412 intf_DbgMsg("subpicture %p\n", p_subpic);
416 /*******************************************************************************
417 * vout_DisplayPicture: display a picture
418 *******************************************************************************
419 * Remove the reservation flag of a picture, which will cause it to be ready for
420 * display. The picture won't be displayed until vout_DatePicture has been
422 *******************************************************************************/
423 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
425 vlc_mutex_lock( &p_vout->picture_lock );
426 switch( p_pic->i_status )
428 case RESERVED_PICTURE:
429 p_pic->i_status = RESERVED_DISP_PICTURE;
431 case RESERVED_DATED_PICTURE:
432 p_pic->i_status = READY_PICTURE;
436 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
442 intf_DbgMsg("picture %p\n", p_pic);
444 vlc_mutex_unlock( &p_vout->picture_lock );
447 /*******************************************************************************
448 * vout_DatePicture: date a picture
449 *******************************************************************************
450 * Remove the reservation flag of a picture, which will cause it to be ready for
451 * display. The picture won't be displayed until vout_DisplayPicture has been
453 *******************************************************************************/
454 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
457 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
460 vlc_mutex_lock( &p_vout->picture_lock );
462 switch( p_pic->i_status )
464 case RESERVED_PICTURE:
465 p_pic->i_status = RESERVED_DATED_PICTURE;
467 case RESERVED_DISP_PICTURE:
468 p_pic->i_status = READY_PICTURE;
472 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
478 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
480 vlc_mutex_unlock( &p_vout->picture_lock );
483 /******************************************************************************
484 * vout_CreatePicture: allocate a picture in the video output heap.
485 ******************************************************************************
486 * This function create a reserved image in the video output heap.
487 * A null pointer is returned if the function fails. This method provides an
488 * already allocated zone of memory in the picture data fields. It needs locking
489 * since several pictures can be created by several producers threads.
490 ******************************************************************************/
491 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
492 int i_width, int i_height )
494 int i_picture; /* picture index */
495 int i_chroma_width = 0; /* chroma width */
496 picture_t * p_free_picture = NULL; /* first free picture */
497 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
500 vlc_mutex_lock( &p_vout->picture_lock );
503 * Look for an empty place
505 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
507 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
509 /* Picture is marked for destruction, but is still allocated - note
510 * that if width and type are the same for two pictures, chroma_width
511 * should also be the same */
512 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
513 (p_vout->p_picture[i_picture].i_height == i_height) &&
514 (p_vout->p_picture[i_picture].i_width == i_width) )
516 /* Memory size do match : memory will not be reallocated, and function
517 * can end immediately - this is the best possible case, since no
518 * memory allocation needs to be done */
519 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
521 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
522 &p_vout->p_picture[i_picture] );
524 vlc_mutex_unlock( &p_vout->picture_lock );
525 return( &p_vout->p_picture[i_picture] );
527 else if( p_destroyed_picture == NULL )
529 /* Memory size do not match, but picture index will be kept in
530 * case no other place are left */
531 p_destroyed_picture = &p_vout->p_picture[i_picture];
534 else if( (p_free_picture == NULL) &&
535 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
537 /* Picture is empty and ready for allocation */
538 p_free_picture = &p_vout->p_picture[i_picture];
542 /* If no free picture is available, use a destroyed picture */
543 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
545 /* No free picture or matching destroyed picture has been found, but
546 * a destroyed picture is still avalaible */
547 free( p_destroyed_picture->p_data );
548 p_free_picture = p_destroyed_picture;
554 if( p_free_picture != NULL )
556 /* Allocate memory */
559 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
560 i_chroma_width = i_width / 2;
561 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
562 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
563 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
564 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
566 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
567 i_chroma_width = i_width / 2;
568 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
569 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
570 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
571 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
573 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
574 i_chroma_width = i_width;
575 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
576 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
577 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
578 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
582 intf_DbgMsg("error: unknown picture type %d\n", i_type );
583 p_free_picture->p_data = NULL;
588 if( p_free_picture->p_data != NULL )
590 /* Copy picture informations, set some default values */
591 p_free_picture->i_type = i_type;
592 p_free_picture->i_status = RESERVED_PICTURE;
593 p_free_picture->i_matrix_coefficients = 1;
594 p_free_picture->i_width = i_width;
595 p_free_picture->i_height = i_height;
596 p_free_picture->i_chroma_width = i_chroma_width;
597 p_free_picture->i_display_horizontal_offset = 0;
598 p_free_picture->i_display_vertical_offset = 0;
599 p_free_picture->i_display_width = i_width;
600 p_free_picture->i_display_height = i_height;
601 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
602 p_free_picture->i_refcount = 0;
606 /* Memory allocation failed : set picture as empty */
607 p_free_picture->i_type = EMPTY_PICTURE;
608 p_free_picture->i_status = FREE_PICTURE;
609 p_free_picture = NULL;
610 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
614 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
616 vlc_mutex_unlock( &p_vout->picture_lock );
617 return( p_free_picture );
620 /* No free or destroyed picture could be found */
621 intf_DbgMsg( "warning: heap is full\n" );
622 vlc_mutex_unlock( &p_vout->picture_lock );
626 /******************************************************************************
627 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
628 ******************************************************************************
629 * This function frees a previously reserved picture or a permanent
630 * picture. It is meant to be used when the construction of a picture aborted.
631 * Note that the picture will be destroyed even if it is linked !
632 * This function does not need locking since reserved pictures are ignored by
634 ******************************************************************************/
635 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
638 /* Check if picture status is valid */
639 if( (p_pic->i_status != RESERVED_PICTURE) &&
640 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
641 (p_pic->i_status != RESERVED_DISP_PICTURE) )
643 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
647 p_pic->i_status = DESTROYED_PICTURE;
650 intf_DbgMsg("picture %p\n", p_pic);
654 /******************************************************************************
655 * vout_LinkPicture: increment reference counter of a picture
656 ******************************************************************************
657 * This function increment the reference counter of a picture in the video
658 * heap. It needs a lock since several producer threads can access the picture.
659 ******************************************************************************/
660 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
662 vlc_mutex_lock( &p_vout->picture_lock );
666 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
669 vlc_mutex_unlock( &p_vout->picture_lock );
672 /******************************************************************************
673 * vout_UnlinkPicture: decrement reference counter of a picture
674 ******************************************************************************
675 * This function decrement the reference counter of a picture in the video heap.
676 ******************************************************************************/
677 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
679 vlc_mutex_lock( &p_vout->picture_lock );
683 if( p_pic->i_refcount < 0 )
685 intf_DbgMsg("error: refcount < 0\n");
686 p_pic->i_refcount = 0;
690 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
692 p_pic->i_status = DESTROYED_PICTURE;
696 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
699 vlc_mutex_unlock( &p_vout->picture_lock );
702 /******************************************************************************
703 * vout_SetBuffers: set buffers adresses
704 ******************************************************************************
705 * This function is called by system drivers to set buffers video memory
707 ******************************************************************************/
708 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
710 /* No picture previously */
711 p_vout->p_buffer[0].i_pic_x = 0;
712 p_vout->p_buffer[0].i_pic_y = 0;
713 p_vout->p_buffer[0].i_pic_width = 0;
714 p_vout->p_buffer[0].i_pic_height = 0;
715 p_vout->p_buffer[1].i_pic_x = 0;
716 p_vout->p_buffer[1].i_pic_y = 0;
717 p_vout->p_buffer[1].i_pic_width = 0;
718 p_vout->p_buffer[1].i_pic_height = 0;
720 /* The first area covers all the screen */
721 p_vout->p_buffer[0].i_areas = 1;
722 p_vout->p_buffer[0].pi_area_begin[0] = 0;
723 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
724 p_vout->p_buffer[1].i_areas = 1;
725 p_vout->p_buffer[1].pi_area_begin[0] = 0;
726 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
729 p_vout->p_buffer[0].p_data = p_buf1;
730 p_vout->p_buffer[1].p_data = p_buf2;
733 /*****************************************************************************
734 * vout_Pixel2RGB: return red, green and blue from pixel value
735 *****************************************************************************
736 * Return color values, in 0-255 range, of the decomposition of a pixel. This
737 * is a slow routine and should only be used for initialization phase.
738 *****************************************************************************/
739 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
741 *pi_red = i_pixel & p_vout->i_red_mask;
742 *pi_green = i_pixel & p_vout->i_green_mask;
743 *pi_blue = i_pixel & p_vout->i_blue_mask;
746 /* following functions are local */
748 /*****************************************************************************
749 * BinaryLog: computes the base 2 log of a binary value
750 *****************************************************************************
751 * This functions is used by MaskToShift, to get a bit index from a binary
753 *****************************************************************************/
754 static int BinaryLog(u32 i)
779 if (i != ((u32)1 << i_log))
781 intf_ErrMsg("internal error: binary log overflow\n");
787 /*****************************************************************************
788 * MaskToShift: transform a color mask into right and left shifts
789 *****************************************************************************
790 * This function is used for obtaining color shifts from masks.
791 *****************************************************************************/
792 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
794 u32 i_low, i_high; /* lower hand higher bits of the mask */
797 i_low = i_mask & (- i_mask); /* lower bit of the mask */
798 i_high = i_mask + i_low; /* higher bit of the mask */
800 /* Transform bits into an index */
801 i_low = BinaryLog (i_low);
802 i_high = BinaryLog (i_high);
804 /* Update pointers and return */
806 *pi_right = (8 - i_high + i_low);
809 /******************************************************************************
810 * InitThread: initialize video output thread
811 ******************************************************************************
812 * This function is called from RunThread and performs the second step of the
813 * initialization. It returns 0 on success. Note that the thread's flag are not
814 * modified inside this function.
815 ******************************************************************************/
816 static int InitThread( vout_thread_t *p_vout )
820 *p_vout->pi_status = THREAD_START;
822 /* Initialize output method - this function issues its own error messages */
823 if( vout_SysInit( p_vout ) )
828 /* Initialize convertion tables and functions */
829 if( vout_InitYUV( p_vout ) )
831 intf_ErrMsg("error: can't allocate YUV translation tables\n");
835 /* Mark thread as running and return */
836 p_vout->b_active = 1;
837 *p_vout->pi_status = THREAD_READY;
838 intf_DbgMsg("thread ready\n");
842 /******************************************************************************
843 * RunThread: video output thread
844 ******************************************************************************
845 * Video output thread. This function does only returns when the thread is
846 * terminated. It handles the pictures arriving in the video heap and the
847 * display device events.
848 ******************************************************************************/
849 static void RunThread( vout_thread_t *p_vout)
851 int i_index; /* index in heap */
852 mtime_t current_date; /* current date */
853 mtime_t display_date; /* display date */
854 boolean_t b_display; /* display flag */
855 picture_t * p_pic; /* picture pointer */
856 subpicture_t * p_subpic; /* subpicture pointer */
861 p_vout->b_error = InitThread( p_vout );
862 if( p_vout->b_error )
864 DestroyThread( p_vout, THREAD_ERROR );
870 * Main loop - it is not executed if an error occured during
873 while( (!p_vout->b_die) && (!p_vout->b_error) )
875 /* Initialize loop variables */
879 current_date = mdate();
882 * Find the picture to display - this operation does not need lock,
883 * since only READY_PICTUREs are handled
885 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
887 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
889 (p_vout->p_picture[i_index].date < display_date) ) )
891 p_pic = &p_vout->p_picture[i_index];
892 display_date = p_pic->date;
899 /* Computes FPS rate */
900 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
902 if( display_date < current_date )
904 /* Picture is late: it will be destroyed and the thread will sleep and
905 * go to next picture */
906 vlc_mutex_lock( &p_vout->picture_lock );
907 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
908 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
909 vlc_mutex_unlock( &p_vout->picture_lock );
913 else if( display_date > current_date + VOUT_DISPLAY_DELAY )
915 /* A picture is ready to be rendered, but its rendering date is
916 * far from the current one so the thread will perform an empty loop
917 * as if no picture were found. The picture state is unchanged */
924 * Find the subpicture to display - this operation does not need lock, since
925 * only READY_SUBPICTURES are handled. If no picture has been selected,
926 * display_date will depend on the subpicture
931 * Perform rendering, sleep and display rendered picture
933 if( p_pic ) /* picture and perhaps subpicture */
935 b_display = p_vout->b_active;
936 p_vout->last_display_date = display_date;
940 /* Set picture dimensions and clear buffer */
941 SetBufferPicture( p_vout, p_pic );
943 /* Render picture and informations */
944 RenderPicture( p_vout, p_pic );
947 RenderPictureInfo( p_vout, p_pic );
948 RenderInfo( p_vout );
952 /* Remove picture from heap */
953 vlc_mutex_lock( &p_vout->picture_lock );
954 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
955 vlc_mutex_unlock( &p_vout->picture_lock );
957 /* Render interface and subpicture */
958 if( b_display && p_vout->b_interface )
960 RenderInterface( p_vout );
966 RenderSubPicture( p_vout, p_subpic );
969 /* Remove subpicture from heap */
970 vlc_mutex_lock( &p_vout->subpicture_lock );
971 p_subpic->i_status = DESTROYED_SUBPICTURE;
972 vlc_mutex_unlock( &p_vout->subpicture_lock );
976 else if( p_subpic ) /* subpicture alone */
978 b_display = p_vout->b_active;
979 p_vout->last_display_date = display_date;
984 SetBufferPicture( p_vout, NULL );
986 /* Render informations, interface and subpicture */
989 RenderInfo( p_vout );
991 if( p_vout->b_interface )
993 RenderInterface( p_vout );
995 RenderSubPicture( p_vout, p_subpic );
998 /* Remove subpicture from heap */
999 vlc_mutex_lock( &p_vout->subpicture_lock );
1000 p_subpic->i_status = DESTROYED_SUBPICTURE;
1001 vlc_mutex_unlock( &p_vout->subpicture_lock );
1003 else if( p_vout->b_active ) /* idle or interface screen alone */
1005 if( p_vout->b_interface && 0 /* && ?? intf_change */ )
1007 /* Interface has changed, so a new rendering is required - force
1008 * it by setting last idle date to 0 */
1009 p_vout->last_idle_date = 0;
1012 /* Render idle screen and update idle date, then render interface if
1014 b_display = RenderIdle( p_vout );
1017 p_vout->last_idle_date = current_date;
1018 if( p_vout->b_interface )
1020 RenderInterface( p_vout );
1031 * Sleep, wake up and display rendered picture
1035 /* Store render time */
1036 p_vout->render_time = mdate() - current_date;
1039 /* Give back change lock */
1040 vlc_mutex_unlock( &p_vout->change_lock );
1042 /* Sleep a while or until a given date */
1043 if( display_date != 0 )
1045 mwait( display_date );
1049 msleep( VOUT_IDLE_SLEEP );
1052 /* On awakening, take back lock and send immediately picture to display,
1053 * then swap buffers */
1054 vlc_mutex_lock( &p_vout->change_lock );
1056 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1057 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1059 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1061 vout_SysDisplay( p_vout );
1062 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1066 * Check events and manage thread
1068 if( vout_SysManage( p_vout ) | Manage( p_vout ) )
1070 /* A fatal error occured, and the thread must terminate immediately,
1071 * without displaying anything - setting b_error to 1 cause the
1072 * immediate end of the main while() loop. */
1073 p_vout->b_error = 1;
1078 * Error loop - wait until the thread destruction is requested
1080 if( p_vout->b_error )
1082 ErrorThread( p_vout );
1086 EndThread( p_vout );
1087 DestroyThread( p_vout, THREAD_OVER );
1088 intf_DbgMsg( "thread end\n" );
1091 /******************************************************************************
1092 * ErrorThread: RunThread() error loop
1093 ******************************************************************************
1094 * This function is called when an error occured during thread main's loop. The
1095 * thread can still receive feed, but must be ready to terminate as soon as
1097 ******************************************************************************/
1098 static void ErrorThread( vout_thread_t *p_vout )
1100 /* Wait until a `die' order */
1102 while( !p_vout->b_die )
1105 msleep( VOUT_IDLE_SLEEP );
1109 /*******************************************************************************
1110 * EndThread: thread destruction
1111 *******************************************************************************
1112 * This function is called when the thread ends after a sucessfull
1113 * initialization. It frees all ressources allocated by InitThread.
1114 *******************************************************************************/
1115 static void EndThread( vout_thread_t *p_vout )
1117 int i_index; /* index in heap */
1121 *p_vout->pi_status = THREAD_END;
1123 /* Destroy all remaining pictures and subpictures */
1124 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1126 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1128 free( p_vout->p_picture[i_index].p_data );
1130 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1132 free( p_vout->p_subpicture[i_index].p_data );
1136 /* Destroy translation tables */
1137 vout_EndYUV( p_vout );
1138 vout_SysEnd( p_vout );
1141 /*******************************************************************************
1142 * DestroyThread: thread destruction
1143 *******************************************************************************
1144 * This function is called when the thread ends. It frees all ressources
1145 * allocated by CreateThread. Status is available at this stage.
1146 *******************************************************************************/
1147 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1149 int *pi_status; /* status adress */
1151 /* Store status adress */
1153 pi_status = p_vout->pi_status;
1155 /* Destroy thread structures allocated by Create and InitThread */
1156 vout_UnloadFont( p_vout->p_default_font );
1157 vout_UnloadFont( p_vout->p_large_font );
1158 vout_SysDestroy( p_vout );
1160 *pi_status = i_status;
1163 /*******************************************************************************
1164 * Print: print simple text on a picture
1165 *******************************************************************************
1166 * This function will print a simple text on the picture. It is designed to
1167 * print debugging or general informations.
1168 *******************************************************************************/
1169 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 )
1171 int i_text_height; /* total text height */
1172 int i_text_width; /* total text width */
1174 /* Update upper left coordinates according to alignment */
1175 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1176 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1178 /* Set area and print text */
1179 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1180 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1181 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1182 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1183 p_vout->i_white_pixel, 0, 0,
1188 /*******************************************************************************
1189 * SetBufferArea: activate an area in current buffer
1190 *******************************************************************************
1191 * This function is called when something is rendered on the current buffer.
1192 * It set the area as active and prepare it to be cleared on next rendering.
1193 * Pay attention to the fact that in this functions, i_h is in fact the end y
1194 * coordinate of the new area.
1195 *******************************************************************************/
1196 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1198 vout_buffer_t * p_buffer; /* current buffer */
1199 int i_area_begin, i_area_end; /* area vertical extension */
1200 int i_area, i_area_copy; /* area index */
1201 int i_area_shift; /* shift distance for areas */
1203 /* Choose buffer and modify h to end of area position */
1204 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1208 * Remove part of the area which is inside the picture - this is done
1209 * by calling again SetBufferArea with the correct areas dimensions.
1211 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1213 i_area_begin = p_buffer->i_pic_y;
1214 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1216 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1217 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1218 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1220 /* Keep the stripe above the picture, if any */
1221 if( i_y < i_area_begin )
1223 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1225 /* Keep the stripe below the picture, if any */
1226 if( i_h > i_area_end )
1228 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1234 /* Skip some extensions until interesting areas */
1236 (i_area < p_buffer->i_areas) &&
1237 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1243 if( i_area == p_buffer->i_areas )
1245 /* New area is below all existing ones: just add it at the end of the
1246 * array, if possible - else, append it to the last one */
1247 if( i_area < VOUT_MAX_AREAS )
1249 p_buffer->pi_area_begin[i_area] = i_y;
1250 p_buffer->pi_area_end[i_area] = i_h;
1251 p_buffer->i_areas++;
1256 intf_DbgMsg("areas overflow\n");
1258 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1263 i_area_begin = p_buffer->pi_area_begin[i_area];
1264 i_area_end = p_buffer->pi_area_end[i_area];
1266 if( i_y < i_area_begin )
1268 if( i_h >= i_area_begin - 1 )
1270 /* Extend area above */
1271 p_buffer->pi_area_begin[i_area] = i_y;
1275 /* Create a new area above : merge last area if overflow, then
1276 * move all old areas down */
1277 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1280 intf_DbgMsg("areas overflow\n");
1282 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1286 p_buffer->i_areas++;
1288 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1290 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1291 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1293 p_buffer->pi_area_begin[i_area] = i_y;
1294 p_buffer->pi_area_end[i_area] = i_h;
1298 if( i_h > i_area_end )
1300 /* Find further areas which can be merged with the new one */
1301 for( i_area_copy = i_area + 1;
1302 (i_area_copy < p_buffer->i_areas) &&
1303 (p_buffer->pi_area_begin[i_area] <= i_h);
1310 if( i_area_copy != i_area )
1312 /* Merge with last possible areas */
1313 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1315 /* Shift lower areas upward */
1316 i_area_shift = i_area_copy - i_area;
1317 p_buffer->i_areas -= i_area_shift;
1318 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1320 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1321 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1326 /* Extend area below */
1327 p_buffer->pi_area_end[i_area] = i_h;
1333 /*******************************************************************************
1334 * SetBufferPicture: clear buffer and set picture area
1335 *******************************************************************************
1336 * This function is called before any rendering. It clears the current
1337 * rendering buffer and set the new picture area. If the picture pointer is
1338 * NULL, then no picture area is defined. Floating operations are avoided since
1339 * some MMX calculations may follow.
1340 *******************************************************************************/
1341 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1343 vout_buffer_t * p_buffer; /* current buffer */
1344 int i_pic_x, i_pic_y; /* picture position */
1345 int i_pic_width, i_pic_height; /* picture dimensions */
1346 int i_old_pic_y, i_old_pic_height; /* old picture area */
1347 int i_vout_width, i_vout_height; /* display dimensions */
1348 int i_area; /* area index */
1349 int i_data_index; /* area data index */
1350 int i_data_size; /* area data size, in 256 bytes blocs */
1351 u64 * p_data; /* area data, for clearing */
1352 byte_t * p_data8; /* area data, for clearing (slow) */
1354 /* Choose buffer and set display dimensions */
1355 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1356 i_vout_width = p_vout->i_width;
1357 i_vout_height = p_vout->i_height;
1360 * Computes new picture size
1364 /* Try horizontal scaling first - width must be a mutiple of 16 */
1365 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1366 i_vout_width : p_pic->i_width) & ~0xf;
1367 switch( p_pic->i_aspect_ratio )
1369 case AR_3_4_PICTURE:
1370 i_pic_height = i_pic_width * 3 / 4;
1372 case AR_16_9_PICTURE:
1373 i_pic_height = i_pic_width * 9 / 16;
1375 case AR_221_1_PICTURE:
1376 i_pic_height = i_pic_width * 100 / 221;
1378 case AR_SQUARE_PICTURE:
1380 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1384 /* If picture dimensions using horizontal scaling are too large, use
1385 * vertical scaling. Since width must be a multiple of 16, height is
1386 * adjusted again after. */
1387 if( i_pic_height > i_vout_height )
1389 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1390 i_vout_height : p_pic->i_height;
1391 switch( p_pic->i_aspect_ratio )
1393 case AR_3_4_PICTURE:
1394 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1395 i_pic_height = i_pic_width * 3 / 4;
1397 case AR_16_9_PICTURE:
1398 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1399 i_pic_height = i_pic_width * 9 / 16;
1401 case AR_221_1_PICTURE:
1402 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1403 i_pic_height = i_pic_width * 100 / 221;
1405 case AR_SQUARE_PICTURE:
1407 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1408 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1413 /* Set picture position */
1414 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1415 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1419 /* No picture: size is 0 */
1427 * Set new picture size - if is is smaller than the previous one, clear
1428 * around it. Since picture are centered, only their size is tested.
1430 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1432 i_old_pic_y = p_buffer->i_pic_y;
1433 i_old_pic_height = p_buffer->i_pic_height;
1434 p_buffer->i_pic_x = i_pic_x;
1435 p_buffer->i_pic_y = i_pic_y;
1436 p_buffer->i_pic_width = i_pic_width;
1437 p_buffer->i_pic_height = i_pic_height;
1438 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1442 p_buffer->i_pic_x = i_pic_x;
1443 p_buffer->i_pic_y = i_pic_y;
1444 p_buffer->i_pic_width = i_pic_width;
1445 p_buffer->i_pic_height = i_pic_height;
1451 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1454 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1455 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1457 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1458 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1459 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1461 /* Clear 256 bytes block */
1462 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1463 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1464 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1465 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1466 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
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;
1471 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1473 /* Clear remaining 16 bytes blocks */
1474 *p_data++ = 0; *p_data++ = 0;
1476 p_data8 = (byte_t *)p_data;
1477 for( i_data_index = i_data_size % 16; i_data_index--; )
1479 /* Clear remaining bytes */
1487 p_buffer->i_areas = 0;
1490 /******************************************************************************
1491 * RenderPicture: render a picture
1492 ******************************************************************************
1493 * This function convert a picture from a video heap to a pixel-encoded image
1494 * and copy it to the current rendering buffer. No lock is required, since the
1495 * rendered picture has been determined as existant, and will only be destroyed
1496 * by the vout thread later.
1497 ******************************************************************************/
1498 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1501 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1502 mtime_t render_time; /* picture rendering time */
1504 vout_buffer_t * p_buffer; /* rendering buffer */
1505 byte_t * p_pic_data; /* convertion destination */
1507 /* Get and set rendering informations */
1508 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1509 p_pic_data = p_buffer->p_data +
1510 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1511 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1513 render_time = mdate();
1517 * Choose appropriate rendering function and render picture
1519 switch( p_pic->i_type )
1521 case YUV_420_PICTURE:
1522 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1523 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1524 p_pic->i_width, p_pic->i_height,
1525 p_buffer->i_pic_width, p_buffer->i_pic_height,
1526 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1527 p_pic->i_matrix_coefficients );
1529 case YUV_422_PICTURE:
1530 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1531 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1532 p_pic->i_width, p_pic->i_height,
1533 p_buffer->i_pic_width, p_buffer->i_pic_height,
1534 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1535 p_pic->i_matrix_coefficients );
1537 case YUV_444_PICTURE:
1538 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1539 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1540 p_pic->i_width, p_pic->i_height,
1541 p_buffer->i_pic_width, p_buffer->i_pic_height,
1542 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1543 p_pic->i_matrix_coefficients );
1547 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1553 /* Print picture date and rendering time */
1554 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1555 p_vout->i_buffer_index, (long) (mdate() - render_time),
1556 mstrtime( psz_date, p_pic->date ));
1560 /******************************************************************************
1561 * RenderPictureInfo: print additionnal informations on a picture
1562 ******************************************************************************
1563 * This function will print informations such as fps and other picture
1564 * dependant informations.
1565 ******************************************************************************/
1566 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1568 #if defined(STATS) || defined(DEBUG)
1569 char psz_buffer[256]; /* string buffer */
1574 * Print FPS rate in upper right corner
1576 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1578 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1579 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1580 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1581 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1585 * Print frames count and loop time in upper left corner
1587 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1588 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1589 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1594 * Print picture information in lower right corner
1596 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1597 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1598 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1599 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1600 p_pic->i_width, p_pic->i_height,
1601 p_pic->i_display_width, p_pic->i_display_height,
1602 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1603 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1604 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1605 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1606 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1607 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1608 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1609 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1610 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1611 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1615 /******************************************************************************
1616 * RenderIdle: render idle picture
1617 ******************************************************************************
1618 * This function will print something on the screen. It will return 0 if
1619 * nothing has been rendered, or 1 if something has been changed on the screen.
1620 * Note that if you absolutely want something to be printed, you will have
1621 * to force it by setting the last idle date to 0.
1622 * Unlike other rendering functions, this one calls the SetBufferPicture
1623 * function when needed.
1624 ******************************************************************************/
1625 static int RenderIdle( vout_thread_t *p_vout )
1627 int i_x = 0, i_y = 0; /* text position */
1628 int i_width, i_height; /* text size */
1629 mtime_t current_date; /* current date */
1630 const char *psz_text = "no stream"; /* text to display */
1633 current_date = mdate();
1634 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1635 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1637 SetBufferPicture( p_vout, NULL );
1638 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1639 &i_width, &i_height );
1640 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1642 vout_Print( p_vout->p_large_font,
1643 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1644 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1645 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1646 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1647 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1648 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1655 /******************************************************************************
1656 * RenderInfo: render additionnal informations
1657 ******************************************************************************
1658 * This function render informations which do not depend of the current picture
1660 ******************************************************************************/
1661 static void RenderInfo( vout_thread_t *p_vout )
1664 char psz_buffer[256]; /* string buffer */
1665 int i_ready_pic = 0; /* ready pictures */
1666 int i_reserved_pic = 0; /* reserved pictures */
1667 int i_picture; /* picture index */
1672 * Print thread state in lower left corner
1674 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1676 switch( p_vout->p_picture[i_picture].i_status )
1678 case RESERVED_PICTURE:
1679 case RESERVED_DATED_PICTURE:
1680 case RESERVED_DISP_PICTURE:
1688 sprintf( psz_buffer, "pic: %d/%d/%d",
1689 i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1690 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1694 /*******************************************************************************
1695 * RenderSubPicture: render a subpicture
1696 *******************************************************************************
1697 * This function render a sub picture unit.
1698 *******************************************************************************/
1699 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1701 p_vout_font_t p_font; /* text font */
1702 int i_width, i_height; /* subpicture dimensions */
1704 switch( p_subpic->i_type )
1706 case TEXT_SUBPICTURE: /* single line text */
1707 /* Select default font if not specified */
1708 p_font = p_subpic->type.text.p_font;
1709 if( p_font == NULL )
1711 p_font = p_vout->p_default_font;
1714 /* Computes text size (width and height fields are ignored) and print it */
1715 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1716 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1717 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1719 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1720 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1721 p_subpic->i_y * p_vout->i_bytes_per_line,
1722 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1723 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1724 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1726 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1732 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1737 /*******************************************************************************
1738 * RenderInterface: render the interface
1739 *******************************************************************************
1740 * This function render the interface, if any.
1741 *******************************************************************************/
1742 static void RenderInterface( vout_thread_t *p_vout )
1744 int i_height, i_text_height; /* total and text height */
1745 int i_width_1, i_width_2; /* text width */
1746 int i_byte; /* byte index */
1747 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1748 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1751 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1752 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1753 i_height += i_text_height;
1755 /* Render background */
1756 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1757 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1761 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1764 /* Render text, if not larger than screen */
1765 if( i_width_1 < p_vout->i_width )
1767 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1768 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1769 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1770 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1771 OUTLINED_TEXT, psz_text_1 );
1773 if( i_width_2 < p_vout->i_width )
1775 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1776 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1777 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1778 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1779 OUTLINED_TEXT, psz_text_2 );
1782 /* Activate modified area */
1783 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1786 /******************************************************************************
1787 * Manage: manage thread
1788 ******************************************************************************
1789 * This function will handle changes in thread configuration.
1790 ******************************************************************************/
1791 static int Manage( vout_thread_t *p_vout )
1794 if( p_vout->i_changes )
1796 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1797 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1801 /* On gamma or grayscale change, rebuild tables */
1802 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1805 if( vout_ResetYUV( p_vout ) )
1807 intf_ErrMsg("error: can't rebuild convertion tables\n");
1812 /* Clear changes flags which does not need management or have been
1814 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1815 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
1816 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
1818 /* Detect unauthorized changes */
1819 if( p_vout->i_changes )
1821 /* Some changes were not acknowledged by vout_SysManage or this function,
1822 * it means they should not be authorized */
1823 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
1830 /******************************************************************************
1831 * Align: align a subpicture in the screen
1832 ******************************************************************************
1833 * This function is used for rendering text or subpictures. It returns non 0
1834 * it the final aera is not fully included in display area. Return coordinates
1836 ******************************************************************************/
1837 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
1838 int i_width, int i_height, int i_h_align, int i_v_align )
1840 /* Align horizontally */
1844 *pi_x -= i_width / 2;
1847 *pi_x += (p_vout->i_width - i_width) / 2;
1853 *pi_x += p_vout->i_width - i_width;
1857 /* Align vertically */
1861 *pi_y -= i_height / 2;
1864 *pi_y += (p_vout->i_height - i_height) / 2;
1870 *pi_y += p_vout->i_height - i_height;
1872 case SUBTITLE_RALIGN:
1873 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
1874 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
1878 /* Return non 0 if clipping failed */
1879 return( (*pi_x < 0) || (*pi_y < 0) ||
1880 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );