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 void Synchronize ( vout_thread_t *p_vout, s64 i_delay );
54 static int Manage ( vout_thread_t *p_vout );
55 static int Align ( vout_thread_t *p_vout, int *pi_x,
56 int *pi_y, int i_width, int i_height,
57 int i_h_align, int i_v_align );
58 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
59 u16 *green, u16 *blue, u16 *transp );
61 /*****************************************************************************
62 * vout_CreateThread: creates a new video output thread
63 *****************************************************************************
64 * This function creates a new video output thread, and returns a pointer
65 * to its description. On error, it returns NULL.
66 * If pi_status is NULL, then the function will block until the thread is ready.
67 * If not, it will be updated using one of the THREAD_* constants.
68 *****************************************************************************/
69 vout_thread_t * vout_CreateThread ( char *psz_display, int i_root_window,
70 int i_width, int i_height, int *pi_status, int i_method )
72 vout_thread_t * p_vout; /* thread descriptor */
73 int i_status; /* thread status */
74 int i_index; /* index for array initialization */
76 /* Allocate descriptor */
78 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
81 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
85 /* Sets method-specific functions */
88 case VOUT_DUMMY_METHOD:
89 p_vout->p_sys_create = vout_DummySysCreate;
90 p_vout->p_sys_init = vout_DummySysInit;
91 p_vout->p_sys_end = vout_DummySysEnd;
92 p_vout->p_sys_destroy = vout_DummySysDestroy;
93 p_vout->p_sys_manage = vout_DummySysManage;
94 p_vout->p_sys_display = vout_DummySysDisplay;
98 p_vout->p_sys_create = vout_X11SysCreate;
99 p_vout->p_sys_init = vout_X11SysInit;
100 p_vout->p_sys_end = vout_X11SysEnd;
101 p_vout->p_sys_destroy = vout_X11SysDestroy;
102 p_vout->p_sys_manage = vout_X11SysManage;
103 p_vout->p_sys_display = vout_X11SysDisplay;
108 p_vout->p_sys_create = vout_FBSysCreate;
109 p_vout->p_sys_init = vout_FBSysInit;
110 p_vout->p_sys_end = vout_FBSysEnd;
111 p_vout->p_sys_destroy = vout_FBSysDestroy;
112 p_vout->p_sys_manage = vout_FBSysManage;
113 p_vout->p_sys_display = vout_FBSysDisplay;
117 case VOUT_GLIDE_METHOD:
118 p_vout->p_sys_create = vout_GlideSysCreate;
119 p_vout->p_sys_init = vout_GlideSysInit;
120 p_vout->p_sys_end = vout_GlideSysEnd;
121 p_vout->p_sys_destroy = vout_GlideSysDestroy;
122 p_vout->p_sys_manage = vout_GlideSysManage;
123 p_vout->p_sys_display = vout_GlideSysDisplay;
127 case VOUT_DGA_METHOD:
128 p_vout->p_sys_create = vout_DGASysCreate;
129 p_vout->p_sys_init = vout_DGASysInit;
130 p_vout->p_sys_end = vout_DGASysEnd;
131 p_vout->p_sys_destroy = vout_DGASysDestroy;
132 p_vout->p_sys_manage = vout_DGASysManage;
133 p_vout->p_sys_display = vout_DGASysDisplay;
137 case VOUT_GGI_METHOD:
138 p_vout->p_sys_create = vout_GGISysCreate;
139 p_vout->p_sys_init = vout_GGISysInit;
140 p_vout->p_sys_end = vout_GGISysEnd;
141 p_vout->p_sys_destroy = vout_GGISysDestroy;
142 p_vout->p_sys_manage = vout_GGISysManage;
143 p_vout->p_sys_display = vout_GGISysDisplay;
147 case VOUT_BEOS_METHOD:
148 p_vout->p_sys_create = vout_BSysCreate;
149 p_vout->p_sys_init = vout_BSysInit;
150 p_vout->p_sys_end = vout_BSysEnd;
151 p_vout->p_sys_destroy = vout_BSysDestroy;
152 p_vout->p_sys_manage = vout_BSysManage;
153 p_vout->p_sys_display = vout_BSysDisplay;
157 intf_ErrMsg( "error: video output method not available\n" );
162 /* Initialize thread properties - thread id and locks will be initialized
166 p_vout->b_active = 0;
167 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
168 *p_vout->pi_status = THREAD_CREATE;
170 /* Initialize some fields used by the system-dependant method - these fields will
171 * probably be modified by the method, and are only preferences */
172 p_vout->i_changes = 0;
173 p_vout->i_width = i_width;
174 p_vout->i_height = i_height;
175 p_vout->i_bytes_per_line = i_width * 2;
176 p_vout->i_screen_depth = 15;
177 p_vout->i_bytes_per_pixel = 2;
178 p_vout->f_gamma = VOUT_GAMMA;
180 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
182 p_vout->b_interface = 0;
185 p_vout->p_set_palette = SetPalette;
187 intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
188 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
189 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
191 /* Initialize idle screen */
192 p_vout->last_display_date = mdate();
193 p_vout->last_display_date = 0;
194 p_vout->last_idle_date = 0;
197 /* Initialize statistics fields */
198 p_vout->render_time = 0;
199 p_vout->c_fps_samples = 0;
202 /* Initialize buffer index */
203 p_vout->i_buffer_index = 0;
205 /* Initialize pictures and subpictures - translation tables and functions
206 * will be initialized later in InitThread */
207 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
209 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
210 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
211 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
212 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
214 p_vout->i_pictures = 0;
216 /* Initialize synchronization informations */
217 p_vout->i_synchro_level = VOUT_SYNCHRO_LEVEL_START;
219 /* Create and initialize system-dependant method - this function issues its
220 * own error messages */
221 if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
226 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
227 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
228 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
229 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
231 /* Calculate shifts from system-updated masks */
232 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
233 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
234 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
236 /* Set some useful colors */
237 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
238 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
239 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
240 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
242 /* Load fonts - fonts must be initialized after the system method since
243 * they may be dependant on screen depth and other thread properties */
244 p_vout->p_default_font = vout_LoadFont( VOUT_DEFAULT_FONT );
245 if( p_vout->p_default_font == NULL )
247 p_vout->p_sys_destroy( p_vout );
251 p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
252 if( p_vout->p_large_font == NULL )
254 vout_UnloadFont( p_vout->p_default_font );
255 p_vout->p_sys_destroy( p_vout );
260 /* Create thread and set locks */
261 vlc_mutex_init( &p_vout->picture_lock );
262 vlc_mutex_init( &p_vout->subpicture_lock );
263 vlc_mutex_init( &p_vout->change_lock );
264 vlc_mutex_lock( &p_vout->change_lock );
265 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
267 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
268 vout_UnloadFont( p_vout->p_default_font );
269 vout_UnloadFont( p_vout->p_large_font );
270 p_vout->p_sys_destroy( p_vout );
275 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
276 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
278 /* If status is NULL, wait until the thread is created */
279 if( pi_status == NULL )
283 msleep( THREAD_SLEEP );
284 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
285 && (i_status != THREAD_FATAL) );
286 if( i_status != THREAD_READY )
294 /*****************************************************************************
295 * vout_DestroyThread: destroys a previously created thread
296 *****************************************************************************
297 * Destroy a terminated thread.
298 * The function will request a destruction of the specified thread. If pi_error
299 * is NULL, it will return once the thread is destroyed. Else, it will be
300 * update using one of the THREAD_* constants.
301 *****************************************************************************/
302 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
304 int i_status; /* thread status */
308 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
309 *p_vout->pi_status = THREAD_DESTROY;
311 /* Request thread destruction */
314 /* If status is NULL, wait until thread has been destroyed */
315 if( pi_status == NULL )
319 msleep( THREAD_SLEEP );
320 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
321 && (i_status != THREAD_FATAL) );
325 /*****************************************************************************
326 * vout_DisplaySubPicture: display a subpicture unit
327 *****************************************************************************
328 * Remove the reservation flag of an subpicture, which will cause it to be ready
329 * for display. The picture does not need to be locked, since it is ignored by
330 * the output thread if is reserved.
331 *****************************************************************************/
332 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
335 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
336 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
340 /* Check if status is valid */
341 if( p_subpic->i_status != RESERVED_SUBPICTURE )
343 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
344 p_subpic->i_status );
348 /* Remove reservation flag */
349 p_subpic->i_status = READY_SUBPICTURE;
352 /* Send subpicture informations */
353 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
354 p_subpic, p_subpic->i_type,
355 mstrtime( psz_begin_date, p_subpic->begin_date ),
356 mstrtime( psz_end_date, p_subpic->end_date ) );
360 /*****************************************************************************
361 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
362 *****************************************************************************
363 * This function create a reserved subpicture in the video output heap.
364 * A null pointer is returned if the function fails. This method provides an
365 * already allocated zone of memory in the spu data fields. It needs locking
366 * since several pictures can be created by several producers threads.
367 *****************************************************************************/
368 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
371 int i_subpic; /* subpicture index */
372 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
373 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
376 vlc_mutex_lock( &p_vout->subpicture_lock );
379 * Look for an empty place
381 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
383 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
385 /* Subpicture is marked for destruction, but is still allocated */
386 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
387 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
389 /* Memory size do match or is smaller : memory will not be reallocated,
390 * and function can end immediately - this is the best possible case,
391 * since no memory allocation needs to be done */
392 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
394 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
395 &p_vout->p_subpicture[i_subpic] );
397 vlc_mutex_unlock( &p_vout->subpicture_lock );
398 return( &p_vout->p_subpicture[i_subpic] );
400 else if( p_destroyed_subpic == NULL )
402 /* Memory size do not match, but subpicture index will be kept in
403 * case no other place are left */
404 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
407 else if( (p_free_subpic == NULL) &&
408 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
410 /* Subpicture is empty and ready for allocation */
411 p_free_subpic = &p_vout->p_subpicture[i_subpic];
415 /* If no free subpicture is available, use a destroyed subpicture */
416 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
418 /* No free subpicture or matching destroyed subpicture has been found, but
419 * a destroyed subpicture is still avalaible */
420 free( p_destroyed_subpic->p_data );
421 p_free_subpic = p_destroyed_subpic;
427 if( p_free_subpic != NULL )
429 /* Allocate memory */
432 case TEXT_SUBPICTURE: /* text subpicture */
433 p_free_subpic->p_data = malloc( i_size + 1 );
437 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
438 p_free_subpic->p_data = NULL;
443 if( p_free_subpic->p_data != NULL )
444 { /* Copy subpicture informations, set some default values */
445 p_free_subpic->i_type = i_type;
446 p_free_subpic->i_status = RESERVED_SUBPICTURE;
447 p_free_subpic->i_size = i_size;
448 p_free_subpic->i_x = 0;
449 p_free_subpic->i_y = 0;
450 p_free_subpic->i_width = 0;
451 p_free_subpic->i_height = 0;
452 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
453 p_free_subpic->i_vertical_align = CENTER_RALIGN;
457 /* Memory allocation failed : set subpicture as empty */
458 p_free_subpic->i_type = EMPTY_SUBPICTURE;
459 p_free_subpic->i_status = FREE_SUBPICTURE;
460 p_free_subpic = NULL;
461 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
465 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
467 vlc_mutex_unlock( &p_vout->subpicture_lock );
468 return( p_free_subpic );
471 /* No free or destroyed subpicture could be found */
472 intf_DbgMsg( "warning: heap is full\n" );
473 vlc_mutex_unlock( &p_vout->subpicture_lock );
477 /*****************************************************************************
478 * vout_DestroySubPicture: remove a subpicture from the heap
479 *****************************************************************************
480 * This function frees a previously reserved subpicture.
481 * It is meant to be used when the construction of a picture aborted.
482 * This function does not need locking since reserved subpictures are ignored
483 * by the output thread.
484 *****************************************************************************/
485 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
488 /* Check if status is valid */
489 if( p_subpic->i_status != RESERVED_SUBPICTURE )
491 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
492 p_subpic, p_subpic->i_status );
496 p_subpic->i_status = DESTROYED_SUBPICTURE;
499 intf_DbgMsg("subpicture %p\n", p_subpic);
503 /*****************************************************************************
504 * vout_DisplayPicture: display a picture
505 *****************************************************************************
506 * Remove the reservation flag of a picture, which will cause it to be ready for
507 * display. The picture won't be displayed until vout_DatePicture has been
509 *****************************************************************************/
510 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
512 vlc_mutex_lock( &p_vout->picture_lock );
513 switch( p_pic->i_status )
515 case RESERVED_PICTURE:
516 p_pic->i_status = RESERVED_DISP_PICTURE;
518 case RESERVED_DATED_PICTURE:
519 p_pic->i_status = READY_PICTURE;
523 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
529 intf_DbgMsg("picture %p\n", p_pic);
531 vlc_mutex_unlock( &p_vout->picture_lock );
534 /*****************************************************************************
535 * vout_DatePicture: date a picture
536 *****************************************************************************
537 * Remove the reservation flag of a picture, which will cause it to be ready for
538 * display. The picture won't be displayed until vout_DisplayPicture has been
540 *****************************************************************************/
541 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
544 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
547 vlc_mutex_lock( &p_vout->picture_lock );
549 switch( p_pic->i_status )
551 case RESERVED_PICTURE:
552 p_pic->i_status = RESERVED_DATED_PICTURE;
554 case RESERVED_DISP_PICTURE:
555 p_pic->i_status = READY_PICTURE;
559 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
565 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
567 vlc_mutex_unlock( &p_vout->picture_lock );
570 /*****************************************************************************
571 * vout_CreatePicture: allocate a picture in the video output heap.
572 *****************************************************************************
573 * This function create a reserved image in the video output heap.
574 * A null pointer is returned if the function fails. This method provides an
575 * already allocated zone of memory in the picture data fields. It needs locking
576 * since several pictures can be created by several producers threads.
577 *****************************************************************************/
578 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
579 int i_width, int i_height )
581 int i_picture; /* picture index */
582 int i_chroma_width = 0; /* chroma width */
583 picture_t * p_free_picture = NULL; /* first free picture */
584 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
587 vlc_mutex_lock( &p_vout->picture_lock );
590 * Look for an empty place
592 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
594 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
596 /* Picture is marked for destruction, but is still allocated - note
597 * that if width and type are the same for two pictures, chroma_width
598 * should also be the same */
599 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
600 (p_vout->p_picture[i_picture].i_height == i_height) &&
601 (p_vout->p_picture[i_picture].i_width == i_width) )
603 /* Memory size do match : memory will not be reallocated, and function
604 * can end immediately - this is the best possible case, since no
605 * memory allocation needs to be done */
606 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
607 p_vout->i_pictures++;
609 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
610 &p_vout->p_picture[i_picture] );
612 vlc_mutex_unlock( &p_vout->picture_lock );
613 return( &p_vout->p_picture[i_picture] );
615 else if( p_destroyed_picture == NULL )
617 /* Memory size do not match, but picture index will be kept in
618 * case no other place are left */
619 p_destroyed_picture = &p_vout->p_picture[i_picture];
622 else if( (p_free_picture == NULL) &&
623 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
625 /* Picture is empty and ready for allocation */
626 p_free_picture = &p_vout->p_picture[i_picture];
630 /* If no free picture is available, use a destroyed picture */
631 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
633 /* No free picture or matching destroyed picture has been found, but
634 * a destroyed picture is still avalaible */
635 free( p_destroyed_picture->p_data );
636 p_free_picture = p_destroyed_picture;
642 if( p_free_picture != NULL )
644 /* Allocate memory */
647 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
648 i_chroma_width = i_width / 2;
649 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
650 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
651 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
652 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
654 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
655 i_chroma_width = i_width / 2;
656 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
657 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
658 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
659 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
661 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
662 i_chroma_width = i_width;
663 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
664 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
665 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
666 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
670 intf_DbgMsg("error: unknown picture type %d\n", i_type );
671 p_free_picture->p_data = NULL;
676 if( p_free_picture->p_data != NULL )
678 /* Copy picture informations, set some default values */
679 p_free_picture->i_type = i_type;
680 p_free_picture->i_status = RESERVED_PICTURE;
681 p_free_picture->i_matrix_coefficients = 1;
682 p_free_picture->i_width = i_width;
683 p_free_picture->i_height = i_height;
684 p_free_picture->i_chroma_width = i_chroma_width;
685 p_free_picture->i_display_horizontal_offset = 0;
686 p_free_picture->i_display_vertical_offset = 0;
687 p_free_picture->i_display_width = i_width;
688 p_free_picture->i_display_height = i_height;
689 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
690 p_free_picture->i_refcount = 0;
691 p_vout->i_pictures++;
695 /* Memory allocation failed : set picture as empty */
696 p_free_picture->i_type = EMPTY_PICTURE;
697 p_free_picture->i_status = FREE_PICTURE;
698 p_free_picture = NULL;
699 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
703 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
705 vlc_mutex_unlock( &p_vout->picture_lock );
706 return( p_free_picture );
709 /* No free or destroyed picture could be found */
710 intf_DbgMsg( "warning: heap is full\n" );
711 vlc_mutex_unlock( &p_vout->picture_lock );
715 /*****************************************************************************
716 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
717 *****************************************************************************
718 * This function frees a previously reserved picture or a permanent
719 * picture. It is meant to be used when the construction of a picture aborted.
720 * Note that the picture will be destroyed even if it is linked !
721 *****************************************************************************/
722 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
724 vlc_mutex_lock( &p_vout->picture_lock );
727 /* Check if picture status is valid */
728 if( (p_pic->i_status != RESERVED_PICTURE) &&
729 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
730 (p_pic->i_status != RESERVED_DISP_PICTURE) )
732 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
736 p_pic->i_status = DESTROYED_PICTURE;
737 p_vout->i_pictures--;
740 intf_DbgMsg("picture %p\n", p_pic);
742 vlc_mutex_unlock( &p_vout->picture_lock );
745 /*****************************************************************************
746 * vout_LinkPicture: increment reference counter of a picture
747 *****************************************************************************
748 * This function increment the reference counter of a picture in the video
749 * heap. It needs a lock since several producer threads can access the picture.
750 *****************************************************************************/
751 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
753 vlc_mutex_lock( &p_vout->picture_lock );
757 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
760 vlc_mutex_unlock( &p_vout->picture_lock );
763 /*****************************************************************************
764 * vout_UnlinkPicture: decrement reference counter of a picture
765 *****************************************************************************
766 * This function decrement the reference counter of a picture in the video heap.
767 *****************************************************************************/
768 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
770 vlc_mutex_lock( &p_vout->picture_lock );
774 if( p_pic->i_refcount < 0 )
776 intf_DbgMsg("error: refcount < 0\n");
777 p_pic->i_refcount = 0;
781 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
783 p_pic->i_status = DESTROYED_PICTURE;
784 p_vout->i_pictures--;
788 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
791 vlc_mutex_unlock( &p_vout->picture_lock );
794 /*****************************************************************************
795 * vout_SetBuffers: set buffers adresses
796 *****************************************************************************
797 * This function is called by system drivers to set buffers video memory
799 *****************************************************************************/
800 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
802 /* No picture previously */
803 p_vout->p_buffer[0].i_pic_x = 0;
804 p_vout->p_buffer[0].i_pic_y = 0;
805 p_vout->p_buffer[0].i_pic_width = 0;
806 p_vout->p_buffer[0].i_pic_height = 0;
807 p_vout->p_buffer[1].i_pic_x = 0;
808 p_vout->p_buffer[1].i_pic_y = 0;
809 p_vout->p_buffer[1].i_pic_width = 0;
810 p_vout->p_buffer[1].i_pic_height = 0;
812 /* The first area covers all the screen */
813 p_vout->p_buffer[0].i_areas = 1;
814 p_vout->p_buffer[0].pi_area_begin[0] = 0;
815 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
816 p_vout->p_buffer[1].i_areas = 1;
817 p_vout->p_buffer[1].pi_area_begin[0] = 0;
818 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
821 p_vout->p_buffer[0].p_data = p_buf1;
822 p_vout->p_buffer[1].p_data = p_buf2;
825 /*****************************************************************************
826 * vout_Pixel2RGB: return red, green and blue from pixel value
827 *****************************************************************************
828 * Return color values, in 0-255 range, of the decomposition of a pixel. This
829 * is a slow routine and should only be used for initialization phase.
830 *****************************************************************************/
831 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
833 *pi_red = i_pixel & p_vout->i_red_mask;
834 *pi_green = i_pixel & p_vout->i_green_mask;
835 *pi_blue = i_pixel & p_vout->i_blue_mask;
838 /* following functions are local */
840 /*****************************************************************************
841 * BinaryLog: computes the base 2 log of a binary value
842 *****************************************************************************
843 * This functions is used by MaskToShift, to get a bit index from a binary
845 *****************************************************************************/
846 static int BinaryLog(u32 i)
871 if (i != ((u32)1 << i_log))
873 intf_ErrMsg("internal error: binary log overflow\n");
879 /*****************************************************************************
880 * MaskToShift: transform a color mask into right and left shifts
881 *****************************************************************************
882 * This function is used for obtaining color shifts from masks.
883 *****************************************************************************/
884 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
886 u32 i_low, i_high; /* lower hand higher bits of the mask */
889 i_low = i_mask & (- i_mask); /* lower bit of the mask */
890 i_high = i_mask + i_low; /* higher bit of the mask */
892 /* Transform bits into an index */
893 i_low = BinaryLog (i_low);
894 i_high = BinaryLog (i_high);
896 /* Update pointers and return */
898 *pi_right = (8 - i_high + i_low);
901 /*****************************************************************************
902 * InitThread: initialize video output thread
903 *****************************************************************************
904 * This function is called from RunThread and performs the second step of the
905 * initialization. It returns 0 on success. Note that the thread's flag are not
906 * modified inside this function.
907 *****************************************************************************/
908 static int InitThread( vout_thread_t *p_vout )
912 *p_vout->pi_status = THREAD_START;
914 /* Initialize output method - this function issues its own error messages */
915 if( p_vout->p_sys_init( p_vout ) )
920 /* Initialize convertion tables and functions */
921 if( vout_InitYUV( p_vout ) )
923 intf_ErrMsg("error: can't allocate YUV translation tables\n");
927 /* Mark thread as running and return */
928 p_vout->b_active = 1;
929 *p_vout->pi_status = THREAD_READY;
930 intf_DbgMsg("thread ready\n");
934 /*****************************************************************************
935 * RunThread: video output thread
936 *****************************************************************************
937 * Video output thread. This function does only returns when the thread is
938 * terminated. It handles the pictures arriving in the video heap and the
939 * display device events.
940 *****************************************************************************/
941 static void RunThread( vout_thread_t *p_vout)
943 int i_index; /* index in heap */
944 mtime_t current_date; /* current date */
945 mtime_t display_date; /* display date */
946 boolean_t b_display; /* display flag */
947 picture_t * p_pic; /* picture pointer */
948 subpicture_t * p_subpic; /* subpicture pointer */
953 p_vout->b_error = InitThread( p_vout );
954 if( p_vout->b_error )
956 DestroyThread( p_vout, THREAD_ERROR );
962 * Main loop - it is not executed if an error occured during
965 while( (!p_vout->b_die) && (!p_vout->b_error) )
967 /* Initialize loop variables */
971 current_date = mdate();
974 * Find the picture to display - this operation does not need lock,
975 * since only READY_PICTUREs are handled
977 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
979 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
981 (p_vout->p_picture[i_index].date < display_date) ) )
983 p_pic = &p_vout->p_picture[i_index];
984 display_date = p_pic->date;
991 /* Computes FPS rate */
992 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
995 if( display_date < current_date )
997 /* Picture is late: it will be destroyed and the thread will sleep and
998 * go to next picture */
1000 vlc_mutex_lock( &p_vout->picture_lock );
1001 if( p_pic->i_refcount )
1003 p_pic->i_status = DISPLAYED_PICTURE;
1007 p_pic->i_status = DESTROYED_PICTURE;
1008 p_vout->i_pictures--;
1010 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
1011 vlc_mutex_unlock( &p_vout->picture_lock );
1016 /* Update synchronization information as if display delay
1018 Synchronize( p_vout, 0 );
1022 if( display_date > current_date + VOUT_DISPLAY_DELAY )
1024 /* A picture is ready to be rendered, but its rendering date is
1025 * far from the current one so the thread will perform an empty loop
1026 * as if no picture were found. The picture state is unchanged */
1032 /* Picture will be displayed, update synchronization
1034 Synchronize( p_vout, display_date - current_date );
1039 * Find the subpicture to display - this operation does not need lock, since
1040 * only READY_SUBPICTURES are handled. If no picture has been selected,
1041 * display_date will depend on the subpicture
1046 * Perform rendering, sleep and display rendered picture
1048 if( p_pic ) /* picture and perhaps subpicture */
1050 b_display = p_vout->b_active;
1051 p_vout->last_display_date = display_date;
1055 /* Set picture dimensions and clear buffer */
1056 SetBufferPicture( p_vout, p_pic );
1058 /* Render picture and informations */
1059 RenderPicture( p_vout, p_pic );
1060 if( p_vout->b_info )
1062 RenderPictureInfo( p_vout, p_pic );
1063 RenderInfo( p_vout );
1067 /* Remove picture from heap */
1068 vlc_mutex_lock( &p_vout->picture_lock );
1069 if( p_pic->i_refcount )
1071 p_pic->i_status = DISPLAYED_PICTURE;
1075 p_pic->i_status = DESTROYED_PICTURE;
1076 p_vout->i_pictures--;
1078 vlc_mutex_unlock( &p_vout->picture_lock );
1080 /* Render interface and subpicture */
1081 if( b_display && p_vout->b_interface )
1083 RenderInterface( p_vout );
1089 RenderSubPicture( p_vout, p_subpic );
1092 /* Remove subpicture from heap */
1093 vlc_mutex_lock( &p_vout->subpicture_lock );
1094 p_subpic->i_status = DESTROYED_SUBPICTURE;
1095 vlc_mutex_unlock( &p_vout->subpicture_lock );
1099 else if( p_subpic ) /* subpicture alone */
1101 b_display = p_vout->b_active;
1102 p_vout->last_display_date = display_date;
1107 SetBufferPicture( p_vout, NULL );
1109 /* Render informations, interface and subpicture */
1110 if( p_vout->b_info )
1112 RenderInfo( p_vout );
1114 if( p_vout->b_interface )
1116 RenderInterface( p_vout );
1118 RenderSubPicture( p_vout, p_subpic );
1121 /* Remove subpicture from heap */
1122 vlc_mutex_lock( &p_vout->subpicture_lock );
1123 p_subpic->i_status = DESTROYED_SUBPICTURE;
1124 vlc_mutex_unlock( &p_vout->subpicture_lock );
1126 else if( p_vout->b_active ) /* idle or interface screen alone */
1128 if( p_vout->b_interface && 0 /* && ?? intf_change */ )
1130 /* Interface has changed, so a new rendering is required - force
1131 * it by setting last idle date to 0 */
1132 p_vout->last_idle_date = 0;
1135 /* Render idle screen and update idle date, then render interface if
1137 b_display = RenderIdle( p_vout );
1140 p_vout->last_idle_date = current_date;
1141 if( p_vout->b_interface )
1143 RenderInterface( p_vout );
1154 * Sleep, wake up and display rendered picture
1158 /* Store render time */
1159 p_vout->render_time = mdate() - current_date;
1162 /* Give back change lock */
1163 vlc_mutex_unlock( &p_vout->change_lock );
1165 /* Sleep a while or until a given date */
1166 if( display_date != 0 )
1168 mwait( display_date );
1172 msleep( VOUT_IDLE_SLEEP );
1175 /* On awakening, take back lock and send immediately picture to display,
1176 * then swap buffers */
1177 vlc_mutex_lock( &p_vout->change_lock );
1179 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1180 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1182 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1184 p_vout->p_sys_display( p_vout );
1185 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1189 * Check events and manage thread
1191 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1193 /* A fatal error occured, and the thread must terminate immediately,
1194 * without displaying anything - setting b_error to 1 cause the
1195 * immediate end of the main while() loop. */
1196 p_vout->b_error = 1;
1201 * Error loop - wait until the thread destruction is requested
1203 if( p_vout->b_error )
1205 ErrorThread( p_vout );
1209 EndThread( p_vout );
1210 DestroyThread( p_vout, THREAD_OVER );
1211 intf_DbgMsg( "thread end\n" );
1214 /*****************************************************************************
1215 * ErrorThread: RunThread() error loop
1216 *****************************************************************************
1217 * This function is called when an error occured during thread main's loop. The
1218 * thread can still receive feed, but must be ready to terminate as soon as
1220 *****************************************************************************/
1221 static void ErrorThread( vout_thread_t *p_vout )
1223 /* Wait until a `die' order */
1225 while( !p_vout->b_die )
1228 msleep( VOUT_IDLE_SLEEP );
1232 /*****************************************************************************
1233 * EndThread: thread destruction
1234 *****************************************************************************
1235 * This function is called when the thread ends after a sucessfull
1236 * initialization. It frees all ressources allocated by InitThread.
1237 *****************************************************************************/
1238 static void EndThread( vout_thread_t *p_vout )
1240 int i_index; /* index in heap */
1244 *p_vout->pi_status = THREAD_END;
1246 /* Destroy all remaining pictures and subpictures */
1247 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1249 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1251 free( p_vout->p_picture[i_index].p_data );
1253 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1255 free( p_vout->p_subpicture[i_index].p_data );
1259 /* Destroy translation tables */
1260 vout_EndYUV( p_vout );
1261 p_vout->p_sys_end( p_vout );
1264 /*****************************************************************************
1265 * DestroyThread: thread destruction
1266 *****************************************************************************
1267 * This function is called when the thread ends. It frees all ressources
1268 * allocated by CreateThread. Status is available at this stage.
1269 *****************************************************************************/
1270 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1272 int *pi_status; /* status adress */
1274 /* Store status adress */
1276 pi_status = p_vout->pi_status;
1278 /* Destroy thread structures allocated by Create and InitThread */
1279 vout_UnloadFont( p_vout->p_default_font );
1280 vout_UnloadFont( p_vout->p_large_font );
1281 p_vout->p_sys_destroy( p_vout );
1283 *pi_status = i_status;
1286 /*****************************************************************************
1287 * Print: print simple text on a picture
1288 *****************************************************************************
1289 * This function will print a simple text on the picture. It is designed to
1290 * print debugging or general informations.
1291 *****************************************************************************/
1292 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 )
1294 int i_text_height; /* total text height */
1295 int i_text_width; /* total text width */
1297 /* Update upper left coordinates according to alignment */
1298 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1299 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1301 /* Set area and print text */
1302 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1303 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1304 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1305 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1306 p_vout->i_white_pixel, 0, 0,
1311 /*****************************************************************************
1312 * SetBufferArea: activate an area in current buffer
1313 *****************************************************************************
1314 * This function is called when something is rendered on the current buffer.
1315 * It set the area as active and prepare it to be cleared on next rendering.
1316 * Pay attention to the fact that in this functions, i_h is in fact the end y
1317 * coordinate of the new area.
1318 *****************************************************************************/
1319 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1321 vout_buffer_t * p_buffer; /* current buffer */
1322 int i_area_begin, i_area_end; /* area vertical extension */
1323 int i_area, i_area_copy; /* area index */
1324 int i_area_shift; /* shift distance for areas */
1326 /* Choose buffer and modify h to end of area position */
1327 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1331 * Remove part of the area which is inside the picture - this is done
1332 * by calling again SetBufferArea with the correct areas dimensions.
1334 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1336 i_area_begin = p_buffer->i_pic_y;
1337 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1339 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1340 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1341 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1343 /* Keep the stripe above the picture, if any */
1344 if( i_y < i_area_begin )
1346 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1348 /* Keep the stripe below the picture, if any */
1349 if( i_h > i_area_end )
1351 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1357 /* Skip some extensions until interesting areas */
1359 (i_area < p_buffer->i_areas) &&
1360 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1366 if( i_area == p_buffer->i_areas )
1368 /* New area is below all existing ones: just add it at the end of the
1369 * array, if possible - else, append it to the last one */
1370 if( i_area < VOUT_MAX_AREAS )
1372 p_buffer->pi_area_begin[i_area] = i_y;
1373 p_buffer->pi_area_end[i_area] = i_h;
1374 p_buffer->i_areas++;
1379 intf_DbgMsg("areas overflow\n");
1381 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1386 i_area_begin = p_buffer->pi_area_begin[i_area];
1387 i_area_end = p_buffer->pi_area_end[i_area];
1389 if( i_y < i_area_begin )
1391 if( i_h >= i_area_begin - 1 )
1393 /* Extend area above */
1394 p_buffer->pi_area_begin[i_area] = i_y;
1398 /* Create a new area above : merge last area if overflow, then
1399 * move all old areas down */
1400 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1403 intf_DbgMsg("areas overflow\n");
1405 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1409 p_buffer->i_areas++;
1411 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1413 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1414 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1416 p_buffer->pi_area_begin[i_area] = i_y;
1417 p_buffer->pi_area_end[i_area] = i_h;
1421 if( i_h > i_area_end )
1423 /* Find further areas which can be merged with the new one */
1424 for( i_area_copy = i_area + 1;
1425 (i_area_copy < p_buffer->i_areas) &&
1426 (p_buffer->pi_area_begin[i_area] <= i_h);
1433 if( i_area_copy != i_area )
1435 /* Merge with last possible areas */
1436 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1438 /* Shift lower areas upward */
1439 i_area_shift = i_area_copy - i_area;
1440 p_buffer->i_areas -= i_area_shift;
1441 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1443 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1444 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1449 /* Extend area below */
1450 p_buffer->pi_area_end[i_area] = i_h;
1456 /*****************************************************************************
1457 * SetBufferPicture: clear buffer and set picture area
1458 *****************************************************************************
1459 * This function is called before any rendering. It clears the current
1460 * rendering buffer and set the new picture area. If the picture pointer is
1461 * NULL, then no picture area is defined. Floating operations are avoided since
1462 * some MMX calculations may follow.
1463 *****************************************************************************/
1464 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1466 vout_buffer_t * p_buffer; /* current buffer */
1467 int i_pic_x, i_pic_y; /* picture position */
1468 int i_pic_width, i_pic_height; /* picture dimensions */
1469 int i_old_pic_y, i_old_pic_height; /* old picture area */
1470 int i_vout_width, i_vout_height; /* display dimensions */
1471 int i_area; /* area index */
1472 int i_data_index; /* area data index */
1473 int i_data_size; /* area data size, in 256 bytes blocs */
1474 u64 * p_data; /* area data, for clearing */
1475 byte_t * p_data8; /* area data, for clearing (slow) */
1477 /* Choose buffer and set display dimensions */
1478 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1479 i_vout_width = p_vout->i_width;
1480 i_vout_height = p_vout->i_height;
1483 * Computes new picture size
1487 /* Try horizontal scaling first - width must be a mutiple of 16 */
1488 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1489 i_vout_width : p_pic->i_width) & ~0xf;
1490 switch( p_pic->i_aspect_ratio )
1492 case AR_3_4_PICTURE:
1493 i_pic_height = i_pic_width * 3 / 4;
1495 case AR_16_9_PICTURE:
1496 i_pic_height = i_pic_width * 9 / 16;
1498 case AR_221_1_PICTURE:
1499 i_pic_height = i_pic_width * 100 / 221;
1501 case AR_SQUARE_PICTURE:
1503 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1507 /* If picture dimensions using horizontal scaling are too large, use
1508 * vertical scaling. Since width must be a multiple of 16, height is
1509 * adjusted again after. */
1510 if( i_pic_height > i_vout_height )
1512 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1513 i_vout_height : p_pic->i_height;
1514 switch( p_pic->i_aspect_ratio )
1516 case AR_3_4_PICTURE:
1517 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1518 i_pic_height = i_pic_width * 3 / 4;
1520 case AR_16_9_PICTURE:
1521 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1522 i_pic_height = i_pic_width * 9 / 16;
1524 case AR_221_1_PICTURE:
1525 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1526 i_pic_height = i_pic_width * 100 / 221;
1528 case AR_SQUARE_PICTURE:
1530 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1531 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1536 /* Set picture position */
1537 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1538 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1542 /* No picture: size is 0 */
1550 * Set new picture size - if is is smaller than the previous one, clear
1551 * around it. Since picture are centered, only their size is tested.
1553 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1555 i_old_pic_y = p_buffer->i_pic_y;
1556 i_old_pic_height = p_buffer->i_pic_height;
1557 p_buffer->i_pic_x = i_pic_x;
1558 p_buffer->i_pic_y = i_pic_y;
1559 p_buffer->i_pic_width = i_pic_width;
1560 p_buffer->i_pic_height = i_pic_height;
1561 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1565 p_buffer->i_pic_x = i_pic_x;
1566 p_buffer->i_pic_y = i_pic_y;
1567 p_buffer->i_pic_width = i_pic_width;
1568 p_buffer->i_pic_height = i_pic_height;
1574 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1577 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1578 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1580 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1581 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1582 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1584 /* Clear 256 bytes block */
1585 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1586 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1587 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1588 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1589 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1590 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1591 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1592 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1594 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1596 /* Clear remaining 16 bytes blocks */
1597 *p_data++ = 0; *p_data++ = 0;
1599 p_data8 = (byte_t *)p_data;
1600 for( i_data_index = i_data_size % 16; i_data_index--; )
1602 /* Clear remaining bytes */
1610 p_buffer->i_areas = 0;
1613 /*****************************************************************************
1614 * RenderPicture: render a picture
1615 *****************************************************************************
1616 * This function convert a picture from a video heap to a pixel-encoded image
1617 * and copy it to the current rendering buffer. No lock is required, since the
1618 * rendered picture has been determined as existant, and will only be destroyed
1619 * by the vout thread later.
1620 *****************************************************************************/
1621 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1624 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1625 mtime_t render_time; /* picture rendering time */
1627 vout_buffer_t * p_buffer; /* rendering buffer */
1628 byte_t * p_pic_data; /* convertion destination */
1630 /* Get and set rendering informations */
1631 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1632 p_pic_data = p_buffer->p_data +
1633 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1634 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1636 render_time = mdate();
1640 * Choose appropriate rendering function and render picture
1642 switch( p_pic->i_type )
1644 case YUV_420_PICTURE:
1645 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1646 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1647 p_pic->i_width, p_pic->i_height,
1648 p_buffer->i_pic_width, p_buffer->i_pic_height,
1649 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1650 p_pic->i_matrix_coefficients );
1652 case YUV_422_PICTURE:
1653 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1654 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1655 p_pic->i_width, p_pic->i_height,
1656 p_buffer->i_pic_width, p_buffer->i_pic_height,
1657 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1658 p_pic->i_matrix_coefficients );
1660 case YUV_444_PICTURE:
1661 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1662 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1663 p_pic->i_width, p_pic->i_height,
1664 p_buffer->i_pic_width, p_buffer->i_pic_height,
1665 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1666 p_pic->i_matrix_coefficients );
1670 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1676 /* Print picture date and rendering time */
1677 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1678 p_vout->i_buffer_index, (long) (mdate() - render_time),
1679 mstrtime( psz_date, p_pic->date ));
1683 /*****************************************************************************
1684 * RenderPictureInfo: print additionnal informations on a picture
1685 *****************************************************************************
1686 * This function will print informations such as fps and other picture
1687 * dependant informations.
1688 *****************************************************************************/
1689 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1691 #if defined(STATS) || defined(DEBUG)
1692 char psz_buffer[256]; /* string buffer */
1697 * Print FPS rate in upper right corner
1699 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1701 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1702 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1703 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1704 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1708 * Print frames count and loop time in upper left corner
1710 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1711 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1712 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1717 * Print picture information in lower right corner
1719 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1720 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1721 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1722 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1723 p_pic->i_width, p_pic->i_height,
1724 p_pic->i_display_width, p_pic->i_display_height,
1725 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1726 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1727 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1728 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1729 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1730 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1731 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1732 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1733 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1734 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1738 /*****************************************************************************
1739 * RenderIdle: render idle picture
1740 *****************************************************************************
1741 * This function will print something on the screen. It will return 0 if
1742 * nothing has been rendered, or 1 if something has been changed on the screen.
1743 * Note that if you absolutely want something to be printed, you will have
1744 * to force it by setting the last idle date to 0.
1745 * Unlike other rendering functions, this one calls the SetBufferPicture
1746 * function when needed.
1747 *****************************************************************************/
1748 static int RenderIdle( vout_thread_t *p_vout )
1750 int i_x = 0, i_y = 0; /* text position */
1751 int i_width, i_height; /* text size */
1752 mtime_t current_date; /* current date */
1753 const char *psz_text = "waiting for stream ..."; /* text to display */
1756 current_date = mdate();
1757 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1758 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1760 SetBufferPicture( p_vout, NULL );
1761 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1762 &i_width, &i_height );
1763 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1765 vout_Print( p_vout->p_large_font,
1766 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1767 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1768 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1769 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1770 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1771 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1778 /*****************************************************************************
1779 * RenderInfo: render additionnal informations
1780 *****************************************************************************
1781 * This function render informations which do not depend of the current picture
1783 *****************************************************************************/
1784 static void RenderInfo( vout_thread_t *p_vout )
1787 char psz_buffer[256]; /* string buffer */
1788 int i_ready_pic = 0; /* ready pictures */
1789 int i_reserved_pic = 0; /* reserved pictures */
1790 int i_picture; /* picture index */
1795 * Print thread state in lower left corner
1797 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1799 switch( p_vout->p_picture[i_picture].i_status )
1801 case RESERVED_PICTURE:
1802 case RESERVED_DATED_PICTURE:
1803 case RESERVED_DISP_PICTURE:
1811 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1812 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1813 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1817 /*****************************************************************************
1818 * RenderSubPicture: render a subpicture
1819 *****************************************************************************
1820 * This function render a sub picture unit.
1821 *****************************************************************************/
1822 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1824 p_vout_font_t p_font; /* text font */
1825 int i_width, i_height; /* subpicture dimensions */
1827 switch( p_subpic->i_type )
1829 case TEXT_SUBPICTURE: /* single line text */
1830 /* Select default font if not specified */
1831 p_font = p_subpic->type.text.p_font;
1832 if( p_font == NULL )
1834 p_font = p_vout->p_default_font;
1837 /* Computes text size (width and height fields are ignored) and print it */
1838 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1839 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1840 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1842 vout_Print( p_font, 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, p_subpic->type.text.i_border_color,
1847 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1849 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1855 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1860 /*****************************************************************************
1861 * RenderInterface: render the interface
1862 *****************************************************************************
1863 * This function render the interface, if any.
1864 *****************************************************************************/
1865 static void RenderInterface( vout_thread_t *p_vout )
1867 int i_height, i_text_height; /* total and text height */
1868 int i_width_1, i_width_2; /* text width */
1869 int i_byte; /* byte index */
1870 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1871 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1874 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1875 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1876 i_height += i_text_height;
1878 /* Render background */
1879 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1880 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1884 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1887 /* Render text, if not larger than screen */
1888 if( i_width_1 < p_vout->i_width )
1890 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1891 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1892 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1893 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1894 OUTLINED_TEXT, psz_text_1 );
1896 if( i_width_2 < p_vout->i_width )
1898 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1899 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1900 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1901 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1902 OUTLINED_TEXT, psz_text_2 );
1905 /* Activate modified area */
1906 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1909 /*****************************************************************************
1910 * Synchronize: update synchro level depending of heap state
1911 *****************************************************************************
1912 * This function is called during the main vout loop.
1913 *****************************************************************************/
1914 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1916 int i_synchro_inc = 0;
1917 //???? gore following
1918 static int i_panic_count = 0;
1919 static int i_last_synchro_inc = 0;
1920 static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1921 static int i_truc = 1;
1923 //?? heap size is p_vout->i_pictures
1927 // intf_Msg("PANIC %d\n", i_panic_count++);
1930 if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1934 else if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1939 if( i_delay < 10000 )
1944 if( i_delay < 20000 )
1948 else if( i_delay > 50000 )
1953 if( i_synchro_inc*i_last_synchro_inc < 0 )
1961 if( i_truc > VOUT_SYNCHRO_LEVEL_MAX || i_delay == 0 )
1966 r_synchro_level += (float)i_synchro_inc / i_truc;
1967 p_vout->i_synchro_level = (int) r_synchro_level;
1969 if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
1971 r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
1974 // printf( "synchro level : %d, (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
1975 // i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
1976 i_last_synchro_inc = i_synchro_inc;
1979 /*****************************************************************************
1980 * Manage: manage thread
1981 *****************************************************************************
1982 * This function will handle changes in thread configuration.
1983 *****************************************************************************/
1984 static int Manage( vout_thread_t *p_vout )
1987 if( p_vout->i_changes )
1989 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1990 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1994 /* On gamma or grayscale change, rebuild tables */
1995 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1998 if( vout_ResetYUV( p_vout ) )
2000 intf_ErrMsg("error: can't rebuild convertion tables\n");
2005 /* Clear changes flags which does not need management or have been
2007 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2008 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
2009 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
2011 /* Detect unauthorized changes */
2012 if( p_vout->i_changes )
2014 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
2015 * function, it means they should not be authorized */
2016 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2023 /*****************************************************************************
2024 * Align: align a subpicture in the screen
2025 *****************************************************************************
2026 * This function is used for rendering text or subpictures. It returns non 0
2027 * it the final aera is not fully included in display area. Return coordinates
2029 *****************************************************************************/
2030 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2031 int i_width, int i_height, int i_h_align, int i_v_align )
2033 /* Align horizontally */
2037 *pi_x -= i_width / 2;
2040 *pi_x += (p_vout->i_width - i_width) / 2;
2046 *pi_x += p_vout->i_width - i_width;
2050 /* Align vertically */
2054 *pi_y -= i_height / 2;
2057 *pi_y += (p_vout->i_height - i_height) / 2;
2063 *pi_y += p_vout->i_height - i_height;
2065 case SUBTITLE_RALIGN:
2066 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2067 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2071 /* Return non 0 if clipping failed */
2072 return( (*pi_x < 0) || (*pi_y < 0) ||
2073 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2076 /*****************************************************************************
2077 * SetPalette: sets an 8 bpp palette
2078 *****************************************************************************
2079 * This function is just a prototype that does nothing. Architectures that
2080 * support palette allocation should override it.
2081 *****************************************************************************/
2082 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2083 u16 *green, u16 *blue, u16 *transp )
2085 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );