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 InitThread ( vout_thread_t *p_vout );
34 static void RunThread ( vout_thread_t *p_vout );
35 static void ErrorThread ( vout_thread_t *p_vout );
36 static void EndThread ( vout_thread_t *p_vout );
37 static void DestroyThread ( vout_thread_t *p_vout, int i_status );
38 static void Print ( vout_thread_t *p_vout, int i_x, int i_y,
39 int i_h_align, int i_v_align, unsigned char *psz_text );
40 static void SetBufferArea ( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h );
41 static void SetBufferPicture ( vout_thread_t *p_vout, picture_t *p_pic );
42 static void RenderPicture ( vout_thread_t *p_vout, picture_t *p_pic );
43 static void RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
44 static void RenderSubPicture ( vout_thread_t *p_vout, subpicture_t *p_subpic );
45 static void RenderInterface ( vout_thread_t *p_vout );
46 static int RenderIdle ( vout_thread_t *p_vout );
47 static void RenderInfo ( vout_thread_t *p_vout );
48 static int Manage ( vout_thread_t *p_vout );
49 static int Align ( vout_thread_t *p_vout, int *pi_x, int *pi_y,
50 int i_width, int i_height, int i_h_align, int i_v_align );
52 /******************************************************************************
53 * vout_CreateThread: creates a new video output thread
54 ******************************************************************************
55 * This function creates a new video output thread, and returns a pointer
56 * to its description. On error, it returns NULL.
57 * If pi_status is NULL, then the function will block until the thread is ready.
58 * If not, it will be updated using one of the THREAD_* constants.
59 ******************************************************************************/
60 vout_thread_t * vout_CreateThread ( char *psz_display, int i_root_window,
61 int i_width, int i_height, int *pi_status )
63 vout_thread_t * p_vout; /* thread descriptor */
64 int i_status; /* thread status */
65 int i_index; /* index for array initialization */
67 /* Allocate descriptor */
69 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
72 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
76 /* Initialize thread properties - thread id and locks will be initialized
81 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
82 *p_vout->pi_status = THREAD_CREATE;
84 /* Initialize some fields used by the system-dependant method - these fields will
85 * probably be modified by the method, and are only preferences */
86 p_vout->i_changes = 0;
87 p_vout->i_width = i_width;
88 p_vout->i_height = i_height;
89 p_vout->i_bytes_per_line = i_width * 2;
90 p_vout->i_screen_depth = 15;
91 p_vout->i_bytes_per_pixel = 2;
92 p_vout->f_gamma = VOUT_GAMMA;
94 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
95 VOUT_GRAYSCALE_DEFAULT );
97 p_vout->b_interface = 0;
100 intf_DbgMsg("wished configuration: %dx%d,%d (%d bytes/pixel, %d bytes/line)\n",
101 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
102 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line );
104 /* Initialize idle screen */
105 p_vout->last_display_date = mdate();
106 p_vout->last_idle_date = 0;
109 /* Initialize statistics fields */
110 p_vout->render_time = 0;
111 p_vout->c_fps_samples = 0;
114 /* Initialize buffer index */
115 p_vout->i_buffer_index = 0;
117 /* Initialize pictures and subpictures - translation tables and functions
118 * will be initialized later in InitThread */
119 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
121 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
122 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
123 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
124 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
127 /* Create and initialize system-dependant method - this function issues its
128 * own error messages */
129 if( vout_SysCreate( p_vout, psz_display, i_root_window ) )
134 intf_DbgMsg("actual configuration: %dx%d,%d (%d bytes/pixel, %d bytes/line)\n",
135 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
136 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line );
138 /* Load fonts - fonts must be initialized after the systme method since
139 * they may be dependant of screen depth and other thread properties */
140 p_vout->p_default_font = vout_LoadFont( VOUT_DEFAULT_FONT );
141 if( p_vout->p_default_font == NULL )
143 vout_SysDestroy( p_vout );
147 p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
148 if( p_vout->p_large_font == NULL )
150 vout_UnloadFont( p_vout->p_default_font );
151 vout_SysDestroy( p_vout );
156 /* Create thread and set locks */
157 vlc_mutex_init( &p_vout->picture_lock );
158 vlc_mutex_init( &p_vout->subpicture_lock );
159 vlc_mutex_init( &p_vout->change_lock );
160 vlc_mutex_lock( &p_vout->change_lock );
161 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
163 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
164 vout_UnloadFont( p_vout->p_default_font );
165 vout_UnloadFont( p_vout->p_large_font );
166 vout_SysDestroy( p_vout );
171 intf_Msg("Video display initialized (%dx%d, %d bpp)\n",
172 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth );
174 /* If status is NULL, wait until the thread is created */
175 if( pi_status == NULL )
179 msleep( THREAD_SLEEP );
180 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
181 && (i_status != THREAD_FATAL) );
182 if( i_status != THREAD_READY )
190 /******************************************************************************
191 * vout_DestroyThread: destroys a previously created thread
192 ******************************************************************************
193 * Destroy a terminated thread.
194 * The function will request a destruction of the specified thread. If pi_error
195 * is NULL, it will return once the thread is destroyed. Else, it will be
196 * update using one of the THREAD_* constants.
197 ******************************************************************************/
198 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
200 int i_status; /* thread status */
204 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
205 *p_vout->pi_status = THREAD_DESTROY;
207 /* Request thread destruction */
210 /* If status is NULL, wait until thread has been destroyed */
211 if( pi_status == NULL )
215 msleep( THREAD_SLEEP );
216 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
217 && (i_status != THREAD_FATAL) );
221 /******************************************************************************
222 * vout_DisplaySubPicture: display a subpicture unit
223 ******************************************************************************
224 * Remove the reservation flag of an subpicture, which will cause it to be ready
225 * for display. The picture does not need to be locked, since it is ignored by
226 * the output thread if is reserved.
227 ******************************************************************************/
228 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
231 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
232 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
236 /* Check if status is valid */
237 if( p_subpic->i_status != RESERVED_SUBPICTURE )
239 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
240 p_subpic->i_status );
244 /* Remove reservation flag */
245 p_subpic->i_status = READY_SUBPICTURE;
248 /* Send subpicture informations */
249 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
250 p_subpic, p_subpic->i_type,
251 mstrtime( psz_begin_date, p_subpic->begin_date ),
252 mstrtime( psz_end_date, p_subpic->end_date ) );
256 /******************************************************************************
257 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
258 ******************************************************************************
259 * This function create a reserved subpicture in the video output heap.
260 * A null pointer is returned if the function fails. This method provides an
261 * already allocated zone of memory in the spu data fields. It needs locking
262 * since several pictures can be created by several producers threads.
263 ******************************************************************************/
264 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
267 int i_subpic; /* subpicture index */
268 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
269 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
272 vlc_mutex_lock( &p_vout->subpicture_lock );
275 * Look for an empty place
277 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
279 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
281 /* Subpicture is marked for destruction, but is still allocated */
282 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
283 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
285 /* Memory size do match or is smaller : memory will not be reallocated,
286 * and function can end immediately - this is the best possible case,
287 * since no memory allocation needs to be done */
288 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
290 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
291 &p_vout->p_subpicture[i_subpic] );
293 vlc_mutex_unlock( &p_vout->subpicture_lock );
294 return( &p_vout->p_subpicture[i_subpic] );
296 else if( p_destroyed_subpic == NULL )
298 /* Memory size do not match, but subpicture index will be kept in
299 * case no other place are left */
300 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
303 else if( (p_free_subpic == NULL) &&
304 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
306 /* Subpicture is empty and ready for allocation */
307 p_free_subpic = &p_vout->p_subpicture[i_subpic];
311 /* If no free subpicture is available, use a destroyed subpicture */
312 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
314 /* No free subpicture or matching destroyed subpicture has been found, but
315 * a destroyed subpicture is still avalaible */
316 free( p_destroyed_subpic->p_data );
317 p_free_subpic = p_destroyed_subpic;
323 if( p_free_subpic != NULL )
325 /* Allocate memory */
328 case TEXT_SUBPICTURE: /* text subpicture */
329 p_free_subpic->p_data = malloc( i_size + 1 );
333 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
334 p_free_subpic->p_data = NULL;
339 if( p_free_subpic->p_data != NULL )
340 { /* Copy subpicture informations, set some default values */
341 p_free_subpic->i_type = i_type;
342 p_free_subpic->i_status = RESERVED_SUBPICTURE;
343 p_free_subpic->i_size = i_size;
344 p_free_subpic->i_x = 0;
345 p_free_subpic->i_y = 0;
346 p_free_subpic->i_width = 0;
347 p_free_subpic->i_height = 0;
348 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
349 p_free_subpic->i_vertical_align = CENTER_RALIGN;
353 /* Memory allocation failed : set subpicture as empty */
354 p_free_subpic->i_type = EMPTY_SUBPICTURE;
355 p_free_subpic->i_status = FREE_SUBPICTURE;
356 p_free_subpic = NULL;
357 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
361 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
363 vlc_mutex_unlock( &p_vout->subpicture_lock );
364 return( p_free_subpic );
367 /* No free or destroyed subpicture could be found */
368 intf_DbgMsg( "warning: heap is full\n" );
369 vlc_mutex_unlock( &p_vout->subpicture_lock );
373 /******************************************************************************
374 * vout_DestroySubPicture: remove a subpicture from the heap
375 ******************************************************************************
376 * This function frees a previously reserved subpicture.
377 * It is meant to be used when the construction of a picture aborted.
378 * This function does not need locking since reserved subpictures are ignored
379 * by the output thread.
380 ******************************************************************************/
381 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
384 /* Check if status is valid */
385 if( p_subpic->i_status != RESERVED_SUBPICTURE )
387 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
388 p_subpic, p_subpic->i_status );
392 p_subpic->i_status = DESTROYED_SUBPICTURE;
395 intf_DbgMsg("subpicture %p\n", p_subpic);
399 /*******************************************************************************
400 * vout_DisplayPicture: display a picture
401 *******************************************************************************
402 * Remove the reservation flag of a picture, which will cause it to be ready for
403 * display. The picture won't be displayed until vout_DatePicture has been
405 *******************************************************************************/
406 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
408 vlc_mutex_lock( &p_vout->picture_lock );
409 switch( p_pic->i_status )
411 case RESERVED_PICTURE:
412 p_pic->i_status = RESERVED_DISP_PICTURE;
414 case RESERVED_DATED_PICTURE:
415 p_pic->i_status = READY_PICTURE;
419 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
425 intf_DbgMsg("picture %p\n", p_pic);
427 vlc_mutex_unlock( &p_vout->picture_lock );
430 /*******************************************************************************
431 * vout_DatePicture: date a picture
432 *******************************************************************************
433 * Remove the reservation flag of a picture, which will cause it to be ready for
434 * display. The picture won't be displayed until vout_DisplayPicture has been
436 *******************************************************************************/
437 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
440 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
443 vlc_mutex_lock( &p_vout->picture_lock );
445 switch( p_pic->i_status )
447 case RESERVED_PICTURE:
448 p_pic->i_status = RESERVED_DATED_PICTURE;
450 case RESERVED_DISP_PICTURE:
451 p_pic->i_status = READY_PICTURE;
455 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
461 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
463 vlc_mutex_unlock( &p_vout->picture_lock );
466 /******************************************************************************
467 * vout_CreatePicture: allocate a picture in the video output heap.
468 ******************************************************************************
469 * This function create a reserved image in the video output heap.
470 * A null pointer is returned if the function fails. This method provides an
471 * already allocated zone of memory in the picture data fields. It needs locking
472 * since several pictures can be created by several producers threads.
473 ******************************************************************************/
474 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
475 int i_width, int i_height )
477 int i_picture; /* picture index */
478 int i_chroma_width = 0; /* chroma width */
479 picture_t * p_free_picture = NULL; /* first free picture */
480 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
483 vlc_mutex_lock( &p_vout->picture_lock );
486 * Look for an empty place
488 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
490 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
492 /* Picture is marked for destruction, but is still allocated - note
493 * that if width and type are the same for two pictures, chroma_width
494 * should also be the same */
495 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
496 (p_vout->p_picture[i_picture].i_height == i_height) &&
497 (p_vout->p_picture[i_picture].i_width == i_width) )
499 /* Memory size do match : memory will not be reallocated, and function
500 * can end immediately - this is the best possible case, since no
501 * memory allocation needs to be done */
502 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
504 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
505 &p_vout->p_picture[i_picture] );
507 vlc_mutex_unlock( &p_vout->picture_lock );
508 return( &p_vout->p_picture[i_picture] );
510 else if( p_destroyed_picture == NULL )
512 /* Memory size do not match, but picture index will be kept in
513 * case no other place are left */
514 p_destroyed_picture = &p_vout->p_picture[i_picture];
517 else if( (p_free_picture == NULL) &&
518 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
520 /* Picture is empty and ready for allocation */
521 p_free_picture = &p_vout->p_picture[i_picture];
525 /* If no free picture is available, use a destroyed picture */
526 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
528 /* No free picture or matching destroyed picture has been found, but
529 * a destroyed picture is still avalaible */
530 free( p_destroyed_picture->p_data );
531 p_free_picture = p_destroyed_picture;
537 if( p_free_picture != NULL )
539 /* Allocate memory */
542 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
543 i_chroma_width = i_width / 2;
544 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
545 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
546 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
547 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
549 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
550 i_chroma_width = i_width / 2;
551 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
552 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
553 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
554 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
556 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
557 i_chroma_width = i_width;
558 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
559 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
560 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
561 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
565 intf_DbgMsg("error: unknown picture type %d\n", i_type );
566 p_free_picture->p_data = NULL;
571 if( p_free_picture->p_data != NULL )
573 /* Copy picture informations, set some default values */
574 p_free_picture->i_type = i_type;
575 p_free_picture->i_status = RESERVED_PICTURE;
576 p_free_picture->i_matrix_coefficients = 1;
577 p_free_picture->i_width = i_width;
578 p_free_picture->i_height = i_height;
579 p_free_picture->i_chroma_width = i_chroma_width;
580 p_free_picture->i_display_horizontal_offset = 0;
581 p_free_picture->i_display_vertical_offset = 0;
582 p_free_picture->i_display_width = i_width;
583 p_free_picture->i_display_height = i_height;
584 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
585 p_free_picture->i_refcount = 0;
589 /* Memory allocation failed : set picture as empty */
590 p_free_picture->i_type = EMPTY_PICTURE;
591 p_free_picture->i_status = FREE_PICTURE;
592 p_free_picture = NULL;
593 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
597 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
599 vlc_mutex_unlock( &p_vout->picture_lock );
600 return( p_free_picture );
603 /* No free or destroyed picture could be found */
604 intf_DbgMsg( "warning: heap is full\n" );
605 vlc_mutex_unlock( &p_vout->picture_lock );
609 /******************************************************************************
610 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
611 ******************************************************************************
612 * This function frees a previously reserved picture or a permanent
613 * picture. It is meant to be used when the construction of a picture aborted.
614 * Note that the picture will be destroyed even if it is linked !
615 * This function does not need locking since reserved pictures are ignored by
617 ******************************************************************************/
618 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
621 /* Check if picture status is valid */
622 if( (p_pic->i_status != RESERVED_PICTURE) &&
623 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
624 (p_pic->i_status != RESERVED_DISP_PICTURE) )
626 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
630 p_pic->i_status = DESTROYED_PICTURE;
633 intf_DbgMsg("picture %p\n", p_pic);
637 /******************************************************************************
638 * vout_LinkPicture: increment reference counter of a picture
639 ******************************************************************************
640 * This function increment the reference counter of a picture in the video
641 * heap. It needs a lock since several producer threads can access the picture.
642 ******************************************************************************/
643 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
645 vlc_mutex_lock( &p_vout->picture_lock );
649 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
652 vlc_mutex_unlock( &p_vout->picture_lock );
655 /******************************************************************************
656 * vout_UnlinkPicture: decrement reference counter of a picture
657 ******************************************************************************
658 * This function decrement the reference counter of a picture in the video heap.
659 ******************************************************************************/
660 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
662 vlc_mutex_lock( &p_vout->picture_lock );
666 if( p_pic->i_refcount < 0 )
668 intf_DbgMsg("error: refcount < 0\n");
669 p_pic->i_refcount = 0;
673 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
675 p_pic->i_status = DESTROYED_PICTURE;
679 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
682 vlc_mutex_unlock( &p_vout->picture_lock );
685 /******************************************************************************
686 * vout_SetBuffers: set buffers adresses
687 ******************************************************************************
688 * This function is called by system drivers to set buffers video memory
690 ******************************************************************************/
691 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
693 /* No picture previously */
694 p_vout->p_buffer[0].i_pic_x = 0;
695 p_vout->p_buffer[0].i_pic_y = 0;
696 p_vout->p_buffer[0].i_pic_width = 0;
697 p_vout->p_buffer[0].i_pic_height = 0;
698 p_vout->p_buffer[1].i_pic_x = 0;
699 p_vout->p_buffer[1].i_pic_y = 0;
700 p_vout->p_buffer[1].i_pic_width = 0;
701 p_vout->p_buffer[1].i_pic_height = 0;
703 /* The first area covers all the screen */
704 p_vout->p_buffer[0].i_areas = 1;
705 p_vout->p_buffer[0].pi_area_begin[0] = 0;
706 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
707 p_vout->p_buffer[1].i_areas = 1;
708 p_vout->p_buffer[1].pi_area_begin[0] = 0;
709 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
712 p_vout->p_buffer[0].p_data = p_buf1;
713 p_vout->p_buffer[1].p_data = p_buf2;
716 /* following functions are local */
718 /******************************************************************************
719 * InitThread: initialize video output thread
720 ******************************************************************************
721 * This function is called from RunThread and performs the second step of the
722 * initialization. It returns 0 on success. Note that the thread's flag are not
723 * modified inside this function.
724 ******************************************************************************/
725 static int InitThread( vout_thread_t *p_vout )
729 *p_vout->pi_status = THREAD_START;
731 /* Initialize output method - this function issues its own error messages */
732 if( vout_SysInit( p_vout ) )
737 /* Initialize convertion tables and functions */
738 if( vout_InitYUV( p_vout ) )
740 intf_ErrMsg("error: can't allocate YUV translation tables\n");
744 /* Mark thread as running and return */
745 p_vout->b_active = 1;
746 *p_vout->pi_status = THREAD_READY;
747 intf_DbgMsg("thread ready\n");
751 /******************************************************************************
752 * RunThread: video output thread
753 ******************************************************************************
754 * Video output thread. This function does only returns when the thread is
755 * terminated. It handles the pictures arriving in the video heap and the
756 * display device events.
757 ******************************************************************************/
758 static void RunThread( vout_thread_t *p_vout)
760 int i_index; /* index in heap */
761 mtime_t current_date; /* current date */
762 mtime_t display_date; /* display date */
763 boolean_t b_display; /* display flag */
764 picture_t * p_pic; /* picture pointer */
765 subpicture_t * p_subpic; /* subpicture pointer */
770 p_vout->b_error = InitThread( p_vout );
771 if( p_vout->b_error )
773 DestroyThread( p_vout, THREAD_ERROR );
779 * Main loop - it is not executed if an error occured during
782 while( (!p_vout->b_die) && (!p_vout->b_error) )
784 /* Initialize loop variables */
788 current_date = mdate();
791 * Find the picture to display - this operation does not need lock,
792 * since only READY_PICTUREs are handled
794 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
796 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
798 (p_vout->p_picture[i_index].date < display_date) ) )
800 p_pic = &p_vout->p_picture[i_index];
801 display_date = p_pic->date;
808 /* Computes FPS rate */
809 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
811 if( display_date < current_date )
813 /* Picture is late: it will be destroyed and the thread will sleep and
814 * go to next picture */
815 vlc_mutex_lock( &p_vout->picture_lock );
816 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
817 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
818 vlc_mutex_unlock( &p_vout->picture_lock );
822 else if( display_date > current_date + VOUT_DISPLAY_DELAY )
824 /* A picture is ready to be rendered, but its rendering date is
825 * far from the current one so the thread will perform an empty loop
826 * as if no picture were found. The picture state is unchanged */
833 * Find the subpicture to display - this operation does not need lock, since
834 * only READY_SUBPICTURES are handled. If no picture has been selected,
835 * display_date will depend on the subpicture
840 * Perform rendering, sleep and display rendered picture
842 if( p_pic ) /* picture and perhaps subpicture */
844 b_display = p_vout->b_active;
845 p_vout->last_display_date = display_date;
849 /* Set picture dimensions and clear buffer */
850 SetBufferPicture( p_vout, p_pic );
852 /* Render picture and informations */
853 RenderPicture( p_vout, p_pic );
856 RenderPictureInfo( p_vout, p_pic );
857 RenderInfo( p_vout );
861 /* Remove picture from heap */
862 vlc_mutex_lock( &p_vout->picture_lock );
863 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
864 vlc_mutex_unlock( &p_vout->picture_lock );
866 /* Render interface and subpicture */
867 if( b_display && p_vout->b_interface )
869 RenderInterface( p_vout );
875 RenderSubPicture( p_vout, p_subpic );
878 /* Remove subpicture from heap */
879 vlc_mutex_lock( &p_vout->subpicture_lock );
880 p_subpic->i_status = DESTROYED_SUBPICTURE;
881 vlc_mutex_unlock( &p_vout->subpicture_lock );
885 else if( p_subpic ) /* subpicture alone */
887 b_display = p_vout->b_active;
888 p_vout->last_display_date = display_date;
893 SetBufferPicture( p_vout, NULL );
895 /* Render informations, interface and subpicture */
898 RenderInfo( p_vout );
900 if( p_vout->b_interface )
902 RenderInterface( p_vout );
904 RenderSubPicture( p_vout, p_subpic );
907 /* Remove subpicture from heap */
908 vlc_mutex_lock( &p_vout->subpicture_lock );
909 p_subpic->i_status = DESTROYED_SUBPICTURE;
910 vlc_mutex_unlock( &p_vout->subpicture_lock );
912 else if( p_vout->b_active ) /* idle or interface screen alone */
914 if( p_vout->b_interface && 0 /* && ?? intf_change */ )
916 /* Interface has changed, so a new rendering is required - force
917 * it by setting last idle date to 0 */
918 p_vout->last_idle_date = 0;
921 /* Render idle screen and update idle date, then render interface if
923 b_display = RenderIdle( p_vout );
926 p_vout->last_idle_date = current_date;
927 if( p_vout->b_interface )
929 RenderInterface( p_vout );
940 * Sleep, wake up and display rendered picture
944 /* Store render time */
945 p_vout->render_time = mdate() - current_date;
948 /* Give back change lock */
949 vlc_mutex_unlock( &p_vout->change_lock );
951 /* Sleep a while or until a given date */
952 if( display_date != 0 )
954 mwait( display_date );
958 msleep( VOUT_IDLE_SLEEP );
961 /* On awakening, take back lock and send immediately picture to display,
962 * then swap buffers */
963 vlc_mutex_lock( &p_vout->change_lock );
965 intf_DbgMsg( "picture %p, subpicture %p\n", p_pic, p_subpic );
967 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
969 vout_SysDisplay( p_vout );
970 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
974 * Check events and manage thread
976 if( vout_SysManage( p_vout ) | Manage( p_vout ) )
978 /* A fatal error occured, and the thread must terminate immediately,
979 * without displaying anything - setting b_error to 1 cause the
980 * immediate end of the main while() loop. */
986 * Error loop - wait until the thread destruction is requested
988 if( p_vout->b_error )
990 ErrorThread( p_vout );
995 DestroyThread( p_vout, THREAD_OVER );
996 intf_DbgMsg( "thread end\n" );
999 /******************************************************************************
1000 * ErrorThread: RunThread() error loop
1001 ******************************************************************************
1002 * This function is called when an error occured during thread main's loop. The
1003 * thread can still receive feed, but must be ready to terminate as soon as
1005 ******************************************************************************/
1006 static void ErrorThread( vout_thread_t *p_vout )
1008 /* Wait until a `die' order */
1010 while( !p_vout->b_die )
1013 msleep( VOUT_IDLE_SLEEP );
1017 /*******************************************************************************
1018 * EndThread: thread destruction
1019 *******************************************************************************
1020 * This function is called when the thread ends after a sucessfull
1021 * initialization. It frees all ressources allocated by InitThread.
1022 *******************************************************************************/
1023 static void EndThread( vout_thread_t *p_vout )
1025 int i_index; /* index in heap */
1029 *p_vout->pi_status = THREAD_END;
1031 /* Destroy all remaining pictures and subpictures */
1032 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1034 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1036 free( p_vout->p_picture[i_index].p_data );
1038 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1040 free( p_vout->p_subpicture[i_index].p_data );
1044 /* Destroy translation tables */
1045 vout_EndYUV( p_vout );
1046 vout_SysEnd( p_vout );
1049 /*******************************************************************************
1050 * DestroyThread: thread destruction
1051 *******************************************************************************
1052 * This function is called when the thread ends. It frees all ressources
1053 * allocated by CreateThread. Status is available at this stage.
1054 *******************************************************************************/
1055 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1057 int *pi_status; /* status adress */
1059 /* Store status adress */
1061 pi_status = p_vout->pi_status;
1063 /* Destroy thread structures allocated by Create and InitThread */
1064 vout_UnloadFont( p_vout->p_default_font );
1065 vout_UnloadFont( p_vout->p_large_font );
1066 vout_SysDestroy( p_vout );
1068 *pi_status = i_status;
1071 /*******************************************************************************
1072 * Print: print simple text on a picture
1073 *******************************************************************************
1074 * This function will print a simple text on the picture. It is designed to
1075 * print debugging or general informations.
1076 *******************************************************************************/
1077 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 )
1079 int i_text_height; /* total text height */
1080 int i_text_width; /* total text width */
1082 /* Update upper left coordinates according to alignment */
1083 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1084 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1086 /* Set area and print text */
1087 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1088 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1089 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1090 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1091 0xffffffff, 0x00000000, 0x00000000, 0, psz_text );
1095 /*******************************************************************************
1096 * SetBufferArea: activate an area in current buffer
1097 *******************************************************************************
1098 * This function is called when something is rendered on the current buffer.
1099 * It set the area as active and prepare it to be cleared on next rendering.
1100 * Pay attention to the fact that in this functions, i_h is in fact the end y
1101 * coordinate of the new area.
1102 *******************************************************************************/
1103 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1105 vout_buffer_t * p_buffer; /* current buffer */
1106 int i_area_begin, i_area_end; /* area vertical extension */
1107 int i_area, i_area_copy; /* area index */
1108 int i_area_shift; /* shift distance for areas */
1110 /* Choose buffer and modify h to end of area position */
1111 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1115 * Remove part of the area which is inside the picture - this is done
1116 * by calling again SetBufferArea with the correct areas dimensions.
1118 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1120 i_area_begin = p_buffer->i_pic_y;
1121 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1123 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1124 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1125 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1127 /* Keep the stripe above the picture, if any */
1128 if( i_y < i_area_begin )
1130 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1132 /* Keep the stripe below the picture, if any */
1133 if( i_h > i_area_end )
1135 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1141 /* Skip some extensions until interesting areas */
1143 (i_area < p_buffer->i_areas) &&
1144 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1150 if( i_area == p_buffer->i_areas )
1152 /* New area is below all existing ones: just add it at the end of the
1153 * array, if possible - else, append it to the last one */
1154 if( i_area < VOUT_MAX_AREAS )
1156 p_buffer->pi_area_begin[i_area] = i_y;
1157 p_buffer->pi_area_end[i_area] = i_h;
1158 p_buffer->i_areas++;
1163 intf_DbgMsg("areas overflow\n");
1165 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1170 i_area_begin = p_buffer->pi_area_begin[i_area];
1171 i_area_end = p_buffer->pi_area_end[i_area];
1173 if( i_y < i_area_begin )
1175 if( i_h >= i_area_begin - 1 )
1177 /* Extend area above */
1178 p_buffer->pi_area_begin[i_area] = i_y;
1182 /* Create a new area above : merge last area if overflow, then
1183 * move all old areas down */
1184 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1187 intf_DbgMsg("areas overflow\n");
1189 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1193 p_buffer->i_areas++;
1195 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1197 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1198 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1200 p_buffer->pi_area_begin[i_area] = i_y;
1201 p_buffer->pi_area_end[i_area] = i_h;
1205 if( i_h > i_area_end )
1207 /* Find further areas which can be merged with the new one */
1208 for( i_area_copy = i_area + 1;
1209 (i_area_copy < p_buffer->i_areas) &&
1210 (p_buffer->pi_area_begin[i_area] <= i_h);
1217 if( i_area_copy != i_area )
1219 /* Merge with last possible areas */
1220 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1222 /* Shift lower areas upward */
1223 i_area_shift = i_area_copy - i_area;
1224 p_buffer->i_areas -= i_area_shift;
1225 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1227 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1228 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1233 /* Extend area below */
1234 p_buffer->pi_area_end[i_area] = i_h;
1240 /*******************************************************************************
1241 * SetBufferPicture: clear buffer and set picture area
1242 *******************************************************************************
1243 * This function is called before any rendering. It clears the current
1244 * rendering buffer and set the new picture area. If the picture pointer is
1245 * NULL, then no picture area is defined. Floating operations are avoided since
1246 * some MMX calculations may follow.
1247 *******************************************************************************/
1248 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1250 vout_buffer_t * p_buffer; /* current buffer */
1251 int i_pic_x, i_pic_y; /* picture position */
1252 int i_pic_width, i_pic_height; /* picture dimensions */
1253 int i_old_pic_y, i_old_pic_height; /* old picture area */
1254 int i_vout_width, i_vout_height; /* display dimensions */
1255 int i_area; /* area index */
1256 int i_data_index; /* area data index */
1257 int i_data_size; /* area data size, in 256 bytes blocs */
1258 u64 * p_data; /* area data */
1260 /* Choose buffer and set display dimensions */
1261 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1262 i_vout_width = p_vout->i_width;
1263 i_vout_height = p_vout->i_height;
1266 * Computes new picture size
1270 /* Try horizontal scaling first - width must be a mutiple of 16 */
1271 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1272 i_vout_width : p_pic->i_width) & ~0xf;
1273 switch( p_pic->i_aspect_ratio )
1275 case AR_3_4_PICTURE:
1276 i_pic_height = i_pic_width * 3 / 4;
1278 case AR_16_9_PICTURE:
1279 i_pic_height = i_pic_width * 9 / 16;
1281 case AR_221_1_PICTURE:
1282 i_pic_height = i_pic_width * 100 / 221;
1284 case AR_SQUARE_PICTURE:
1286 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1290 /* If picture dimensions using horizontal scaling are too large, use
1291 * vertical scaling. Since width must be a multiple of 16, height is
1292 * adjusted again after. */
1293 if( i_pic_height > i_vout_height )
1295 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1296 i_vout_height : p_pic->i_height;
1297 switch( p_pic->i_aspect_ratio )
1299 case AR_3_4_PICTURE:
1300 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1301 i_pic_height = i_pic_width * 3 / 4;
1303 case AR_16_9_PICTURE:
1304 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1305 i_pic_height = i_pic_width * 9 / 16;
1307 case AR_221_1_PICTURE:
1308 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1309 i_pic_height = i_pic_width * 100 / 221;
1311 case AR_SQUARE_PICTURE:
1313 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1314 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1319 /* Set picture position */
1320 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1321 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1325 /* No picture: size is 0 */
1333 * Set new picture size - if is is smaller than the previous one, clear
1334 * around it. Since picture are centered, only their size is tested.
1336 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1338 i_old_pic_y = p_buffer->i_pic_y;
1339 i_old_pic_height = p_buffer->i_pic_height;
1340 p_buffer->i_pic_x = i_pic_x;
1341 p_buffer->i_pic_y = i_pic_y;
1342 p_buffer->i_pic_width = i_pic_width;
1343 p_buffer->i_pic_height = i_pic_height;
1344 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1348 p_buffer->i_pic_x = i_pic_x;
1349 p_buffer->i_pic_y = i_pic_y;
1350 p_buffer->i_pic_width = i_pic_width;
1351 p_buffer->i_pic_height = i_pic_height;
1357 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1360 intf_DbgMsg("clearing picture %p area: %d-%d\n", p_pic,
1361 p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area]);
1363 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1364 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) *
1365 p_vout->i_bytes_per_line / 256;
1366 for( i_data_index = 0; i_data_index < i_data_size; i_data_index++ )
1368 /* Clear 256 bytes block */
1369 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1370 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1371 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1372 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1373 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1374 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1375 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1376 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1378 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) *
1379 p_vout->i_bytes_per_line % 256 / 4;
1380 for( i_data_index = 0; i_data_index < i_data_size; i_data_index++ )
1382 /* Clear remaining 4 bytes blocks */
1390 p_buffer->i_areas = 0;
1394 * In DEBUG_VIDEO mode, draw white pixels at the beginning and the end of
1395 * the picture area. These pixels should not be erased by rendering functions,
1396 * otherwise segmentation fault is menacing !
1400 *(u16*)(p_buffer->p_data + p_vout->i_bytes_per_line * i_pic_y +
1401 p_vout->i_bytes_per_pixel * (i_pic_x - 1)) = 0xffff;
1405 *(u16*)(p_buffer->p_data + p_vout->i_bytes_per_line * (i_pic_y - 1) +
1406 p_vout->i_bytes_per_pixel * i_pic_x ) = 0xffff;
1408 if( i_pic_x + i_pic_width < p_vout->i_width )
1410 *(u16*)(p_buffer->p_data + p_vout->i_bytes_per_line * (i_pic_y + i_pic_height - 1) +
1411 p_vout->i_bytes_per_pixel * (i_pic_x + i_pic_width) ) = 0xffff;
1413 if( i_pic_y + i_pic_height < p_vout->i_height )
1415 *(u16*)(p_buffer->p_data + p_vout->i_bytes_per_line * (i_pic_y + i_pic_height) +
1416 p_vout->i_bytes_per_pixel * (i_pic_x + i_pic_width - 1) ) = 0xffff;
1421 /******************************************************************************
1422 * RenderPicture: render a picture
1423 ******************************************************************************
1424 * This function convert a picture from a video heap to a pixel-encoded image
1425 * and copy it to the current rendering buffer. No lock is required, since the
1426 * rendered picture has been determined as existant, and will only be destroyed
1427 * by the vout thread later.
1428 ******************************************************************************/
1429 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1432 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1433 mtime_t render_time; /* picture rendering time */
1435 vout_buffer_t * p_buffer; /* rendering buffer */
1436 byte_t * p_pic_data; /* convertion destination */
1438 /* Get and set rendering informations */
1439 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1440 p_pic_data = p_buffer->p_data +
1441 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1442 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1444 render_time = mdate();
1448 * Choose appropriate rendering function and render picture
1450 switch( p_pic->i_type )
1452 case YUV_420_PICTURE:
1453 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1454 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1455 p_pic->i_width, p_pic->i_height,
1456 p_buffer->i_pic_width, p_buffer->i_pic_height,
1457 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1458 p_pic->i_matrix_coefficients );
1460 case YUV_422_PICTURE:
1461 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1462 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1463 p_pic->i_width, p_pic->i_height,
1464 p_buffer->i_pic_width, p_buffer->i_pic_height,
1465 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1466 p_pic->i_matrix_coefficients );
1468 case YUV_444_PICTURE:
1469 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1470 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1471 p_pic->i_width, p_pic->i_height,
1472 p_buffer->i_pic_width, p_buffer->i_pic_height,
1473 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1474 p_pic->i_matrix_coefficients );
1478 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1484 /* Print picture date and rendering time */
1485 intf_DbgMsg("picture %p rendered (%ld us), display date: %s\n", p_pic,
1486 (long) (mdate() - render_time), mstrtime( psz_date, p_pic->date ));
1490 /******************************************************************************
1491 * RenderPictureInfo: print additionnal informations on a picture
1492 ******************************************************************************
1493 * This function will print informations such as fps and other picture
1494 * dependant informations.
1495 ******************************************************************************/
1496 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1498 #if defined(STATS) || defined(DEBUG)
1499 char psz_buffer[256]; /* string buffer */
1504 * Print FPS rate in upper right corner
1506 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1508 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1509 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1510 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1511 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1515 * Print frames count and loop time in upper left corner
1517 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1518 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1519 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1524 * Print picture information in lower right corner
1526 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1527 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1528 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1529 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1530 p_pic->i_width, p_pic->i_height,
1531 p_pic->i_display_width, p_pic->i_display_height,
1532 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1533 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1534 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1535 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1536 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1537 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1538 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1539 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1540 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1541 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1545 /******************************************************************************
1546 * RenderIdle: render idle picture
1547 ******************************************************************************
1548 * This function will print something on the screen. It will return 0 if
1549 * nothing has been rendered, or 1 if something has been changed on the screen.
1550 * Note that if you absolutely want something to be printed, you will have
1551 * to force it by setting the last idle date to 0.
1552 * Unlike other rendering functions, this one calls the SetBufferPicture
1553 * function when needed.
1554 ******************************************************************************/
1555 static int RenderIdle( vout_thread_t *p_vout )
1557 int i_x = 0, i_y = 0; /* text position */
1558 int i_width, i_height; /* text size */
1559 mtime_t current_date; /* current date */
1560 const char *psz_text = "no stream"; /* text to display */
1563 current_date = mdate();
1564 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1565 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1567 SetBufferPicture( p_vout, NULL );
1568 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1569 &i_width, &i_height );
1570 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1572 vout_Print( p_vout->p_large_font,
1573 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1574 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1575 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1576 0xffffffff, 0x33333333, 0,
1577 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1578 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1585 /******************************************************************************
1586 * RenderInfo: render additionnal informations
1587 ******************************************************************************
1588 * This function render informations which do not depend of the current picture
1590 ******************************************************************************/
1591 static void RenderInfo( vout_thread_t *p_vout )
1594 char psz_buffer[256]; /* string buffer */
1595 int i_ready_pic = 0; /* ready pictures */
1596 int i_reserved_pic = 0; /* reserved pictures */
1597 int i_picture; /* picture index */
1602 * Print thread state in lower left corner
1604 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1606 switch( p_vout->p_picture[i_picture].i_status )
1608 case RESERVED_PICTURE:
1609 case RESERVED_DATED_PICTURE:
1610 case RESERVED_DISP_PICTURE:
1618 sprintf( psz_buffer, "pic: %d/%d/%d",
1619 i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1620 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1624 /*******************************************************************************
1625 * RenderSubPicture: render a subpicture
1626 *******************************************************************************
1627 * This function render a sub picture unit.
1628 *******************************************************************************/
1629 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1631 p_vout_font_t p_font; /* text font */
1632 int i_width, i_height; /* subpicture dimensions */
1634 switch( p_subpic->i_type )
1636 case TEXT_SUBPICTURE: /* single line text */
1637 /* Select default font if not specified */
1638 p_font = p_subpic->type.text.p_font;
1639 if( p_font == NULL )
1641 p_font = p_vout->p_default_font;
1644 /* Computes text size (width and height fields are ignored) and print it */
1645 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1646 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1647 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1649 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1650 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1651 p_subpic->i_y * p_vout->i_bytes_per_line,
1652 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1653 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1654 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1656 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1662 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1667 /*******************************************************************************
1668 * RenderInterface: render the interface
1669 *******************************************************************************
1670 * This function render the interface, if any.
1671 *******************************************************************************/
1672 static void RenderInterface( vout_thread_t *p_vout )
1674 int i_height, i_text_height; /* total and text height */
1675 int i_width_1, i_width_2; /* text width */
1676 int i_byte; /* byte index */
1677 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1678 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1681 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1682 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1683 i_height += i_text_height;
1685 /* Render background - effective background color will depend of the screen
1687 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1688 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1691 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = 0x33;
1694 /* Render text, if not larger than screen */
1695 if( i_width_1 < p_vout->i_width )
1697 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1698 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1699 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1700 0xffffffff, 0x00000000, 0x00000000,
1701 OUTLINED_TEXT, psz_text_1 );
1703 if( i_width_2 < p_vout->i_width )
1705 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1706 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1707 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1708 0xffffffff, 0x00000000, 0x00000000,
1709 OUTLINED_TEXT, psz_text_2 );
1712 /* Activate modified area */
1713 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1716 /******************************************************************************
1717 * Manage: manage thread
1718 ******************************************************************************
1719 * This function will handle changes in thread configuration.
1720 ******************************************************************************/
1721 static int Manage( vout_thread_t *p_vout )
1724 if( p_vout->i_changes )
1726 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1727 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1731 /* On gamma or grayscale change, rebuild tables */
1732 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1735 if( vout_ResetYUV( p_vout ) )
1737 intf_ErrMsg("error: can't rebuild convertion tables\n");
1742 /* Clear changes flags which does not need management or have been
1744 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1745 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
1746 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
1748 /* Detect unauthorized changes */
1749 if( p_vout->i_changes )
1751 /* Some changes were not acknowledged by vout_SysManage or this function,
1752 * it means they should not be authorized */
1753 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
1760 /******************************************************************************
1761 * Align: align a subpicture in the screen
1762 ******************************************************************************
1763 * This function is used for rendering text or subpictures. It returns non 0
1764 * it the final aera is not fully included in display area. Return coordinates
1766 ******************************************************************************/
1767 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
1768 int i_width, int i_height, int i_h_align, int i_v_align )
1770 /* Align horizontally */
1774 *pi_x -= i_width / 2;
1777 *pi_x += (p_vout->i_width - i_width) / 2;
1783 *pi_x += p_vout->i_width - i_width;
1787 /* Align vertically */
1791 *pi_y -= i_height / 2;
1794 *pi_y += (p_vout->i_height - i_height) / 2;
1800 *pi_y += p_vout->i_height - i_height;
1802 case SUBTITLE_RALIGN:
1803 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
1804 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
1808 /* Return non 0 if clipping failed */
1809 return( (*pi_x < 0) || (*pi_y < 0) ||
1810 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );