1 /*****************************************************************************
2 * video_output.c : video output thread
3 * This module describes the programming interface for video output threads.
4 * It includes functions allowing to open a new thread, send pictures to a
5 * thread, and destroy a previously oppened video output thread.
6 *****************************************************************************
7 * Copyright (C) 2000 VideoLAN
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <errno.h> /* ENOMEM */
32 #include <stdlib.h> /* free() */
33 #include <stdio.h> /* sprintf() */
34 #include <string.h> /* strerror() */
42 #include "video_output.h"
43 #include "video_text.h"
44 #include "video_spu.h"
45 #include "video_yuv.h"
50 /*****************************************************************************
52 *****************************************************************************/
53 static int BinaryLog ( u32 i );
54 static void MaskToShift ( int *pi_left, int *pi_right, u32 i_mask );
55 static int InitThread ( vout_thread_t *p_vout );
56 static void RunThread ( vout_thread_t *p_vout );
57 static void ErrorThread ( vout_thread_t *p_vout );
58 static void EndThread ( vout_thread_t *p_vout );
59 static void DestroyThread ( vout_thread_t *p_vout, int i_status );
60 static void Print ( vout_thread_t *p_vout, int i_x, int i_y,
61 int i_h_align, int i_v_align,
62 unsigned char *psz_text );
63 static void SetBufferArea ( vout_thread_t *p_vout, int i_x, int i_y,
65 static void SetBufferPicture ( vout_thread_t *p_vout, picture_t *p_pic );
66 static void RenderPicture ( vout_thread_t *p_vout, picture_t *p_pic );
67 static void RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
68 static void RenderSubPicture ( vout_thread_t *p_vout,
69 subpicture_t *p_subpic );
70 static void RenderInterface ( vout_thread_t *p_vout );
71 static int RenderIdle ( vout_thread_t *p_vout );
72 static void RenderInfo ( vout_thread_t *p_vout );
73 static void Synchronize ( vout_thread_t *p_vout, s64 i_delay );
74 static int Manage ( vout_thread_t *p_vout );
75 static int Align ( vout_thread_t *p_vout, int *pi_x,
76 int *pi_y, int i_width, int i_height,
77 int i_h_align, int i_v_align );
78 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
79 u16 *green, u16 *blue, u16 *transp );
81 /*****************************************************************************
82 * vout_CreateThread: creates a new video output thread
83 *****************************************************************************
84 * This function creates a new video output thread, and returns a pointer
85 * to its description. On error, it returns NULL.
86 * If pi_status is NULL, then the function will block until the thread is ready.
87 * If not, it will be updated using one of the THREAD_* constants.
88 *****************************************************************************/
89 vout_thread_t * vout_CreateThread ( char *psz_display, int i_root_window,
90 int i_width, int i_height, int *pi_status,
91 int i_method, void *p_data )
93 vout_thread_t * p_vout; /* thread descriptor */
94 typedef void ( vout_getplugin_t ) ( vout_thread_t * p_vout );
95 int i_status; /* thread status */
96 int i_index; /* index for array initialization */
98 /* Allocate descriptor */
100 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
103 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
107 p_vout->p_set_palette = SetPalette;
109 /* Get a suitable video plugin */
110 for( i_index = 0 ; i_index < p_main->p_bank->i_plugin_count ; i_index++ )
112 /* If there's a plugin in p_info ... */
113 if( p_main->p_bank->p_info[ i_index ] != NULL )
115 /* ... and if this plugin provides the functions we want ... */
116 if( p_main->p_bank->p_info[ i_index ]->vout_GetPlugin != NULL )
118 /* ... then get these functions */
119 ( (vout_getplugin_t *)
120 p_main->p_bank->p_info[ i_index ]->vout_GetPlugin )( p_vout );
125 /* Initialize thread properties - thread id and locks will be initialized
129 p_vout->b_active = 0;
130 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
131 *p_vout->pi_status = THREAD_CREATE;
133 /* Initialize some fields used by the system-dependant method - these
134 * fields will probably be modified by the method, and are only
136 p_vout->i_changes = 0;
137 p_vout->i_width = i_width;
138 p_vout->i_height = i_height;
139 p_vout->i_bytes_per_line = i_width * 2;
140 p_vout->i_screen_depth = 15;
141 p_vout->i_bytes_per_pixel = 2;
142 p_vout->f_gamma = VOUT_GAMMA;
144 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
145 VOUT_GRAYSCALE_DEFAULT );
147 p_vout->b_interface = 0;
150 intf_DbgMsg( "wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
151 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
152 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
154 /* Initialize idle screen */
155 p_vout->last_display_date = mdate();
156 p_vout->last_display_date = 0;
157 p_vout->last_idle_date = 0;
160 /* Initialize statistics fields */
161 p_vout->render_time = 0;
162 p_vout->c_fps_samples = 0;
165 /* Initialize buffer index */
166 p_vout->i_buffer_index = 0;
168 /* Initialize pictures and subpictures - translation tables and functions
169 * will be initialized later in InitThread */
170 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
172 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
173 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
175 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
177 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
178 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
180 p_vout->i_pictures = 0;
182 /* Initialize synchronization informations */
183 p_vout->i_synchro_level = VOUT_SYNCHRO_LEVEL_START;
185 /* Create and initialize system-dependant method - this function issues its
186 * own error messages */
187 if( p_vout->p_sys_create( p_vout, psz_display, i_root_window, p_data ) )
192 intf_DbgMsg( "actual configuration: %dx%d, %d/%d bpp (%d Bpl), "
193 "masks: 0x%x/0x%x/0x%x\n",
194 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
195 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
196 p_vout->i_red_mask, p_vout->i_green_mask,
197 p_vout->i_blue_mask );
199 /* Calculate shifts from system-updated masks */
200 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift,
201 p_vout->i_red_mask );
202 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift,
203 p_vout->i_green_mask );
204 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift,
205 p_vout->i_blue_mask );
207 /* Set some useful colors */
208 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
209 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
210 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
211 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
213 /* Load fonts - fonts must be initialized after the system method since
214 * they may be dependant on screen depth and other thread properties */
215 p_vout->p_default_font = vout_LoadFont( DATA_PATH "/" VOUT_DEFAULT_FONT );
216 if( p_vout->p_default_font == NULL )
218 p_vout->p_default_font = vout_LoadFont( "share/" VOUT_DEFAULT_FONT );
220 if( p_vout->p_default_font == NULL )
222 intf_ErrMsg( "vout error: could not load default font\n" );
223 p_vout->p_sys_destroy( p_vout );
227 p_vout->p_large_font = vout_LoadFont( DATA_PATH "/" VOUT_LARGE_FONT );
228 if( p_vout->p_large_font == NULL )
230 p_vout->p_large_font = vout_LoadFont( "share/" VOUT_LARGE_FONT );
232 if( p_vout->p_large_font == NULL )
234 intf_ErrMsg( "vout error: could not load large font\n" );
235 vout_UnloadFont( p_vout->p_default_font );
236 p_vout->p_sys_destroy( p_vout );
241 /* Create thread and set locks */
242 vlc_mutex_init( &p_vout->picture_lock );
243 vlc_mutex_init( &p_vout->subpicture_lock );
244 vlc_mutex_init( &p_vout->change_lock );
245 vlc_mutex_lock( &p_vout->change_lock );
246 if( vlc_thread_create( &p_vout->thread_id, "video output",
247 (void *) RunThread, (void *) p_vout) )
249 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
250 vout_UnloadFont( p_vout->p_default_font );
251 vout_UnloadFont( p_vout->p_large_font );
252 p_vout->p_sys_destroy( p_vout );
257 intf_Msg( "Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
258 p_vout->i_height, p_vout->i_screen_depth,
259 p_vout->i_bytes_per_pixel * 8 );
261 /* If status is NULL, wait until the thread is created */
262 if( pi_status == NULL )
266 msleep( THREAD_SLEEP );
267 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
268 && (i_status != THREAD_FATAL) );
269 if( i_status != THREAD_READY )
277 /*****************************************************************************
278 * vout_DestroyThread: destroys a previously created thread
279 *****************************************************************************
280 * Destroy a terminated thread.
281 * The function will request a destruction of the specified thread. If pi_error
282 * is NULL, it will return once the thread is destroyed. Else, it will be
283 * update using one of the THREAD_* constants.
284 *****************************************************************************/
285 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
287 int i_status; /* thread status */
291 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
292 *p_vout->pi_status = THREAD_DESTROY;
294 /* Request thread destruction */
297 /* If status is NULL, wait until thread has been destroyed */
298 if( pi_status == NULL )
302 msleep( THREAD_SLEEP );
303 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
304 && (i_status != THREAD_FATAL) );
308 /*****************************************************************************
309 * vout_DisplaySubPicture: display a subpicture unit
310 *****************************************************************************
311 * Remove the reservation flag of an subpicture, which will cause it to be ready
312 * for display. The picture does not need to be locked, since it is ignored by
313 * the output thread if is reserved.
314 *****************************************************************************/
315 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
318 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
319 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
323 /* Check if status is valid */
324 if( p_subpic->i_status != RESERVED_SUBPICTURE )
326 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
327 p_subpic->i_status );
331 /* Remove reservation flag */
332 p_subpic->i_status = READY_SUBPICTURE;
335 /* Send subpicture informations */
336 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
337 p_subpic, p_subpic->i_type,
338 mstrtime( psz_begin_date, p_subpic->begin_date ),
339 mstrtime( psz_end_date, p_subpic->end_date ) );
343 /*****************************************************************************
344 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
345 *****************************************************************************
346 * This function create a reserved subpicture in the video output heap.
347 * A null pointer is returned if the function fails. This method provides an
348 * already allocated zone of memory in the spu data fields. It needs locking
349 * since several pictures can be created by several producers threads.
350 *****************************************************************************/
351 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
354 int i_subpic; /* subpicture index */
355 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
356 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
359 vlc_mutex_lock( &p_vout->subpicture_lock );
362 * Look for an empty place
364 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
366 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
368 /* Subpicture is marked for destruction, but is still allocated */
369 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
370 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
372 /* Memory size do match or is smaller : memory will not be
373 * reallocated, and function can end immediately - this is
374 * the best possible case, since no memory allocation needs
376 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
378 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
379 &p_vout->p_subpicture[i_subpic] );
381 vlc_mutex_unlock( &p_vout->subpicture_lock );
382 return( &p_vout->p_subpicture[i_subpic] );
384 else if( p_destroyed_subpic == NULL )
386 /* Memory size do not match, but subpicture index will be kept in
387 * case no other place are left */
388 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
391 else if( (p_free_subpic == NULL) &&
392 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
394 /* Subpicture is empty and ready for allocation */
395 p_free_subpic = &p_vout->p_subpicture[i_subpic];
399 /* If no free subpicture is available, use a destroyed subpicture */
400 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
402 /* No free subpicture or matching destroyed subpicture has been
403 * found, but a destroyed subpicture is still avalaible */
404 free( p_destroyed_subpic->p_data );
405 p_free_subpic = p_destroyed_subpic;
411 if( p_free_subpic != NULL )
413 /* Allocate memory */
416 case TEXT_SUBPICTURE: /* text subpicture */
417 p_free_subpic->p_data = malloc( i_size + 1 );
419 case DVD_SUBPICTURE: /* DVD subpicture unit */
420 p_free_subpic->p_data = malloc( i_size );
424 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
425 p_free_subpic->p_data = NULL;
430 if( p_free_subpic->p_data != NULL )
431 { /* Copy subpicture informations, set some default values */
432 p_free_subpic->i_type = i_type;
433 p_free_subpic->i_status = RESERVED_SUBPICTURE;
434 p_free_subpic->i_size = i_size;
435 p_free_subpic->i_x = 0;
436 p_free_subpic->i_y = 0;
437 p_free_subpic->i_width = 0;
438 p_free_subpic->i_height = 0;
439 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
440 p_free_subpic->i_vertical_align = CENTER_RALIGN;
444 /* Memory allocation failed : set subpicture as empty */
445 p_free_subpic->i_type = EMPTY_SUBPICTURE;
446 p_free_subpic->i_status = FREE_SUBPICTURE;
447 p_free_subpic = NULL;
448 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
452 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
454 vlc_mutex_unlock( &p_vout->subpicture_lock );
455 return( p_free_subpic );
458 /* No free or destroyed subpicture could be found */
459 intf_DbgMsg( "warning: heap is full\n" );
460 vlc_mutex_unlock( &p_vout->subpicture_lock );
464 /*****************************************************************************
465 * vout_DestroySubPicture: remove a subpicture from the heap
466 *****************************************************************************
467 * This function frees a previously reserved subpicture.
468 * It is meant to be used when the construction of a picture aborted.
469 * This function does not need locking since reserved subpictures are ignored
470 * by the output thread.
471 *****************************************************************************/
472 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
475 /* Check if status is valid */
476 if( p_subpic->i_status != RESERVED_SUBPICTURE )
478 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
479 p_subpic, p_subpic->i_status );
483 p_subpic->i_status = DESTROYED_SUBPICTURE;
486 intf_DbgMsg("subpicture %p\n", p_subpic);
490 /*****************************************************************************
491 * vout_DisplayPicture: display a picture
492 *****************************************************************************
493 * Remove the reservation flag of a picture, which will cause it to be ready for
494 * display. The picture won't be displayed until vout_DatePicture has been
496 *****************************************************************************/
497 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
499 vlc_mutex_lock( &p_vout->picture_lock );
500 switch( p_pic->i_status )
502 case RESERVED_PICTURE:
503 p_pic->i_status = RESERVED_DISP_PICTURE;
505 case RESERVED_DATED_PICTURE:
506 p_pic->i_status = READY_PICTURE;
510 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
516 intf_DbgMsg("picture %p\n", p_pic);
518 vlc_mutex_unlock( &p_vout->picture_lock );
521 /*****************************************************************************
522 * vout_DatePicture: date a picture
523 *****************************************************************************
524 * Remove the reservation flag of a picture, which will cause it to be ready for
525 * display. The picture won't be displayed until vout_DisplayPicture has been
527 *****************************************************************************/
528 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
531 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
534 vlc_mutex_lock( &p_vout->picture_lock );
536 switch( p_pic->i_status )
538 case RESERVED_PICTURE:
539 p_pic->i_status = RESERVED_DATED_PICTURE;
541 case RESERVED_DISP_PICTURE:
542 p_pic->i_status = READY_PICTURE;
546 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
552 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
554 vlc_mutex_unlock( &p_vout->picture_lock );
557 /*****************************************************************************
558 * vout_CreatePicture: allocate a picture in the video output heap.
559 *****************************************************************************
560 * This function create a reserved image in the video output heap.
561 * A null pointer is returned if the function fails. This method provides an
562 * already allocated zone of memory in the picture data fields. It needs locking
563 * since several pictures can be created by several producers threads.
564 *****************************************************************************/
565 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
566 int i_width, int i_height )
568 int i_picture; /* picture index */
569 int i_chroma_width = 0; /* chroma width */
570 picture_t * p_free_picture = NULL; /* first free picture */
571 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
574 vlc_mutex_lock( &p_vout->picture_lock );
577 * Look for an empty place
579 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
581 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
583 /* Picture is marked for destruction, but is still allocated - note
584 * that if width and type are the same for two pictures, chroma_width
585 * should also be the same */
586 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
587 (p_vout->p_picture[i_picture].i_height == i_height) &&
588 (p_vout->p_picture[i_picture].i_width == i_width) )
590 /* Memory size do match : memory will not be reallocated, and function
591 * can end immediately - this is the best possible case, since no
592 * memory allocation needs to be done */
593 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
594 p_vout->i_pictures++;
596 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
597 &p_vout->p_picture[i_picture] );
599 vlc_mutex_unlock( &p_vout->picture_lock );
600 return( &p_vout->p_picture[i_picture] );
602 else if( p_destroyed_picture == NULL )
604 /* Memory size do not match, but picture index will be kept in
605 * case no other place are left */
606 p_destroyed_picture = &p_vout->p_picture[i_picture];
609 else if( (p_free_picture == NULL) &&
610 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
612 /* Picture is empty and ready for allocation */
613 p_free_picture = &p_vout->p_picture[i_picture];
617 /* If no free picture is available, use a destroyed picture */
618 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
620 /* No free picture or matching destroyed picture has been found, but
621 * a destroyed picture is still avalaible */
622 free( p_destroyed_picture->p_data );
623 p_free_picture = p_destroyed_picture;
629 if( p_free_picture != NULL )
631 /* Allocate memory */
634 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
635 i_chroma_width = i_width / 2;
636 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
637 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
638 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
639 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
641 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
642 i_chroma_width = i_width / 2;
643 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
644 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
645 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
646 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
648 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
649 i_chroma_width = i_width;
650 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
651 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
652 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
653 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
657 intf_DbgMsg("error: unknown picture type %d\n", i_type );
658 p_free_picture->p_data = NULL;
663 if( p_free_picture->p_data != NULL )
665 /* Copy picture informations, set some default values */
666 p_free_picture->i_type = i_type;
667 p_free_picture->i_status = RESERVED_PICTURE;
668 p_free_picture->i_matrix_coefficients = 1;
669 p_free_picture->i_width = i_width;
670 p_free_picture->i_height = i_height;
671 p_free_picture->i_chroma_width = i_chroma_width;
672 p_free_picture->i_display_horizontal_offset = 0;
673 p_free_picture->i_display_vertical_offset = 0;
674 p_free_picture->i_display_width = i_width;
675 p_free_picture->i_display_height = i_height;
676 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
677 p_free_picture->i_refcount = 0;
678 p_vout->i_pictures++;
682 /* Memory allocation failed : set picture as empty */
683 p_free_picture->i_type = EMPTY_PICTURE;
684 p_free_picture->i_status = FREE_PICTURE;
685 p_free_picture = NULL;
686 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
690 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
692 vlc_mutex_unlock( &p_vout->picture_lock );
693 return( p_free_picture );
696 /* No free or destroyed picture could be found */
697 intf_DbgMsg( "warning: heap is full\n" );
698 vlc_mutex_unlock( &p_vout->picture_lock );
702 /*****************************************************************************
703 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
704 *****************************************************************************
705 * This function frees a previously reserved picture or a permanent
706 * picture. It is meant to be used when the construction of a picture aborted.
707 * Note that the picture will be destroyed even if it is linked !
708 *****************************************************************************/
709 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
711 vlc_mutex_lock( &p_vout->picture_lock );
714 /* Check if picture status is valid */
715 if( (p_pic->i_status != RESERVED_PICTURE) &&
716 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
717 (p_pic->i_status != RESERVED_DISP_PICTURE) )
719 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
723 p_pic->i_status = DESTROYED_PICTURE;
724 p_vout->i_pictures--;
727 intf_DbgMsg("picture %p\n", p_pic);
729 vlc_mutex_unlock( &p_vout->picture_lock );
732 /*****************************************************************************
733 * vout_LinkPicture: increment reference counter of a picture
734 *****************************************************************************
735 * This function increment the reference counter of a picture in the video
736 * heap. It needs a lock since several producer threads can access the picture.
737 *****************************************************************************/
738 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
740 vlc_mutex_lock( &p_vout->picture_lock );
744 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
747 vlc_mutex_unlock( &p_vout->picture_lock );
750 /*****************************************************************************
751 * vout_UnlinkPicture: decrement reference counter of a picture
752 *****************************************************************************
753 * This function decrement the reference counter of a picture in the video heap.
754 *****************************************************************************/
755 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
757 vlc_mutex_lock( &p_vout->picture_lock );
761 if( p_pic->i_refcount < 0 )
763 intf_DbgMsg("error: refcount < 0\n");
764 p_pic->i_refcount = 0;
768 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
770 p_pic->i_status = DESTROYED_PICTURE;
771 p_vout->i_pictures--;
775 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
778 vlc_mutex_unlock( &p_vout->picture_lock );
781 /*****************************************************************************
782 * vout_SetBuffers: set buffers adresses
783 *****************************************************************************
784 * This function is called by system drivers to set buffers video memory
786 *****************************************************************************/
787 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
789 /* No picture previously */
790 p_vout->p_buffer[0].i_pic_x = 0;
791 p_vout->p_buffer[0].i_pic_y = 0;
792 p_vout->p_buffer[0].i_pic_width = 0;
793 p_vout->p_buffer[0].i_pic_height = 0;
794 p_vout->p_buffer[1].i_pic_x = 0;
795 p_vout->p_buffer[1].i_pic_y = 0;
796 p_vout->p_buffer[1].i_pic_width = 0;
797 p_vout->p_buffer[1].i_pic_height = 0;
799 /* The first area covers all the screen */
800 p_vout->p_buffer[0].i_areas = 1;
801 p_vout->p_buffer[0].pi_area_begin[0] = 0;
802 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
803 p_vout->p_buffer[1].i_areas = 1;
804 p_vout->p_buffer[1].pi_area_begin[0] = 0;
805 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
808 p_vout->p_buffer[0].p_data = p_buf1;
809 p_vout->p_buffer[1].p_data = p_buf2;
812 /*****************************************************************************
813 * vout_Pixel2RGB: return red, green and blue from pixel value
814 *****************************************************************************
815 * Return color values, in 0-255 range, of the decomposition of a pixel. This
816 * is a slow routine and should only be used for initialization phase.
817 *****************************************************************************/
818 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
820 *pi_red = i_pixel & p_vout->i_red_mask;
821 *pi_green = i_pixel & p_vout->i_green_mask;
822 *pi_blue = i_pixel & p_vout->i_blue_mask;
825 /* following functions are local */
827 /*****************************************************************************
828 * BinaryLog: computes the base 2 log of a binary value
829 *****************************************************************************
830 * This functions is used by MaskToShift, to get a bit index from a binary
832 *****************************************************************************/
833 static int BinaryLog(u32 i)
858 if (i != ((u32)1 << i_log))
860 intf_DbgMsg("internal error: binary log overflow\n");
866 /*****************************************************************************
867 * MaskToShift: transform a color mask into right and left shifts
868 *****************************************************************************
869 * This function is used for obtaining color shifts from masks.
870 *****************************************************************************/
871 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
873 u32 i_low, i_high; /* lower hand higher bits of the mask */
876 i_low = i_mask & (- i_mask); /* lower bit of the mask */
877 i_high = i_mask + i_low; /* higher bit of the mask */
879 /* Transform bits into an index */
880 i_low = BinaryLog (i_low);
881 i_high = BinaryLog (i_high);
883 /* Update pointers and return */
885 *pi_right = (8 - i_high + i_low);
888 /*****************************************************************************
889 * InitThread: initialize video output thread
890 *****************************************************************************
891 * This function is called from RunThread and performs the second step of the
892 * initialization. It returns 0 on success. Note that the thread's flag are not
893 * modified inside this function.
894 *****************************************************************************/
895 static int InitThread( vout_thread_t *p_vout )
899 *p_vout->pi_status = THREAD_START;
901 /* Initialize output method - this function issues its own error messages */
902 if( p_vout->p_sys_init( p_vout ) )
907 /* Initialize convertion tables and functions */
908 if( vout_InitYUV( p_vout ) )
910 intf_ErrMsg("error: can't allocate YUV translation tables\n");
914 /* Mark thread as running and return */
915 p_vout->b_active = 1;
916 *p_vout->pi_status = THREAD_READY;
917 intf_DbgMsg("thread ready\n");
921 /*****************************************************************************
922 * RunThread: video output thread
923 *****************************************************************************
924 * Video output thread. This function does only returns when the thread is
925 * terminated. It handles the pictures arriving in the video heap and the
926 * display device events.
927 *****************************************************************************/
928 static void RunThread( vout_thread_t *p_vout)
930 /* XXX?? welcome to gore land */
931 static int i_trash_count = 0;
932 static mtime_t last_display_date = 0;
934 int i_index; /* index in heap */
935 mtime_t current_date; /* current date */
936 mtime_t display_date; /* display date */
937 boolean_t b_display; /* display flag */
938 picture_t * p_pic; /* picture pointer */
939 subpicture_t * p_subpic; /* subpicture pointer */
944 p_vout->b_error = InitThread( p_vout );
945 if( p_vout->b_error )
947 DestroyThread( p_vout, THREAD_ERROR );
953 * Main loop - it is not executed if an error occured during
956 while( (!p_vout->b_die) && (!p_vout->b_error) )
958 /* Initialize loop variables */
962 current_date = mdate();
965 * Find the picture to display - this operation does not need lock,
966 * since only READY_PICTUREs are handled
968 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
970 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
972 (p_vout->p_picture[i_index].date < display_date) ) )
974 p_pic = &p_vout->p_picture[i_index];
975 display_date = p_pic->date;
982 /* Computes FPS rate */
983 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
987 //fprintf( stderr, "gap : %Ld\n", display_date-last_display_date );
988 last_display_date = display_date;
990 if( display_date < current_date && i_trash_count > 4 )
992 /* Picture is late: it will be destroyed and the thread
993 * will sleep and go to next picture */
995 vlc_mutex_lock( &p_vout->picture_lock );
996 if( p_pic->i_refcount )
998 p_pic->i_status = DISPLAYED_PICTURE;
1002 p_pic->i_status = DESTROYED_PICTURE;
1003 p_vout->i_pictures--;
1005 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
1006 vlc_mutex_unlock( &p_vout->picture_lock );
1008 /* Update synchronization information as if display delay
1010 Synchronize( p_vout, display_date - current_date );
1018 if( display_date > current_date + VOUT_DISPLAY_DELAY )
1020 /* A picture is ready to be rendered, but its rendering date
1021 * is far from the current one so the thread will perform an
1022 * empty loop as if no picture were found. The picture state
1029 /* Picture will be displayed, update synchronization
1031 Synchronize( p_vout, display_date - current_date );
1035 * Find the subpictures to display - this operation does not need
1036 * lock, since only READY_SUBPICTURE are handled. If no picture
1037 * has been selected, display_date will depend on the subpicture.
1038 * We get an easily parsable chained list of subpictures which
1039 * ends with NULL since p_subpic was initialized to NULL.
1041 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1043 if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
1045 p_vout->p_subpicture[i_index].p_next = p_subpic;
1046 p_subpic = &p_vout->p_subpicture[i_index];
1051 * Perform rendering, sleep and display rendered picture
1053 if( p_pic ) /* picture and perhaps subpicture */
1055 b_display = p_vout->b_active;
1056 p_vout->last_display_date = display_date;
1060 /* Set picture dimensions and clear buffer */
1061 SetBufferPicture( p_vout, p_pic );
1063 /* Render picture and informations */
1064 RenderPicture( p_vout, p_pic );
1065 if( p_vout->b_info )
1067 RenderPictureInfo( p_vout, p_pic );
1068 RenderInfo( p_vout );
1072 RenderSubPicture( p_vout, p_subpic );
1076 /* Remove picture from heap */
1077 vlc_mutex_lock( &p_vout->picture_lock );
1078 if( p_pic->i_refcount )
1080 p_pic->i_status = DISPLAYED_PICTURE;
1084 p_pic->i_status = DESTROYED_PICTURE;
1085 p_vout->i_pictures--;
1087 vlc_mutex_unlock( &p_vout->picture_lock );
1089 /* Render interface and subpicture */
1090 if( b_display && p_vout->b_interface )
1092 RenderInterface( p_vout );
1096 else if( p_vout->b_active ) /* idle or interface screen alone */
1098 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1100 /* Interface has changed, so a new rendering is required - force
1101 * it by setting last idle date to 0 */
1102 p_vout->last_idle_date = 0;
1105 /* Render idle screen and update idle date, then render interface if
1107 b_display = RenderIdle( p_vout );
1110 p_vout->last_idle_date = current_date;
1111 if( p_vout->b_interface )
1113 RenderInterface( p_vout );
1124 * Sleep, wake up and display rendered picture
1128 /* Store render time */
1129 p_vout->render_time = mdate() - current_date;
1132 /* Give back change lock */
1133 vlc_mutex_unlock( &p_vout->change_lock );
1135 /* Sleep a while or until a given date */
1136 if( display_date != 0 )
1138 mwait( display_date );
1142 msleep( VOUT_IDLE_SLEEP );
1145 /* On awakening, take back lock and send immediately picture to display,
1146 * then swap buffers */
1147 vlc_mutex_lock( &p_vout->change_lock );
1149 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1150 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1152 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1154 p_vout->p_sys_display( p_vout );
1156 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1161 * Check events and manage thread
1163 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1165 /* A fatal error occured, and the thread must terminate immediately,
1166 * without displaying anything - setting b_error to 1 cause the
1167 * immediate end of the main while() loop. */
1168 p_vout->b_error = 1;
1173 * Error loop - wait until the thread destruction is requested
1175 if( p_vout->b_error )
1177 ErrorThread( p_vout );
1181 EndThread( p_vout );
1182 DestroyThread( p_vout, THREAD_OVER );
1183 intf_DbgMsg( "thread end\n" );
1186 /*****************************************************************************
1187 * ErrorThread: RunThread() error loop
1188 *****************************************************************************
1189 * This function is called when an error occured during thread main's loop. The
1190 * thread can still receive feed, but must be ready to terminate as soon as
1192 *****************************************************************************/
1193 static void ErrorThread( vout_thread_t *p_vout )
1195 /* Wait until a `die' order */
1197 while( !p_vout->b_die )
1200 msleep( VOUT_IDLE_SLEEP );
1204 /*****************************************************************************
1205 * EndThread: thread destruction
1206 *****************************************************************************
1207 * This function is called when the thread ends after a sucessful
1208 * initialization. It frees all ressources allocated by InitThread.
1209 *****************************************************************************/
1210 static void EndThread( vout_thread_t *p_vout )
1212 int i_index; /* index in heap */
1216 *p_vout->pi_status = THREAD_END;
1218 /* Destroy all remaining pictures and subpictures */
1219 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1221 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1223 free( p_vout->p_picture[i_index].p_data );
1226 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1228 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1230 free( p_vout->p_subpicture[i_index].p_data );
1234 /* Destroy translation tables */
1235 vout_EndYUV( p_vout );
1236 p_vout->p_sys_end( p_vout );
1239 /*****************************************************************************
1240 * DestroyThread: thread destruction
1241 *****************************************************************************
1242 * This function is called when the thread ends. It frees all ressources
1243 * allocated by CreateThread. Status is available at this stage.
1244 *****************************************************************************/
1245 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1247 int *pi_status; /* status adress */
1249 /* Store status adress */
1251 pi_status = p_vout->pi_status;
1253 /* Destroy thread structures allocated by Create and InitThread */
1254 vout_UnloadFont( p_vout->p_default_font );
1255 vout_UnloadFont( p_vout->p_large_font );
1256 p_vout->p_sys_destroy( p_vout );
1258 /* Free structure */
1260 *pi_status = i_status;
1263 /*****************************************************************************
1264 * Print: print simple text on a picture
1265 *****************************************************************************
1266 * This function will print a simple text on the picture. It is designed to
1267 * print debugging or general informations.
1268 *****************************************************************************/
1269 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 )
1271 int i_text_height; /* total text height */
1272 int i_text_width; /* total text width */
1274 /* Update upper left coordinates according to alignment */
1275 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1276 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1278 /* Set area and print text */
1279 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1280 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1281 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1282 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1283 p_vout->i_white_pixel, 0, 0,
1288 /*****************************************************************************
1289 * SetBufferArea: activate an area in current buffer
1290 *****************************************************************************
1291 * This function is called when something is rendered on the current buffer.
1292 * It set the area as active and prepare it to be cleared on next rendering.
1293 * Pay attention to the fact that in this functions, i_h is in fact the end y
1294 * coordinate of the new area.
1295 *****************************************************************************/
1296 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1298 vout_buffer_t * p_buffer; /* current buffer */
1299 int i_area_begin, i_area_end; /* area vertical extension */
1300 int i_area, i_area_copy; /* area index */
1301 int i_area_shift; /* shift distance for areas */
1303 /* Choose buffer and modify h to end of area position */
1304 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1308 * Remove part of the area which is inside the picture - this is done
1309 * by calling again SetBufferArea with the correct areas dimensions.
1311 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1313 i_area_begin = p_buffer->i_pic_y;
1314 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1316 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1317 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1318 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1320 /* Keep the stripe above the picture, if any */
1321 if( i_y < i_area_begin )
1323 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1325 /* Keep the stripe below the picture, if any */
1326 if( i_h > i_area_end )
1328 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1334 /* Skip some extensions until interesting areas */
1336 (i_area < p_buffer->i_areas) &&
1337 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1343 if( i_area == p_buffer->i_areas )
1345 /* New area is below all existing ones: just add it at the end of the
1346 * array, if possible - else, append it to the last one */
1347 if( i_area < VOUT_MAX_AREAS )
1349 p_buffer->pi_area_begin[i_area] = i_y;
1350 p_buffer->pi_area_end[i_area] = i_h;
1351 p_buffer->i_areas++;
1356 intf_DbgMsg("areas overflow\n");
1358 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1363 i_area_begin = p_buffer->pi_area_begin[i_area];
1364 i_area_end = p_buffer->pi_area_end[i_area];
1366 if( i_y < i_area_begin )
1368 if( i_h >= i_area_begin - 1 )
1370 /* Extend area above */
1371 p_buffer->pi_area_begin[i_area] = i_y;
1375 /* Create a new area above : merge last area if overflow, then
1376 * move all old areas down */
1377 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1380 intf_DbgMsg("areas overflow\n");
1382 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1386 p_buffer->i_areas++;
1388 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1390 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1391 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1393 p_buffer->pi_area_begin[i_area] = i_y;
1394 p_buffer->pi_area_end[i_area] = i_h;
1398 if( i_h > i_area_end )
1400 /* Find further areas which can be merged with the new one */
1401 for( i_area_copy = i_area + 1;
1402 (i_area_copy < p_buffer->i_areas) &&
1403 (p_buffer->pi_area_begin[i_area] <= i_h);
1410 if( i_area_copy != i_area )
1412 /* Merge with last possible areas */
1413 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1415 /* Shift lower areas upward */
1416 i_area_shift = i_area_copy - i_area;
1417 p_buffer->i_areas -= i_area_shift;
1418 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1420 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1421 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1426 /* Extend area below */
1427 p_buffer->pi_area_end[i_area] = i_h;
1433 /*****************************************************************************
1434 * SetBufferPicture: clear buffer and set picture area
1435 *****************************************************************************
1436 * This function is called before any rendering. It clears the current
1437 * rendering buffer and set the new picture area. If the picture pointer is
1438 * NULL, then no picture area is defined. Floating operations are avoided since
1439 * some MMX calculations may follow.
1440 *****************************************************************************/
1441 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1443 vout_buffer_t * p_buffer; /* current buffer */
1444 int i_pic_x, i_pic_y; /* picture position */
1445 int i_pic_width, i_pic_height; /* picture dimensions */
1446 int i_old_pic_y, i_old_pic_height; /* old picture area */
1447 int i_vout_width, i_vout_height; /* display dimensions */
1448 int i_area; /* area index */
1449 int i_data_index; /* area data index */
1450 int i_data_size; /* area data size, in 256 bytes blocs */
1451 u64 * p_data; /* area data, for clearing */
1452 byte_t * p_data8; /* area data, for clearing (slow) */
1454 /* Choose buffer and set display dimensions */
1455 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1456 i_vout_width = p_vout->i_width;
1457 i_vout_height = p_vout->i_height;
1460 * Computes new picture size
1464 /* Try horizontal scaling first - width must be a mutiple of 16 */
1465 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1466 i_vout_width : p_pic->i_width) & ~0xf;
1467 switch( p_pic->i_aspect_ratio )
1469 case AR_3_4_PICTURE:
1470 i_pic_height = i_pic_width * 3 / 4;
1472 case AR_16_9_PICTURE:
1473 i_pic_height = i_pic_width * 9 / 16;
1475 case AR_221_1_PICTURE:
1476 i_pic_height = i_pic_width * 100 / 221;
1478 case AR_SQUARE_PICTURE:
1480 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1484 /* If picture dimensions using horizontal scaling are too large, use
1485 * vertical scaling. Since width must be a multiple of 16, height is
1486 * adjusted again after. */
1487 if( i_pic_height > i_vout_height )
1489 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1490 i_vout_height : p_pic->i_height;
1491 switch( p_pic->i_aspect_ratio )
1493 case AR_3_4_PICTURE:
1494 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1495 i_pic_height = i_pic_width * 3 / 4;
1497 case AR_16_9_PICTURE:
1498 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1499 i_pic_height = i_pic_width * 9 / 16;
1501 case AR_221_1_PICTURE:
1502 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1503 i_pic_height = i_pic_width * 100 / 221;
1505 case AR_SQUARE_PICTURE:
1507 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1508 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1513 /* Set picture position */
1514 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1515 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1519 /* No picture: size is 0 */
1527 * Set new picture size - if it is smaller than the previous one, clear
1528 * around it. Since picture are centered, only their size is tested.
1530 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1532 i_old_pic_y = p_buffer->i_pic_y;
1533 i_old_pic_height = p_buffer->i_pic_height;
1534 p_buffer->i_pic_x = i_pic_x;
1535 p_buffer->i_pic_y = i_pic_y;
1536 p_buffer->i_pic_width = i_pic_width;
1537 p_buffer->i_pic_height = i_pic_height;
1538 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1542 p_buffer->i_pic_x = i_pic_x;
1543 p_buffer->i_pic_y = i_pic_y;
1544 p_buffer->i_pic_width = i_pic_width;
1545 p_buffer->i_pic_height = i_pic_height;
1551 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1554 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1555 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1557 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1558 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1559 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1561 /* Clear 256 bytes block */
1562 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1563 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1564 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1565 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1566 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1567 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1568 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1569 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1571 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1573 /* Clear remaining 16 bytes blocks */
1574 *p_data++ = 0; *p_data++ = 0;
1576 p_data8 = (byte_t *)p_data;
1577 for( i_data_index = i_data_size % 16; i_data_index--; )
1579 /* Clear remaining bytes */
1587 p_buffer->i_areas = 0;
1590 /*****************************************************************************
1591 * RenderPicture: render a picture
1592 *****************************************************************************
1593 * This function convert a picture from a video heap to a pixel-encoded image
1594 * and copy it to the current rendering buffer. No lock is required, since the
1595 * rendered picture has been determined as existant, and will only be destroyed
1596 * by the vout thread later.
1597 *****************************************************************************/
1598 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1601 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1602 mtime_t render_time; /* picture rendering time */
1604 vout_buffer_t * p_buffer; /* rendering buffer */
1605 byte_t * p_pic_data; /* convertion destination */
1607 /* Get and set rendering informations */
1608 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1609 p_pic_data = p_buffer->p_data +
1610 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1611 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1613 render_time = mdate();
1617 * Choose appropriate rendering function and render picture
1619 switch( p_pic->i_type )
1621 case YUV_420_PICTURE:
1622 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1623 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1624 p_pic->i_width, p_pic->i_height,
1625 p_buffer->i_pic_width, p_buffer->i_pic_height,
1626 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1627 p_pic->i_matrix_coefficients );
1629 case YUV_422_PICTURE:
1630 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1631 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1632 p_pic->i_width, p_pic->i_height,
1633 p_buffer->i_pic_width, p_buffer->i_pic_height,
1634 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1635 p_pic->i_matrix_coefficients );
1637 case YUV_444_PICTURE:
1638 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1639 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1640 p_pic->i_width, p_pic->i_height,
1641 p_buffer->i_pic_width, p_buffer->i_pic_height,
1642 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1643 p_pic->i_matrix_coefficients );
1647 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1653 /* Print picture date and rendering time */
1654 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1655 p_vout->i_buffer_index, (long) (mdate() - render_time),
1656 mstrtime( psz_date, p_pic->date ));
1660 /*****************************************************************************
1661 * RenderPictureInfo: print additionnal informations on a picture
1662 *****************************************************************************
1663 * This function will print informations such as fps and other picture
1664 * dependant informations.
1665 *****************************************************************************/
1666 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1668 #if defined(STATS) || defined(DEBUG)
1669 char psz_buffer[256]; /* string buffer */
1674 * Print FPS rate in upper right corner
1676 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1678 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1679 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1680 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1681 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1685 * Print frames count and loop time in upper left corner
1687 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1688 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1689 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1694 * Print picture information in lower right corner
1696 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1697 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1698 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1699 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1700 p_pic->i_width, p_pic->i_height,
1701 p_pic->i_display_width, p_pic->i_display_height,
1702 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1703 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1704 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1705 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1706 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1707 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1708 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1709 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1710 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1711 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1715 /*****************************************************************************
1716 * RenderIdle: render idle picture
1717 *****************************************************************************
1718 * This function will print something on the screen. It will return 0 if
1719 * nothing has been rendered, or 1 if something has been changed on the screen.
1720 * Note that if you absolutely want something to be printed, you will have
1721 * to force it by setting the last idle date to 0.
1722 * Unlike other rendering functions, this one calls the SetBufferPicture
1723 * function when needed.
1724 *****************************************************************************/
1725 static int RenderIdle( vout_thread_t *p_vout )
1727 int i_x = 0, i_y = 0; /* text position */
1728 int i_width, i_height; /* text size */
1729 mtime_t current_date; /* current date */
1730 const char *psz_text = "waiting for stream ..."; /* text to display */
1733 current_date = mdate();
1734 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1735 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1737 SetBufferPicture( p_vout, NULL );
1738 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1739 &i_width, &i_height );
1740 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1742 vout_Print( p_vout->p_large_font,
1743 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1744 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1745 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1746 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1747 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1748 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1755 /*****************************************************************************
1756 * RenderInfo: render additionnal informations
1757 *****************************************************************************
1758 * This function render informations which do not depend of the current picture
1760 *****************************************************************************/
1761 static void RenderInfo( vout_thread_t *p_vout )
1764 char psz_buffer[256]; /* string buffer */
1765 int i_ready_pic = 0; /* ready pictures */
1766 int i_reserved_pic = 0; /* reserved pictures */
1767 int i_picture; /* picture index */
1772 * Print thread state in lower left corner
1774 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1776 switch( p_vout->p_picture[i_picture].i_status )
1778 case RESERVED_PICTURE:
1779 case RESERVED_DATED_PICTURE:
1780 case RESERVED_DISP_PICTURE:
1788 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1789 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1790 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1794 /*****************************************************************************
1795 * RenderSubPicture: render a subpicture
1796 *****************************************************************************
1797 * This function render a sub picture unit.
1798 *****************************************************************************/
1799 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1801 p_vout_font_t p_font; /* text font */
1802 int i_width, i_height; /* subpicture dimensions */
1804 while( p_subpic != NULL )
1806 switch( p_subpic->i_type )
1808 case DVD_SUBPICTURE: /* DVD subpicture unit */
1809 /* test if the picture really has to be displayed */
1810 if( mdate() < p_subpic->begin_date )
1812 /* not yet, see you later */
1815 if( mdate() > p_subpic->end_date )
1817 /* too late, destroying the subpic */
1818 vout_DestroySubPicture( p_vout, p_subpic );
1821 vout_RenderSPU( &p_vout->p_buffer[ p_vout->i_buffer_index ],
1822 p_subpic, p_vout->i_bytes_per_pixel,
1823 p_vout->i_bytes_per_line );
1825 case TEXT_SUBPICTURE: /* single line text */
1826 /* Select default font if not specified */
1827 p_font = p_subpic->type.text.p_font;
1828 if( p_font == NULL )
1830 p_font = p_vout->p_default_font;
1833 /* Compute text size (width and height fields are ignored)
1835 vout_TextSize( p_font, p_subpic->type.text.i_style,
1836 p_subpic->p_data, &i_width, &i_height );
1837 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y,
1838 i_width, i_height, p_subpic->i_horizontal_align,
1839 p_subpic->i_vertical_align ) )
1842 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1843 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1844 p_subpic->i_y * p_vout->i_bytes_per_line,
1845 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1846 p_subpic->type.text.i_char_color,
1847 p_subpic->type.text.i_border_color,
1848 p_subpic->type.text.i_bg_color,
1849 p_subpic->type.text.i_style, p_subpic->p_data );
1850 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y,
1851 i_width, i_height );
1857 intf_DbgMsg( "error: unknown subpicture %p type %d\n",
1858 p_subpic, p_subpic->i_type );
1862 p_subpic = p_subpic->p_next;
1866 /*****************************************************************************
1867 * RenderInterface: render the interface
1868 *****************************************************************************
1869 * This function render the interface, if any.
1870 *****************************************************************************/
1871 static void RenderInterface( vout_thread_t *p_vout )
1873 int i_height, i_text_height; /* total and text height */
1874 int i_width_1, i_width_2; /* text width */
1875 int i_byte; /* byte index */
1876 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1877 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1880 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1881 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1882 i_height += i_text_height;
1884 /* Render background */
1885 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1886 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1890 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1893 /* Render text, if not larger than screen */
1894 if( i_width_1 < p_vout->i_width )
1896 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1897 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1898 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1899 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1900 OUTLINED_TEXT, psz_text_1 );
1902 if( i_width_2 < p_vout->i_width )
1904 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1905 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1906 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1907 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1908 OUTLINED_TEXT, psz_text_2 );
1911 /* Activate modified area */
1912 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1915 /*****************************************************************************
1916 * Synchronize: update synchro level depending of heap state
1917 *****************************************************************************
1918 * This function is called during the main vout loop.
1919 *****************************************************************************/
1920 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1922 int i_synchro_inc = 0;
1923 /* XXX?? gore following */
1924 static int i_panic_count = 0;
1925 static int i_last_synchro_inc = 0;
1926 static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1927 static int i_truc = 10;
1931 //fprintf( stderr, "PANIC %d\n", i_panic_count );
1937 if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1940 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1945 if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1948 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1952 if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1953 i_synchro_inc*i_last_synchro_inc < 0 )
1958 if( i_delay < 6000 )
1963 else if( i_delay < 70000 )
1965 i_truc = 24+(24*i_delay)/70000;
1968 i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
1970 else if( i_delay > 100000 )
1972 r_synchro_level += 1;
1973 if( i_delay > 130000 )
1974 r_synchro_level += 1;
1977 r_synchro_level += (float)i_synchro_inc / i_truc;
1978 p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
1980 if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
1982 r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
1985 //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
1986 // p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
1987 i_last_synchro_inc = i_synchro_inc;
1990 /*****************************************************************************
1991 * Manage: manage thread
1992 *****************************************************************************
1993 * This function will handle changes in thread configuration.
1994 *****************************************************************************/
1995 static int Manage( vout_thread_t *p_vout )
1998 if( p_vout->i_changes )
2000 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
2001 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
2005 /* On gamma or grayscale change, rebuild tables */
2006 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2009 if( vout_ResetYUV( p_vout ) )
2011 intf_ErrMsg("error: can't rebuild convertion tables\n");
2016 /* Clear changes flags which does not need management or have been
2018 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2019 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
2020 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
2022 /* Detect unauthorized changes */
2023 if( p_vout->i_changes )
2025 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
2026 * function, it means they should not be authorized */
2027 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2034 /*****************************************************************************
2035 * Align: align a subpicture in the screen
2036 *****************************************************************************
2037 * This function is used for rendering text or subpictures. It returns non 0
2038 * it the final aera is not fully included in display area. Return coordinates
2040 *****************************************************************************/
2041 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2042 int i_width, int i_height, int i_h_align, int i_v_align )
2044 /* Align horizontally */
2048 *pi_x -= i_width / 2;
2051 *pi_x += (p_vout->i_width - i_width) / 2;
2057 *pi_x += p_vout->i_width - i_width;
2061 /* Align vertically */
2065 *pi_y -= i_height / 2;
2068 *pi_y += (p_vout->i_height - i_height) / 2;
2074 *pi_y += p_vout->i_height - i_height;
2076 case SUBTITLE_RALIGN:
2077 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2078 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2082 /* Return non 0 if clipping failed */
2083 return( (*pi_x < 0) || (*pi_y < 0) ||
2084 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2087 /*****************************************************************************
2088 * SetPalette: sets an 8 bpp palette
2089 *****************************************************************************
2090 * This function is just a prototype that does nothing. Architectures that
2091 * support palette allocation should override it.
2092 *****************************************************************************/
2093 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2094 u16 *green, u16 *blue, u16 *transp )
2096 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );