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 in buffer %d, display=%d\n", p_pic, p_subpic,
966 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
968 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
970 vout_SysDisplay( p_vout );
971 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
975 * Check events and manage thread
977 if( vout_SysManage( p_vout ) | Manage( p_vout ) )
979 /* A fatal error occured, and the thread must terminate immediately,
980 * without displaying anything - setting b_error to 1 cause the
981 * immediate end of the main while() loop. */
987 * Error loop - wait until the thread destruction is requested
989 if( p_vout->b_error )
991 ErrorThread( p_vout );
996 DestroyThread( p_vout, THREAD_OVER );
997 intf_DbgMsg( "thread end\n" );
1000 /******************************************************************************
1001 * ErrorThread: RunThread() error loop
1002 ******************************************************************************
1003 * This function is called when an error occured during thread main's loop. The
1004 * thread can still receive feed, but must be ready to terminate as soon as
1006 ******************************************************************************/
1007 static void ErrorThread( vout_thread_t *p_vout )
1009 /* Wait until a `die' order */
1011 while( !p_vout->b_die )
1014 msleep( VOUT_IDLE_SLEEP );
1018 /*******************************************************************************
1019 * EndThread: thread destruction
1020 *******************************************************************************
1021 * This function is called when the thread ends after a sucessfull
1022 * initialization. It frees all ressources allocated by InitThread.
1023 *******************************************************************************/
1024 static void EndThread( vout_thread_t *p_vout )
1026 int i_index; /* index in heap */
1030 *p_vout->pi_status = THREAD_END;
1032 /* Destroy all remaining pictures and subpictures */
1033 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1035 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1037 free( p_vout->p_picture[i_index].p_data );
1039 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1041 free( p_vout->p_subpicture[i_index].p_data );
1045 /* Destroy translation tables */
1046 vout_EndYUV( p_vout );
1047 vout_SysEnd( p_vout );
1050 /*******************************************************************************
1051 * DestroyThread: thread destruction
1052 *******************************************************************************
1053 * This function is called when the thread ends. It frees all ressources
1054 * allocated by CreateThread. Status is available at this stage.
1055 *******************************************************************************/
1056 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1058 int *pi_status; /* status adress */
1060 /* Store status adress */
1062 pi_status = p_vout->pi_status;
1064 /* Destroy thread structures allocated by Create and InitThread */
1065 vout_UnloadFont( p_vout->p_default_font );
1066 vout_UnloadFont( p_vout->p_large_font );
1067 vout_SysDestroy( p_vout );
1069 *pi_status = i_status;
1072 /*******************************************************************************
1073 * Print: print simple text on a picture
1074 *******************************************************************************
1075 * This function will print a simple text on the picture. It is designed to
1076 * print debugging or general informations.
1077 *******************************************************************************/
1078 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 )
1080 int i_text_height; /* total text height */
1081 int i_text_width; /* total text width */
1083 /* Update upper left coordinates according to alignment */
1084 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1085 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1087 /* Set area and print text */
1088 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1089 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1090 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1091 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1092 0xffffffff, 0x00000000, 0x00000000, 0, psz_text );
1096 /*******************************************************************************
1097 * SetBufferArea: activate an area in current buffer
1098 *******************************************************************************
1099 * This function is called when something is rendered on the current buffer.
1100 * It set the area as active and prepare it to be cleared on next rendering.
1101 * Pay attention to the fact that in this functions, i_h is in fact the end y
1102 * coordinate of the new area.
1103 *******************************************************************************/
1104 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1106 vout_buffer_t * p_buffer; /* current buffer */
1107 int i_area_begin, i_area_end; /* area vertical extension */
1108 int i_area, i_area_copy; /* area index */
1109 int i_area_shift; /* shift distance for areas */
1111 /* Choose buffer and modify h to end of area position */
1112 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1116 * Remove part of the area which is inside the picture - this is done
1117 * by calling again SetBufferArea with the correct areas dimensions.
1119 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1121 i_area_begin = p_buffer->i_pic_y;
1122 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1124 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1125 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1126 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1128 /* Keep the stripe above the picture, if any */
1129 if( i_y < i_area_begin )
1131 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1133 /* Keep the stripe below the picture, if any */
1134 if( i_h > i_area_end )
1136 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1142 /* Skip some extensions until interesting areas */
1144 (i_area < p_buffer->i_areas) &&
1145 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1151 if( i_area == p_buffer->i_areas )
1153 /* New area is below all existing ones: just add it at the end of the
1154 * array, if possible - else, append it to the last one */
1155 if( i_area < VOUT_MAX_AREAS )
1157 p_buffer->pi_area_begin[i_area] = i_y;
1158 p_buffer->pi_area_end[i_area] = i_h;
1159 p_buffer->i_areas++;
1164 intf_DbgMsg("areas overflow\n");
1166 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1171 i_area_begin = p_buffer->pi_area_begin[i_area];
1172 i_area_end = p_buffer->pi_area_end[i_area];
1174 if( i_y < i_area_begin )
1176 if( i_h >= i_area_begin - 1 )
1178 /* Extend area above */
1179 p_buffer->pi_area_begin[i_area] = i_y;
1183 /* Create a new area above : merge last area if overflow, then
1184 * move all old areas down */
1185 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1188 intf_DbgMsg("areas overflow\n");
1190 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1194 p_buffer->i_areas++;
1196 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1198 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1199 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1201 p_buffer->pi_area_begin[i_area] = i_y;
1202 p_buffer->pi_area_end[i_area] = i_h;
1206 if( i_h > i_area_end )
1208 /* Find further areas which can be merged with the new one */
1209 for( i_area_copy = i_area + 1;
1210 (i_area_copy < p_buffer->i_areas) &&
1211 (p_buffer->pi_area_begin[i_area] <= i_h);
1218 if( i_area_copy != i_area )
1220 /* Merge with last possible areas */
1221 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1223 /* Shift lower areas upward */
1224 i_area_shift = i_area_copy - i_area;
1225 p_buffer->i_areas -= i_area_shift;
1226 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1228 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1229 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1234 /* Extend area below */
1235 p_buffer->pi_area_end[i_area] = i_h;
1241 /*******************************************************************************
1242 * SetBufferPicture: clear buffer and set picture area
1243 *******************************************************************************
1244 * This function is called before any rendering. It clears the current
1245 * rendering buffer and set the new picture area. If the picture pointer is
1246 * NULL, then no picture area is defined. Floating operations are avoided since
1247 * some MMX calculations may follow.
1248 *******************************************************************************/
1249 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1251 vout_buffer_t * p_buffer; /* current buffer */
1252 int i_pic_x, i_pic_y; /* picture position */
1253 int i_pic_width, i_pic_height; /* picture dimensions */
1254 int i_old_pic_y, i_old_pic_height; /* old picture area */
1255 int i_vout_width, i_vout_height; /* display dimensions */
1256 int i_area; /* area index */
1257 int i_data_index; /* area data index */
1258 int i_data_size; /* area data size, in 256 bytes blocs */
1259 u64 * p_data; /* area data, for clearing */
1260 byte_t * p_data8; /* area data, for clearing (slow) */
1262 /* Choose buffer and set display dimensions */
1263 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1264 i_vout_width = p_vout->i_width;
1265 i_vout_height = p_vout->i_height;
1268 * Computes new picture size
1272 /* Try horizontal scaling first - width must be a mutiple of 16 */
1273 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1274 i_vout_width : p_pic->i_width) & ~0xf;
1275 switch( p_pic->i_aspect_ratio )
1277 case AR_3_4_PICTURE:
1278 i_pic_height = i_pic_width * 3 / 4;
1280 case AR_16_9_PICTURE:
1281 i_pic_height = i_pic_width * 9 / 16;
1283 case AR_221_1_PICTURE:
1284 i_pic_height = i_pic_width * 100 / 221;
1286 case AR_SQUARE_PICTURE:
1288 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1292 /* If picture dimensions using horizontal scaling are too large, use
1293 * vertical scaling. Since width must be a multiple of 16, height is
1294 * adjusted again after. */
1295 if( i_pic_height > i_vout_height )
1297 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1298 i_vout_height : p_pic->i_height;
1299 switch( p_pic->i_aspect_ratio )
1301 case AR_3_4_PICTURE:
1302 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1303 i_pic_height = i_pic_width * 3 / 4;
1305 case AR_16_9_PICTURE:
1306 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1307 i_pic_height = i_pic_width * 9 / 16;
1309 case AR_221_1_PICTURE:
1310 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1311 i_pic_height = i_pic_width * 100 / 221;
1313 case AR_SQUARE_PICTURE:
1315 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1316 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1321 /* Set picture position */
1322 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1323 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1327 /* No picture: size is 0 */
1335 * Set new picture size - if is is smaller than the previous one, clear
1336 * around it. Since picture are centered, only their size is tested.
1338 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1340 i_old_pic_y = p_buffer->i_pic_y;
1341 i_old_pic_height = p_buffer->i_pic_height;
1342 p_buffer->i_pic_x = i_pic_x;
1343 p_buffer->i_pic_y = i_pic_y;
1344 p_buffer->i_pic_width = i_pic_width;
1345 p_buffer->i_pic_height = i_pic_height;
1346 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1350 p_buffer->i_pic_x = i_pic_x;
1351 p_buffer->i_pic_y = i_pic_y;
1352 p_buffer->i_pic_width = i_pic_width;
1353 p_buffer->i_pic_height = i_pic_height;
1359 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1362 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1363 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1365 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1366 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1367 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1369 /* Clear 256 bytes block */
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;
1377 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1379 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1381 /* Clear remaining 16 bytes blocks */
1382 *p_data++ = 0; *p_data++ = 0;
1384 p_data8 = (byte_t *)p_data;
1385 for( i_data_index = i_data_size % 16; i_data_index--; )
1387 /* Clear remaining bytes */
1395 p_buffer->i_areas = 0;
1399 * In DEBUG_VIDEO mode, draw white pixels at the beginning and the end of
1400 * the picture area. These pixels should not be erased by rendering functions,
1401 * otherwise segmentation fault is menacing !
1405 *(u16*)(p_buffer->p_data + p_vout->i_bytes_per_line * i_pic_y +
1406 p_vout->i_bytes_per_pixel * (i_pic_x - 1)) = 0xffff;
1410 *(u16*)(p_buffer->p_data + p_vout->i_bytes_per_line * (i_pic_y - 1) +
1411 p_vout->i_bytes_per_pixel * i_pic_x ) = 0xffff;
1413 if( i_pic_x + i_pic_width < p_vout->i_width )
1415 *(u16*)(p_buffer->p_data + p_vout->i_bytes_per_line * (i_pic_y + i_pic_height - 1) +
1416 p_vout->i_bytes_per_pixel * (i_pic_x + i_pic_width) ) = 0xffff;
1418 if( i_pic_y + i_pic_height < p_vout->i_height )
1420 *(u16*)(p_buffer->p_data + p_vout->i_bytes_per_line * (i_pic_y + i_pic_height) +
1421 p_vout->i_bytes_per_pixel * (i_pic_x + i_pic_width - 1) ) = 0xffff;
1426 /******************************************************************************
1427 * RenderPicture: render a picture
1428 ******************************************************************************
1429 * This function convert a picture from a video heap to a pixel-encoded image
1430 * and copy it to the current rendering buffer. No lock is required, since the
1431 * rendered picture has been determined as existant, and will only be destroyed
1432 * by the vout thread later.
1433 ******************************************************************************/
1434 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1437 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1438 mtime_t render_time; /* picture rendering time */
1440 vout_buffer_t * p_buffer; /* rendering buffer */
1441 byte_t * p_pic_data; /* convertion destination */
1443 /* Get and set rendering informations */
1444 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1445 p_pic_data = p_buffer->p_data +
1446 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1447 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1449 render_time = mdate();
1453 * Choose appropriate rendering function and render picture
1455 switch( p_pic->i_type )
1457 case YUV_420_PICTURE:
1458 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1459 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1460 p_pic->i_width, p_pic->i_height,
1461 p_buffer->i_pic_width, p_buffer->i_pic_height,
1462 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1463 p_pic->i_matrix_coefficients );
1465 case YUV_422_PICTURE:
1466 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1467 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1468 p_pic->i_width, p_pic->i_height,
1469 p_buffer->i_pic_width, p_buffer->i_pic_height,
1470 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1471 p_pic->i_matrix_coefficients );
1473 case YUV_444_PICTURE:
1474 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1475 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1476 p_pic->i_width, p_pic->i_height,
1477 p_buffer->i_pic_width, p_buffer->i_pic_height,
1478 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1479 p_pic->i_matrix_coefficients );
1483 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1489 /* Print picture date and rendering time */
1490 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1491 p_vout->i_buffer_index, (long) (mdate() - render_time),
1492 mstrtime( psz_date, p_pic->date ));
1496 /******************************************************************************
1497 * RenderPictureInfo: print additionnal informations on a picture
1498 ******************************************************************************
1499 * This function will print informations such as fps and other picture
1500 * dependant informations.
1501 ******************************************************************************/
1502 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1504 #if defined(STATS) || defined(DEBUG)
1505 char psz_buffer[256]; /* string buffer */
1510 * Print FPS rate in upper right corner
1512 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1514 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1515 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1516 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1517 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1521 * Print frames count and loop time in upper left corner
1523 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1524 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1525 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1530 * Print picture information in lower right corner
1532 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1533 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1534 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1535 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1536 p_pic->i_width, p_pic->i_height,
1537 p_pic->i_display_width, p_pic->i_display_height,
1538 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1539 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1540 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1541 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1542 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1543 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1544 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1545 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1546 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1547 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1551 /******************************************************************************
1552 * RenderIdle: render idle picture
1553 ******************************************************************************
1554 * This function will print something on the screen. It will return 0 if
1555 * nothing has been rendered, or 1 if something has been changed on the screen.
1556 * Note that if you absolutely want something to be printed, you will have
1557 * to force it by setting the last idle date to 0.
1558 * Unlike other rendering functions, this one calls the SetBufferPicture
1559 * function when needed.
1560 ******************************************************************************/
1561 static int RenderIdle( vout_thread_t *p_vout )
1563 int i_x = 0, i_y = 0; /* text position */
1564 int i_width, i_height; /* text size */
1565 mtime_t current_date; /* current date */
1566 const char *psz_text = "no stream"; /* text to display */
1569 current_date = mdate();
1570 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1571 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1573 SetBufferPicture( p_vout, NULL );
1574 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1575 &i_width, &i_height );
1576 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1578 vout_Print( p_vout->p_large_font,
1579 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1580 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1581 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1582 0xffffffff, 0x33333333, 0,
1583 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1584 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1591 /******************************************************************************
1592 * RenderInfo: render additionnal informations
1593 ******************************************************************************
1594 * This function render informations which do not depend of the current picture
1596 ******************************************************************************/
1597 static void RenderInfo( vout_thread_t *p_vout )
1600 char psz_buffer[256]; /* string buffer */
1601 int i_ready_pic = 0; /* ready pictures */
1602 int i_reserved_pic = 0; /* reserved pictures */
1603 int i_picture; /* picture index */
1608 * Print thread state in lower left corner
1610 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1612 switch( p_vout->p_picture[i_picture].i_status )
1614 case RESERVED_PICTURE:
1615 case RESERVED_DATED_PICTURE:
1616 case RESERVED_DISP_PICTURE:
1624 sprintf( psz_buffer, "pic: %d/%d/%d",
1625 i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1626 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1630 /*******************************************************************************
1631 * RenderSubPicture: render a subpicture
1632 *******************************************************************************
1633 * This function render a sub picture unit.
1634 *******************************************************************************/
1635 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1637 p_vout_font_t p_font; /* text font */
1638 int i_width, i_height; /* subpicture dimensions */
1640 switch( p_subpic->i_type )
1642 case TEXT_SUBPICTURE: /* single line text */
1643 /* Select default font if not specified */
1644 p_font = p_subpic->type.text.p_font;
1645 if( p_font == NULL )
1647 p_font = p_vout->p_default_font;
1650 /* Computes text size (width and height fields are ignored) and print it */
1651 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1652 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1653 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1655 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1656 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1657 p_subpic->i_y * p_vout->i_bytes_per_line,
1658 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1659 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1660 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1662 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1668 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1673 /*******************************************************************************
1674 * RenderInterface: render the interface
1675 *******************************************************************************
1676 * This function render the interface, if any.
1677 *******************************************************************************/
1678 static void RenderInterface( vout_thread_t *p_vout )
1680 int i_height, i_text_height; /* total and text height */
1681 int i_width_1, i_width_2; /* text width */
1682 int i_byte; /* byte index */
1683 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1684 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1687 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1688 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1689 i_height += i_text_height;
1691 /* Render background - effective background color will depend of the screen
1693 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1694 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1697 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = 0x33;
1700 /* Render text, if not larger than screen */
1701 if( i_width_1 < p_vout->i_width )
1703 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1704 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1705 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1706 0xffffffff, 0x00000000, 0x00000000,
1707 OUTLINED_TEXT, psz_text_1 );
1709 if( i_width_2 < p_vout->i_width )
1711 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1712 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1713 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1714 0xffffffff, 0x00000000, 0x00000000,
1715 OUTLINED_TEXT, psz_text_2 );
1718 /* Activate modified area */
1719 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1722 /******************************************************************************
1723 * Manage: manage thread
1724 ******************************************************************************
1725 * This function will handle changes in thread configuration.
1726 ******************************************************************************/
1727 static int Manage( vout_thread_t *p_vout )
1730 if( p_vout->i_changes )
1732 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1733 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1737 /* On gamma or grayscale change, rebuild tables */
1738 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1741 if( vout_ResetYUV( p_vout ) )
1743 intf_ErrMsg("error: can't rebuild convertion tables\n");
1748 /* Clear changes flags which does not need management or have been
1750 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1751 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
1752 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
1754 /* Detect unauthorized changes */
1755 if( p_vout->i_changes )
1757 /* Some changes were not acknowledged by vout_SysManage or this function,
1758 * it means they should not be authorized */
1759 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
1766 /******************************************************************************
1767 * Align: align a subpicture in the screen
1768 ******************************************************************************
1769 * This function is used for rendering text or subpictures. It returns non 0
1770 * it the final aera is not fully included in display area. Return coordinates
1772 ******************************************************************************/
1773 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
1774 int i_width, int i_height, int i_h_align, int i_v_align )
1776 /* Align horizontally */
1780 *pi_x -= i_width / 2;
1783 *pi_x += (p_vout->i_width - i_width) / 2;
1789 *pi_x += p_vout->i_width - i_width;
1793 /* Align vertically */
1797 *pi_y -= i_height / 2;
1800 *pi_y += (p_vout->i_height - i_height) / 2;
1806 *pi_y += p_vout->i_height - i_height;
1808 case SUBTITLE_RALIGN:
1809 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
1810 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
1814 /* Return non 0 if clipping failed */
1815 return( (*pi_x < 0) || (*pi_y < 0) ||
1816 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );