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 );
428 vlc_mutex_unlock( &p_vout->picture_lock );
431 /******************************************************************************
432 * vout_DatePicture: date a picture
433 ******************************************************************************
434 * Remove the reservation flag of a picture, which will cause it to be ready for
435 * display. The picture won't be displayed until vout_DisplayPicture has been
437 ******************************************************************************/
438 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
440 vlc_mutex_lock( &p_vout->picture_lock );
442 switch( p_pic->i_status )
444 case RESERVED_PICTURE:
445 p_pic->i_status = RESERVED_DATED_PICTURE;
447 case RESERVED_DISP_PICTURE:
448 p_pic->i_status = READY_PICTURE;
452 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
458 intf_DbgMsg("picture %p\n", p_pic);
461 vlc_mutex_unlock( &p_vout->picture_lock );
464 /******************************************************************************
465 * vout_CreatePicture: allocate a picture in the video output heap.
466 ******************************************************************************
467 * This function create a reserved image in the video output heap.
468 * A null pointer is returned if the function fails. This method provides an
469 * already allocated zone of memory in the picture data fields. It needs locking
470 * since several pictures can be created by several producers threads.
471 ******************************************************************************/
472 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
473 int i_width, int i_height )
475 int i_picture; /* picture index */
476 int i_chroma_width = 0; /* chroma width */
477 picture_t * p_free_picture = NULL; /* first free picture */
478 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
481 vlc_mutex_lock( &p_vout->picture_lock );
484 * Look for an empty place
486 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
488 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
490 /* Picture is marked for destruction, but is still allocated - note
491 * that if width and type are the same for two pictures, chroma_width
492 * should also be the same */
493 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
494 (p_vout->p_picture[i_picture].i_height == i_height) &&
495 (p_vout->p_picture[i_picture].i_width == i_width) )
497 /* Memory size do match : memory will not be reallocated, and function
498 * can end immediately - this is the best possible case, since no
499 * memory allocation needs to be done */
500 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
502 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
503 &p_vout->p_picture[i_picture] );
505 vlc_mutex_unlock( &p_vout->picture_lock );
506 return( &p_vout->p_picture[i_picture] );
508 else if( p_destroyed_picture == NULL )
510 /* Memory size do not match, but picture index will be kept in
511 * case no other place are left */
512 p_destroyed_picture = &p_vout->p_picture[i_picture];
515 else if( (p_free_picture == NULL) &&
516 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
518 /* Picture is empty and ready for allocation */
519 p_free_picture = &p_vout->p_picture[i_picture];
523 /* If no free picture is available, use a destroyed picture */
524 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
526 /* No free picture or matching destroyed picture has been found, but
527 * a destroyed picture is still avalaible */
528 free( p_destroyed_picture->p_data );
529 p_free_picture = p_destroyed_picture;
535 if( p_free_picture != NULL )
537 /* Allocate memory */
540 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
541 i_chroma_width = i_width / 2;
542 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
543 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
544 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
545 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
547 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
548 i_chroma_width = i_width / 2;
549 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
550 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
551 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
552 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
554 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
555 i_chroma_width = i_width;
556 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
557 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
558 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
559 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
563 intf_DbgMsg("error: unknown picture type %d\n", i_type );
564 p_free_picture->p_data = NULL;
569 if( p_free_picture->p_data != NULL )
571 /* Copy picture informations, set some default values */
572 p_free_picture->i_type = i_type;
573 p_free_picture->i_status = RESERVED_PICTURE;
574 p_free_picture->i_matrix_coefficients = 1;
575 p_free_picture->i_width = i_width;
576 p_free_picture->i_height = i_height;
577 p_free_picture->i_chroma_width = i_chroma_width;
578 p_free_picture->i_display_horizontal_offset = 0;
579 p_free_picture->i_display_vertical_offset = 0;
580 p_free_picture->i_display_width = i_width;
581 p_free_picture->i_display_height = i_height;
582 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
583 p_free_picture->i_refcount = 0;
587 /* Memory allocation failed : set picture as empty */
588 p_free_picture->i_type = EMPTY_PICTURE;
589 p_free_picture->i_status = FREE_PICTURE;
590 p_free_picture = NULL;
591 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
595 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
597 vlc_mutex_unlock( &p_vout->picture_lock );
598 return( p_free_picture );
601 /* No free or destroyed picture could be found */
602 intf_DbgMsg( "warning: heap is full\n" );
603 vlc_mutex_unlock( &p_vout->picture_lock );
607 /******************************************************************************
608 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
609 ******************************************************************************
610 * This function frees a previously reserved picture or a permanent
611 * picture. It is meant to be used when the construction of a picture aborted.
612 * Note that the picture will be destroyed even if it is linked !
613 * This function does not need locking since reserved pictures are ignored by
615 ******************************************************************************/
616 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
619 /* Check if picture status is valid */
620 if( (p_pic->i_status != RESERVED_PICTURE) &&
621 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
622 (p_pic->i_status != RESERVED_DISP_PICTURE) )
624 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
628 p_pic->i_status = DESTROYED_PICTURE;
631 intf_DbgMsg("picture %p\n", p_pic);
635 /******************************************************************************
636 * vout_LinkPicture: increment reference counter of a picture
637 ******************************************************************************
638 * This function increment the reference counter of a picture in the video
639 * heap. It needs a lock since several producer threads can access the picture.
640 ******************************************************************************/
641 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
643 vlc_mutex_lock( &p_vout->picture_lock );
647 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
650 vlc_mutex_unlock( &p_vout->picture_lock );
653 /******************************************************************************
654 * vout_UnlinkPicture: decrement reference counter of a picture
655 ******************************************************************************
656 * This function decrement the reference counter of a picture in the video heap.
657 ******************************************************************************/
658 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
660 vlc_mutex_lock( &p_vout->picture_lock );
664 if( p_pic->i_refcount < 0 )
666 intf_DbgMsg("error: refcount < 0\n");
667 p_pic->i_refcount = 0;
671 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
673 p_pic->i_status = DESTROYED_PICTURE;
677 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
680 vlc_mutex_unlock( &p_vout->picture_lock );
683 /******************************************************************************
684 * vout_SetBuffers: set buffers adresses
685 ******************************************************************************
686 * This function is called by system drivers to set buffers video memory
688 ******************************************************************************/
689 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
691 /* No picture previously */
692 p_vout->p_buffer[0].i_pic_x = 0;
693 p_vout->p_buffer[0].i_pic_y = 0;
694 p_vout->p_buffer[0].i_pic_width = 0;
695 p_vout->p_buffer[0].i_pic_height = 0;
696 p_vout->p_buffer[1].i_pic_x = 0;
697 p_vout->p_buffer[1].i_pic_y = 0;
698 p_vout->p_buffer[1].i_pic_width = 0;
699 p_vout->p_buffer[1].i_pic_height = 0;
701 /* The first area covers all the screen */
702 p_vout->p_buffer[0].i_areas = 1;
703 p_vout->p_buffer[0].pi_area_begin[0] = 0;
704 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
705 p_vout->p_buffer[1].i_areas = 1;
706 p_vout->p_buffer[1].pi_area_begin[0] = 0;
707 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
710 p_vout->p_buffer[0].p_data = p_buf1;
711 p_vout->p_buffer[1].p_data = p_buf2;
714 /* following functions are local */
716 /******************************************************************************
717 * InitThread: initialize video output thread
718 ******************************************************************************
719 * This function is called from RunThread and performs the second step of the
720 * initialization. It returns 0 on success. Note that the thread's flag are not
721 * modified inside this function.
722 ******************************************************************************/
723 static int InitThread( vout_thread_t *p_vout )
727 *p_vout->pi_status = THREAD_START;
729 /* Initialize output method - this function issues its own error messages */
730 if( vout_SysInit( p_vout ) )
735 /* Initialize convertion tables and functions */
736 if( vout_InitYUV( p_vout ) )
738 intf_ErrMsg("error: can't allocate YUV translation tables\n");
742 /* Mark thread as running and return */
743 p_vout->b_active = 1;
744 *p_vout->pi_status = THREAD_READY;
745 intf_DbgMsg("thread ready\n");
749 /******************************************************************************
750 * RunThread: video output thread
751 ******************************************************************************
752 * Video output thread. This function does only returns when the thread is
753 * terminated. It handles the pictures arriving in the video heap and the
754 * display device events.
755 ******************************************************************************/
756 static void RunThread( vout_thread_t *p_vout)
758 int i_index; /* index in heap */
759 mtime_t current_date; /* current date */
760 mtime_t display_date; /* display date */
761 boolean_t b_display; /* display flag */
762 picture_t * p_pic; /* picture pointer */
763 subpicture_t * p_subpic; /* subpicture pointer */
768 p_vout->b_error = InitThread( p_vout );
769 if( p_vout->b_error )
771 DestroyThread( p_vout, THREAD_ERROR );
777 * Main loop - it is not executed if an error occured during
780 while( (!p_vout->b_die) && (!p_vout->b_error) )
782 /* Initialize loop variables */
786 current_date = mdate();
789 * Find the picture to display - this operation does not need lock,
790 * since only READY_PICTUREs are handled
792 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
794 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
796 (p_vout->p_picture[i_index].date < display_date) ) )
798 p_pic = &p_vout->p_picture[i_index];
799 display_date = p_pic->date;
806 /* Computes FPS rate */
807 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
809 if( display_date < current_date )
811 /* Picture is late: it will be destroyed and the thread will sleep and
812 * go to next picture */
813 vlc_mutex_lock( &p_vout->picture_lock );
814 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
815 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
816 vlc_mutex_unlock( &p_vout->picture_lock );
820 else if( display_date > current_date + VOUT_DISPLAY_DELAY )
822 /* A picture is ready to be rendered, but its rendering date is
823 * far from the current one so the thread will perform an empty loop
824 * as if no picture were found. The picture state is unchanged */
831 * Find the subpicture to display - this operation does not need lock, since
832 * only READY_SUBPICTURES are handled. If no picture has been selected,
833 * display_date will depend on the subpicture
838 * Perform rendering, sleep and display rendered picture
840 if( p_pic ) /* picture and perhaps subpicture */
842 b_display = p_vout->b_active;
843 p_vout->last_display_date = display_date;
847 /* Set picture dimensions and clear buffer */
848 SetBufferPicture( p_vout, p_pic );
850 /* Render picture and informations */
851 RenderPicture( p_vout, p_pic );
854 RenderPictureInfo( p_vout, p_pic );
855 RenderInfo( p_vout );
859 /* Remove picture from heap */
860 vlc_mutex_lock( &p_vout->picture_lock );
861 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
862 vlc_mutex_unlock( &p_vout->picture_lock );
864 /* Render interface and subpicture */
865 if( b_display && p_vout->b_interface )
867 RenderInterface( p_vout );
873 RenderSubPicture( p_vout, p_subpic );
876 /* Remove subpicture from heap */
877 vlc_mutex_lock( &p_vout->subpicture_lock );
878 p_subpic->i_status = DESTROYED_SUBPICTURE;
879 vlc_mutex_unlock( &p_vout->subpicture_lock );
883 else if( p_subpic ) /* subpicture alone */
885 b_display = p_vout->b_active;
886 p_vout->last_display_date = display_date;
891 SetBufferPicture( p_vout, NULL );
893 /* Render informations, interface and subpicture */
896 RenderInfo( p_vout );
898 if( p_vout->b_interface )
900 RenderInterface( p_vout );
902 RenderSubPicture( p_vout, p_subpic );
905 /* Remove subpicture from heap */
906 vlc_mutex_lock( &p_vout->subpicture_lock );
907 p_subpic->i_status = DESTROYED_SUBPICTURE;
908 vlc_mutex_unlock( &p_vout->subpicture_lock );
910 else if( p_vout->b_active ) /* idle or interface screen alone */
912 if( p_vout->b_interface && 0 /* && ?? intf_change */ )
914 /* Interface has changed, so a new rendering is required - force
915 * it by setting last idle date to 0 */
916 p_vout->last_idle_date = 0;
919 /* Render idle screen and update idle date, then render interface if
921 b_display = RenderIdle( p_vout );
924 p_vout->last_idle_date = current_date;
925 if( p_vout->b_interface )
927 RenderInterface( p_vout );
938 * Sleep, wake up and display rendered picture
942 /* Store render time */
943 p_vout->render_time = mdate() - current_date;
946 /* Give back change lock */
947 vlc_mutex_unlock( &p_vout->change_lock );
949 /* Sleep a while or until a given date */
950 if( display_date != 0 )
952 mwait( display_date );
956 msleep( VOUT_IDLE_SLEEP );
959 /* On awakening, take back lock and send immediately picture to display,
960 * then swap buffers */
961 vlc_mutex_lock( &p_vout->change_lock );
963 intf_DbgMsg( "picture %p, subpicture %p\n", p_pic, p_subpic );
965 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
967 vout_SysDisplay( p_vout );
968 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
972 * Check events and manage thread
974 if( vout_SysManage( p_vout ) | Manage( p_vout ) )
976 /* A fatal error occured, and the thread must terminate immediately,
977 * without displaying anything - setting b_error to 1 cause the
978 * immediate end of the main while() loop. */
984 * Error loop - wait until the thread destruction is requested
986 if( p_vout->b_error )
988 ErrorThread( p_vout );
993 DestroyThread( p_vout, THREAD_OVER );
994 intf_DbgMsg( "thread end\n" );
997 /******************************************************************************
998 * ErrorThread: RunThread() error loop
999 ******************************************************************************
1000 * This function is called when an error occured during thread main's loop. The
1001 * thread can still receive feed, but must be ready to terminate as soon as
1003 ******************************************************************************/
1004 static void ErrorThread( vout_thread_t *p_vout )
1006 /* Wait until a `die' order */
1008 while( !p_vout->b_die )
1011 msleep( VOUT_IDLE_SLEEP );
1015 /*******************************************************************************
1016 * EndThread: thread destruction
1017 *******************************************************************************
1018 * This function is called when the thread ends after a sucessfull
1019 * initialization. It frees all ressources allocated by InitThread.
1020 *******************************************************************************/
1021 static void EndThread( vout_thread_t *p_vout )
1023 int i_index; /* index in heap */
1027 *p_vout->pi_status = THREAD_END;
1029 /* Destroy all remaining pictures and subpictures */
1030 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1032 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1034 free( p_vout->p_picture[i_index].p_data );
1036 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1038 free( p_vout->p_subpicture[i_index].p_data );
1042 /* Destroy translation tables */
1043 vout_EndYUV( p_vout );
1044 vout_SysEnd( p_vout );
1047 /*******************************************************************************
1048 * DestroyThread: thread destruction
1049 *******************************************************************************
1050 * This function is called when the thread ends. It frees all ressources
1051 * allocated by CreateThread. Status is available at this stage.
1052 *******************************************************************************/
1053 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1055 int *pi_status; /* status adress */
1057 /* Store status adress */
1059 pi_status = p_vout->pi_status;
1061 /* Destroy thread structures allocated by Create and InitThread */
1062 vout_UnloadFont( p_vout->p_default_font );
1063 vout_UnloadFont( p_vout->p_large_font );
1064 vout_SysDestroy( p_vout );
1066 *pi_status = i_status;
1069 /*******************************************************************************
1070 * Print: print simple text on a picture
1071 *******************************************************************************
1072 * This function will print a simple text on the picture. It is designed to
1073 * print debugging or general informations.
1074 *******************************************************************************/
1075 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 )
1077 int i_text_height; /* total text height */
1078 int i_text_width; /* total text width */
1080 /* Update upper left coordinates according to alignment */
1081 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1082 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1084 /* Set area and print text */
1085 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1086 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1087 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1088 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1089 0xffffffff, 0x00000000, 0x00000000, 0, psz_text );
1093 /*******************************************************************************
1094 * SetBufferArea: activate an area in current buffer
1095 *******************************************************************************
1096 * This function is called when something is rendered on the current buffer.
1097 * It set the area as active and prepare it to be cleared on next rendering.
1098 * Pay attention to the fact that in this functions, i_h is in fact the end y
1099 * coordinate of the new area.
1100 *******************************************************************************/
1101 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1103 vout_buffer_t * p_buffer; /* current buffer */
1104 int i_area_begin, i_area_end; /* area vertical extension */
1105 int i_area, i_area_copy; /* area index */
1106 int i_area_shift; /* shift distance for areas */
1108 /* Choose buffer and modify h to end of area position */
1109 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1113 * Remove part of the area which is inside the picture - this is done
1114 * by calling again SetBufferArea with the correct areas dimensions.
1116 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1118 i_area_begin = p_buffer->i_pic_y;
1119 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1121 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1122 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1123 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1125 /* Keep the stripe above the picture, if any */
1126 if( i_y < i_area_begin )
1128 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1130 /* Keep the stripe below the picture, if any */
1131 if( i_h > i_area_end )
1133 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1139 /* Skip some extensions until interesting areas */
1141 (i_area < p_buffer->i_areas) &&
1142 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1148 if( i_area == p_buffer->i_areas )
1150 /* New area is below all existing ones: just add it at the end of the
1151 * array, if possible - else, append it to the last one */
1152 if( i_area < VOUT_MAX_AREAS )
1154 p_buffer->pi_area_begin[i_area] = i_y;
1155 p_buffer->pi_area_end[i_area] = i_h;
1156 p_buffer->i_areas++;
1161 intf_DbgMsg("areas overflow\n");
1163 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1168 i_area_begin = p_buffer->pi_area_begin[i_area];
1169 i_area_end = p_buffer->pi_area_end[i_area];
1171 if( i_y < i_area_begin )
1173 if( i_h >= i_area_begin - 1 )
1175 /* Extend area above */
1176 p_buffer->pi_area_begin[i_area] = i_y;
1180 /* Create a new area above : merge last area if overflow, then
1181 * move all old areas down */
1182 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1185 intf_DbgMsg("areas overflow\n");
1187 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1191 p_buffer->i_areas++;
1193 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1195 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1196 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1198 p_buffer->pi_area_begin[i_area] = i_y;
1199 p_buffer->pi_area_end[i_area] = i_h;
1203 if( i_h > i_area_end )
1205 /* Find further areas which can be merged with the new one */
1206 for( i_area_copy = i_area + 1;
1207 (i_area_copy < p_buffer->i_areas) &&
1208 (p_buffer->pi_area_begin[i_area] <= i_h);
1215 if( i_area_copy != i_area )
1217 /* Merge with last possible areas */
1218 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1220 /* Shift lower areas upward */
1221 i_area_shift = i_area_copy - i_area;
1222 p_buffer->i_areas -= i_area_shift;
1223 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1225 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1226 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1231 /* Extend area below */
1232 p_buffer->pi_area_end[i_area] = i_h;
1238 /*******************************************************************************
1239 * SetBufferPicture: clear buffer and set picture area
1240 *******************************************************************************
1241 * This function is called before any rendering. It clears the current
1242 * rendering buffer and set the new picture area. If the picture pointer is
1243 * NULL, then no picture area is defined. Floating operations are avoided since
1244 * some MMX calculations may follow.
1245 *******************************************************************************/
1246 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1248 vout_buffer_t * p_buffer; /* current buffer */
1249 int i_pic_x, i_pic_y; /* picture position */
1250 int i_pic_width, i_pic_height; /* picture dimensions */
1251 int i_old_pic_y, i_old_pic_height; /* old picture area */
1252 int i_vout_width, i_vout_height; /* display dimensions */
1253 int i_area; /* area index */
1254 int i_data_index; /* area data index */
1255 int i_data_size; /* area data size, in 256 bytes blocs */
1256 u64 * p_data; /* area data */
1258 /* Choose buffer and set display dimensions */
1259 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1260 i_vout_width = p_vout->i_width;
1261 i_vout_height = p_vout->i_height;
1264 * Computes new picture size
1268 /* Try horizontal scaling first */
1269 i_pic_width = ( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1270 i_vout_width : p_pic->i_width;
1271 i_pic_width = i_pic_width;
1272 switch( p_pic->i_aspect_ratio )
1274 case AR_3_4_PICTURE:
1275 i_pic_height = i_pic_width * 3 / 4;
1277 case AR_16_9_PICTURE:
1278 i_pic_height = i_pic_width * 9 / 16;
1280 case AR_221_1_PICTURE:
1281 i_pic_height = i_pic_width * 100 / 221;
1283 case AR_SQUARE_PICTURE:
1285 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1289 /* If picture dimensions using horizontal scaling are too large, use
1290 * vertical scaling */
1291 if( i_pic_height > i_vout_height )
1293 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1294 i_vout_height : p_pic->i_height;
1295 switch( p_pic->i_aspect_ratio )
1297 case AR_3_4_PICTURE:
1298 i_pic_width = i_pic_height * 4 / 3;
1300 case AR_16_9_PICTURE:
1301 i_pic_width = i_pic_height * 16 / 9;
1303 case AR_221_1_PICTURE:
1304 i_pic_width = i_pic_height * 221 / 100;
1306 case AR_SQUARE_PICTURE:
1308 i_pic_width = p_pic->i_width * i_pic_height / p_pic->i_height;
1311 i_pic_width = i_pic_width;
1314 /* Set picture position */
1315 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1316 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1320 /* No picture: size is 0 */
1328 * Set new picture size - if is is smaller than the previous one, clear
1329 * around it. Since picture are centered, only their size is tested.
1331 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1333 i_old_pic_y = p_buffer->i_pic_y;
1334 i_old_pic_height = p_buffer->i_pic_height;
1335 p_buffer->i_pic_x = i_pic_x;
1336 p_buffer->i_pic_y = i_pic_y;
1337 p_buffer->i_pic_width = i_pic_width;
1338 p_buffer->i_pic_height = i_pic_height;
1339 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1343 p_buffer->i_pic_x = i_pic_x;
1344 p_buffer->i_pic_y = i_pic_y;
1345 p_buffer->i_pic_width = i_pic_width;
1346 p_buffer->i_pic_height = i_pic_height;
1352 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1355 intf_DbgMsg("clearing picture %p area: %d-%d\n", p_pic,
1356 p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area]);
1358 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1359 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) *
1360 p_vout->i_bytes_per_line / 256;
1361 for( i_data_index = 0; i_data_index < i_data_size; i_data_index++ )
1363 /* Clear 256 bytes block */
1364 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1365 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1366 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1367 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1368 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
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;
1373 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) *
1374 p_vout->i_bytes_per_line % 256 / 4;
1375 for( i_data_index = 0; i_data_index < i_data_size; i_data_index++ )
1377 /* Clear remaining 4 bytes blocks */
1385 p_buffer->i_areas = 0;
1389 * In DEBUG_VIDEO_MODE, draw white pixels at the beginning and the end of
1390 * the picture area. These pixels should not be erased by rendering functions,
1391 * otherwise segmentation fault is menacing !
1395 *(u16*)(p_buffer->p_data + p_vout->i_bytes_per_line * i_pic_y +
1396 p_vout->i_bytes_per_pixel * (i_pic_x - 1)) = 0xffff;
1400 *(u16*)(p_buffer->p_data + p_vout->i_bytes_per_line * (i_pic_y - 1) +
1401 p_vout->i_bytes_per_pixel * i_pic_x ) = 0xffff;
1403 if( i_pic_x + i_pic_width < p_vout->i_width )
1405 *(u16*)(p_buffer->p_data + p_vout->i_bytes_per_line * (i_pic_y + i_pic_height - 1) +
1406 p_vout->i_bytes_per_pixel * (i_pic_x + i_pic_width) ) = 0xffff;
1408 if( i_pic_y + i_pic_height < p_vout->i_height )
1410 *(u16*)(p_buffer->p_data + p_vout->i_bytes_per_line * (i_pic_y + i_pic_height) +
1411 p_vout->i_bytes_per_pixel * (i_pic_x + i_pic_width - 1) ) = 0xffff;
1416 /******************************************************************************
1417 * RenderPicture: render a picture
1418 ******************************************************************************
1419 * This function convert a picture from a video heap to a pixel-encoded image
1420 * and copy it to the current rendering buffer. No lock is required, since the
1421 * rendered picture has been determined as existant, and will only be destroyed
1422 * by the vout thread later.
1423 ******************************************************************************/
1424 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1426 vout_buffer_t * p_buffer; /* rendering buffer */
1427 byte_t * p_pic_data; /* convertion destination */
1429 /* Get and set rendering informations */
1430 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1431 p_pic_data = p_buffer->p_data +
1432 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1433 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1436 * Choose appropriate rendering function and render picture
1438 switch( p_pic->i_type )
1440 case YUV_420_PICTURE:
1441 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1442 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1443 p_pic->i_width, p_pic->i_height,
1444 p_buffer->i_pic_width, p_buffer->i_pic_height,
1445 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1446 p_pic->i_matrix_coefficients );
1448 case YUV_422_PICTURE:
1449 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1450 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1451 p_pic->i_width, p_pic->i_height,
1452 p_buffer->i_pic_width, p_buffer->i_pic_height,
1453 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1454 p_pic->i_matrix_coefficients );
1456 case YUV_444_PICTURE:
1457 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1458 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1459 p_pic->i_width, p_pic->i_height,
1460 p_buffer->i_pic_width, p_buffer->i_pic_height,
1461 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1462 p_pic->i_matrix_coefficients );
1466 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1472 /******************************************************************************
1473 * RenderPictureInfo: print additionnal informations on a picture
1474 ******************************************************************************
1475 * This function will print informations such as fps and other picture
1476 * dependant informations.
1477 ******************************************************************************/
1478 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1480 #if defined(STATS) || defined(DEBUG)
1481 char psz_buffer[256]; /* string buffer */
1486 * Print FPS rate in upper right corner
1488 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1490 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1491 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1492 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1493 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1497 * Print frames count and loop time in upper left corner
1499 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1500 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1501 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1506 * Print picture information in lower right corner
1508 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1509 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1510 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1511 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1512 p_pic->i_width, p_pic->i_height,
1513 p_pic->i_display_width, p_pic->i_display_height,
1514 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1515 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1516 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1517 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1518 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1519 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1520 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1521 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1522 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1523 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1527 /******************************************************************************
1528 * RenderIdle: render idle picture
1529 ******************************************************************************
1530 * This function will print something on the screen. It will return 0 if
1531 * nothing has been rendered, or 1 if something has been changed on the screen.
1532 * Note that if you absolutely want something to be printed, you will have
1533 * to force it by setting the last idle date to 0.
1534 * Unlike other rendering functions, this one calls the SetBufferPicture
1535 * function when needed.
1536 ******************************************************************************/
1537 static int RenderIdle( vout_thread_t *p_vout )
1539 int i_x = 0, i_y = 0; /* text position */
1540 int i_width, i_height; /* text size */
1541 mtime_t current_date; /* current date */
1542 const char *psz_text = "no stream"; /* text to display */
1545 current_date = mdate();
1546 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1547 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1549 SetBufferPicture( p_vout, NULL );
1550 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1551 &i_width, &i_height );
1552 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1554 vout_Print( p_vout->p_large_font,
1555 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1556 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1557 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1558 0xffffffff, 0x33333333, 0,
1559 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1560 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1567 /******************************************************************************
1568 * RenderInfo: render additionnal informations
1569 ******************************************************************************
1570 * This function render informations which do not depend of the current picture
1572 ******************************************************************************/
1573 static void RenderInfo( vout_thread_t *p_vout )
1576 char psz_buffer[256]; /* string buffer */
1577 int i_ready_pic = 0; /* ready pictures */
1578 int i_reserved_pic = 0; /* reserved pictures */
1579 int i_picture; /* picture index */
1584 * Print thread state in lower left corner
1586 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1588 switch( p_vout->p_picture[i_picture].i_status )
1590 case RESERVED_PICTURE:
1591 case RESERVED_DATED_PICTURE:
1592 case RESERVED_DISP_PICTURE:
1600 sprintf( psz_buffer, "pic: %d/%d/%d",
1601 i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1602 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1606 /*******************************************************************************
1607 * RenderSubPicture: render a subpicture
1608 *******************************************************************************
1609 * This function render a sub picture unit.
1610 *******************************************************************************/
1611 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1613 p_vout_font_t p_font; /* text font */
1614 int i_width, i_height; /* subpicture dimensions */
1616 switch( p_subpic->i_type )
1618 case TEXT_SUBPICTURE: /* single line text */
1619 /* Select default font if not specified */
1620 p_font = p_subpic->type.text.p_font;
1621 if( p_font == NULL )
1623 p_font = p_vout->p_default_font;
1626 /* Computes text size (width and height fields are ignored) and print it */
1627 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1628 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1629 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1631 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1632 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1633 p_subpic->i_y * p_vout->i_bytes_per_line,
1634 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1635 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1636 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1638 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1644 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1649 /*******************************************************************************
1650 * RenderInterface: render the interface
1651 *******************************************************************************
1652 * This function render the interface, if any.
1653 *******************************************************************************/
1654 static void RenderInterface( vout_thread_t *p_vout )
1656 int i_height, i_text_height; /* total and text height */
1657 int i_width_1, i_width_2; /* text width */
1658 int i_byte; /* byte index */
1659 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1660 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1663 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1664 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1665 i_height += i_text_height;
1667 /* Render background - effective background color will depend of the screen
1669 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1670 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1673 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = 0x33;
1676 /* Render text, if not larger than screen */
1677 if( i_width_1 < p_vout->i_width )
1679 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1680 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1681 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1682 0xffffffff, 0x00000000, 0x00000000,
1683 OUTLINED_TEXT, psz_text_1 );
1685 if( i_width_2 < p_vout->i_width )
1687 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1688 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1689 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1690 0xffffffff, 0x00000000, 0x00000000,
1691 OUTLINED_TEXT, psz_text_2 );
1694 /* Activate modified area */
1695 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1698 /******************************************************************************
1699 * Manage: manage thread
1700 ******************************************************************************
1701 * This function will handle changes in thread configuration.
1702 ******************************************************************************/
1703 static int Manage( vout_thread_t *p_vout )
1706 if( p_vout->i_changes )
1708 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1709 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1713 /* On gamma or grayscale change, rebuild tables */
1714 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1717 if( vout_ResetYUV( p_vout ) )
1719 intf_ErrMsg("error: can't rebuild convertion tables\n");
1724 /* Clear changes flags which does not need management or have been
1726 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1727 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
1728 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
1730 /* Detect unauthorized changes */
1731 if( p_vout->i_changes )
1733 /* Some changes were not acknowledged by vout_SysManage or this function,
1734 * it means they should not be authorized */
1735 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
1742 /******************************************************************************
1743 * Align: align a subpicture in the screen
1744 ******************************************************************************
1745 * This function is used for rendering text or subpictures. It returns non 0
1746 * it the final aera is not fully included in display area. Return coordinates
1748 ******************************************************************************/
1749 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
1750 int i_width, int i_height, int i_h_align, int i_v_align )
1752 /* Align horizontally */
1756 *pi_x -= i_width / 2;
1759 *pi_x += (p_vout->i_width - i_width) / 2;
1765 *pi_x += p_vout->i_width - i_width;
1769 /* Align vertically */
1773 *pi_y -= i_height / 2;
1776 *pi_y += (p_vout->i_height - i_height) / 2;
1782 *pi_y += p_vout->i_height - i_height;
1784 case SUBTITLE_RALIGN:
1785 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
1786 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
1790 /* Return non 0 if clipping failed */
1791 return( (*pi_x < 0) || (*pi_y < 0) ||
1792 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );