1 /******************************************************************************
2 * video_output.c : video output thread
4 ******************************************************************************
5 * This module describes the programming interface for video output threads.
6 * It includes functions allowing to open a new thread, send pictures to a
7 * thread, and destroy a previously oppenned video output thread.
8 ******************************************************************************/
10 /******************************************************************************
12 ******************************************************************************/
21 #include "vlc_thread.h"
23 #include "video_output.h"
24 #include "video_text.h"
25 #include "video_sys.h"
26 #include "video_yuv.h"
30 /******************************************************************************
32 ******************************************************************************/
33 static int BinaryLog ( u32 i );
34 static void MaskToShift ( int *pi_left, int *pi_right, u32 i_mask );
35 static int InitThread ( vout_thread_t *p_vout );
36 static void RunThread ( vout_thread_t *p_vout );
37 static void ErrorThread ( vout_thread_t *p_vout );
38 static void EndThread ( vout_thread_t *p_vout );
39 static void DestroyThread ( vout_thread_t *p_vout, int i_status );
40 static void Print ( vout_thread_t *p_vout, int i_x, int i_y,
41 int i_h_align, int i_v_align,
42 unsigned char *psz_text );
43 static void SetBufferArea ( vout_thread_t *p_vout, int i_x, int i_y,
45 static void SetBufferPicture ( vout_thread_t *p_vout, picture_t *p_pic );
46 static void RenderPicture ( vout_thread_t *p_vout, picture_t *p_pic );
47 static void RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
48 static void RenderSubPicture ( vout_thread_t *p_vout,
49 subpicture_t *p_subpic );
50 static void RenderInterface ( vout_thread_t *p_vout );
51 static int RenderIdle ( vout_thread_t *p_vout );
52 static void RenderInfo ( vout_thread_t *p_vout );
53 static int Manage ( vout_thread_t *p_vout );
54 static int Align ( vout_thread_t *p_vout, int *pi_x,
55 int *pi_y, int i_width, int i_height,
56 int i_h_align, int i_v_align );
57 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
58 u16 *green, u16 *blue, u16 *transp );
60 /******************************************************************************
61 * vout_CreateThread: creates a new video output thread
62 ******************************************************************************
63 * This function creates a new video output thread, and returns a pointer
64 * to its description. On error, it returns NULL.
65 * If pi_status is NULL, then the function will block until the thread is ready.
66 * If not, it will be updated using one of the THREAD_* constants.
67 ******************************************************************************/
68 vout_thread_t * vout_CreateThread ( char *psz_display, int i_root_window,
69 int i_width, int i_height, int *pi_status )
71 vout_thread_t * p_vout; /* thread descriptor */
72 int i_status; /* thread status */
73 int i_index; /* index for array initialization */
75 /* Allocate descriptor */
77 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
80 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
84 /* Initialize thread properties - thread id and locks will be initialized
89 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
90 *p_vout->pi_status = THREAD_CREATE;
92 /* Initialize some fields used by the system-dependant method - these fields will
93 * probably be modified by the method, and are only preferences */
94 p_vout->i_changes = 0;
95 p_vout->i_width = i_width;
96 p_vout->i_height = i_height;
97 p_vout->i_bytes_per_line = i_width * 2;
98 p_vout->i_screen_depth = 15;
99 p_vout->i_bytes_per_pixel = 2;
100 p_vout->f_gamma = VOUT_GAMMA;
102 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
104 p_vout->b_interface = 0;
107 p_vout->p_set_palette = SetPalette;
109 intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
110 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
111 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
113 /* Initialize idle screen */
114 p_vout->last_display_date = mdate();
115 p_vout->last_idle_date = 0;
118 /* Initialize statistics fields */
119 p_vout->render_time = 0;
120 p_vout->c_fps_samples = 0;
123 /* Initialize buffer index */
124 p_vout->i_buffer_index = 0;
126 /* Initialize pictures and subpictures - translation tables and functions
127 * will be initialized later in InitThread */
128 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
130 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
131 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
132 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
133 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
136 /* Create and initialize system-dependant method - this function issues its
137 * own error messages */
138 if( vout_SysCreate( p_vout, psz_display, i_root_window ) )
143 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
144 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
145 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
146 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
148 /* Calculate shifts from system-updated masks */
149 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
150 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
151 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
153 /* Set some useful colors */
154 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
155 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
156 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
157 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
159 /* Load fonts - fonts must be initialized after the systme method since
160 * they may be dependant of screen depth and other thread properties */
161 p_vout->p_default_font = vout_LoadFont( VOUT_DEFAULT_FONT );
162 if( p_vout->p_default_font == NULL )
164 vout_SysDestroy( p_vout );
168 p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
169 if( p_vout->p_large_font == NULL )
171 vout_UnloadFont( p_vout->p_default_font );
172 vout_SysDestroy( p_vout );
177 /* Create thread and set locks */
178 vlc_mutex_init( &p_vout->picture_lock );
179 vlc_mutex_init( &p_vout->subpicture_lock );
180 vlc_mutex_init( &p_vout->change_lock );
181 vlc_mutex_lock( &p_vout->change_lock );
182 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
184 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
185 vout_UnloadFont( p_vout->p_default_font );
186 vout_UnloadFont( p_vout->p_large_font );
187 vout_SysDestroy( p_vout );
192 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
193 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
195 /* If status is NULL, wait until the thread is created */
196 if( pi_status == NULL )
200 msleep( THREAD_SLEEP );
201 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
202 && (i_status != THREAD_FATAL) );
203 if( i_status != THREAD_READY )
211 /******************************************************************************
212 * vout_DestroyThread: destroys a previously created thread
213 ******************************************************************************
214 * Destroy a terminated thread.
215 * The function will request a destruction of the specified thread. If pi_error
216 * is NULL, it will return once the thread is destroyed. Else, it will be
217 * update using one of the THREAD_* constants.
218 ******************************************************************************/
219 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
221 int i_status; /* thread status */
225 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
226 *p_vout->pi_status = THREAD_DESTROY;
228 /* Request thread destruction */
231 /* If status is NULL, wait until thread has been destroyed */
232 if( pi_status == NULL )
236 msleep( THREAD_SLEEP );
237 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
238 && (i_status != THREAD_FATAL) );
242 /******************************************************************************
243 * vout_DisplaySubPicture: display a subpicture unit
244 ******************************************************************************
245 * Remove the reservation flag of an subpicture, which will cause it to be ready
246 * for display. The picture does not need to be locked, since it is ignored by
247 * the output thread if is reserved.
248 ******************************************************************************/
249 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
252 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
253 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
257 /* Check if status is valid */
258 if( p_subpic->i_status != RESERVED_SUBPICTURE )
260 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
261 p_subpic->i_status );
265 /* Remove reservation flag */
266 p_subpic->i_status = READY_SUBPICTURE;
269 /* Send subpicture informations */
270 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
271 p_subpic, p_subpic->i_type,
272 mstrtime( psz_begin_date, p_subpic->begin_date ),
273 mstrtime( psz_end_date, p_subpic->end_date ) );
277 /******************************************************************************
278 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
279 ******************************************************************************
280 * This function create a reserved subpicture in the video output heap.
281 * A null pointer is returned if the function fails. This method provides an
282 * already allocated zone of memory in the spu data fields. It needs locking
283 * since several pictures can be created by several producers threads.
284 ******************************************************************************/
285 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
288 int i_subpic; /* subpicture index */
289 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
290 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
293 vlc_mutex_lock( &p_vout->subpicture_lock );
296 * Look for an empty place
298 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
300 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
302 /* Subpicture is marked for destruction, but is still allocated */
303 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
304 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
306 /* Memory size do match or is smaller : memory will not be reallocated,
307 * and function can end immediately - this is the best possible case,
308 * since no memory allocation needs to be done */
309 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
311 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
312 &p_vout->p_subpicture[i_subpic] );
314 vlc_mutex_unlock( &p_vout->subpicture_lock );
315 return( &p_vout->p_subpicture[i_subpic] );
317 else if( p_destroyed_subpic == NULL )
319 /* Memory size do not match, but subpicture index will be kept in
320 * case no other place are left */
321 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
324 else if( (p_free_subpic == NULL) &&
325 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
327 /* Subpicture is empty and ready for allocation */
328 p_free_subpic = &p_vout->p_subpicture[i_subpic];
332 /* If no free subpicture is available, use a destroyed subpicture */
333 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
335 /* No free subpicture or matching destroyed subpicture has been found, but
336 * a destroyed subpicture is still avalaible */
337 free( p_destroyed_subpic->p_data );
338 p_free_subpic = p_destroyed_subpic;
344 if( p_free_subpic != NULL )
346 /* Allocate memory */
349 case TEXT_SUBPICTURE: /* text subpicture */
350 p_free_subpic->p_data = malloc( i_size + 1 );
354 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
355 p_free_subpic->p_data = NULL;
360 if( p_free_subpic->p_data != NULL )
361 { /* Copy subpicture informations, set some default values */
362 p_free_subpic->i_type = i_type;
363 p_free_subpic->i_status = RESERVED_SUBPICTURE;
364 p_free_subpic->i_size = i_size;
365 p_free_subpic->i_x = 0;
366 p_free_subpic->i_y = 0;
367 p_free_subpic->i_width = 0;
368 p_free_subpic->i_height = 0;
369 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
370 p_free_subpic->i_vertical_align = CENTER_RALIGN;
374 /* Memory allocation failed : set subpicture as empty */
375 p_free_subpic->i_type = EMPTY_SUBPICTURE;
376 p_free_subpic->i_status = FREE_SUBPICTURE;
377 p_free_subpic = NULL;
378 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
382 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
384 vlc_mutex_unlock( &p_vout->subpicture_lock );
385 return( p_free_subpic );
388 /* No free or destroyed subpicture could be found */
389 intf_DbgMsg( "warning: heap is full\n" );
390 vlc_mutex_unlock( &p_vout->subpicture_lock );
394 /******************************************************************************
395 * vout_DestroySubPicture: remove a subpicture from the heap
396 ******************************************************************************
397 * This function frees a previously reserved subpicture.
398 * It is meant to be used when the construction of a picture aborted.
399 * This function does not need locking since reserved subpictures are ignored
400 * by the output thread.
401 ******************************************************************************/
402 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
405 /* Check if status is valid */
406 if( p_subpic->i_status != RESERVED_SUBPICTURE )
408 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
409 p_subpic, p_subpic->i_status );
413 p_subpic->i_status = DESTROYED_SUBPICTURE;
416 intf_DbgMsg("subpicture %p\n", p_subpic);
420 /*******************************************************************************
421 * vout_DisplayPicture: display a picture
422 *******************************************************************************
423 * Remove the reservation flag of a picture, which will cause it to be ready for
424 * display. The picture won't be displayed until vout_DatePicture has been
426 *******************************************************************************/
427 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
429 vlc_mutex_lock( &p_vout->picture_lock );
430 switch( p_pic->i_status )
432 case RESERVED_PICTURE:
433 p_pic->i_status = RESERVED_DISP_PICTURE;
435 case RESERVED_DATED_PICTURE:
436 p_pic->i_status = READY_PICTURE;
440 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
446 intf_DbgMsg("picture %p\n", p_pic);
448 vlc_mutex_unlock( &p_vout->picture_lock );
451 /*******************************************************************************
452 * vout_DatePicture: date a picture
453 *******************************************************************************
454 * Remove the reservation flag of a picture, which will cause it to be ready for
455 * display. The picture won't be displayed until vout_DisplayPicture has been
457 *******************************************************************************/
458 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
461 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
464 vlc_mutex_lock( &p_vout->picture_lock );
466 switch( p_pic->i_status )
468 case RESERVED_PICTURE:
469 p_pic->i_status = RESERVED_DATED_PICTURE;
471 case RESERVED_DISP_PICTURE:
472 p_pic->i_status = READY_PICTURE;
476 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
482 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
484 vlc_mutex_unlock( &p_vout->picture_lock );
487 /******************************************************************************
488 * vout_CreatePicture: allocate a picture in the video output heap.
489 ******************************************************************************
490 * This function create a reserved image in the video output heap.
491 * A null pointer is returned if the function fails. This method provides an
492 * already allocated zone of memory in the picture data fields. It needs locking
493 * since several pictures can be created by several producers threads.
494 ******************************************************************************/
495 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
496 int i_width, int i_height )
498 int i_picture; /* picture index */
499 int i_chroma_width = 0; /* chroma width */
500 picture_t * p_free_picture = NULL; /* first free picture */
501 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
504 vlc_mutex_lock( &p_vout->picture_lock );
507 * Look for an empty place
509 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
511 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
513 /* Picture is marked for destruction, but is still allocated - note
514 * that if width and type are the same for two pictures, chroma_width
515 * should also be the same */
516 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
517 (p_vout->p_picture[i_picture].i_height == i_height) &&
518 (p_vout->p_picture[i_picture].i_width == i_width) )
520 /* Memory size do match : memory will not be reallocated, and function
521 * can end immediately - this is the best possible case, since no
522 * memory allocation needs to be done */
523 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
525 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
526 &p_vout->p_picture[i_picture] );
528 vlc_mutex_unlock( &p_vout->picture_lock );
529 return( &p_vout->p_picture[i_picture] );
531 else if( p_destroyed_picture == NULL )
533 /* Memory size do not match, but picture index will be kept in
534 * case no other place are left */
535 p_destroyed_picture = &p_vout->p_picture[i_picture];
538 else if( (p_free_picture == NULL) &&
539 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
541 /* Picture is empty and ready for allocation */
542 p_free_picture = &p_vout->p_picture[i_picture];
546 /* If no free picture is available, use a destroyed picture */
547 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
549 /* No free picture or matching destroyed picture has been found, but
550 * a destroyed picture is still avalaible */
551 free( p_destroyed_picture->p_data );
552 p_free_picture = p_destroyed_picture;
558 if( p_free_picture != NULL )
560 /* Allocate memory */
563 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
564 i_chroma_width = i_width / 2;
565 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
566 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
567 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
568 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
570 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
571 i_chroma_width = i_width / 2;
572 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
573 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
574 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
575 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
577 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
578 i_chroma_width = i_width;
579 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
580 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
581 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
582 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
586 intf_DbgMsg("error: unknown picture type %d\n", i_type );
587 p_free_picture->p_data = NULL;
592 if( p_free_picture->p_data != NULL )
594 /* Copy picture informations, set some default values */
595 p_free_picture->i_type = i_type;
596 p_free_picture->i_status = RESERVED_PICTURE;
597 p_free_picture->i_matrix_coefficients = 1;
598 p_free_picture->i_width = i_width;
599 p_free_picture->i_height = i_height;
600 p_free_picture->i_chroma_width = i_chroma_width;
601 p_free_picture->i_display_horizontal_offset = 0;
602 p_free_picture->i_display_vertical_offset = 0;
603 p_free_picture->i_display_width = i_width;
604 p_free_picture->i_display_height = i_height;
605 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
606 p_free_picture->i_refcount = 0;
610 /* Memory allocation failed : set picture as empty */
611 p_free_picture->i_type = EMPTY_PICTURE;
612 p_free_picture->i_status = FREE_PICTURE;
613 p_free_picture = NULL;
614 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
618 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
620 vlc_mutex_unlock( &p_vout->picture_lock );
621 return( p_free_picture );
624 /* No free or destroyed picture could be found */
625 intf_DbgMsg( "warning: heap is full\n" );
626 vlc_mutex_unlock( &p_vout->picture_lock );
630 /******************************************************************************
631 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
632 ******************************************************************************
633 * This function frees a previously reserved picture or a permanent
634 * picture. It is meant to be used when the construction of a picture aborted.
635 * Note that the picture will be destroyed even if it is linked !
636 * This function does not need locking since reserved pictures are ignored by
638 ******************************************************************************/
639 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
642 /* Check if picture status is valid */
643 if( (p_pic->i_status != RESERVED_PICTURE) &&
644 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
645 (p_pic->i_status != RESERVED_DISP_PICTURE) )
647 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
651 p_pic->i_status = DESTROYED_PICTURE;
654 intf_DbgMsg("picture %p\n", p_pic);
658 /******************************************************************************
659 * vout_LinkPicture: increment reference counter of a picture
660 ******************************************************************************
661 * This function increment the reference counter of a picture in the video
662 * heap. It needs a lock since several producer threads can access the picture.
663 ******************************************************************************/
664 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
666 vlc_mutex_lock( &p_vout->picture_lock );
670 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
673 vlc_mutex_unlock( &p_vout->picture_lock );
676 /******************************************************************************
677 * vout_UnlinkPicture: decrement reference counter of a picture
678 ******************************************************************************
679 * This function decrement the reference counter of a picture in the video heap.
680 ******************************************************************************/
681 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
683 vlc_mutex_lock( &p_vout->picture_lock );
687 if( p_pic->i_refcount < 0 )
689 intf_DbgMsg("error: refcount < 0\n");
690 p_pic->i_refcount = 0;
694 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
696 p_pic->i_status = DESTROYED_PICTURE;
700 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
703 vlc_mutex_unlock( &p_vout->picture_lock );
706 /******************************************************************************
707 * vout_SetBuffers: set buffers adresses
708 ******************************************************************************
709 * This function is called by system drivers to set buffers video memory
711 ******************************************************************************/
712 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
714 /* No picture previously */
715 p_vout->p_buffer[0].i_pic_x = 0;
716 p_vout->p_buffer[0].i_pic_y = 0;
717 p_vout->p_buffer[0].i_pic_width = 0;
718 p_vout->p_buffer[0].i_pic_height = 0;
719 p_vout->p_buffer[1].i_pic_x = 0;
720 p_vout->p_buffer[1].i_pic_y = 0;
721 p_vout->p_buffer[1].i_pic_width = 0;
722 p_vout->p_buffer[1].i_pic_height = 0;
724 /* The first area covers all the screen */
725 p_vout->p_buffer[0].i_areas = 1;
726 p_vout->p_buffer[0].pi_area_begin[0] = 0;
727 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
728 p_vout->p_buffer[1].i_areas = 1;
729 p_vout->p_buffer[1].pi_area_begin[0] = 0;
730 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
733 p_vout->p_buffer[0].p_data = p_buf1;
734 p_vout->p_buffer[1].p_data = p_buf2;
737 /*****************************************************************************
738 * vout_Pixel2RGB: return red, green and blue from pixel value
739 *****************************************************************************
740 * Return color values, in 0-255 range, of the decomposition of a pixel. This
741 * is a slow routine and should only be used for initialization phase.
742 *****************************************************************************/
743 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
745 *pi_red = i_pixel & p_vout->i_red_mask;
746 *pi_green = i_pixel & p_vout->i_green_mask;
747 *pi_blue = i_pixel & p_vout->i_blue_mask;
750 /* following functions are local */
752 /*****************************************************************************
753 * BinaryLog: computes the base 2 log of a binary value
754 *****************************************************************************
755 * This functions is used by MaskToShift, to get a bit index from a binary
757 *****************************************************************************/
758 static int BinaryLog(u32 i)
783 if (i != ((u32)1 << i_log))
785 intf_ErrMsg("internal error: binary log overflow\n");
791 /*****************************************************************************
792 * MaskToShift: transform a color mask into right and left shifts
793 *****************************************************************************
794 * This function is used for obtaining color shifts from masks.
795 *****************************************************************************/
796 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
798 u32 i_low, i_high; /* lower hand higher bits of the mask */
801 i_low = i_mask & (- i_mask); /* lower bit of the mask */
802 i_high = i_mask + i_low; /* higher bit of the mask */
804 /* Transform bits into an index */
805 i_low = BinaryLog (i_low);
806 i_high = BinaryLog (i_high);
808 /* Update pointers and return */
810 *pi_right = (8 - i_high + i_low);
813 /******************************************************************************
814 * InitThread: initialize video output thread
815 ******************************************************************************
816 * This function is called from RunThread and performs the second step of the
817 * initialization. It returns 0 on success. Note that the thread's flag are not
818 * modified inside this function.
819 ******************************************************************************/
820 static int InitThread( vout_thread_t *p_vout )
824 *p_vout->pi_status = THREAD_START;
826 /* Initialize output method - this function issues its own error messages */
827 if( vout_SysInit( p_vout ) )
832 /* Initialize convertion tables and functions */
833 if( vout_InitYUV( p_vout ) )
835 intf_ErrMsg("error: can't allocate YUV translation tables\n");
839 /* Mark thread as running and return */
840 p_vout->b_active = 1;
841 *p_vout->pi_status = THREAD_READY;
842 intf_DbgMsg("thread ready\n");
846 /******************************************************************************
847 * RunThread: video output thread
848 ******************************************************************************
849 * Video output thread. This function does only returns when the thread is
850 * terminated. It handles the pictures arriving in the video heap and the
851 * display device events.
852 ******************************************************************************/
853 static void RunThread( vout_thread_t *p_vout)
855 int i_index; /* index in heap */
856 mtime_t current_date; /* current date */
857 mtime_t display_date; /* display date */
858 boolean_t b_display; /* display flag */
859 picture_t * p_pic; /* picture pointer */
860 subpicture_t * p_subpic; /* subpicture pointer */
865 p_vout->b_error = InitThread( p_vout );
866 if( p_vout->b_error )
868 DestroyThread( p_vout, THREAD_ERROR );
874 * Main loop - it is not executed if an error occured during
877 while( (!p_vout->b_die) && (!p_vout->b_error) )
879 /* Initialize loop variables */
883 current_date = mdate();
886 * Find the picture to display - this operation does not need lock,
887 * since only READY_PICTUREs are handled
889 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
891 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
893 (p_vout->p_picture[i_index].date < display_date) ) )
895 p_pic = &p_vout->p_picture[i_index];
896 display_date = p_pic->date;
903 /* Computes FPS rate */
904 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
906 if( display_date < current_date )
908 /* Picture is late: it will be destroyed and the thread will sleep and
909 * go to next picture */
910 vlc_mutex_lock( &p_vout->picture_lock );
911 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
912 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
913 vlc_mutex_unlock( &p_vout->picture_lock );
917 else if( display_date > current_date + VOUT_DISPLAY_DELAY )
919 /* A picture is ready to be rendered, but its rendering date is
920 * far from the current one so the thread will perform an empty loop
921 * as if no picture were found. The picture state is unchanged */
928 * Find the subpicture to display - this operation does not need lock, since
929 * only READY_SUBPICTURES are handled. If no picture has been selected,
930 * display_date will depend on the subpicture
935 * Perform rendering, sleep and display rendered picture
937 if( p_pic ) /* picture and perhaps subpicture */
939 b_display = p_vout->b_active;
940 p_vout->last_display_date = display_date;
944 /* Set picture dimensions and clear buffer */
945 SetBufferPicture( p_vout, p_pic );
947 /* Render picture and informations */
948 RenderPicture( p_vout, p_pic );
951 RenderPictureInfo( p_vout, p_pic );
952 RenderInfo( p_vout );
956 /* Remove picture from heap */
957 vlc_mutex_lock( &p_vout->picture_lock );
958 p_pic->i_status = p_pic->i_refcount ? DISPLAYED_PICTURE : DESTROYED_PICTURE;
959 vlc_mutex_unlock( &p_vout->picture_lock );
961 /* Render interface and subpicture */
962 if( b_display && p_vout->b_interface )
964 RenderInterface( p_vout );
970 RenderSubPicture( p_vout, p_subpic );
973 /* Remove subpicture from heap */
974 vlc_mutex_lock( &p_vout->subpicture_lock );
975 p_subpic->i_status = DESTROYED_SUBPICTURE;
976 vlc_mutex_unlock( &p_vout->subpicture_lock );
980 else if( p_subpic ) /* subpicture alone */
982 b_display = p_vout->b_active;
983 p_vout->last_display_date = display_date;
988 SetBufferPicture( p_vout, NULL );
990 /* Render informations, interface and subpicture */
993 RenderInfo( p_vout );
995 if( p_vout->b_interface )
997 RenderInterface( p_vout );
999 RenderSubPicture( p_vout, p_subpic );
1002 /* Remove subpicture from heap */
1003 vlc_mutex_lock( &p_vout->subpicture_lock );
1004 p_subpic->i_status = DESTROYED_SUBPICTURE;
1005 vlc_mutex_unlock( &p_vout->subpicture_lock );
1007 else if( p_vout->b_active ) /* idle or interface screen alone */
1009 if( p_vout->b_interface && 0 /* && ?? intf_change */ )
1011 /* Interface has changed, so a new rendering is required - force
1012 * it by setting last idle date to 0 */
1013 p_vout->last_idle_date = 0;
1016 /* Render idle screen and update idle date, then render interface if
1018 b_display = RenderIdle( p_vout );
1021 p_vout->last_idle_date = current_date;
1022 if( p_vout->b_interface )
1024 RenderInterface( p_vout );
1035 * Sleep, wake up and display rendered picture
1039 /* Store render time */
1040 p_vout->render_time = mdate() - current_date;
1043 /* Give back change lock */
1044 vlc_mutex_unlock( &p_vout->change_lock );
1046 /* Sleep a while or until a given date */
1047 if( display_date != 0 )
1049 mwait( display_date );
1053 msleep( VOUT_IDLE_SLEEP );
1056 /* On awakening, take back lock and send immediately picture to display,
1057 * then swap buffers */
1058 vlc_mutex_lock( &p_vout->change_lock );
1060 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1061 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1063 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1065 vout_SysDisplay( p_vout );
1066 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1070 * Check events and manage thread
1072 if( vout_SysManage( p_vout ) | Manage( p_vout ) )
1074 /* A fatal error occured, and the thread must terminate immediately,
1075 * without displaying anything - setting b_error to 1 cause the
1076 * immediate end of the main while() loop. */
1077 p_vout->b_error = 1;
1082 * Error loop - wait until the thread destruction is requested
1084 if( p_vout->b_error )
1086 ErrorThread( p_vout );
1090 EndThread( p_vout );
1091 DestroyThread( p_vout, THREAD_OVER );
1092 intf_DbgMsg( "thread end\n" );
1095 /******************************************************************************
1096 * ErrorThread: RunThread() error loop
1097 ******************************************************************************
1098 * This function is called when an error occured during thread main's loop. The
1099 * thread can still receive feed, but must be ready to terminate as soon as
1101 ******************************************************************************/
1102 static void ErrorThread( vout_thread_t *p_vout )
1104 /* Wait until a `die' order */
1106 while( !p_vout->b_die )
1109 msleep( VOUT_IDLE_SLEEP );
1113 /*******************************************************************************
1114 * EndThread: thread destruction
1115 *******************************************************************************
1116 * This function is called when the thread ends after a sucessfull
1117 * initialization. It frees all ressources allocated by InitThread.
1118 *******************************************************************************/
1119 static void EndThread( vout_thread_t *p_vout )
1121 int i_index; /* index in heap */
1125 *p_vout->pi_status = THREAD_END;
1127 /* Destroy all remaining pictures and subpictures */
1128 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1130 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1132 free( p_vout->p_picture[i_index].p_data );
1134 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1136 free( p_vout->p_subpicture[i_index].p_data );
1140 /* Destroy translation tables */
1141 vout_EndYUV( p_vout );
1142 vout_SysEnd( p_vout );
1145 /*******************************************************************************
1146 * DestroyThread: thread destruction
1147 *******************************************************************************
1148 * This function is called when the thread ends. It frees all ressources
1149 * allocated by CreateThread. Status is available at this stage.
1150 *******************************************************************************/
1151 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1153 int *pi_status; /* status adress */
1155 /* Store status adress */
1157 pi_status = p_vout->pi_status;
1159 /* Destroy thread structures allocated by Create and InitThread */
1160 vout_UnloadFont( p_vout->p_default_font );
1161 vout_UnloadFont( p_vout->p_large_font );
1162 vout_SysDestroy( p_vout );
1164 *pi_status = i_status;
1167 /*******************************************************************************
1168 * Print: print simple text on a picture
1169 *******************************************************************************
1170 * This function will print a simple text on the picture. It is designed to
1171 * print debugging or general informations.
1172 *******************************************************************************/
1173 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 )
1175 int i_text_height; /* total text height */
1176 int i_text_width; /* total text width */
1178 /* Update upper left coordinates according to alignment */
1179 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1180 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1182 /* Set area and print text */
1183 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1184 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1185 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1186 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1187 p_vout->i_white_pixel, 0, 0,
1192 /*******************************************************************************
1193 * SetBufferArea: activate an area in current buffer
1194 *******************************************************************************
1195 * This function is called when something is rendered on the current buffer.
1196 * It set the area as active and prepare it to be cleared on next rendering.
1197 * Pay attention to the fact that in this functions, i_h is in fact the end y
1198 * coordinate of the new area.
1199 *******************************************************************************/
1200 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1202 vout_buffer_t * p_buffer; /* current buffer */
1203 int i_area_begin, i_area_end; /* area vertical extension */
1204 int i_area, i_area_copy; /* area index */
1205 int i_area_shift; /* shift distance for areas */
1207 /* Choose buffer and modify h to end of area position */
1208 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1212 * Remove part of the area which is inside the picture - this is done
1213 * by calling again SetBufferArea with the correct areas dimensions.
1215 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1217 i_area_begin = p_buffer->i_pic_y;
1218 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1220 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1221 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1222 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1224 /* Keep the stripe above the picture, if any */
1225 if( i_y < i_area_begin )
1227 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1229 /* Keep the stripe below the picture, if any */
1230 if( i_h > i_area_end )
1232 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1238 /* Skip some extensions until interesting areas */
1240 (i_area < p_buffer->i_areas) &&
1241 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1247 if( i_area == p_buffer->i_areas )
1249 /* New area is below all existing ones: just add it at the end of the
1250 * array, if possible - else, append it to the last one */
1251 if( i_area < VOUT_MAX_AREAS )
1253 p_buffer->pi_area_begin[i_area] = i_y;
1254 p_buffer->pi_area_end[i_area] = i_h;
1255 p_buffer->i_areas++;
1260 intf_DbgMsg("areas overflow\n");
1262 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1267 i_area_begin = p_buffer->pi_area_begin[i_area];
1268 i_area_end = p_buffer->pi_area_end[i_area];
1270 if( i_y < i_area_begin )
1272 if( i_h >= i_area_begin - 1 )
1274 /* Extend area above */
1275 p_buffer->pi_area_begin[i_area] = i_y;
1279 /* Create a new area above : merge last area if overflow, then
1280 * move all old areas down */
1281 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1284 intf_DbgMsg("areas overflow\n");
1286 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1290 p_buffer->i_areas++;
1292 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1294 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1295 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1297 p_buffer->pi_area_begin[i_area] = i_y;
1298 p_buffer->pi_area_end[i_area] = i_h;
1302 if( i_h > i_area_end )
1304 /* Find further areas which can be merged with the new one */
1305 for( i_area_copy = i_area + 1;
1306 (i_area_copy < p_buffer->i_areas) &&
1307 (p_buffer->pi_area_begin[i_area] <= i_h);
1314 if( i_area_copy != i_area )
1316 /* Merge with last possible areas */
1317 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1319 /* Shift lower areas upward */
1320 i_area_shift = i_area_copy - i_area;
1321 p_buffer->i_areas -= i_area_shift;
1322 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1324 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1325 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1330 /* Extend area below */
1331 p_buffer->pi_area_end[i_area] = i_h;
1337 /*******************************************************************************
1338 * SetBufferPicture: clear buffer and set picture area
1339 *******************************************************************************
1340 * This function is called before any rendering. It clears the current
1341 * rendering buffer and set the new picture area. If the picture pointer is
1342 * NULL, then no picture area is defined. Floating operations are avoided since
1343 * some MMX calculations may follow.
1344 *******************************************************************************/
1345 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1347 vout_buffer_t * p_buffer; /* current buffer */
1348 int i_pic_x, i_pic_y; /* picture position */
1349 int i_pic_width, i_pic_height; /* picture dimensions */
1350 int i_old_pic_y, i_old_pic_height; /* old picture area */
1351 int i_vout_width, i_vout_height; /* display dimensions */
1352 int i_area; /* area index */
1353 int i_data_index; /* area data index */
1354 int i_data_size; /* area data size, in 256 bytes blocs */
1355 u64 * p_data; /* area data, for clearing */
1356 byte_t * p_data8; /* area data, for clearing (slow) */
1358 /* Choose buffer and set display dimensions */
1359 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1360 i_vout_width = p_vout->i_width;
1361 i_vout_height = p_vout->i_height;
1364 * Computes new picture size
1368 /* Try horizontal scaling first - width must be a mutiple of 16 */
1369 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1370 i_vout_width : p_pic->i_width) & ~0xf;
1371 switch( p_pic->i_aspect_ratio )
1373 case AR_3_4_PICTURE:
1374 i_pic_height = i_pic_width * 3 / 4;
1376 case AR_16_9_PICTURE:
1377 i_pic_height = i_pic_width * 9 / 16;
1379 case AR_221_1_PICTURE:
1380 i_pic_height = i_pic_width * 100 / 221;
1382 case AR_SQUARE_PICTURE:
1384 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1388 /* If picture dimensions using horizontal scaling are too large, use
1389 * vertical scaling. Since width must be a multiple of 16, height is
1390 * adjusted again after. */
1391 if( i_pic_height > i_vout_height )
1393 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1394 i_vout_height : p_pic->i_height;
1395 switch( p_pic->i_aspect_ratio )
1397 case AR_3_4_PICTURE:
1398 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1399 i_pic_height = i_pic_width * 3 / 4;
1401 case AR_16_9_PICTURE:
1402 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1403 i_pic_height = i_pic_width * 9 / 16;
1405 case AR_221_1_PICTURE:
1406 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1407 i_pic_height = i_pic_width * 100 / 221;
1409 case AR_SQUARE_PICTURE:
1411 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1412 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1417 /* Set picture position */
1418 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1419 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1423 /* No picture: size is 0 */
1431 * Set new picture size - if is is smaller than the previous one, clear
1432 * around it. Since picture are centered, only their size is tested.
1434 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1436 i_old_pic_y = p_buffer->i_pic_y;
1437 i_old_pic_height = p_buffer->i_pic_height;
1438 p_buffer->i_pic_x = i_pic_x;
1439 p_buffer->i_pic_y = i_pic_y;
1440 p_buffer->i_pic_width = i_pic_width;
1441 p_buffer->i_pic_height = i_pic_height;
1442 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1446 p_buffer->i_pic_x = i_pic_x;
1447 p_buffer->i_pic_y = i_pic_y;
1448 p_buffer->i_pic_width = i_pic_width;
1449 p_buffer->i_pic_height = i_pic_height;
1455 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1458 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1459 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1461 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1462 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1463 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1465 /* Clear 256 bytes block */
1466 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1467 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1468 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1469 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1470 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1471 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1472 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1473 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1475 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1477 /* Clear remaining 16 bytes blocks */
1478 *p_data++ = 0; *p_data++ = 0;
1480 p_data8 = (byte_t *)p_data;
1481 for( i_data_index = i_data_size % 16; i_data_index--; )
1483 /* Clear remaining bytes */
1491 p_buffer->i_areas = 0;
1494 /******************************************************************************
1495 * RenderPicture: render a picture
1496 ******************************************************************************
1497 * This function convert a picture from a video heap to a pixel-encoded image
1498 * and copy it to the current rendering buffer. No lock is required, since the
1499 * rendered picture has been determined as existant, and will only be destroyed
1500 * by the vout thread later.
1501 ******************************************************************************/
1502 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1505 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1506 mtime_t render_time; /* picture rendering time */
1508 vout_buffer_t * p_buffer; /* rendering buffer */
1509 byte_t * p_pic_data; /* convertion destination */
1511 /* Get and set rendering informations */
1512 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1513 p_pic_data = p_buffer->p_data +
1514 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1515 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1517 render_time = mdate();
1521 * Choose appropriate rendering function and render picture
1523 switch( p_pic->i_type )
1525 case YUV_420_PICTURE:
1526 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1527 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1528 p_pic->i_width, p_pic->i_height,
1529 p_buffer->i_pic_width, p_buffer->i_pic_height,
1530 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1531 p_pic->i_matrix_coefficients );
1533 case YUV_422_PICTURE:
1534 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1535 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1536 p_pic->i_width, p_pic->i_height,
1537 p_buffer->i_pic_width, p_buffer->i_pic_height,
1538 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1539 p_pic->i_matrix_coefficients );
1541 case YUV_444_PICTURE:
1542 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1543 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1544 p_pic->i_width, p_pic->i_height,
1545 p_buffer->i_pic_width, p_buffer->i_pic_height,
1546 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1547 p_pic->i_matrix_coefficients );
1551 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1557 /* Print picture date and rendering time */
1558 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1559 p_vout->i_buffer_index, (long) (mdate() - render_time),
1560 mstrtime( psz_date, p_pic->date ));
1564 /******************************************************************************
1565 * RenderPictureInfo: print additionnal informations on a picture
1566 ******************************************************************************
1567 * This function will print informations such as fps and other picture
1568 * dependant informations.
1569 ******************************************************************************/
1570 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1572 #if defined(STATS) || defined(DEBUG)
1573 char psz_buffer[256]; /* string buffer */
1578 * Print FPS rate in upper right corner
1580 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1582 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1583 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1584 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1585 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1589 * Print frames count and loop time in upper left corner
1591 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1592 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1593 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1598 * Print picture information in lower right corner
1600 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1601 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1602 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1603 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1604 p_pic->i_width, p_pic->i_height,
1605 p_pic->i_display_width, p_pic->i_display_height,
1606 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1607 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1608 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1609 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1610 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1611 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1612 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1613 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1614 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1615 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1619 /******************************************************************************
1620 * RenderIdle: render idle picture
1621 ******************************************************************************
1622 * This function will print something on the screen. It will return 0 if
1623 * nothing has been rendered, or 1 if something has been changed on the screen.
1624 * Note that if you absolutely want something to be printed, you will have
1625 * to force it by setting the last idle date to 0.
1626 * Unlike other rendering functions, this one calls the SetBufferPicture
1627 * function when needed.
1628 ******************************************************************************/
1629 static int RenderIdle( vout_thread_t *p_vout )
1631 int i_x = 0, i_y = 0; /* text position */
1632 int i_width, i_height; /* text size */
1633 mtime_t current_date; /* current date */
1634 const char *psz_text = "no stream"; /* text to display */
1637 current_date = mdate();
1638 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1639 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1641 SetBufferPicture( p_vout, NULL );
1642 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1643 &i_width, &i_height );
1644 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1646 vout_Print( p_vout->p_large_font,
1647 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1648 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1649 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1650 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1651 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1652 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1659 /******************************************************************************
1660 * RenderInfo: render additionnal informations
1661 ******************************************************************************
1662 * This function render informations which do not depend of the current picture
1664 ******************************************************************************/
1665 static void RenderInfo( vout_thread_t *p_vout )
1668 char psz_buffer[256]; /* string buffer */
1669 int i_ready_pic = 0; /* ready pictures */
1670 int i_reserved_pic = 0; /* reserved pictures */
1671 int i_picture; /* picture index */
1676 * Print thread state in lower left corner
1678 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1680 switch( p_vout->p_picture[i_picture].i_status )
1682 case RESERVED_PICTURE:
1683 case RESERVED_DATED_PICTURE:
1684 case RESERVED_DISP_PICTURE:
1692 sprintf( psz_buffer, "pic: %d/%d/%d",
1693 i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1694 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1698 /*******************************************************************************
1699 * RenderSubPicture: render a subpicture
1700 *******************************************************************************
1701 * This function render a sub picture unit.
1702 *******************************************************************************/
1703 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1705 p_vout_font_t p_font; /* text font */
1706 int i_width, i_height; /* subpicture dimensions */
1708 switch( p_subpic->i_type )
1710 case TEXT_SUBPICTURE: /* single line text */
1711 /* Select default font if not specified */
1712 p_font = p_subpic->type.text.p_font;
1713 if( p_font == NULL )
1715 p_font = p_vout->p_default_font;
1718 /* Computes text size (width and height fields are ignored) and print it */
1719 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1720 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1721 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1723 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1724 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1725 p_subpic->i_y * p_vout->i_bytes_per_line,
1726 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1727 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1728 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1730 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1736 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1741 /*******************************************************************************
1742 * RenderInterface: render the interface
1743 *******************************************************************************
1744 * This function render the interface, if any.
1745 *******************************************************************************/
1746 static void RenderInterface( vout_thread_t *p_vout )
1748 int i_height, i_text_height; /* total and text height */
1749 int i_width_1, i_width_2; /* text width */
1750 int i_byte; /* byte index */
1751 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1752 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1755 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1756 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1757 i_height += i_text_height;
1759 /* Render background */
1760 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1761 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1765 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1768 /* Render text, if not larger than screen */
1769 if( i_width_1 < p_vout->i_width )
1771 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1772 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1773 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1774 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1775 OUTLINED_TEXT, psz_text_1 );
1777 if( i_width_2 < p_vout->i_width )
1779 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1780 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1781 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1782 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1783 OUTLINED_TEXT, psz_text_2 );
1786 /* Activate modified area */
1787 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1790 /******************************************************************************
1791 * Manage: manage thread
1792 ******************************************************************************
1793 * This function will handle changes in thread configuration.
1794 ******************************************************************************/
1795 static int Manage( vout_thread_t *p_vout )
1798 if( p_vout->i_changes )
1800 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1801 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1805 /* On gamma or grayscale change, rebuild tables */
1806 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1809 if( vout_ResetYUV( p_vout ) )
1811 intf_ErrMsg("error: can't rebuild convertion tables\n");
1816 /* Clear changes flags which does not need management or have been
1818 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1819 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
1820 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
1822 /* Detect unauthorized changes */
1823 if( p_vout->i_changes )
1825 /* Some changes were not acknowledged by vout_SysManage or this function,
1826 * it means they should not be authorized */
1827 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
1834 /******************************************************************************
1835 * Align: align a subpicture in the screen
1836 ******************************************************************************
1837 * This function is used for rendering text or subpictures. It returns non 0
1838 * it the final aera is not fully included in display area. Return coordinates
1840 ******************************************************************************/
1841 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
1842 int i_width, int i_height, int i_h_align, int i_v_align )
1844 /* Align horizontally */
1848 *pi_x -= i_width / 2;
1851 *pi_x += (p_vout->i_width - i_width) / 2;
1857 *pi_x += p_vout->i_width - i_width;
1861 /* Align vertically */
1865 *pi_y -= i_height / 2;
1868 *pi_y += (p_vout->i_height - i_height) / 2;
1874 *pi_y += p_vout->i_height - i_height;
1876 case SUBTITLE_RALIGN:
1877 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
1878 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
1882 /* Return non 0 if clipping failed */
1883 return( (*pi_x < 0) || (*pi_y < 0) ||
1884 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
1887 /******************************************************************************
1888 * SetPalette: sets an 8 bpp palette
1889 ******************************************************************************
1890 * This function is just a prototype that does nothing. Architectures that
1891 * support palette allocation should override it.
1892 ******************************************************************************/
1893 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
1894 u16 *green, u16 *blue, u16 *transp )
1896 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );