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 *****************************************************************************/
18 #include <dlfcn.h> /* plugins */
23 #include "vlc_thread.h"
25 #include "video_output.h"
26 #include "video_text.h"
27 #include "video_yuv.h"
31 /*****************************************************************************
33 *****************************************************************************/
34 static int BinaryLog ( u32 i );
35 static void MaskToShift ( int *pi_left, int *pi_right, u32 i_mask );
36 static int InitThread ( vout_thread_t *p_vout );
37 static void RunThread ( vout_thread_t *p_vout );
38 static void ErrorThread ( vout_thread_t *p_vout );
39 static void EndThread ( vout_thread_t *p_vout );
40 static void DestroyThread ( vout_thread_t *p_vout, int i_status );
41 static void Print ( vout_thread_t *p_vout, int i_x, int i_y,
42 int i_h_align, int i_v_align,
43 unsigned char *psz_text );
44 static void SetBufferArea ( vout_thread_t *p_vout, int i_x, int i_y,
46 static void SetBufferPicture ( vout_thread_t *p_vout, picture_t *p_pic );
47 static void RenderPicture ( vout_thread_t *p_vout, picture_t *p_pic );
48 static void RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
49 static void RenderSubPicture ( vout_thread_t *p_vout,
50 subpicture_t *p_subpic );
51 static void RenderInterface ( vout_thread_t *p_vout );
52 static int RenderIdle ( vout_thread_t *p_vout );
53 static void RenderInfo ( vout_thread_t *p_vout );
54 static void Synchronize ( vout_thread_t *p_vout, s64 i_delay );
55 static int Manage ( vout_thread_t *p_vout );
56 static int Align ( vout_thread_t *p_vout, int *pi_x,
57 int *pi_y, int i_width, int i_height,
58 int i_h_align, int i_v_align );
59 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
60 u16 *green, u16 *blue, u16 *transp );
62 /*****************************************************************************
63 * vout_CreateThread: creates a new video output thread
64 *****************************************************************************
65 * This function creates a new video output thread, and returns a pointer
66 * to its description. On error, it returns NULL.
67 * If pi_status is NULL, then the function will block until the thread is ready.
68 * If not, it will be updated using one of the THREAD_* constants.
69 *****************************************************************************/
70 vout_thread_t * vout_CreateThread ( char *psz_display, int i_root_window,
71 int i_width, int i_height, int *pi_status, int i_method )
73 vout_thread_t * p_vout; /* thread descriptor */
74 int i_status; /* thread status */
75 int i_index; /* index for array initialization */
79 /* Allocate descriptor */
81 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
84 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
88 /* Initialize method-dependent functions */
89 psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
91 psz_plugin = malloc( sizeof("./video_output/vout_.so") + strlen(psz_method) );
92 sprintf( psz_plugin, "./video_output/vout_%s.so", psz_method );
94 p_vout->p_vout_plugin = dlopen( psz_plugin, RTLD_NOW | RTLD_GLOBAL );
96 if( p_vout->p_vout_plugin == NULL )
98 intf_ErrMsg( "error: could not open video plugin %s\n", psz_plugin );
106 p_vout->p_sys_create = dlsym(p_vout->p_vout_plugin, "vout_SysCreate");
107 p_vout->p_sys_init = dlsym(p_vout->p_vout_plugin, "vout_SysInit");
108 p_vout->p_sys_end = dlsym(p_vout->p_vout_plugin, "vout_SysEnd");
109 p_vout->p_sys_destroy = dlsym(p_vout->p_vout_plugin, "vout_SysDestroy");
110 p_vout->p_sys_manage = dlsym(p_vout->p_vout_plugin, "vout_SysManage");
111 p_vout->p_sys_display = dlsym(p_vout->p_vout_plugin, "vout_SysDisplay");
113 /* Initialize thread properties - thread id and locks will be initialized
117 p_vout->b_active = 0;
118 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
119 *p_vout->pi_status = THREAD_CREATE;
121 /* Initialize some fields used by the system-dependant method - these fields will
122 * probably be modified by the method, and are only preferences */
123 p_vout->i_changes = 0;
124 p_vout->i_width = i_width;
125 p_vout->i_height = i_height;
126 p_vout->i_bytes_per_line = i_width * 2;
127 p_vout->i_screen_depth = 15;
128 p_vout->i_bytes_per_pixel = 2;
129 p_vout->f_gamma = VOUT_GAMMA;
131 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
133 p_vout->b_interface = 0;
136 p_vout->p_set_palette = SetPalette;
138 intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
139 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
140 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
142 /* Initialize idle screen */
143 p_vout->last_display_date = mdate();
144 p_vout->last_display_date = 0;
145 p_vout->last_idle_date = 0;
148 /* Initialize statistics fields */
149 p_vout->render_time = 0;
150 p_vout->c_fps_samples = 0;
153 /* Initialize buffer index */
154 p_vout->i_buffer_index = 0;
156 /* Initialize pictures and subpictures - translation tables and functions
157 * will be initialized later in InitThread */
158 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
160 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
161 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
162 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
163 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
165 p_vout->i_pictures = 0;
167 /* Initialize synchronization informations */
168 p_vout->i_synchro_level = VOUT_SYNCHRO_LEVEL_START;
170 /* Create and initialize system-dependant method - this function issues its
171 * own error messages */
172 if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
174 dlclose( p_vout->p_vout_plugin );
178 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
179 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
180 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
181 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
183 /* Calculate shifts from system-updated masks */
184 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
185 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
186 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
188 /* Set some useful colors */
189 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
190 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
191 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
192 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
194 /* Load fonts - fonts must be initialized after the system method since
195 * they may be dependant on screen depth and other thread properties */
196 p_vout->p_default_font = vout_LoadFont( VOUT_DEFAULT_FONT );
197 if( p_vout->p_default_font == NULL )
199 p_vout->p_sys_destroy( p_vout );
200 dlclose( p_vout->p_vout_plugin );
204 p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
205 if( p_vout->p_large_font == NULL )
207 vout_UnloadFont( p_vout->p_default_font );
208 p_vout->p_sys_destroy( p_vout );
209 dlclose( p_vout->p_vout_plugin );
214 /* Create thread and set locks */
215 vlc_mutex_init( &p_vout->picture_lock );
216 vlc_mutex_init( &p_vout->subpicture_lock );
217 vlc_mutex_init( &p_vout->change_lock );
218 vlc_mutex_lock( &p_vout->change_lock );
219 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
221 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
222 vout_UnloadFont( p_vout->p_default_font );
223 vout_UnloadFont( p_vout->p_large_font );
224 p_vout->p_sys_destroy( p_vout );
225 dlclose( p_vout->p_vout_plugin );
230 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
231 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
233 /* If status is NULL, wait until the thread is created */
234 if( pi_status == NULL )
238 msleep( THREAD_SLEEP );
239 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
240 && (i_status != THREAD_FATAL) );
241 if( i_status != THREAD_READY )
249 /*****************************************************************************
250 * vout_DestroyThread: destroys a previously created thread
251 *****************************************************************************
252 * Destroy a terminated thread.
253 * The function will request a destruction of the specified thread. If pi_error
254 * is NULL, it will return once the thread is destroyed. Else, it will be
255 * update using one of the THREAD_* constants.
256 *****************************************************************************/
257 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
259 int i_status; /* thread status */
263 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
264 *p_vout->pi_status = THREAD_DESTROY;
266 /* Request thread destruction */
269 /* If status is NULL, wait until thread has been destroyed */
270 if( pi_status == NULL )
274 msleep( THREAD_SLEEP );
275 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
276 && (i_status != THREAD_FATAL) );
280 /*****************************************************************************
281 * vout_DisplaySubPicture: display a subpicture unit
282 *****************************************************************************
283 * Remove the reservation flag of an subpicture, which will cause it to be ready
284 * for display. The picture does not need to be locked, since it is ignored by
285 * the output thread if is reserved.
286 *****************************************************************************/
287 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
290 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
291 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
295 /* Check if status is valid */
296 if( p_subpic->i_status != RESERVED_SUBPICTURE )
298 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
299 p_subpic->i_status );
303 /* Remove reservation flag */
304 p_subpic->i_status = READY_SUBPICTURE;
307 /* Send subpicture informations */
308 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
309 p_subpic, p_subpic->i_type,
310 mstrtime( psz_begin_date, p_subpic->begin_date ),
311 mstrtime( psz_end_date, p_subpic->end_date ) );
315 /*****************************************************************************
316 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
317 *****************************************************************************
318 * This function create a reserved subpicture in the video output heap.
319 * A null pointer is returned if the function fails. This method provides an
320 * already allocated zone of memory in the spu data fields. It needs locking
321 * since several pictures can be created by several producers threads.
322 *****************************************************************************/
323 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
326 int i_subpic; /* subpicture index */
327 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
328 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
331 vlc_mutex_lock( &p_vout->subpicture_lock );
334 * Look for an empty place
336 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
338 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
340 /* Subpicture is marked for destruction, but is still allocated */
341 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
342 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
344 /* Memory size do match or is smaller : memory will not be reallocated,
345 * and function can end immediately - this is the best possible case,
346 * since no memory allocation needs to be done */
347 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
349 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
350 &p_vout->p_subpicture[i_subpic] );
352 vlc_mutex_unlock( &p_vout->subpicture_lock );
353 return( &p_vout->p_subpicture[i_subpic] );
355 else if( p_destroyed_subpic == NULL )
357 /* Memory size do not match, but subpicture index will be kept in
358 * case no other place are left */
359 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
362 else if( (p_free_subpic == NULL) &&
363 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
365 /* Subpicture is empty and ready for allocation */
366 p_free_subpic = &p_vout->p_subpicture[i_subpic];
370 /* If no free subpicture is available, use a destroyed subpicture */
371 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
373 /* No free subpicture or matching destroyed subpicture has been found, but
374 * a destroyed subpicture is still avalaible */
375 free( p_destroyed_subpic->p_data );
376 p_free_subpic = p_destroyed_subpic;
382 if( p_free_subpic != NULL )
384 /* Allocate memory */
387 case TEXT_SUBPICTURE: /* text subpicture */
388 p_free_subpic->p_data = malloc( i_size + 1 );
392 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
393 p_free_subpic->p_data = NULL;
398 if( p_free_subpic->p_data != NULL )
399 { /* Copy subpicture informations, set some default values */
400 p_free_subpic->i_type = i_type;
401 p_free_subpic->i_status = RESERVED_SUBPICTURE;
402 p_free_subpic->i_size = i_size;
403 p_free_subpic->i_x = 0;
404 p_free_subpic->i_y = 0;
405 p_free_subpic->i_width = 0;
406 p_free_subpic->i_height = 0;
407 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
408 p_free_subpic->i_vertical_align = CENTER_RALIGN;
412 /* Memory allocation failed : set subpicture as empty */
413 p_free_subpic->i_type = EMPTY_SUBPICTURE;
414 p_free_subpic->i_status = FREE_SUBPICTURE;
415 p_free_subpic = NULL;
416 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
420 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
422 vlc_mutex_unlock( &p_vout->subpicture_lock );
423 return( p_free_subpic );
426 /* No free or destroyed subpicture could be found */
427 intf_DbgMsg( "warning: heap is full\n" );
428 vlc_mutex_unlock( &p_vout->subpicture_lock );
432 /*****************************************************************************
433 * vout_DestroySubPicture: remove a subpicture from the heap
434 *****************************************************************************
435 * This function frees a previously reserved subpicture.
436 * It is meant to be used when the construction of a picture aborted.
437 * This function does not need locking since reserved subpictures are ignored
438 * by the output thread.
439 *****************************************************************************/
440 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
443 /* Check if status is valid */
444 if( p_subpic->i_status != RESERVED_SUBPICTURE )
446 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
447 p_subpic, p_subpic->i_status );
451 p_subpic->i_status = DESTROYED_SUBPICTURE;
454 intf_DbgMsg("subpicture %p\n", p_subpic);
458 /*****************************************************************************
459 * vout_DisplayPicture: display a picture
460 *****************************************************************************
461 * Remove the reservation flag of a picture, which will cause it to be ready for
462 * display. The picture won't be displayed until vout_DatePicture has been
464 *****************************************************************************/
465 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
467 vlc_mutex_lock( &p_vout->picture_lock );
468 switch( p_pic->i_status )
470 case RESERVED_PICTURE:
471 p_pic->i_status = RESERVED_DISP_PICTURE;
473 case RESERVED_DATED_PICTURE:
474 p_pic->i_status = READY_PICTURE;
478 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
484 intf_DbgMsg("picture %p\n", p_pic);
486 vlc_mutex_unlock( &p_vout->picture_lock );
489 /*****************************************************************************
490 * vout_DatePicture: date a picture
491 *****************************************************************************
492 * Remove the reservation flag of a picture, which will cause it to be ready for
493 * display. The picture won't be displayed until vout_DisplayPicture has been
495 *****************************************************************************/
496 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
499 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
502 vlc_mutex_lock( &p_vout->picture_lock );
504 switch( p_pic->i_status )
506 case RESERVED_PICTURE:
507 p_pic->i_status = RESERVED_DATED_PICTURE;
509 case RESERVED_DISP_PICTURE:
510 p_pic->i_status = READY_PICTURE;
514 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
520 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
522 vlc_mutex_unlock( &p_vout->picture_lock );
525 /*****************************************************************************
526 * vout_CreatePicture: allocate a picture in the video output heap.
527 *****************************************************************************
528 * This function create a reserved image in the video output heap.
529 * A null pointer is returned if the function fails. This method provides an
530 * already allocated zone of memory in the picture data fields. It needs locking
531 * since several pictures can be created by several producers threads.
532 *****************************************************************************/
533 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
534 int i_width, int i_height )
536 int i_picture; /* picture index */
537 int i_chroma_width = 0; /* chroma width */
538 picture_t * p_free_picture = NULL; /* first free picture */
539 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
542 vlc_mutex_lock( &p_vout->picture_lock );
545 * Look for an empty place
547 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
549 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
551 /* Picture is marked for destruction, but is still allocated - note
552 * that if width and type are the same for two pictures, chroma_width
553 * should also be the same */
554 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
555 (p_vout->p_picture[i_picture].i_height == i_height) &&
556 (p_vout->p_picture[i_picture].i_width == i_width) )
558 /* Memory size do match : memory will not be reallocated, and function
559 * can end immediately - this is the best possible case, since no
560 * memory allocation needs to be done */
561 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
562 p_vout->i_pictures++;
564 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
565 &p_vout->p_picture[i_picture] );
567 vlc_mutex_unlock( &p_vout->picture_lock );
568 return( &p_vout->p_picture[i_picture] );
570 else if( p_destroyed_picture == NULL )
572 /* Memory size do not match, but picture index will be kept in
573 * case no other place are left */
574 p_destroyed_picture = &p_vout->p_picture[i_picture];
577 else if( (p_free_picture == NULL) &&
578 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
580 /* Picture is empty and ready for allocation */
581 p_free_picture = &p_vout->p_picture[i_picture];
585 /* If no free picture is available, use a destroyed picture */
586 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
588 /* No free picture or matching destroyed picture has been found, but
589 * a destroyed picture is still avalaible */
590 free( p_destroyed_picture->p_data );
591 p_free_picture = p_destroyed_picture;
597 if( p_free_picture != NULL )
599 /* Allocate memory */
602 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
603 i_chroma_width = i_width / 2;
604 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
605 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
606 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
607 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
609 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
610 i_chroma_width = i_width / 2;
611 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
612 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
613 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
614 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
616 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
617 i_chroma_width = i_width;
618 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
619 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
620 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
621 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
625 intf_DbgMsg("error: unknown picture type %d\n", i_type );
626 p_free_picture->p_data = NULL;
631 if( p_free_picture->p_data != NULL )
633 /* Copy picture informations, set some default values */
634 p_free_picture->i_type = i_type;
635 p_free_picture->i_status = RESERVED_PICTURE;
636 p_free_picture->i_matrix_coefficients = 1;
637 p_free_picture->i_width = i_width;
638 p_free_picture->i_height = i_height;
639 p_free_picture->i_chroma_width = i_chroma_width;
640 p_free_picture->i_display_horizontal_offset = 0;
641 p_free_picture->i_display_vertical_offset = 0;
642 p_free_picture->i_display_width = i_width;
643 p_free_picture->i_display_height = i_height;
644 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
645 p_free_picture->i_refcount = 0;
646 p_vout->i_pictures++;
650 /* Memory allocation failed : set picture as empty */
651 p_free_picture->i_type = EMPTY_PICTURE;
652 p_free_picture->i_status = FREE_PICTURE;
653 p_free_picture = NULL;
654 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
658 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
660 vlc_mutex_unlock( &p_vout->picture_lock );
661 return( p_free_picture );
664 /* No free or destroyed picture could be found */
665 intf_DbgMsg( "warning: heap is full\n" );
666 vlc_mutex_unlock( &p_vout->picture_lock );
670 /*****************************************************************************
671 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
672 *****************************************************************************
673 * This function frees a previously reserved picture or a permanent
674 * picture. It is meant to be used when the construction of a picture aborted.
675 * Note that the picture will be destroyed even if it is linked !
676 *****************************************************************************/
677 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
679 vlc_mutex_lock( &p_vout->picture_lock );
682 /* Check if picture status is valid */
683 if( (p_pic->i_status != RESERVED_PICTURE) &&
684 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
685 (p_pic->i_status != RESERVED_DISP_PICTURE) )
687 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
691 p_pic->i_status = DESTROYED_PICTURE;
692 p_vout->i_pictures--;
695 intf_DbgMsg("picture %p\n", p_pic);
697 vlc_mutex_unlock( &p_vout->picture_lock );
700 /*****************************************************************************
701 * vout_LinkPicture: increment reference counter of a picture
702 *****************************************************************************
703 * This function increment the reference counter of a picture in the video
704 * heap. It needs a lock since several producer threads can access the picture.
705 *****************************************************************************/
706 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
708 vlc_mutex_lock( &p_vout->picture_lock );
712 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
715 vlc_mutex_unlock( &p_vout->picture_lock );
718 /*****************************************************************************
719 * vout_UnlinkPicture: decrement reference counter of a picture
720 *****************************************************************************
721 * This function decrement the reference counter of a picture in the video heap.
722 *****************************************************************************/
723 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
725 vlc_mutex_lock( &p_vout->picture_lock );
729 if( p_pic->i_refcount < 0 )
731 intf_DbgMsg("error: refcount < 0\n");
732 p_pic->i_refcount = 0;
736 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
738 p_pic->i_status = DESTROYED_PICTURE;
739 p_vout->i_pictures--;
743 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
746 vlc_mutex_unlock( &p_vout->picture_lock );
749 /*****************************************************************************
750 * vout_SetBuffers: set buffers adresses
751 *****************************************************************************
752 * This function is called by system drivers to set buffers video memory
754 *****************************************************************************/
755 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
757 /* No picture previously */
758 p_vout->p_buffer[0].i_pic_x = 0;
759 p_vout->p_buffer[0].i_pic_y = 0;
760 p_vout->p_buffer[0].i_pic_width = 0;
761 p_vout->p_buffer[0].i_pic_height = 0;
762 p_vout->p_buffer[1].i_pic_x = 0;
763 p_vout->p_buffer[1].i_pic_y = 0;
764 p_vout->p_buffer[1].i_pic_width = 0;
765 p_vout->p_buffer[1].i_pic_height = 0;
767 /* The first area covers all the screen */
768 p_vout->p_buffer[0].i_areas = 1;
769 p_vout->p_buffer[0].pi_area_begin[0] = 0;
770 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
771 p_vout->p_buffer[1].i_areas = 1;
772 p_vout->p_buffer[1].pi_area_begin[0] = 0;
773 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
776 p_vout->p_buffer[0].p_data = p_buf1;
777 p_vout->p_buffer[1].p_data = p_buf2;
780 /*****************************************************************************
781 * vout_Pixel2RGB: return red, green and blue from pixel value
782 *****************************************************************************
783 * Return color values, in 0-255 range, of the decomposition of a pixel. This
784 * is a slow routine and should only be used for initialization phase.
785 *****************************************************************************/
786 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
788 *pi_red = i_pixel & p_vout->i_red_mask;
789 *pi_green = i_pixel & p_vout->i_green_mask;
790 *pi_blue = i_pixel & p_vout->i_blue_mask;
793 /* following functions are local */
795 /*****************************************************************************
796 * BinaryLog: computes the base 2 log of a binary value
797 *****************************************************************************
798 * This functions is used by MaskToShift, to get a bit index from a binary
800 *****************************************************************************/
801 static int BinaryLog(u32 i)
826 if (i != ((u32)1 << i_log))
828 intf_ErrMsg("internal error: binary log overflow\n");
834 /*****************************************************************************
835 * MaskToShift: transform a color mask into right and left shifts
836 *****************************************************************************
837 * This function is used for obtaining color shifts from masks.
838 *****************************************************************************/
839 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
841 u32 i_low, i_high; /* lower hand higher bits of the mask */
844 i_low = i_mask & (- i_mask); /* lower bit of the mask */
845 i_high = i_mask + i_low; /* higher bit of the mask */
847 /* Transform bits into an index */
848 i_low = BinaryLog (i_low);
849 i_high = BinaryLog (i_high);
851 /* Update pointers and return */
853 *pi_right = (8 - i_high + i_low);
856 /*****************************************************************************
857 * InitThread: initialize video output thread
858 *****************************************************************************
859 * This function is called from RunThread and performs the second step of the
860 * initialization. It returns 0 on success. Note that the thread's flag are not
861 * modified inside this function.
862 *****************************************************************************/
863 static int InitThread( vout_thread_t *p_vout )
867 *p_vout->pi_status = THREAD_START;
869 /* Initialize output method - this function issues its own error messages */
870 if( p_vout->p_sys_init( p_vout ) )
875 /* Initialize convertion tables and functions */
876 if( vout_InitYUV( p_vout ) )
878 intf_ErrMsg("error: can't allocate YUV translation tables\n");
882 /* Mark thread as running and return */
883 p_vout->b_active = 1;
884 *p_vout->pi_status = THREAD_READY;
885 intf_DbgMsg("thread ready\n");
889 /*****************************************************************************
890 * RunThread: video output thread
891 *****************************************************************************
892 * Video output thread. This function does only returns when the thread is
893 * terminated. It handles the pictures arriving in the video heap and the
894 * display device events.
895 *****************************************************************************/
896 static void RunThread( vout_thread_t *p_vout)
898 /* XXX?? welcome to gore land */
899 static int i_trash_count = 0;
900 static mtime_t last_display_date = 0;
902 int i_index; /* index in heap */
903 mtime_t current_date; /* current date */
904 mtime_t display_date; /* display date */
905 boolean_t b_display; /* display flag */
906 picture_t * p_pic; /* picture pointer */
907 subpicture_t * p_subpic; /* subpicture pointer */
912 p_vout->b_error = InitThread( p_vout );
913 if( p_vout->b_error )
915 DestroyThread( p_vout, THREAD_ERROR );
921 * Main loop - it is not executed if an error occured during
924 while( (!p_vout->b_die) && (!p_vout->b_error) )
926 /* Initialize loop variables */
930 current_date = mdate();
933 * Find the picture to display - this operation does not need lock,
934 * since only READY_PICTUREs are handled
936 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
938 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
940 (p_vout->p_picture[i_index].date < display_date) ) )
942 p_pic = &p_vout->p_picture[i_index];
943 display_date = p_pic->date;
950 /* Computes FPS rate */
951 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
955 //fprintf( stderr, "gap : %Ld\n", display_date-last_display_date );
956 last_display_date = display_date;
958 if( display_date < current_date && i_trash_count > 4 )
960 /* Picture is late: it will be destroyed and the thread will sleep and
961 * go to next picture */
963 vlc_mutex_lock( &p_vout->picture_lock );
964 if( p_pic->i_refcount )
966 p_pic->i_status = DISPLAYED_PICTURE;
970 p_pic->i_status = DESTROYED_PICTURE;
971 p_vout->i_pictures--;
973 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
974 vlc_mutex_unlock( &p_vout->picture_lock );
976 /* Update synchronization information as if display delay
978 Synchronize( p_vout, display_date - current_date );
986 if( display_date > current_date + VOUT_DISPLAY_DELAY )
988 /* A picture is ready to be rendered, but its rendering date is
989 * far from the current one so the thread will perform an empty loop
990 * as if no picture were found. The picture state is unchanged */
996 /* Picture will be displayed, update synchronization
998 Synchronize( p_vout, display_date - current_date );
1002 * Find the subpicture to display - this operation does not need lock, since
1003 * only READY_SUBPICTURES are handled. If no picture has been selected,
1004 * display_date will depend on the subpicture
1009 * Perform rendering, sleep and display rendered picture
1011 if( p_pic ) /* picture and perhaps subpicture */
1013 b_display = p_vout->b_active;
1014 p_vout->last_display_date = display_date;
1018 /* Set picture dimensions and clear buffer */
1019 SetBufferPicture( p_vout, p_pic );
1021 /* Render picture and informations */
1022 RenderPicture( p_vout, p_pic );
1023 if( p_vout->b_info )
1025 RenderPictureInfo( p_vout, p_pic );
1026 RenderInfo( p_vout );
1030 /* Remove picture from heap */
1031 vlc_mutex_lock( &p_vout->picture_lock );
1032 if( p_pic->i_refcount )
1034 p_pic->i_status = DISPLAYED_PICTURE;
1038 p_pic->i_status = DESTROYED_PICTURE;
1039 p_vout->i_pictures--;
1041 vlc_mutex_unlock( &p_vout->picture_lock );
1043 /* Render interface and subpicture */
1044 if( b_display && p_vout->b_interface )
1046 RenderInterface( p_vout );
1052 RenderSubPicture( p_vout, p_subpic );
1055 /* Remove subpicture from heap */
1056 vlc_mutex_lock( &p_vout->subpicture_lock );
1057 p_subpic->i_status = DESTROYED_SUBPICTURE;
1058 vlc_mutex_unlock( &p_vout->subpicture_lock );
1062 else if( p_subpic ) /* subpicture alone */
1064 b_display = p_vout->b_active;
1065 p_vout->last_display_date = display_date;
1070 SetBufferPicture( p_vout, NULL );
1072 /* Render informations, interface and subpicture */
1073 if( p_vout->b_info )
1075 RenderInfo( p_vout );
1077 if( p_vout->b_interface )
1079 RenderInterface( p_vout );
1081 RenderSubPicture( p_vout, p_subpic );
1084 /* Remove subpicture from heap */
1085 vlc_mutex_lock( &p_vout->subpicture_lock );
1086 p_subpic->i_status = DESTROYED_SUBPICTURE;
1087 vlc_mutex_unlock( &p_vout->subpicture_lock );
1089 else if( p_vout->b_active ) /* idle or interface screen alone */
1091 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1093 /* Interface has changed, so a new rendering is required - force
1094 * it by setting last idle date to 0 */
1095 p_vout->last_idle_date = 0;
1098 /* Render idle screen and update idle date, then render interface if
1100 b_display = RenderIdle( p_vout );
1103 p_vout->last_idle_date = current_date;
1104 if( p_vout->b_interface )
1106 RenderInterface( p_vout );
1117 * Sleep, wake up and display rendered picture
1121 /* Store render time */
1122 p_vout->render_time = mdate() - current_date;
1125 /* Give back change lock */
1126 vlc_mutex_unlock( &p_vout->change_lock );
1128 /* Sleep a while or until a given date */
1129 if( display_date != 0 )
1131 mwait( display_date );
1135 msleep( VOUT_IDLE_SLEEP );
1138 /* On awakening, take back lock and send immediately picture to display,
1139 * then swap buffers */
1140 vlc_mutex_lock( &p_vout->change_lock );
1142 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1143 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1145 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1147 p_vout->p_sys_display( p_vout );
1148 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1152 * Check events and manage thread
1154 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1156 /* A fatal error occured, and the thread must terminate immediately,
1157 * without displaying anything - setting b_error to 1 cause the
1158 * immediate end of the main while() loop. */
1159 p_vout->b_error = 1;
1164 * Error loop - wait until the thread destruction is requested
1166 if( p_vout->b_error )
1168 ErrorThread( p_vout );
1172 EndThread( p_vout );
1173 DestroyThread( p_vout, THREAD_OVER );
1174 intf_DbgMsg( "thread end\n" );
1177 /*****************************************************************************
1178 * ErrorThread: RunThread() error loop
1179 *****************************************************************************
1180 * This function is called when an error occured during thread main's loop. The
1181 * thread can still receive feed, but must be ready to terminate as soon as
1183 *****************************************************************************/
1184 static void ErrorThread( vout_thread_t *p_vout )
1186 /* Wait until a `die' order */
1188 while( !p_vout->b_die )
1191 msleep( VOUT_IDLE_SLEEP );
1195 /*****************************************************************************
1196 * EndThread: thread destruction
1197 *****************************************************************************
1198 * This function is called when the thread ends after a sucessfull
1199 * initialization. It frees all ressources allocated by InitThread.
1200 *****************************************************************************/
1201 static void EndThread( vout_thread_t *p_vout )
1203 int i_index; /* index in heap */
1207 *p_vout->pi_status = THREAD_END;
1209 /* Destroy all remaining pictures and subpictures */
1210 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1212 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1214 free( p_vout->p_picture[i_index].p_data );
1216 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1218 free( p_vout->p_subpicture[i_index].p_data );
1222 /* Destroy translation tables */
1223 vout_EndYUV( p_vout );
1224 p_vout->p_sys_end( p_vout );
1227 /*****************************************************************************
1228 * DestroyThread: thread destruction
1229 *****************************************************************************
1230 * This function is called when the thread ends. It frees all ressources
1231 * allocated by CreateThread. Status is available at this stage.
1232 *****************************************************************************/
1233 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1235 int *pi_status; /* status adress */
1237 /* Store status adress */
1239 pi_status = p_vout->pi_status;
1241 /* Destroy thread structures allocated by Create and InitThread */
1242 vout_UnloadFont( p_vout->p_default_font );
1243 vout_UnloadFont( p_vout->p_large_font );
1244 p_vout->p_sys_destroy( p_vout );
1247 dlclose( p_vout->p_vout_plugin );
1249 /* Free structure */
1251 *pi_status = i_status;
1254 /*****************************************************************************
1255 * Print: print simple text on a picture
1256 *****************************************************************************
1257 * This function will print a simple text on the picture. It is designed to
1258 * print debugging or general informations.
1259 *****************************************************************************/
1260 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 )
1262 int i_text_height; /* total text height */
1263 int i_text_width; /* total text width */
1265 /* Update upper left coordinates according to alignment */
1266 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1267 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1269 /* Set area and print text */
1270 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1271 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1272 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1273 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1274 p_vout->i_white_pixel, 0, 0,
1279 /*****************************************************************************
1280 * SetBufferArea: activate an area in current buffer
1281 *****************************************************************************
1282 * This function is called when something is rendered on the current buffer.
1283 * It set the area as active and prepare it to be cleared on next rendering.
1284 * Pay attention to the fact that in this functions, i_h is in fact the end y
1285 * coordinate of the new area.
1286 *****************************************************************************/
1287 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1289 vout_buffer_t * p_buffer; /* current buffer */
1290 int i_area_begin, i_area_end; /* area vertical extension */
1291 int i_area, i_area_copy; /* area index */
1292 int i_area_shift; /* shift distance for areas */
1294 /* Choose buffer and modify h to end of area position */
1295 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1299 * Remove part of the area which is inside the picture - this is done
1300 * by calling again SetBufferArea with the correct areas dimensions.
1302 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1304 i_area_begin = p_buffer->i_pic_y;
1305 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1307 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1308 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1309 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1311 /* Keep the stripe above the picture, if any */
1312 if( i_y < i_area_begin )
1314 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1316 /* Keep the stripe below the picture, if any */
1317 if( i_h > i_area_end )
1319 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1325 /* Skip some extensions until interesting areas */
1327 (i_area < p_buffer->i_areas) &&
1328 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1334 if( i_area == p_buffer->i_areas )
1336 /* New area is below all existing ones: just add it at the end of the
1337 * array, if possible - else, append it to the last one */
1338 if( i_area < VOUT_MAX_AREAS )
1340 p_buffer->pi_area_begin[i_area] = i_y;
1341 p_buffer->pi_area_end[i_area] = i_h;
1342 p_buffer->i_areas++;
1347 intf_DbgMsg("areas overflow\n");
1349 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1354 i_area_begin = p_buffer->pi_area_begin[i_area];
1355 i_area_end = p_buffer->pi_area_end[i_area];
1357 if( i_y < i_area_begin )
1359 if( i_h >= i_area_begin - 1 )
1361 /* Extend area above */
1362 p_buffer->pi_area_begin[i_area] = i_y;
1366 /* Create a new area above : merge last area if overflow, then
1367 * move all old areas down */
1368 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1371 intf_DbgMsg("areas overflow\n");
1373 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1377 p_buffer->i_areas++;
1379 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1381 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1382 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1384 p_buffer->pi_area_begin[i_area] = i_y;
1385 p_buffer->pi_area_end[i_area] = i_h;
1389 if( i_h > i_area_end )
1391 /* Find further areas which can be merged with the new one */
1392 for( i_area_copy = i_area + 1;
1393 (i_area_copy < p_buffer->i_areas) &&
1394 (p_buffer->pi_area_begin[i_area] <= i_h);
1401 if( i_area_copy != i_area )
1403 /* Merge with last possible areas */
1404 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1406 /* Shift lower areas upward */
1407 i_area_shift = i_area_copy - i_area;
1408 p_buffer->i_areas -= i_area_shift;
1409 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1411 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1412 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1417 /* Extend area below */
1418 p_buffer->pi_area_end[i_area] = i_h;
1424 /*****************************************************************************
1425 * SetBufferPicture: clear buffer and set picture area
1426 *****************************************************************************
1427 * This function is called before any rendering. It clears the current
1428 * rendering buffer and set the new picture area. If the picture pointer is
1429 * NULL, then no picture area is defined. Floating operations are avoided since
1430 * some MMX calculations may follow.
1431 *****************************************************************************/
1432 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1434 vout_buffer_t * p_buffer; /* current buffer */
1435 int i_pic_x, i_pic_y; /* picture position */
1436 int i_pic_width, i_pic_height; /* picture dimensions */
1437 int i_old_pic_y, i_old_pic_height; /* old picture area */
1438 int i_vout_width, i_vout_height; /* display dimensions */
1439 int i_area; /* area index */
1440 int i_data_index; /* area data index */
1441 int i_data_size; /* area data size, in 256 bytes blocs */
1442 u64 * p_data; /* area data, for clearing */
1443 byte_t * p_data8; /* area data, for clearing (slow) */
1445 /* Choose buffer and set display dimensions */
1446 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1447 i_vout_width = p_vout->i_width;
1448 i_vout_height = p_vout->i_height;
1451 * Computes new picture size
1455 /* Try horizontal scaling first - width must be a mutiple of 16 */
1456 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1457 i_vout_width : p_pic->i_width) & ~0xf;
1458 switch( p_pic->i_aspect_ratio )
1460 case AR_3_4_PICTURE:
1461 i_pic_height = i_pic_width * 3 / 4;
1463 case AR_16_9_PICTURE:
1464 i_pic_height = i_pic_width * 9 / 16;
1466 case AR_221_1_PICTURE:
1467 i_pic_height = i_pic_width * 100 / 221;
1469 case AR_SQUARE_PICTURE:
1471 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1475 /* If picture dimensions using horizontal scaling are too large, use
1476 * vertical scaling. Since width must be a multiple of 16, height is
1477 * adjusted again after. */
1478 if( i_pic_height > i_vout_height )
1480 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1481 i_vout_height : p_pic->i_height;
1482 switch( p_pic->i_aspect_ratio )
1484 case AR_3_4_PICTURE:
1485 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1486 i_pic_height = i_pic_width * 3 / 4;
1488 case AR_16_9_PICTURE:
1489 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1490 i_pic_height = i_pic_width * 9 / 16;
1492 case AR_221_1_PICTURE:
1493 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1494 i_pic_height = i_pic_width * 100 / 221;
1496 case AR_SQUARE_PICTURE:
1498 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1499 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1504 /* Set picture position */
1505 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1506 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1510 /* No picture: size is 0 */
1518 * Set new picture size - if is is smaller than the previous one, clear
1519 * around it. Since picture are centered, only their size is tested.
1521 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1523 i_old_pic_y = p_buffer->i_pic_y;
1524 i_old_pic_height = p_buffer->i_pic_height;
1525 p_buffer->i_pic_x = i_pic_x;
1526 p_buffer->i_pic_y = i_pic_y;
1527 p_buffer->i_pic_width = i_pic_width;
1528 p_buffer->i_pic_height = i_pic_height;
1529 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1533 p_buffer->i_pic_x = i_pic_x;
1534 p_buffer->i_pic_y = i_pic_y;
1535 p_buffer->i_pic_width = i_pic_width;
1536 p_buffer->i_pic_height = i_pic_height;
1542 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1545 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1546 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1548 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1549 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1550 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1552 /* Clear 256 bytes block */
1553 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1554 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1555 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1556 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1557 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1558 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1559 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1560 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1562 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1564 /* Clear remaining 16 bytes blocks */
1565 *p_data++ = 0; *p_data++ = 0;
1567 p_data8 = (byte_t *)p_data;
1568 for( i_data_index = i_data_size % 16; i_data_index--; )
1570 /* Clear remaining bytes */
1578 p_buffer->i_areas = 0;
1581 /*****************************************************************************
1582 * RenderPicture: render a picture
1583 *****************************************************************************
1584 * This function convert a picture from a video heap to a pixel-encoded image
1585 * and copy it to the current rendering buffer. No lock is required, since the
1586 * rendered picture has been determined as existant, and will only be destroyed
1587 * by the vout thread later.
1588 *****************************************************************************/
1589 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1592 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1593 mtime_t render_time; /* picture rendering time */
1595 vout_buffer_t * p_buffer; /* rendering buffer */
1596 byte_t * p_pic_data; /* convertion destination */
1598 /* Get and set rendering informations */
1599 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1600 p_pic_data = p_buffer->p_data +
1601 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1602 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1604 render_time = mdate();
1608 * Choose appropriate rendering function and render picture
1610 switch( p_pic->i_type )
1612 case YUV_420_PICTURE:
1613 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1614 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1615 p_pic->i_width, p_pic->i_height,
1616 p_buffer->i_pic_width, p_buffer->i_pic_height,
1617 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1618 p_pic->i_matrix_coefficients );
1620 case YUV_422_PICTURE:
1621 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1622 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1623 p_pic->i_width, p_pic->i_height,
1624 p_buffer->i_pic_width, p_buffer->i_pic_height,
1625 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1626 p_pic->i_matrix_coefficients );
1628 case YUV_444_PICTURE:
1629 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1630 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1631 p_pic->i_width, p_pic->i_height,
1632 p_buffer->i_pic_width, p_buffer->i_pic_height,
1633 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1634 p_pic->i_matrix_coefficients );
1638 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1644 /* Print picture date and rendering time */
1645 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1646 p_vout->i_buffer_index, (long) (mdate() - render_time),
1647 mstrtime( psz_date, p_pic->date ));
1651 /*****************************************************************************
1652 * RenderPictureInfo: print additionnal informations on a picture
1653 *****************************************************************************
1654 * This function will print informations such as fps and other picture
1655 * dependant informations.
1656 *****************************************************************************/
1657 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1659 #if defined(STATS) || defined(DEBUG)
1660 char psz_buffer[256]; /* string buffer */
1665 * Print FPS rate in upper right corner
1667 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1669 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1670 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1671 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1672 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1676 * Print frames count and loop time in upper left corner
1678 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1679 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1680 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1685 * Print picture information in lower right corner
1687 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1688 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1689 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1690 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1691 p_pic->i_width, p_pic->i_height,
1692 p_pic->i_display_width, p_pic->i_display_height,
1693 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1694 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1695 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1696 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1697 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1698 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1699 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1700 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1701 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1702 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1706 /*****************************************************************************
1707 * RenderIdle: render idle picture
1708 *****************************************************************************
1709 * This function will print something on the screen. It will return 0 if
1710 * nothing has been rendered, or 1 if something has been changed on the screen.
1711 * Note that if you absolutely want something to be printed, you will have
1712 * to force it by setting the last idle date to 0.
1713 * Unlike other rendering functions, this one calls the SetBufferPicture
1714 * function when needed.
1715 *****************************************************************************/
1716 static int RenderIdle( vout_thread_t *p_vout )
1718 int i_x = 0, i_y = 0; /* text position */
1719 int i_width, i_height; /* text size */
1720 mtime_t current_date; /* current date */
1721 const char *psz_text = "waiting for stream ..."; /* text to display */
1724 current_date = mdate();
1725 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1726 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1728 SetBufferPicture( p_vout, NULL );
1729 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1730 &i_width, &i_height );
1731 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1733 vout_Print( p_vout->p_large_font,
1734 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1735 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1736 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1737 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1738 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1739 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1746 /*****************************************************************************
1747 * RenderInfo: render additionnal informations
1748 *****************************************************************************
1749 * This function render informations which do not depend of the current picture
1751 *****************************************************************************/
1752 static void RenderInfo( vout_thread_t *p_vout )
1755 char psz_buffer[256]; /* string buffer */
1756 int i_ready_pic = 0; /* ready pictures */
1757 int i_reserved_pic = 0; /* reserved pictures */
1758 int i_picture; /* picture index */
1763 * Print thread state in lower left corner
1765 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1767 switch( p_vout->p_picture[i_picture].i_status )
1769 case RESERVED_PICTURE:
1770 case RESERVED_DATED_PICTURE:
1771 case RESERVED_DISP_PICTURE:
1779 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1780 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1781 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1785 /*****************************************************************************
1786 * RenderSubPicture: render a subpicture
1787 *****************************************************************************
1788 * This function render a sub picture unit.
1789 *****************************************************************************/
1790 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1792 p_vout_font_t p_font; /* text font */
1793 int i_width, i_height; /* subpicture dimensions */
1795 switch( p_subpic->i_type )
1797 case TEXT_SUBPICTURE: /* single line text */
1798 /* Select default font if not specified */
1799 p_font = p_subpic->type.text.p_font;
1800 if( p_font == NULL )
1802 p_font = p_vout->p_default_font;
1805 /* Computes text size (width and height fields are ignored) and print it */
1806 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1807 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1808 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1810 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1811 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1812 p_subpic->i_y * p_vout->i_bytes_per_line,
1813 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1814 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1815 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1817 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1823 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1828 /*****************************************************************************
1829 * RenderInterface: render the interface
1830 *****************************************************************************
1831 * This function render the interface, if any.
1832 *****************************************************************************/
1833 static void RenderInterface( vout_thread_t *p_vout )
1835 int i_height, i_text_height; /* total and text height */
1836 int i_width_1, i_width_2; /* text width */
1837 int i_byte; /* byte index */
1838 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1839 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1842 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1843 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1844 i_height += i_text_height;
1846 /* Render background */
1847 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1848 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1852 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1855 /* Render text, if not larger than screen */
1856 if( i_width_1 < p_vout->i_width )
1858 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1859 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1860 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1861 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1862 OUTLINED_TEXT, psz_text_1 );
1864 if( i_width_2 < p_vout->i_width )
1866 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1867 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1868 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1869 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1870 OUTLINED_TEXT, psz_text_2 );
1873 /* Activate modified area */
1874 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1877 /*****************************************************************************
1878 * Synchronize: update synchro level depending of heap state
1879 *****************************************************************************
1880 * This function is called during the main vout loop.
1881 *****************************************************************************/
1882 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1884 int i_synchro_inc = 0;
1885 /* XXX?? gore following */
1886 static int i_panic_count = 0;
1887 static int i_last_synchro_inc = 0;
1888 static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1889 static int i_truc = 10;
1893 //fprintf( stderr, "PANIC %d\n", i_panic_count );
1899 if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1902 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1907 if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1910 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1914 if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1915 i_synchro_inc*i_last_synchro_inc < 0 )
1920 if( i_delay < 6000 )
1925 else if( i_delay < 70000 )
1927 i_truc = 24+(24*i_delay)/70000;
1930 i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
1932 else if( i_delay > 100000 )
1934 r_synchro_level += 1;
1935 if( i_delay > 130000 )
1936 r_synchro_level += 1;
1939 r_synchro_level += (float)i_synchro_inc / i_truc;
1940 p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
1942 if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
1944 r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
1947 //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
1948 // p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
1949 i_last_synchro_inc = i_synchro_inc;
1952 /*****************************************************************************
1953 * Manage: manage thread
1954 *****************************************************************************
1955 * This function will handle changes in thread configuration.
1956 *****************************************************************************/
1957 static int Manage( vout_thread_t *p_vout )
1960 if( p_vout->i_changes )
1962 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1963 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1967 /* On gamma or grayscale change, rebuild tables */
1968 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1971 if( vout_ResetYUV( p_vout ) )
1973 intf_ErrMsg("error: can't rebuild convertion tables\n");
1978 /* Clear changes flags which does not need management or have been
1980 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1981 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
1982 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
1984 /* Detect unauthorized changes */
1985 if( p_vout->i_changes )
1987 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
1988 * function, it means they should not be authorized */
1989 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
1996 /*****************************************************************************
1997 * Align: align a subpicture in the screen
1998 *****************************************************************************
1999 * This function is used for rendering text or subpictures. It returns non 0
2000 * it the final aera is not fully included in display area. Return coordinates
2002 *****************************************************************************/
2003 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2004 int i_width, int i_height, int i_h_align, int i_v_align )
2006 /* Align horizontally */
2010 *pi_x -= i_width / 2;
2013 *pi_x += (p_vout->i_width - i_width) / 2;
2019 *pi_x += p_vout->i_width - i_width;
2023 /* Align vertically */
2027 *pi_y -= i_height / 2;
2030 *pi_y += (p_vout->i_height - i_height) / 2;
2036 *pi_y += p_vout->i_height - i_height;
2038 case SUBTITLE_RALIGN:
2039 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2040 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2044 /* Return non 0 if clipping failed */
2045 return( (*pi_x < 0) || (*pi_y < 0) ||
2046 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2049 /*****************************************************************************
2050 * SetPalette: sets an 8 bpp palette
2051 *****************************************************************************
2052 * This function is just a prototype that does nothing. Architectures that
2053 * support palette allocation should override it.
2054 *****************************************************************************/
2055 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2056 u16 *green, u16 *blue, u16 *transp )
2058 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );