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 );
1092 if( b_display && p_subpic )
1094 RenderSubPicture( p_vout, p_subpic );
1098 else if( p_vout->b_active ) /* idle or interface screen alone */
1100 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1102 /* Interface has changed, so a new rendering is required - force
1103 * it by setting last idle date to 0 */
1104 p_vout->last_idle_date = 0;
1107 /* Render idle screen and update idle date, then render interface if
1109 b_display = RenderIdle( p_vout );
1112 p_vout->last_idle_date = current_date;
1113 if( p_vout->b_interface )
1115 RenderInterface( p_vout );
1126 * Sleep, wake up and display rendered picture
1130 /* Store render time */
1131 p_vout->render_time = mdate() - current_date;
1134 /* Give back change lock */
1135 vlc_mutex_unlock( &p_vout->change_lock );
1137 /* Sleep a while or until a given date */
1138 if( display_date != 0 )
1140 mwait( display_date );
1144 msleep( VOUT_IDLE_SLEEP );
1147 /* On awakening, take back lock and send immediately picture to display,
1148 * then swap buffers */
1149 vlc_mutex_lock( &p_vout->change_lock );
1151 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1152 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1154 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1156 p_vout->p_sys_display( p_vout );
1158 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1163 * Check events and manage thread
1165 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1167 /* A fatal error occured, and the thread must terminate immediately,
1168 * without displaying anything - setting b_error to 1 cause the
1169 * immediate end of the main while() loop. */
1170 p_vout->b_error = 1;
1175 * Error loop - wait until the thread destruction is requested
1177 if( p_vout->b_error )
1179 ErrorThread( p_vout );
1183 EndThread( p_vout );
1184 DestroyThread( p_vout, THREAD_OVER );
1185 intf_DbgMsg( "thread end\n" );
1188 /*****************************************************************************
1189 * ErrorThread: RunThread() error loop
1190 *****************************************************************************
1191 * This function is called when an error occured during thread main's loop. The
1192 * thread can still receive feed, but must be ready to terminate as soon as
1194 *****************************************************************************/
1195 static void ErrorThread( vout_thread_t *p_vout )
1197 /* Wait until a `die' order */
1199 while( !p_vout->b_die )
1202 msleep( VOUT_IDLE_SLEEP );
1206 /*****************************************************************************
1207 * EndThread: thread destruction
1208 *****************************************************************************
1209 * This function is called when the thread ends after a sucessful
1210 * initialization. It frees all ressources allocated by InitThread.
1211 *****************************************************************************/
1212 static void EndThread( vout_thread_t *p_vout )
1214 int i_index; /* index in heap */
1218 *p_vout->pi_status = THREAD_END;
1220 /* Destroy all remaining pictures and subpictures */
1221 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1223 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1225 free( p_vout->p_picture[i_index].p_data );
1228 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1230 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1232 free( p_vout->p_subpicture[i_index].p_data );
1236 /* Destroy translation tables */
1237 vout_EndYUV( p_vout );
1238 p_vout->p_sys_end( p_vout );
1241 /*****************************************************************************
1242 * DestroyThread: thread destruction
1243 *****************************************************************************
1244 * This function is called when the thread ends. It frees all ressources
1245 * allocated by CreateThread. Status is available at this stage.
1246 *****************************************************************************/
1247 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1249 int *pi_status; /* status adress */
1251 /* Store status adress */
1253 pi_status = p_vout->pi_status;
1255 /* Destroy thread structures allocated by Create and InitThread */
1256 vout_UnloadFont( p_vout->p_default_font );
1257 vout_UnloadFont( p_vout->p_large_font );
1258 p_vout->p_sys_destroy( p_vout );
1261 TrashPlugin( p_vout->vout_plugin );
1263 /* Free structure */
1265 *pi_status = i_status;
1268 /*****************************************************************************
1269 * Print: print simple text on a picture
1270 *****************************************************************************
1271 * This function will print a simple text on the picture. It is designed to
1272 * print debugging or general informations.
1273 *****************************************************************************/
1274 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 )
1276 int i_text_height; /* total text height */
1277 int i_text_width; /* total text width */
1279 /* Update upper left coordinates according to alignment */
1280 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1281 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1283 /* Set area and print text */
1284 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1285 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1286 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1287 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1288 p_vout->i_white_pixel, 0, 0,
1293 /*****************************************************************************
1294 * SetBufferArea: activate an area in current buffer
1295 *****************************************************************************
1296 * This function is called when something is rendered on the current buffer.
1297 * It set the area as active and prepare it to be cleared on next rendering.
1298 * Pay attention to the fact that in this functions, i_h is in fact the end y
1299 * coordinate of the new area.
1300 *****************************************************************************/
1301 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1303 vout_buffer_t * p_buffer; /* current buffer */
1304 int i_area_begin, i_area_end; /* area vertical extension */
1305 int i_area, i_area_copy; /* area index */
1306 int i_area_shift; /* shift distance for areas */
1308 /* Choose buffer and modify h to end of area position */
1309 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1313 * Remove part of the area which is inside the picture - this is done
1314 * by calling again SetBufferArea with the correct areas dimensions.
1316 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1318 i_area_begin = p_buffer->i_pic_y;
1319 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1321 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1322 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1323 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1325 /* Keep the stripe above the picture, if any */
1326 if( i_y < i_area_begin )
1328 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1330 /* Keep the stripe below the picture, if any */
1331 if( i_h > i_area_end )
1333 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1339 /* Skip some extensions until interesting areas */
1341 (i_area < p_buffer->i_areas) &&
1342 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1348 if( i_area == p_buffer->i_areas )
1350 /* New area is below all existing ones: just add it at the end of the
1351 * array, if possible - else, append it to the last one */
1352 if( i_area < VOUT_MAX_AREAS )
1354 p_buffer->pi_area_begin[i_area] = i_y;
1355 p_buffer->pi_area_end[i_area] = i_h;
1356 p_buffer->i_areas++;
1361 intf_DbgMsg("areas overflow\n");
1363 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1368 i_area_begin = p_buffer->pi_area_begin[i_area];
1369 i_area_end = p_buffer->pi_area_end[i_area];
1371 if( i_y < i_area_begin )
1373 if( i_h >= i_area_begin - 1 )
1375 /* Extend area above */
1376 p_buffer->pi_area_begin[i_area] = i_y;
1380 /* Create a new area above : merge last area if overflow, then
1381 * move all old areas down */
1382 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1385 intf_DbgMsg("areas overflow\n");
1387 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1391 p_buffer->i_areas++;
1393 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1395 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1396 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1398 p_buffer->pi_area_begin[i_area] = i_y;
1399 p_buffer->pi_area_end[i_area] = i_h;
1403 if( i_h > i_area_end )
1405 /* Find further areas which can be merged with the new one */
1406 for( i_area_copy = i_area + 1;
1407 (i_area_copy < p_buffer->i_areas) &&
1408 (p_buffer->pi_area_begin[i_area] <= i_h);
1415 if( i_area_copy != i_area )
1417 /* Merge with last possible areas */
1418 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1420 /* Shift lower areas upward */
1421 i_area_shift = i_area_copy - i_area;
1422 p_buffer->i_areas -= i_area_shift;
1423 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1425 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1426 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1431 /* Extend area below */
1432 p_buffer->pi_area_end[i_area] = i_h;
1438 /*****************************************************************************
1439 * SetBufferPicture: clear buffer and set picture area
1440 *****************************************************************************
1441 * This function is called before any rendering. It clears the current
1442 * rendering buffer and set the new picture area. If the picture pointer is
1443 * NULL, then no picture area is defined. Floating operations are avoided since
1444 * some MMX calculations may follow.
1445 *****************************************************************************/
1446 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1448 vout_buffer_t * p_buffer; /* current buffer */
1449 int i_pic_x, i_pic_y; /* picture position */
1450 int i_pic_width, i_pic_height; /* picture dimensions */
1451 int i_old_pic_y, i_old_pic_height; /* old picture area */
1452 int i_vout_width, i_vout_height; /* display dimensions */
1453 int i_area; /* area index */
1454 int i_data_index; /* area data index */
1455 int i_data_size; /* area data size, in 256 bytes blocs */
1456 u64 * p_data; /* area data, for clearing */
1457 byte_t * p_data8; /* area data, for clearing (slow) */
1459 /* Choose buffer and set display dimensions */
1460 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1461 i_vout_width = p_vout->i_width;
1462 i_vout_height = p_vout->i_height;
1465 * Computes new picture size
1469 /* Try horizontal scaling first - width must be a mutiple of 16 */
1470 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1471 i_vout_width : p_pic->i_width) & ~0xf;
1472 switch( p_pic->i_aspect_ratio )
1474 case AR_3_4_PICTURE:
1475 i_pic_height = i_pic_width * 3 / 4;
1477 case AR_16_9_PICTURE:
1478 i_pic_height = i_pic_width * 9 / 16;
1480 case AR_221_1_PICTURE:
1481 i_pic_height = i_pic_width * 100 / 221;
1483 case AR_SQUARE_PICTURE:
1485 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1489 /* If picture dimensions using horizontal scaling are too large, use
1490 * vertical scaling. Since width must be a multiple of 16, height is
1491 * adjusted again after. */
1492 if( i_pic_height > i_vout_height )
1494 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1495 i_vout_height : p_pic->i_height;
1496 switch( p_pic->i_aspect_ratio )
1498 case AR_3_4_PICTURE:
1499 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1500 i_pic_height = i_pic_width * 3 / 4;
1502 case AR_16_9_PICTURE:
1503 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1504 i_pic_height = i_pic_width * 9 / 16;
1506 case AR_221_1_PICTURE:
1507 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1508 i_pic_height = i_pic_width * 100 / 221;
1510 case AR_SQUARE_PICTURE:
1512 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1513 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1518 /* Set picture position */
1519 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1520 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1524 /* No picture: size is 0 */
1532 * Set new picture size - if is is smaller than the previous one, clear
1533 * around it. Since picture are centered, only their size is tested.
1535 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1537 i_old_pic_y = p_buffer->i_pic_y;
1538 i_old_pic_height = p_buffer->i_pic_height;
1539 p_buffer->i_pic_x = i_pic_x;
1540 p_buffer->i_pic_y = i_pic_y;
1541 p_buffer->i_pic_width = i_pic_width;
1542 p_buffer->i_pic_height = i_pic_height;
1543 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1547 p_buffer->i_pic_x = i_pic_x;
1548 p_buffer->i_pic_y = i_pic_y;
1549 p_buffer->i_pic_width = i_pic_width;
1550 p_buffer->i_pic_height = i_pic_height;
1556 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1559 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1560 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1562 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1563 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1564 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1566 /* Clear 256 bytes block */
1567 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1568 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1569 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1570 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1571 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1572 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1573 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1574 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1576 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1578 /* Clear remaining 16 bytes blocks */
1579 *p_data++ = 0; *p_data++ = 0;
1581 p_data8 = (byte_t *)p_data;
1582 for( i_data_index = i_data_size % 16; i_data_index--; )
1584 /* Clear remaining bytes */
1592 p_buffer->i_areas = 0;
1595 /*****************************************************************************
1596 * RenderPicture: render a picture
1597 *****************************************************************************
1598 * This function convert a picture from a video heap to a pixel-encoded image
1599 * and copy it to the current rendering buffer. No lock is required, since the
1600 * rendered picture has been determined as existant, and will only be destroyed
1601 * by the vout thread later.
1602 *****************************************************************************/
1603 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1606 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1607 mtime_t render_time; /* picture rendering time */
1609 vout_buffer_t * p_buffer; /* rendering buffer */
1610 byte_t * p_pic_data; /* convertion destination */
1612 /* Get and set rendering informations */
1613 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1614 p_pic_data = p_buffer->p_data +
1615 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1616 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1618 render_time = mdate();
1622 * Choose appropriate rendering function and render picture
1624 switch( p_pic->i_type )
1626 case YUV_420_PICTURE:
1627 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1628 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1629 p_pic->i_width, p_pic->i_height,
1630 p_buffer->i_pic_width, p_buffer->i_pic_height,
1631 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1632 p_pic->i_matrix_coefficients );
1634 case YUV_422_PICTURE:
1635 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1636 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1637 p_pic->i_width, p_pic->i_height,
1638 p_buffer->i_pic_width, p_buffer->i_pic_height,
1639 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1640 p_pic->i_matrix_coefficients );
1642 case YUV_444_PICTURE:
1643 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1644 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1645 p_pic->i_width, p_pic->i_height,
1646 p_buffer->i_pic_width, p_buffer->i_pic_height,
1647 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1648 p_pic->i_matrix_coefficients );
1652 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1658 /* Print picture date and rendering time */
1659 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1660 p_vout->i_buffer_index, (long) (mdate() - render_time),
1661 mstrtime( psz_date, p_pic->date ));
1665 /*****************************************************************************
1666 * RenderPictureInfo: print additionnal informations on a picture
1667 *****************************************************************************
1668 * This function will print informations such as fps and other picture
1669 * dependant informations.
1670 *****************************************************************************/
1671 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1673 #if defined(STATS) || defined(DEBUG)
1674 char psz_buffer[256]; /* string buffer */
1679 * Print FPS rate in upper right corner
1681 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1683 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1684 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1685 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1686 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1690 * Print frames count and loop time in upper left corner
1692 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1693 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1694 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1699 * Print picture information in lower right corner
1701 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1702 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1703 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1704 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1705 p_pic->i_width, p_pic->i_height,
1706 p_pic->i_display_width, p_pic->i_display_height,
1707 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1708 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1709 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1710 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1711 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1712 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1713 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1714 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1715 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1716 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1720 /*****************************************************************************
1721 * RenderIdle: render idle picture
1722 *****************************************************************************
1723 * This function will print something on the screen. It will return 0 if
1724 * nothing has been rendered, or 1 if something has been changed on the screen.
1725 * Note that if you absolutely want something to be printed, you will have
1726 * to force it by setting the last idle date to 0.
1727 * Unlike other rendering functions, this one calls the SetBufferPicture
1728 * function when needed.
1729 *****************************************************************************/
1730 static int RenderIdle( vout_thread_t *p_vout )
1732 int i_x = 0, i_y = 0; /* text position */
1733 int i_width, i_height; /* text size */
1734 mtime_t current_date; /* current date */
1735 const char *psz_text = "waiting for stream ..."; /* text to display */
1738 current_date = mdate();
1739 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1740 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1742 SetBufferPicture( p_vout, NULL );
1743 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1744 &i_width, &i_height );
1745 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1747 vout_Print( p_vout->p_large_font,
1748 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1749 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1750 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1751 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1752 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1753 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1760 /*****************************************************************************
1761 * RenderInfo: render additionnal informations
1762 *****************************************************************************
1763 * This function render informations which do not depend of the current picture
1765 *****************************************************************************/
1766 static void RenderInfo( vout_thread_t *p_vout )
1769 char psz_buffer[256]; /* string buffer */
1770 int i_ready_pic = 0; /* ready pictures */
1771 int i_reserved_pic = 0; /* reserved pictures */
1772 int i_picture; /* picture index */
1777 * Print thread state in lower left corner
1779 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1781 switch( p_vout->p_picture[i_picture].i_status )
1783 case RESERVED_PICTURE:
1784 case RESERVED_DATED_PICTURE:
1785 case RESERVED_DISP_PICTURE:
1793 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1794 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1795 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1799 /*****************************************************************************
1800 * RenderSubPicture: render a subpicture
1801 *****************************************************************************
1802 * This function render a sub picture unit.
1803 *****************************************************************************/
1804 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1806 p_vout_font_t p_font; /* text font */
1807 int i_width, i_height; /* subpicture dimensions */
1809 while( p_subpic != NULL )
1811 switch( p_subpic->i_type )
1813 case DVD_SUBPICTURE: /* DVD subpicture unit */
1814 /* test if the picture really has to be displayed */
1815 if( mdate() < p_subpic->begin_date )
1817 /* not yet, see you later */
1820 if( mdate() > p_subpic->end_date )
1822 /* too late, destroying the subpic */
1823 vout_DestroySubPicture( p_vout, p_subpic );
1826 vout_RenderSPU( p_vout, p_subpic );
1828 case TEXT_SUBPICTURE: /* single line text */
1829 /* Select default font if not specified */
1830 p_font = p_subpic->type.text.p_font;
1831 if( p_font == NULL )
1833 p_font = p_vout->p_default_font;
1836 /* Compute text size (width and height fields are ignored)
1838 vout_TextSize( p_font, p_subpic->type.text.i_style,
1839 p_subpic->p_data, &i_width, &i_height );
1840 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y,
1841 i_width, i_height, p_subpic->i_horizontal_align,
1842 p_subpic->i_vertical_align ) )
1845 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1846 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1847 p_subpic->i_y * p_vout->i_bytes_per_line,
1848 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1849 p_subpic->type.text.i_char_color,
1850 p_subpic->type.text.i_border_color,
1851 p_subpic->type.text.i_bg_color,
1852 p_subpic->type.text.i_style, p_subpic->p_data );
1853 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y,
1854 i_width, i_height );
1860 intf_DbgMsg( "error: unknown subpicture %p type %d\n",
1861 p_subpic, p_subpic->i_type );
1865 p_subpic = p_subpic->p_next;
1869 /*****************************************************************************
1870 * RenderInterface: render the interface
1871 *****************************************************************************
1872 * This function render the interface, if any.
1873 *****************************************************************************/
1874 static void RenderInterface( vout_thread_t *p_vout )
1876 int i_height, i_text_height; /* total and text height */
1877 int i_width_1, i_width_2; /* text width */
1878 int i_byte; /* byte index */
1879 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1880 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1883 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1884 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1885 i_height += i_text_height;
1887 /* Render background */
1888 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1889 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1893 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1896 /* Render text, if not larger than screen */
1897 if( i_width_1 < p_vout->i_width )
1899 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1900 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1901 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1902 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1903 OUTLINED_TEXT, psz_text_1 );
1905 if( i_width_2 < p_vout->i_width )
1907 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1908 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1909 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1910 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1911 OUTLINED_TEXT, psz_text_2 );
1914 /* Activate modified area */
1915 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1918 /*****************************************************************************
1919 * Synchronize: update synchro level depending of heap state
1920 *****************************************************************************
1921 * This function is called during the main vout loop.
1922 *****************************************************************************/
1923 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1925 int i_synchro_inc = 0;
1926 /* XXX?? gore following */
1927 static int i_panic_count = 0;
1928 static int i_last_synchro_inc = 0;
1929 static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1930 static int i_truc = 10;
1934 //fprintf( stderr, "PANIC %d\n", i_panic_count );
1940 if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1943 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1948 if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1951 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1955 if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1956 i_synchro_inc*i_last_synchro_inc < 0 )
1961 if( i_delay < 6000 )
1966 else if( i_delay < 70000 )
1968 i_truc = 24+(24*i_delay)/70000;
1971 i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
1973 else if( i_delay > 100000 )
1975 r_synchro_level += 1;
1976 if( i_delay > 130000 )
1977 r_synchro_level += 1;
1980 r_synchro_level += (float)i_synchro_inc / i_truc;
1981 p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
1983 if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
1985 r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
1988 //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
1989 // p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
1990 i_last_synchro_inc = i_synchro_inc;
1993 /*****************************************************************************
1994 * Manage: manage thread
1995 *****************************************************************************
1996 * This function will handle changes in thread configuration.
1997 *****************************************************************************/
1998 static int Manage( vout_thread_t *p_vout )
2001 if( p_vout->i_changes )
2003 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
2004 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
2008 /* On gamma or grayscale change, rebuild tables */
2009 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2012 if( vout_ResetYUV( p_vout ) )
2014 intf_ErrMsg("error: can't rebuild convertion tables\n");
2019 /* Clear changes flags which does not need management or have been
2021 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2022 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
2023 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
2025 /* Detect unauthorized changes */
2026 if( p_vout->i_changes )
2028 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
2029 * function, it means they should not be authorized */
2030 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2037 /*****************************************************************************
2038 * Align: align a subpicture in the screen
2039 *****************************************************************************
2040 * This function is used for rendering text or subpictures. It returns non 0
2041 * it the final aera is not fully included in display area. Return coordinates
2043 *****************************************************************************/
2044 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2045 int i_width, int i_height, int i_h_align, int i_v_align )
2047 /* Align horizontally */
2051 *pi_x -= i_width / 2;
2054 *pi_x += (p_vout->i_width - i_width) / 2;
2060 *pi_x += p_vout->i_width - i_width;
2064 /* Align vertically */
2068 *pi_y -= i_height / 2;
2071 *pi_y += (p_vout->i_height - i_height) / 2;
2077 *pi_y += p_vout->i_height - i_height;
2079 case SUBTITLE_RALIGN:
2080 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2081 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2085 /* Return non 0 if clipping failed */
2086 return( (*pi_x < 0) || (*pi_y < 0) ||
2087 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2090 /*****************************************************************************
2091 * SetPalette: sets an 8 bpp palette
2092 *****************************************************************************
2093 * This function is just a prototype that does nothing. Architectures that
2094 * support palette allocation should override it.
2095 *****************************************************************************/
2096 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2097 u16 *green, u16 *blue, u16 *transp )
2099 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );