1 /*****************************************************************************
2 * video_output.c : video output thread
3 * This module describes the programming interface for video output threads.
4 * It includes functions allowing to open a new thread, send pictures to a
5 * thread, and destroy a previously oppened video output thread.
6 *****************************************************************************
7 * Copyright (C) 2000 VideoLAN
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <errno.h> /* ENOMEM */
32 #include <stdlib.h> /* free() */
33 #include <stdio.h> /* sprintf() */
34 #include <string.h> /* strerror() */
42 #include "video_output.h"
43 #include "video_text.h"
44 #include "video_spu.h"
45 #include "video_yuv.h"
50 /*****************************************************************************
52 *****************************************************************************/
53 static int BinaryLog ( u32 i );
54 static void MaskToShift ( int *pi_left, int *pi_right, u32 i_mask );
55 static int InitThread ( vout_thread_t *p_vout );
56 static void RunThread ( vout_thread_t *p_vout );
57 static void ErrorThread ( vout_thread_t *p_vout );
58 static void EndThread ( vout_thread_t *p_vout );
59 static void DestroyThread ( vout_thread_t *p_vout, int i_status );
60 static void Print ( vout_thread_t *p_vout, int i_x, int i_y,
61 int i_h_align, int i_v_align,
62 unsigned char *psz_text );
63 static void SetBufferArea ( vout_thread_t *p_vout, int i_x, int i_y,
65 static void SetBufferPicture ( vout_thread_t *p_vout, picture_t *p_pic );
66 static void RenderPicture ( vout_thread_t *p_vout, picture_t *p_pic );
67 static void RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
68 static void RenderSubPicture ( vout_thread_t *p_vout,
69 subpicture_t *p_subpic );
70 static void RenderInterface ( vout_thread_t *p_vout );
71 static int RenderIdle ( vout_thread_t *p_vout );
72 static void RenderInfo ( vout_thread_t *p_vout );
73 static void Synchronize ( vout_thread_t *p_vout, s64 i_delay );
74 static int Manage ( vout_thread_t *p_vout );
75 static int Align ( vout_thread_t *p_vout, int *pi_x,
76 int *pi_y, int i_width, int i_height,
77 int i_h_align, int i_v_align );
78 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
79 u16 *green, u16 *blue, u16 *transp );
81 /*****************************************************************************
82 * vout_CreateThread: creates a new video output thread
83 *****************************************************************************
84 * This function creates a new video output thread, and returns a pointer
85 * to its description. On error, it returns NULL.
86 * If pi_status is NULL, then the function will block until the thread is ready.
87 * If not, it will be updated using one of the THREAD_* constants.
88 *****************************************************************************/
89 vout_thread_t * vout_CreateThread ( char *psz_display, int i_root_window,
90 int i_width, int i_height, int *pi_status,
91 int i_method, void *p_data )
93 vout_thread_t * p_vout; /* thread descriptor */
94 int i_status; /* thread status */
95 int i_index; /* index for array initialization */
98 /* Allocate descriptor */
100 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
103 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
107 /* Request an interface plugin */
108 psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
110 if( RequestPlugin( &p_vout->vout_plugin, "vout", psz_method ) < 0 )
112 intf_ErrMsg( "error: could not open video plugin vout_%s.so\n", psz_method );
118 p_vout->p_sys_create =
119 GetPluginFunction( p_vout->vout_plugin, "vout_SysCreate" );
121 GetPluginFunction( p_vout->vout_plugin, "vout_SysInit" );
123 GetPluginFunction( p_vout->vout_plugin, "vout_SysEnd" );
124 p_vout->p_sys_destroy =
125 GetPluginFunction( p_vout->vout_plugin, "vout_SysDestroy" );
126 p_vout->p_sys_manage =
127 GetPluginFunction( p_vout->vout_plugin, "vout_SysManage" );
128 p_vout->p_sys_display =
129 GetPluginFunction( p_vout->vout_plugin, "vout_SysDisplay" );
131 /* Initialize thread properties - thread id and locks will be initialized
135 p_vout->b_active = 0;
136 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
137 *p_vout->pi_status = THREAD_CREATE;
139 /* Initialize some fields used by the system-dependant method - these fields will
140 * probably be modified by the method, and are only preferences */
141 p_vout->i_changes = 0;
142 p_vout->i_width = i_width;
143 p_vout->i_height = i_height;
144 p_vout->i_bytes_per_line = i_width * 2;
145 p_vout->i_screen_depth = 15;
146 p_vout->i_bytes_per_pixel = 2;
147 p_vout->f_gamma = VOUT_GAMMA;
149 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
150 VOUT_GRAYSCALE_DEFAULT );
152 p_vout->b_interface = 0;
155 p_vout->p_set_palette = SetPalette;
157 intf_DbgMsg( "wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
158 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
159 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
161 /* Initialize idle screen */
162 p_vout->last_display_date = mdate();
163 p_vout->last_display_date = 0;
164 p_vout->last_idle_date = 0;
167 /* Initialize statistics fields */
168 p_vout->render_time = 0;
169 p_vout->c_fps_samples = 0;
172 /* Initialize buffer index */
173 p_vout->i_buffer_index = 0;
175 /* Initialize pictures and subpictures - translation tables and functions
176 * will be initialized later in InitThread */
177 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
179 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
180 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
182 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
184 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
185 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
187 p_vout->i_pictures = 0;
189 /* Initialize synchronization informations */
190 p_vout->i_synchro_level = VOUT_SYNCHRO_LEVEL_START;
192 /* Create and initialize system-dependant method - this function issues its
193 * own error messages */
194 if( p_vout->p_sys_create( p_vout, psz_display, i_root_window, p_data ) )
196 TrashPlugin( p_vout->vout_plugin );
200 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
201 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
202 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
203 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
205 /* Calculate shifts from system-updated masks */
206 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
207 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
208 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
210 /* Set some useful colors */
211 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
212 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
213 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
214 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
216 /* Load fonts - fonts must be initialized after the system method since
217 * they may be dependant on screen depth and other thread properties */
218 p_vout->p_default_font = vout_LoadFont( DATA_PATH "/" VOUT_DEFAULT_FONT );
219 if( p_vout->p_default_font == NULL )
221 p_vout->p_default_font = vout_LoadFont( "share/" VOUT_DEFAULT_FONT );
223 if( p_vout->p_default_font == NULL )
225 p_vout->p_sys_destroy( p_vout );
226 TrashPlugin( p_vout->vout_plugin );
230 p_vout->p_large_font = vout_LoadFont( DATA_PATH "/" VOUT_LARGE_FONT );
231 if( p_vout->p_large_font == NULL )
233 p_vout->p_large_font = vout_LoadFont( "share/" VOUT_LARGE_FONT );
235 if( p_vout->p_large_font == NULL )
237 vout_UnloadFont( p_vout->p_default_font );
238 p_vout->p_sys_destroy( p_vout );
239 TrashPlugin( p_vout->vout_plugin );
244 /* Create thread and set locks */
245 vlc_mutex_init( &p_vout->picture_lock );
246 vlc_mutex_init( &p_vout->subpicture_lock );
247 vlc_mutex_init( &p_vout->change_lock );
248 vlc_mutex_lock( &p_vout->change_lock );
249 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
251 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
252 vout_UnloadFont( p_vout->p_default_font );
253 vout_UnloadFont( p_vout->p_large_font );
254 p_vout->p_sys_destroy( p_vout );
255 TrashPlugin( p_vout->vout_plugin );
260 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
261 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
263 /* If status is NULL, wait until the thread is created */
264 if( pi_status == NULL )
268 msleep( THREAD_SLEEP );
269 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
270 && (i_status != THREAD_FATAL) );
271 if( i_status != THREAD_READY )
279 /*****************************************************************************
280 * vout_DestroyThread: destroys a previously created thread
281 *****************************************************************************
282 * Destroy a terminated thread.
283 * The function will request a destruction of the specified thread. If pi_error
284 * is NULL, it will return once the thread is destroyed. Else, it will be
285 * update using one of the THREAD_* constants.
286 *****************************************************************************/
287 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
289 int i_status; /* thread status */
293 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
294 *p_vout->pi_status = THREAD_DESTROY;
296 /* Request thread destruction */
299 /* If status is NULL, wait until thread has been destroyed */
300 if( pi_status == NULL )
304 msleep( THREAD_SLEEP );
305 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
306 && (i_status != THREAD_FATAL) );
310 /*****************************************************************************
311 * vout_DisplaySubPicture: display a subpicture unit
312 *****************************************************************************
313 * Remove the reservation flag of an subpicture, which will cause it to be ready
314 * for display. The picture does not need to be locked, since it is ignored by
315 * the output thread if is reserved.
316 *****************************************************************************/
317 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
320 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
321 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
325 /* Check if status is valid */
326 if( p_subpic->i_status != RESERVED_SUBPICTURE )
328 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
329 p_subpic->i_status );
333 /* Remove reservation flag */
334 p_subpic->i_status = READY_SUBPICTURE;
337 /* Send subpicture informations */
338 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
339 p_subpic, p_subpic->i_type,
340 mstrtime( psz_begin_date, p_subpic->begin_date ),
341 mstrtime( psz_end_date, p_subpic->end_date ) );
345 /*****************************************************************************
346 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
347 *****************************************************************************
348 * This function create a reserved subpicture in the video output heap.
349 * A null pointer is returned if the function fails. This method provides an
350 * already allocated zone of memory in the spu data fields. It needs locking
351 * since several pictures can be created by several producers threads.
352 *****************************************************************************/
353 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
356 int i_subpic; /* subpicture index */
357 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
358 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
361 vlc_mutex_lock( &p_vout->subpicture_lock );
364 * Look for an empty place
366 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
368 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
370 /* Subpicture is marked for destruction, but is still allocated */
371 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
372 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
374 /* Memory size do match or is smaller : memory will not be
375 * reallocated, and function can end immediately - this is
376 * the best possible case, since no memory allocation needs
378 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
380 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
381 &p_vout->p_subpicture[i_subpic] );
383 vlc_mutex_unlock( &p_vout->subpicture_lock );
384 return( &p_vout->p_subpicture[i_subpic] );
386 else if( p_destroyed_subpic == NULL )
388 /* Memory size do not match, but subpicture index will be kept in
389 * case no other place are left */
390 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
393 else if( (p_free_subpic == NULL) &&
394 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
396 /* Subpicture is empty and ready for allocation */
397 p_free_subpic = &p_vout->p_subpicture[i_subpic];
401 /* If no free subpicture is available, use a destroyed subpicture */
402 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
404 /* No free subpicture or matching destroyed subpicture has been
405 * found, but a destroyed subpicture is still avalaible */
406 free( p_destroyed_subpic->p_data );
407 p_free_subpic = p_destroyed_subpic;
413 if( p_free_subpic != NULL )
415 /* Allocate memory */
418 case TEXT_SUBPICTURE: /* text subpicture */
419 p_free_subpic->p_data = malloc( i_size + 1 );
421 case DVD_SUBPICTURE: /* DVD subpicture unit */
422 p_free_subpic->p_data = malloc( i_size );
426 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
427 p_free_subpic->p_data = NULL;
432 if( p_free_subpic->p_data != NULL )
433 { /* Copy subpicture informations, set some default values */
434 p_free_subpic->i_type = i_type;
435 p_free_subpic->i_status = RESERVED_SUBPICTURE;
436 p_free_subpic->i_size = i_size;
437 p_free_subpic->i_x = 0;
438 p_free_subpic->i_y = 0;
439 p_free_subpic->i_width = 0;
440 p_free_subpic->i_height = 0;
441 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
442 p_free_subpic->i_vertical_align = CENTER_RALIGN;
446 /* Memory allocation failed : set subpicture as empty */
447 p_free_subpic->i_type = EMPTY_SUBPICTURE;
448 p_free_subpic->i_status = FREE_SUBPICTURE;
449 p_free_subpic = NULL;
450 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
454 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
456 vlc_mutex_unlock( &p_vout->subpicture_lock );
457 return( p_free_subpic );
460 /* No free or destroyed subpicture could be found */
461 intf_DbgMsg( "warning: heap is full\n" );
462 vlc_mutex_unlock( &p_vout->subpicture_lock );
466 /*****************************************************************************
467 * vout_DestroySubPicture: remove a subpicture from the heap
468 *****************************************************************************
469 * This function frees a previously reserved subpicture.
470 * It is meant to be used when the construction of a picture aborted.
471 * This function does not need locking since reserved subpictures are ignored
472 * by the output thread.
473 *****************************************************************************/
474 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
477 /* Check if status is valid */
478 if( p_subpic->i_status != RESERVED_SUBPICTURE )
480 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
481 p_subpic, p_subpic->i_status );
485 p_subpic->i_status = DESTROYED_SUBPICTURE;
488 intf_DbgMsg("subpicture %p\n", p_subpic);
492 /*****************************************************************************
493 * vout_DisplayPicture: display a picture
494 *****************************************************************************
495 * Remove the reservation flag of a picture, which will cause it to be ready for
496 * display. The picture won't be displayed until vout_DatePicture has been
498 *****************************************************************************/
499 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
501 vlc_mutex_lock( &p_vout->picture_lock );
502 switch( p_pic->i_status )
504 case RESERVED_PICTURE:
505 p_pic->i_status = RESERVED_DISP_PICTURE;
507 case RESERVED_DATED_PICTURE:
508 p_pic->i_status = READY_PICTURE;
512 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
518 intf_DbgMsg("picture %p\n", p_pic);
520 vlc_mutex_unlock( &p_vout->picture_lock );
523 /*****************************************************************************
524 * vout_DatePicture: date a picture
525 *****************************************************************************
526 * Remove the reservation flag of a picture, which will cause it to be ready for
527 * display. The picture won't be displayed until vout_DisplayPicture has been
529 *****************************************************************************/
530 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
533 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
536 vlc_mutex_lock( &p_vout->picture_lock );
538 switch( p_pic->i_status )
540 case RESERVED_PICTURE:
541 p_pic->i_status = RESERVED_DATED_PICTURE;
543 case RESERVED_DISP_PICTURE:
544 p_pic->i_status = READY_PICTURE;
548 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
554 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
556 vlc_mutex_unlock( &p_vout->picture_lock );
559 /*****************************************************************************
560 * vout_CreatePicture: allocate a picture in the video output heap.
561 *****************************************************************************
562 * This function create a reserved image in the video output heap.
563 * A null pointer is returned if the function fails. This method provides an
564 * already allocated zone of memory in the picture data fields. It needs locking
565 * since several pictures can be created by several producers threads.
566 *****************************************************************************/
567 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
568 int i_width, int i_height )
570 int i_picture; /* picture index */
571 int i_chroma_width = 0; /* chroma width */
572 picture_t * p_free_picture = NULL; /* first free picture */
573 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
576 vlc_mutex_lock( &p_vout->picture_lock );
579 * Look for an empty place
581 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
583 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
585 /* Picture is marked for destruction, but is still allocated - note
586 * that if width and type are the same for two pictures, chroma_width
587 * should also be the same */
588 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
589 (p_vout->p_picture[i_picture].i_height == i_height) &&
590 (p_vout->p_picture[i_picture].i_width == i_width) )
592 /* Memory size do match : memory will not be reallocated, and function
593 * can end immediately - this is the best possible case, since no
594 * memory allocation needs to be done */
595 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
596 p_vout->i_pictures++;
598 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
599 &p_vout->p_picture[i_picture] );
601 vlc_mutex_unlock( &p_vout->picture_lock );
602 return( &p_vout->p_picture[i_picture] );
604 else if( p_destroyed_picture == NULL )
606 /* Memory size do not match, but picture index will be kept in
607 * case no other place are left */
608 p_destroyed_picture = &p_vout->p_picture[i_picture];
611 else if( (p_free_picture == NULL) &&
612 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
614 /* Picture is empty and ready for allocation */
615 p_free_picture = &p_vout->p_picture[i_picture];
619 /* If no free picture is available, use a destroyed picture */
620 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
622 /* No free picture or matching destroyed picture has been found, but
623 * a destroyed picture is still avalaible */
624 free( p_destroyed_picture->p_data );
625 p_free_picture = p_destroyed_picture;
631 if( p_free_picture != NULL )
633 /* Allocate memory */
636 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
637 i_chroma_width = i_width / 2;
638 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
639 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
640 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
641 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
643 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
644 i_chroma_width = i_width / 2;
645 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
646 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
647 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
648 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
650 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
651 i_chroma_width = i_width;
652 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
653 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
654 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
655 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
659 intf_DbgMsg("error: unknown picture type %d\n", i_type );
660 p_free_picture->p_data = NULL;
665 if( p_free_picture->p_data != NULL )
667 /* Copy picture informations, set some default values */
668 p_free_picture->i_type = i_type;
669 p_free_picture->i_status = RESERVED_PICTURE;
670 p_free_picture->i_matrix_coefficients = 1;
671 p_free_picture->i_width = i_width;
672 p_free_picture->i_height = i_height;
673 p_free_picture->i_chroma_width = i_chroma_width;
674 p_free_picture->i_display_horizontal_offset = 0;
675 p_free_picture->i_display_vertical_offset = 0;
676 p_free_picture->i_display_width = i_width;
677 p_free_picture->i_display_height = i_height;
678 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
679 p_free_picture->i_refcount = 0;
680 p_vout->i_pictures++;
684 /* Memory allocation failed : set picture as empty */
685 p_free_picture->i_type = EMPTY_PICTURE;
686 p_free_picture->i_status = FREE_PICTURE;
687 p_free_picture = NULL;
688 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
692 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
694 vlc_mutex_unlock( &p_vout->picture_lock );
695 return( p_free_picture );
698 /* No free or destroyed picture could be found */
699 intf_DbgMsg( "warning: heap is full\n" );
700 vlc_mutex_unlock( &p_vout->picture_lock );
704 /*****************************************************************************
705 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
706 *****************************************************************************
707 * This function frees a previously reserved picture or a permanent
708 * picture. It is meant to be used when the construction of a picture aborted.
709 * Note that the picture will be destroyed even if it is linked !
710 *****************************************************************************/
711 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
713 vlc_mutex_lock( &p_vout->picture_lock );
716 /* Check if picture status is valid */
717 if( (p_pic->i_status != RESERVED_PICTURE) &&
718 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
719 (p_pic->i_status != RESERVED_DISP_PICTURE) )
721 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
725 p_pic->i_status = DESTROYED_PICTURE;
726 p_vout->i_pictures--;
729 intf_DbgMsg("picture %p\n", p_pic);
731 vlc_mutex_unlock( &p_vout->picture_lock );
734 /*****************************************************************************
735 * vout_LinkPicture: increment reference counter of a picture
736 *****************************************************************************
737 * This function increment the reference counter of a picture in the video
738 * heap. It needs a lock since several producer threads can access the picture.
739 *****************************************************************************/
740 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
742 vlc_mutex_lock( &p_vout->picture_lock );
746 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
749 vlc_mutex_unlock( &p_vout->picture_lock );
752 /*****************************************************************************
753 * vout_UnlinkPicture: decrement reference counter of a picture
754 *****************************************************************************
755 * This function decrement the reference counter of a picture in the video heap.
756 *****************************************************************************/
757 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
759 vlc_mutex_lock( &p_vout->picture_lock );
763 if( p_pic->i_refcount < 0 )
765 intf_DbgMsg("error: refcount < 0\n");
766 p_pic->i_refcount = 0;
770 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
772 p_pic->i_status = DESTROYED_PICTURE;
773 p_vout->i_pictures--;
777 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
780 vlc_mutex_unlock( &p_vout->picture_lock );
783 /*****************************************************************************
784 * vout_SetBuffers: set buffers adresses
785 *****************************************************************************
786 * This function is called by system drivers to set buffers video memory
788 *****************************************************************************/
789 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
791 /* No picture previously */
792 p_vout->p_buffer[0].i_pic_x = 0;
793 p_vout->p_buffer[0].i_pic_y = 0;
794 p_vout->p_buffer[0].i_pic_width = 0;
795 p_vout->p_buffer[0].i_pic_height = 0;
796 p_vout->p_buffer[1].i_pic_x = 0;
797 p_vout->p_buffer[1].i_pic_y = 0;
798 p_vout->p_buffer[1].i_pic_width = 0;
799 p_vout->p_buffer[1].i_pic_height = 0;
801 /* The first area covers all the screen */
802 p_vout->p_buffer[0].i_areas = 1;
803 p_vout->p_buffer[0].pi_area_begin[0] = 0;
804 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
805 p_vout->p_buffer[1].i_areas = 1;
806 p_vout->p_buffer[1].pi_area_begin[0] = 0;
807 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
810 p_vout->p_buffer[0].p_data = p_buf1;
811 p_vout->p_buffer[1].p_data = p_buf2;
814 /*****************************************************************************
815 * vout_Pixel2RGB: return red, green and blue from pixel value
816 *****************************************************************************
817 * Return color values, in 0-255 range, of the decomposition of a pixel. This
818 * is a slow routine and should only be used for initialization phase.
819 *****************************************************************************/
820 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
822 *pi_red = i_pixel & p_vout->i_red_mask;
823 *pi_green = i_pixel & p_vout->i_green_mask;
824 *pi_blue = i_pixel & p_vout->i_blue_mask;
827 /* following functions are local */
829 /*****************************************************************************
830 * BinaryLog: computes the base 2 log of a binary value
831 *****************************************************************************
832 * This functions is used by MaskToShift, to get a bit index from a binary
834 *****************************************************************************/
835 static int BinaryLog(u32 i)
860 if (i != ((u32)1 << i_log))
862 intf_ErrMsg("internal error: binary log overflow\n");
868 /*****************************************************************************
869 * MaskToShift: transform a color mask into right and left shifts
870 *****************************************************************************
871 * This function is used for obtaining color shifts from masks.
872 *****************************************************************************/
873 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
875 u32 i_low, i_high; /* lower hand higher bits of the mask */
878 i_low = i_mask & (- i_mask); /* lower bit of the mask */
879 i_high = i_mask + i_low; /* higher bit of the mask */
881 /* Transform bits into an index */
882 i_low = BinaryLog (i_low);
883 i_high = BinaryLog (i_high);
885 /* Update pointers and return */
887 *pi_right = (8 - i_high + i_low);
890 /*****************************************************************************
891 * InitThread: initialize video output thread
892 *****************************************************************************
893 * This function is called from RunThread and performs the second step of the
894 * initialization. It returns 0 on success. Note that the thread's flag are not
895 * modified inside this function.
896 *****************************************************************************/
897 static int InitThread( vout_thread_t *p_vout )
901 *p_vout->pi_status = THREAD_START;
903 /* Initialize output method - this function issues its own error messages */
904 if( p_vout->p_sys_init( p_vout ) )
909 /* Initialize convertion tables and functions */
910 if( vout_InitYUV( p_vout ) )
912 intf_ErrMsg("error: can't allocate YUV translation tables\n");
916 /* Mark thread as running and return */
917 p_vout->b_active = 1;
918 *p_vout->pi_status = THREAD_READY;
919 intf_DbgMsg("thread ready\n");
923 /*****************************************************************************
924 * RunThread: video output thread
925 *****************************************************************************
926 * Video output thread. This function does only returns when the thread is
927 * terminated. It handles the pictures arriving in the video heap and the
928 * display device events.
929 *****************************************************************************/
930 static void RunThread( vout_thread_t *p_vout)
932 /* XXX?? welcome to gore land */
933 static int i_trash_count = 0;
934 static mtime_t last_display_date = 0;
936 int i_index; /* index in heap */
937 mtime_t current_date; /* current date */
938 mtime_t display_date; /* display date */
939 boolean_t b_display; /* display flag */
940 picture_t * p_pic; /* picture pointer */
941 subpicture_t * p_subpic; /* subpicture pointer */
946 p_vout->b_error = InitThread( p_vout );
947 if( p_vout->b_error )
949 DestroyThread( p_vout, THREAD_ERROR );
955 * Main loop - it is not executed if an error occured during
958 while( (!p_vout->b_die) && (!p_vout->b_error) )
960 /* Initialize loop variables */
964 current_date = mdate();
967 * Find the picture to display - this operation does not need lock,
968 * since only READY_PICTUREs are handled
970 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
972 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
974 (p_vout->p_picture[i_index].date < display_date) ) )
976 p_pic = &p_vout->p_picture[i_index];
977 display_date = p_pic->date;
984 /* Computes FPS rate */
985 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
989 //fprintf( stderr, "gap : %Ld\n", display_date-last_display_date );
990 last_display_date = display_date;
992 if( display_date < current_date && i_trash_count > 4 )
994 /* Picture is late: it will be destroyed and the thread
995 * will sleep and go to next picture */
997 vlc_mutex_lock( &p_vout->picture_lock );
998 if( p_pic->i_refcount )
1000 p_pic->i_status = DISPLAYED_PICTURE;
1004 p_pic->i_status = DESTROYED_PICTURE;
1005 p_vout->i_pictures--;
1007 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
1008 vlc_mutex_unlock( &p_vout->picture_lock );
1010 /* Update synchronization information as if display delay
1012 Synchronize( p_vout, display_date - current_date );
1020 if( display_date > current_date + VOUT_DISPLAY_DELAY )
1022 /* A picture is ready to be rendered, but its rendering date
1023 * is far from the current one so the thread will perform an
1024 * empty loop as if no picture were found. The picture state
1031 /* Picture will be displayed, update synchronization
1033 Synchronize( p_vout, display_date - current_date );
1037 * Find the subpictures to display - this operation does not need
1038 * lock, since only READY_SUBPICTURE are handled. If no picture
1039 * has been selected, display_date will depend on the subpicture.
1040 * We get an easily parsable chained list of subpictures which
1041 * ends with NULL since p_subpic was initialized to NULL.
1043 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1045 if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
1047 p_vout->p_subpicture[i_index].p_next = p_subpic;
1048 p_subpic = &p_vout->p_subpicture[i_index];
1053 * Perform rendering, sleep and display rendered picture
1055 if( p_pic ) /* picture and perhaps subpicture */
1057 b_display = p_vout->b_active;
1058 p_vout->last_display_date = display_date;
1062 /* Set picture dimensions and clear buffer */
1063 SetBufferPicture( p_vout, p_pic );
1065 /* Render picture and informations */
1066 RenderPicture( p_vout, p_pic );
1067 if( p_vout->b_info )
1069 RenderPictureInfo( p_vout, p_pic );
1070 RenderInfo( p_vout );
1074 /* Remove picture from heap */
1075 vlc_mutex_lock( &p_vout->picture_lock );
1076 if( p_pic->i_refcount )
1078 p_pic->i_status = DISPLAYED_PICTURE;
1082 p_pic->i_status = DESTROYED_PICTURE;
1083 p_vout->i_pictures--;
1085 vlc_mutex_unlock( &p_vout->picture_lock );
1087 /* Render interface and subpicture */
1088 if( b_display && p_vout->b_interface )
1090 RenderInterface( p_vout );
1096 RenderSubPicture( p_vout, p_subpic );
1099 /* Remove subpicture from heap */
1100 /*vlc_mutex_lock( &p_vout->subpicture_lock );
1101 p_subpic->i_status = DESTROYED_SUBPICTURE;
1102 vlc_mutex_unlock( &p_vout->subpicture_lock );*/
1107 else if( p_subpic ) /* subpicture alone */
1109 b_display = p_vout->b_active;
1110 p_vout->last_display_date = display_date;
1115 SetBufferPicture( p_vout, NULL );
1117 /* Render informations, interface and subpicture */
1118 if( p_vout->b_info )
1120 RenderInfo( p_vout );
1122 if( p_vout->b_interface )
1124 RenderInterface( p_vout );
1126 RenderSubPicture( p_vout, p_subpic );
1129 /* Remove subpicture from heap */
1130 /*vlc_mutex_lock( &p_vout->subpicture_lock );
1131 p_subpic->i_status = DESTROYED_SUBPICTURE;
1132 vlc_mutex_unlock( &p_vout->subpicture_lock );*/
1135 else if( p_vout->b_active ) /* idle or interface screen alone */
1137 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1139 /* Interface has changed, so a new rendering is required - force
1140 * it by setting last idle date to 0 */
1141 p_vout->last_idle_date = 0;
1144 /* Render idle screen and update idle date, then render interface if
1146 b_display = RenderIdle( p_vout );
1149 p_vout->last_idle_date = current_date;
1150 if( p_vout->b_interface )
1152 RenderInterface( p_vout );
1163 * Sleep, wake up and display rendered picture
1167 /* Store render time */
1168 p_vout->render_time = mdate() - current_date;
1171 /* Give back change lock */
1172 vlc_mutex_unlock( &p_vout->change_lock );
1174 /* Sleep a while or until a given date */
1175 if( display_date != 0 )
1177 mwait( display_date );
1181 msleep( VOUT_IDLE_SLEEP );
1184 /* On awakening, take back lock and send immediately picture to display,
1185 * then swap buffers */
1186 vlc_mutex_lock( &p_vout->change_lock );
1188 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1189 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1191 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1193 p_vout->p_sys_display( p_vout );
1195 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1200 * Check events and manage thread
1202 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1204 /* A fatal error occured, and the thread must terminate immediately,
1205 * without displaying anything - setting b_error to 1 cause the
1206 * immediate end of the main while() loop. */
1207 p_vout->b_error = 1;
1212 * Error loop - wait until the thread destruction is requested
1214 if( p_vout->b_error )
1216 ErrorThread( p_vout );
1220 EndThread( p_vout );
1221 DestroyThread( p_vout, THREAD_OVER );
1222 intf_DbgMsg( "thread end\n" );
1225 /*****************************************************************************
1226 * ErrorThread: RunThread() error loop
1227 *****************************************************************************
1228 * This function is called when an error occured during thread main's loop. The
1229 * thread can still receive feed, but must be ready to terminate as soon as
1231 *****************************************************************************/
1232 static void ErrorThread( vout_thread_t *p_vout )
1234 /* Wait until a `die' order */
1236 while( !p_vout->b_die )
1239 msleep( VOUT_IDLE_SLEEP );
1243 /*****************************************************************************
1244 * EndThread: thread destruction
1245 *****************************************************************************
1246 * This function is called when the thread ends after a sucessful
1247 * initialization. It frees all ressources allocated by InitThread.
1248 *****************************************************************************/
1249 static void EndThread( vout_thread_t *p_vout )
1251 int i_index; /* index in heap */
1255 *p_vout->pi_status = THREAD_END;
1257 /* Destroy all remaining pictures and subpictures */
1258 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1260 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1262 free( p_vout->p_picture[i_index].p_data );
1265 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1267 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1269 free( p_vout->p_subpicture[i_index].p_data );
1273 /* Destroy translation tables */
1274 vout_EndYUV( p_vout );
1275 p_vout->p_sys_end( p_vout );
1278 /*****************************************************************************
1279 * DestroyThread: thread destruction
1280 *****************************************************************************
1281 * This function is called when the thread ends. It frees all ressources
1282 * allocated by CreateThread. Status is available at this stage.
1283 *****************************************************************************/
1284 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1286 int *pi_status; /* status adress */
1288 /* Store status adress */
1290 pi_status = p_vout->pi_status;
1292 /* Destroy thread structures allocated by Create and InitThread */
1293 vout_UnloadFont( p_vout->p_default_font );
1294 vout_UnloadFont( p_vout->p_large_font );
1295 p_vout->p_sys_destroy( p_vout );
1298 TrashPlugin( p_vout->vout_plugin );
1300 /* Free structure */
1302 *pi_status = i_status;
1305 /*****************************************************************************
1306 * Print: print simple text on a picture
1307 *****************************************************************************
1308 * This function will print a simple text on the picture. It is designed to
1309 * print debugging or general informations.
1310 *****************************************************************************/
1311 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 )
1313 int i_text_height; /* total text height */
1314 int i_text_width; /* total text width */
1316 /* Update upper left coordinates according to alignment */
1317 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1318 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1320 /* Set area and print text */
1321 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1322 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1323 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1324 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1325 p_vout->i_white_pixel, 0, 0,
1330 /*****************************************************************************
1331 * SetBufferArea: activate an area in current buffer
1332 *****************************************************************************
1333 * This function is called when something is rendered on the current buffer.
1334 * It set the area as active and prepare it to be cleared on next rendering.
1335 * Pay attention to the fact that in this functions, i_h is in fact the end y
1336 * coordinate of the new area.
1337 *****************************************************************************/
1338 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1340 vout_buffer_t * p_buffer; /* current buffer */
1341 int i_area_begin, i_area_end; /* area vertical extension */
1342 int i_area, i_area_copy; /* area index */
1343 int i_area_shift; /* shift distance for areas */
1345 /* Choose buffer and modify h to end of area position */
1346 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1350 * Remove part of the area which is inside the picture - this is done
1351 * by calling again SetBufferArea with the correct areas dimensions.
1353 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1355 i_area_begin = p_buffer->i_pic_y;
1356 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1358 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1359 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1360 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1362 /* Keep the stripe above the picture, if any */
1363 if( i_y < i_area_begin )
1365 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1367 /* Keep the stripe below the picture, if any */
1368 if( i_h > i_area_end )
1370 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1376 /* Skip some extensions until interesting areas */
1378 (i_area < p_buffer->i_areas) &&
1379 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1385 if( i_area == p_buffer->i_areas )
1387 /* New area is below all existing ones: just add it at the end of the
1388 * array, if possible - else, append it to the last one */
1389 if( i_area < VOUT_MAX_AREAS )
1391 p_buffer->pi_area_begin[i_area] = i_y;
1392 p_buffer->pi_area_end[i_area] = i_h;
1393 p_buffer->i_areas++;
1398 intf_DbgMsg("areas overflow\n");
1400 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1405 i_area_begin = p_buffer->pi_area_begin[i_area];
1406 i_area_end = p_buffer->pi_area_end[i_area];
1408 if( i_y < i_area_begin )
1410 if( i_h >= i_area_begin - 1 )
1412 /* Extend area above */
1413 p_buffer->pi_area_begin[i_area] = i_y;
1417 /* Create a new area above : merge last area if overflow, then
1418 * move all old areas down */
1419 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1422 intf_DbgMsg("areas overflow\n");
1424 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1428 p_buffer->i_areas++;
1430 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1432 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1433 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1435 p_buffer->pi_area_begin[i_area] = i_y;
1436 p_buffer->pi_area_end[i_area] = i_h;
1440 if( i_h > i_area_end )
1442 /* Find further areas which can be merged with the new one */
1443 for( i_area_copy = i_area + 1;
1444 (i_area_copy < p_buffer->i_areas) &&
1445 (p_buffer->pi_area_begin[i_area] <= i_h);
1452 if( i_area_copy != i_area )
1454 /* Merge with last possible areas */
1455 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1457 /* Shift lower areas upward */
1458 i_area_shift = i_area_copy - i_area;
1459 p_buffer->i_areas -= i_area_shift;
1460 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1462 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1463 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1468 /* Extend area below */
1469 p_buffer->pi_area_end[i_area] = i_h;
1475 /*****************************************************************************
1476 * SetBufferPicture: clear buffer and set picture area
1477 *****************************************************************************
1478 * This function is called before any rendering. It clears the current
1479 * rendering buffer and set the new picture area. If the picture pointer is
1480 * NULL, then no picture area is defined. Floating operations are avoided since
1481 * some MMX calculations may follow.
1482 *****************************************************************************/
1483 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1485 vout_buffer_t * p_buffer; /* current buffer */
1486 int i_pic_x, i_pic_y; /* picture position */
1487 int i_pic_width, i_pic_height; /* picture dimensions */
1488 int i_old_pic_y, i_old_pic_height; /* old picture area */
1489 int i_vout_width, i_vout_height; /* display dimensions */
1490 int i_area; /* area index */
1491 int i_data_index; /* area data index */
1492 int i_data_size; /* area data size, in 256 bytes blocs */
1493 u64 * p_data; /* area data, for clearing */
1494 byte_t * p_data8; /* area data, for clearing (slow) */
1496 /* Choose buffer and set display dimensions */
1497 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1498 i_vout_width = p_vout->i_width;
1499 i_vout_height = p_vout->i_height;
1502 * Computes new picture size
1506 /* Try horizontal scaling first - width must be a mutiple of 16 */
1507 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1508 i_vout_width : p_pic->i_width) & ~0xf;
1509 switch( p_pic->i_aspect_ratio )
1511 case AR_3_4_PICTURE:
1512 i_pic_height = i_pic_width * 3 / 4;
1514 case AR_16_9_PICTURE:
1515 i_pic_height = i_pic_width * 9 / 16;
1517 case AR_221_1_PICTURE:
1518 i_pic_height = i_pic_width * 100 / 221;
1520 case AR_SQUARE_PICTURE:
1522 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1526 /* If picture dimensions using horizontal scaling are too large, use
1527 * vertical scaling. Since width must be a multiple of 16, height is
1528 * adjusted again after. */
1529 if( i_pic_height > i_vout_height )
1531 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1532 i_vout_height : p_pic->i_height;
1533 switch( p_pic->i_aspect_ratio )
1535 case AR_3_4_PICTURE:
1536 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1537 i_pic_height = i_pic_width * 3 / 4;
1539 case AR_16_9_PICTURE:
1540 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1541 i_pic_height = i_pic_width * 9 / 16;
1543 case AR_221_1_PICTURE:
1544 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1545 i_pic_height = i_pic_width * 100 / 221;
1547 case AR_SQUARE_PICTURE:
1549 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1550 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1555 /* Set picture position */
1556 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1557 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1561 /* No picture: size is 0 */
1569 * Set new picture size - if is is smaller than the previous one, clear
1570 * around it. Since picture are centered, only their size is tested.
1572 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1574 i_old_pic_y = p_buffer->i_pic_y;
1575 i_old_pic_height = p_buffer->i_pic_height;
1576 p_buffer->i_pic_x = i_pic_x;
1577 p_buffer->i_pic_y = i_pic_y;
1578 p_buffer->i_pic_width = i_pic_width;
1579 p_buffer->i_pic_height = i_pic_height;
1580 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1584 p_buffer->i_pic_x = i_pic_x;
1585 p_buffer->i_pic_y = i_pic_y;
1586 p_buffer->i_pic_width = i_pic_width;
1587 p_buffer->i_pic_height = i_pic_height;
1593 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1596 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1597 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1599 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1600 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1601 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1603 /* Clear 256 bytes block */
1604 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1605 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1606 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1607 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1608 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1609 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1610 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1611 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1613 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1615 /* Clear remaining 16 bytes blocks */
1616 *p_data++ = 0; *p_data++ = 0;
1618 p_data8 = (byte_t *)p_data;
1619 for( i_data_index = i_data_size % 16; i_data_index--; )
1621 /* Clear remaining bytes */
1629 p_buffer->i_areas = 0;
1632 /*****************************************************************************
1633 * RenderPicture: render a picture
1634 *****************************************************************************
1635 * This function convert a picture from a video heap to a pixel-encoded image
1636 * and copy it to the current rendering buffer. No lock is required, since the
1637 * rendered picture has been determined as existant, and will only be destroyed
1638 * by the vout thread later.
1639 *****************************************************************************/
1640 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1643 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1644 mtime_t render_time; /* picture rendering time */
1646 vout_buffer_t * p_buffer; /* rendering buffer */
1647 byte_t * p_pic_data; /* convertion destination */
1649 /* Get and set rendering informations */
1650 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1651 p_pic_data = p_buffer->p_data +
1652 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1653 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1655 render_time = mdate();
1659 * Choose appropriate rendering function and render picture
1661 switch( p_pic->i_type )
1663 case YUV_420_PICTURE:
1664 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1665 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1666 p_pic->i_width, p_pic->i_height,
1667 p_buffer->i_pic_width, p_buffer->i_pic_height,
1668 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1669 p_pic->i_matrix_coefficients );
1671 case YUV_422_PICTURE:
1672 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1673 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1674 p_pic->i_width, p_pic->i_height,
1675 p_buffer->i_pic_width, p_buffer->i_pic_height,
1676 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1677 p_pic->i_matrix_coefficients );
1679 case YUV_444_PICTURE:
1680 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1681 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1682 p_pic->i_width, p_pic->i_height,
1683 p_buffer->i_pic_width, p_buffer->i_pic_height,
1684 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1685 p_pic->i_matrix_coefficients );
1689 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1695 /* Print picture date and rendering time */
1696 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1697 p_vout->i_buffer_index, (long) (mdate() - render_time),
1698 mstrtime( psz_date, p_pic->date ));
1702 /*****************************************************************************
1703 * RenderPictureInfo: print additionnal informations on a picture
1704 *****************************************************************************
1705 * This function will print informations such as fps and other picture
1706 * dependant informations.
1707 *****************************************************************************/
1708 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1710 #if defined(STATS) || defined(DEBUG)
1711 char psz_buffer[256]; /* string buffer */
1716 * Print FPS rate in upper right corner
1718 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1720 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1721 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1722 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1723 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1727 * Print frames count and loop time in upper left corner
1729 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1730 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1731 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1736 * Print picture information in lower right corner
1738 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1739 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1740 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1741 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1742 p_pic->i_width, p_pic->i_height,
1743 p_pic->i_display_width, p_pic->i_display_height,
1744 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1745 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1746 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1747 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1748 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1749 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1750 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1751 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1752 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1753 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1757 /*****************************************************************************
1758 * RenderIdle: render idle picture
1759 *****************************************************************************
1760 * This function will print something on the screen. It will return 0 if
1761 * nothing has been rendered, or 1 if something has been changed on the screen.
1762 * Note that if you absolutely want something to be printed, you will have
1763 * to force it by setting the last idle date to 0.
1764 * Unlike other rendering functions, this one calls the SetBufferPicture
1765 * function when needed.
1766 *****************************************************************************/
1767 static int RenderIdle( vout_thread_t *p_vout )
1769 int i_x = 0, i_y = 0; /* text position */
1770 int i_width, i_height; /* text size */
1771 mtime_t current_date; /* current date */
1772 const char *psz_text = "waiting for stream ..."; /* text to display */
1775 current_date = mdate();
1776 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1777 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1779 SetBufferPicture( p_vout, NULL );
1780 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1781 &i_width, &i_height );
1782 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1784 vout_Print( p_vout->p_large_font,
1785 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1786 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1787 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1788 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1789 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1790 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1797 /*****************************************************************************
1798 * RenderInfo: render additionnal informations
1799 *****************************************************************************
1800 * This function render informations which do not depend of the current picture
1802 *****************************************************************************/
1803 static void RenderInfo( vout_thread_t *p_vout )
1806 char psz_buffer[256]; /* string buffer */
1807 int i_ready_pic = 0; /* ready pictures */
1808 int i_reserved_pic = 0; /* reserved pictures */
1809 int i_picture; /* picture index */
1814 * Print thread state in lower left corner
1816 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1818 switch( p_vout->p_picture[i_picture].i_status )
1820 case RESERVED_PICTURE:
1821 case RESERVED_DATED_PICTURE:
1822 case RESERVED_DISP_PICTURE:
1830 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1831 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1832 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1836 /*****************************************************************************
1837 * RenderSubPicture: render a subpicture
1838 *****************************************************************************
1839 * This function render a sub picture unit.
1840 *****************************************************************************/
1841 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1843 p_vout_font_t p_font; /* text font */
1844 int i_width, i_height; /* subpicture dimensions */
1846 while( p_subpic != NULL )
1848 switch( p_subpic->i_type )
1850 case DVD_SUBPICTURE: /* DVD subpicture unit */
1851 /* test if the picture really has to be displayed */
1852 if( mdate() < p_subpic->begin_date )
1856 if( mdate() > p_subpic->end_date )
1858 /* too late, destroying the subpic */
1859 vout_DestroySubPicture( p_vout, p_subpic );
1862 vout_RenderSPU( p_vout, p_subpic );
1864 case TEXT_SUBPICTURE: /* single line text */
1865 /* Select default font if not specified */
1866 p_font = p_subpic->type.text.p_font;
1867 if( p_font == NULL )
1869 p_font = p_vout->p_default_font;
1872 /* Compute text size (width and height fields are ignored)
1874 vout_TextSize( p_font, p_subpic->type.text.i_style,
1875 p_subpic->p_data, &i_width, &i_height );
1876 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y,
1877 i_width, i_height, p_subpic->i_horizontal_align,
1878 p_subpic->i_vertical_align ) )
1881 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1882 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1883 p_subpic->i_y * p_vout->i_bytes_per_line,
1884 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1885 p_subpic->type.text.i_char_color,
1886 p_subpic->type.text.i_border_color,
1887 p_subpic->type.text.i_bg_color,
1888 p_subpic->type.text.i_style, p_subpic->p_data );
1889 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y,
1890 i_width, i_height );
1896 intf_DbgMsg( "error: unknown subpicture %p type %d\n",
1897 p_subpic, p_subpic->i_type );
1901 p_subpic = p_subpic->p_next;
1905 /*****************************************************************************
1906 * RenderInterface: render the interface
1907 *****************************************************************************
1908 * This function render the interface, if any.
1909 *****************************************************************************/
1910 static void RenderInterface( vout_thread_t *p_vout )
1912 int i_height, i_text_height; /* total and text height */
1913 int i_width_1, i_width_2; /* text width */
1914 int i_byte; /* byte index */
1915 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1916 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1919 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1920 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1921 i_height += i_text_height;
1923 /* Render background */
1924 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1925 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1929 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1932 /* Render text, if not larger than screen */
1933 if( i_width_1 < p_vout->i_width )
1935 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1936 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1937 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1938 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1939 OUTLINED_TEXT, psz_text_1 );
1941 if( i_width_2 < p_vout->i_width )
1943 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1944 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1945 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1946 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1947 OUTLINED_TEXT, psz_text_2 );
1950 /* Activate modified area */
1951 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1954 /*****************************************************************************
1955 * Synchronize: update synchro level depending of heap state
1956 *****************************************************************************
1957 * This function is called during the main vout loop.
1958 *****************************************************************************/
1959 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1961 int i_synchro_inc = 0;
1962 /* XXX?? gore following */
1963 static int i_panic_count = 0;
1964 static int i_last_synchro_inc = 0;
1965 static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1966 static int i_truc = 10;
1970 //fprintf( stderr, "PANIC %d\n", i_panic_count );
1976 if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1979 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1984 if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1987 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1991 if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1992 i_synchro_inc*i_last_synchro_inc < 0 )
1997 if( i_delay < 6000 )
2002 else if( i_delay < 70000 )
2004 i_truc = 24+(24*i_delay)/70000;
2007 i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
2009 else if( i_delay > 100000 )
2011 r_synchro_level += 1;
2012 if( i_delay > 130000 )
2013 r_synchro_level += 1;
2016 r_synchro_level += (float)i_synchro_inc / i_truc;
2017 p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
2019 if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
2021 r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
2024 //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
2025 // p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
2026 i_last_synchro_inc = i_synchro_inc;
2029 /*****************************************************************************
2030 * Manage: manage thread
2031 *****************************************************************************
2032 * This function will handle changes in thread configuration.
2033 *****************************************************************************/
2034 static int Manage( vout_thread_t *p_vout )
2037 if( p_vout->i_changes )
2039 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
2040 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
2044 /* On gamma or grayscale change, rebuild tables */
2045 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2048 if( vout_ResetYUV( p_vout ) )
2050 intf_ErrMsg("error: can't rebuild convertion tables\n");
2055 /* Clear changes flags which does not need management or have been
2057 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2058 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
2059 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
2061 /* Detect unauthorized changes */
2062 if( p_vout->i_changes )
2064 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
2065 * function, it means they should not be authorized */
2066 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2073 /*****************************************************************************
2074 * Align: align a subpicture in the screen
2075 *****************************************************************************
2076 * This function is used for rendering text or subpictures. It returns non 0
2077 * it the final aera is not fully included in display area. Return coordinates
2079 *****************************************************************************/
2080 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2081 int i_width, int i_height, int i_h_align, int i_v_align )
2083 /* Align horizontally */
2087 *pi_x -= i_width / 2;
2090 *pi_x += (p_vout->i_width - i_width) / 2;
2096 *pi_x += p_vout->i_width - i_width;
2100 /* Align vertically */
2104 *pi_y -= i_height / 2;
2107 *pi_y += (p_vout->i_height - i_height) / 2;
2113 *pi_y += p_vout->i_height - i_height;
2115 case SUBTITLE_RALIGN:
2116 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2117 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2121 /* Return non 0 if clipping failed */
2122 return( (*pi_x < 0) || (*pi_y < 0) ||
2123 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2126 /*****************************************************************************
2127 * SetPalette: sets an 8 bpp palette
2128 *****************************************************************************
2129 * This function is just a prototype that does nothing. Architectures that
2130 * support palette allocation should override it.
2131 *****************************************************************************/
2132 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2133 u16 *green, u16 *blue, u16 *transp )
2135 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );