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 GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <errno.h> /* ENOMEM */
33 #include <stdlib.h> /* free() */
34 #include <stdio.h> /* sprintf() */
35 #include <string.h> /* strerror() */
43 #include "video_output.h"
44 #include "video_text.h"
45 #include "video_spu.h"
46 #include "video_yuv.h"
51 /*****************************************************************************
53 *****************************************************************************/
54 static int BinaryLog ( u32 i );
55 static void MaskToShift ( int *pi_left, int *pi_right, u32 i_mask );
56 static int InitThread ( vout_thread_t *p_vout );
57 static void RunThread ( vout_thread_t *p_vout );
58 static void ErrorThread ( vout_thread_t *p_vout );
59 static void EndThread ( vout_thread_t *p_vout );
60 static void DestroyThread ( vout_thread_t *p_vout, int i_status );
61 static void Print ( vout_thread_t *p_vout, int i_x, int i_y,
62 int i_h_align, int i_v_align,
63 unsigned char *psz_text );
64 static void SetBufferArea ( vout_thread_t *p_vout, int i_x, int i_y,
66 static void SetBufferPicture ( vout_thread_t *p_vout, picture_t *p_pic );
67 static void RenderPicture ( vout_thread_t *p_vout, picture_t *p_pic );
68 static void RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
69 static void RenderSubPicture ( vout_thread_t *p_vout,
70 subpicture_t *p_subpic );
71 static void RenderInterface ( vout_thread_t *p_vout );
72 static int RenderIdle ( vout_thread_t *p_vout );
73 static void RenderInfo ( vout_thread_t *p_vout );
74 static void Synchronize ( vout_thread_t *p_vout, s64 i_delay );
75 static int Manage ( vout_thread_t *p_vout );
76 static int Align ( vout_thread_t *p_vout, int *pi_x,
77 int *pi_y, int i_width, int i_height,
78 int i_h_align, int i_v_align );
79 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
80 u16 *green, u16 *blue, u16 *transp );
82 /*****************************************************************************
83 * vout_CreateThread: creates a new video output thread
84 *****************************************************************************
85 * This function creates a new video output thread, and returns a pointer
86 * to its description. On error, it returns NULL.
87 * If pi_status is NULL, then the function will block until the thread is ready.
88 * If not, it will be updated using one of the THREAD_* constants.
89 *****************************************************************************/
90 vout_thread_t * vout_CreateThread ( char *psz_display, int i_root_window,
91 int i_width, int i_height, int *pi_status, int i_method )
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 = GetPluginFunction( p_vout->vout_plugin, "vout_SysCreate" );
119 p_vout->p_sys_init = GetPluginFunction( p_vout->vout_plugin, "vout_SysInit" );
120 p_vout->p_sys_end = GetPluginFunction( p_vout->vout_plugin, "vout_SysEnd" );
121 p_vout->p_sys_destroy = GetPluginFunction( p_vout->vout_plugin, "vout_SysDestroy" );
122 p_vout->p_sys_manage = GetPluginFunction( p_vout->vout_plugin, "vout_SysManage" );
123 p_vout->p_sys_display = GetPluginFunction( p_vout->vout_plugin, "vout_SysDisplay" );
125 /* Initialize thread properties - thread id and locks will be initialized
129 p_vout->b_active = 0;
130 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
131 *p_vout->pi_status = THREAD_CREATE;
133 /* Initialize some fields used by the system-dependant method - these fields will
134 * probably be modified by the method, and are only preferences */
135 p_vout->i_changes = 0;
136 p_vout->i_width = i_width;
137 p_vout->i_height = i_height;
138 p_vout->i_bytes_per_line = i_width * 2;
139 p_vout->i_screen_depth = 15;
140 p_vout->i_bytes_per_pixel = 2;
141 p_vout->f_gamma = VOUT_GAMMA;
143 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
145 p_vout->b_interface = 0;
148 p_vout->p_set_palette = SetPalette;
150 intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
151 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
152 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
154 /* Initialize idle screen */
155 p_vout->last_display_date = mdate();
156 p_vout->last_display_date = 0;
157 p_vout->last_idle_date = 0;
160 /* Initialize statistics fields */
161 p_vout->render_time = 0;
162 p_vout->c_fps_samples = 0;
165 /* Initialize buffer index */
166 p_vout->i_buffer_index = 0;
168 /* Initialize pictures and subpictures - translation tables and functions
169 * will be initialized later in InitThread */
170 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
172 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
173 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
174 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
175 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
177 p_vout->i_pictures = 0;
179 /* Initialize synchronization informations */
180 p_vout->i_synchro_level = VOUT_SYNCHRO_LEVEL_START;
182 /* Create and initialize system-dependant method - this function issues its
183 * own error messages */
184 if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
186 TrashPlugin( p_vout->vout_plugin );
190 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
191 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
192 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
193 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
195 /* Calculate shifts from system-updated masks */
196 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
197 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
198 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
200 /* Set some useful colors */
201 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
202 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
203 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
204 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
206 /* Load fonts - fonts must be initialized after the system method since
207 * they may be dependant on screen depth and other thread properties */
208 p_vout->p_default_font = vout_LoadFont( DATA_PATH "/" VOUT_DEFAULT_FONT );
209 if( p_vout->p_default_font == NULL )
211 p_vout->p_default_font = vout_LoadFont( "share/" VOUT_DEFAULT_FONT );
213 if( p_vout->p_default_font == NULL )
215 p_vout->p_sys_destroy( p_vout );
216 TrashPlugin( p_vout->vout_plugin );
220 p_vout->p_large_font = vout_LoadFont( DATA_PATH "/" VOUT_LARGE_FONT );
221 if( p_vout->p_large_font == NULL )
223 p_vout->p_large_font = vout_LoadFont( "share/" VOUT_LARGE_FONT );
225 if( p_vout->p_large_font == NULL )
227 vout_UnloadFont( p_vout->p_default_font );
228 p_vout->p_sys_destroy( p_vout );
229 TrashPlugin( p_vout->vout_plugin );
234 /* Create thread and set locks */
235 vlc_mutex_init( &p_vout->picture_lock );
236 vlc_mutex_init( &p_vout->subpicture_lock );
237 vlc_mutex_init( &p_vout->change_lock );
238 vlc_mutex_lock( &p_vout->change_lock );
239 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
241 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
242 vout_UnloadFont( p_vout->p_default_font );
243 vout_UnloadFont( p_vout->p_large_font );
244 p_vout->p_sys_destroy( p_vout );
245 TrashPlugin( p_vout->vout_plugin );
250 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
251 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
253 /* If status is NULL, wait until the thread is created */
254 if( pi_status == NULL )
258 msleep( THREAD_SLEEP );
259 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
260 && (i_status != THREAD_FATAL) );
261 if( i_status != THREAD_READY )
269 /*****************************************************************************
270 * vout_DestroyThread: destroys a previously created thread
271 *****************************************************************************
272 * Destroy a terminated thread.
273 * The function will request a destruction of the specified thread. If pi_error
274 * is NULL, it will return once the thread is destroyed. Else, it will be
275 * update using one of the THREAD_* constants.
276 *****************************************************************************/
277 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
279 int i_status; /* thread status */
283 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
284 *p_vout->pi_status = THREAD_DESTROY;
286 /* Request thread destruction */
289 /* If status is NULL, wait until thread has been destroyed */
290 if( pi_status == NULL )
294 msleep( THREAD_SLEEP );
295 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
296 && (i_status != THREAD_FATAL) );
300 /*****************************************************************************
301 * vout_DisplaySubPicture: display a subpicture unit
302 *****************************************************************************
303 * Remove the reservation flag of an subpicture, which will cause it to be ready
304 * for display. The picture does not need to be locked, since it is ignored by
305 * the output thread if is reserved.
306 *****************************************************************************/
307 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
310 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
311 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
315 /* Check if status is valid */
316 if( p_subpic->i_status != RESERVED_SUBPICTURE )
318 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
319 p_subpic->i_status );
323 /* Remove reservation flag */
324 p_subpic->i_status = READY_SUBPICTURE;
327 /* Send subpicture informations */
328 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
329 p_subpic, p_subpic->i_type,
330 mstrtime( psz_begin_date, p_subpic->begin_date ),
331 mstrtime( psz_end_date, p_subpic->end_date ) );
335 /*****************************************************************************
336 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
337 *****************************************************************************
338 * This function create a reserved subpicture in the video output heap.
339 * A null pointer is returned if the function fails. This method provides an
340 * already allocated zone of memory in the spu data fields. It needs locking
341 * since several pictures can be created by several producers threads.
342 *****************************************************************************/
343 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
346 int i_subpic; /* subpicture index */
347 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
348 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
351 vlc_mutex_lock( &p_vout->subpicture_lock );
354 * Look for an empty place
356 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
358 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
360 /* Subpicture is marked for destruction, but is still allocated */
361 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
362 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
364 /* Memory size do match or is smaller : memory will not be
365 * reallocated, and function can end immediately - this is
366 * the best possible case, since no memory allocation needs
368 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
370 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
371 &p_vout->p_subpicture[i_subpic] );
373 vlc_mutex_unlock( &p_vout->subpicture_lock );
374 return( &p_vout->p_subpicture[i_subpic] );
376 else if( p_destroyed_subpic == NULL )
378 /* Memory size do not match, but subpicture index will be kept in
379 * case no other place are left */
380 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
383 else if( (p_free_subpic == NULL) &&
384 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
386 /* Subpicture is empty and ready for allocation */
387 p_free_subpic = &p_vout->p_subpicture[i_subpic];
391 /* If no free subpicture is available, use a destroyed subpicture */
392 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
394 /* No free subpicture or matching destroyed subpicture has been
395 * found, but a destroyed subpicture is still avalaible */
396 free( p_destroyed_subpic->p_data );
397 p_free_subpic = p_destroyed_subpic;
403 if( p_free_subpic != NULL )
405 /* Allocate memory */
408 case TEXT_SUBPICTURE: /* text subpicture */
409 p_free_subpic->p_data = malloc( i_size + 1 );
411 case DVD_SUBPICTURE: /* DVD subpicture unit */
412 p_free_subpic->p_data = malloc( i_size );
416 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
417 p_free_subpic->p_data = NULL;
422 if( p_free_subpic->p_data != NULL )
423 { /* Copy subpicture informations, set some default values */
424 p_free_subpic->i_type = i_type;
425 p_free_subpic->i_status = RESERVED_SUBPICTURE;
426 p_free_subpic->i_size = i_size;
427 p_free_subpic->i_x = 0;
428 p_free_subpic->i_y = 0;
429 p_free_subpic->i_width = 0;
430 p_free_subpic->i_height = 0;
431 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
432 p_free_subpic->i_vertical_align = CENTER_RALIGN;
436 /* Memory allocation failed : set subpicture as empty */
437 p_free_subpic->i_type = EMPTY_SUBPICTURE;
438 p_free_subpic->i_status = FREE_SUBPICTURE;
439 p_free_subpic = NULL;
440 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
444 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
446 vlc_mutex_unlock( &p_vout->subpicture_lock );
447 return( p_free_subpic );
450 /* No free or destroyed subpicture could be found */
451 intf_DbgMsg( "warning: heap is full\n" );
452 vlc_mutex_unlock( &p_vout->subpicture_lock );
456 /*****************************************************************************
457 * vout_DestroySubPicture: remove a subpicture from the heap
458 *****************************************************************************
459 * This function frees a previously reserved subpicture.
460 * It is meant to be used when the construction of a picture aborted.
461 * This function does not need locking since reserved subpictures are ignored
462 * by the output thread.
463 *****************************************************************************/
464 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
467 /* Check if status is valid */
468 if( p_subpic->i_status != RESERVED_SUBPICTURE )
470 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
471 p_subpic, p_subpic->i_status );
475 p_subpic->i_status = DESTROYED_SUBPICTURE;
478 intf_DbgMsg("subpicture %p\n", p_subpic);
482 /*****************************************************************************
483 * vout_DisplayPicture: display a picture
484 *****************************************************************************
485 * Remove the reservation flag of a picture, which will cause it to be ready for
486 * display. The picture won't be displayed until vout_DatePicture has been
488 *****************************************************************************/
489 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
491 vlc_mutex_lock( &p_vout->picture_lock );
492 switch( p_pic->i_status )
494 case RESERVED_PICTURE:
495 p_pic->i_status = RESERVED_DISP_PICTURE;
497 case RESERVED_DATED_PICTURE:
498 p_pic->i_status = READY_PICTURE;
502 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
508 intf_DbgMsg("picture %p\n", p_pic);
510 vlc_mutex_unlock( &p_vout->picture_lock );
513 /*****************************************************************************
514 * vout_DatePicture: date a picture
515 *****************************************************************************
516 * Remove the reservation flag of a picture, which will cause it to be ready for
517 * display. The picture won't be displayed until vout_DisplayPicture has been
519 *****************************************************************************/
520 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
523 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
526 vlc_mutex_lock( &p_vout->picture_lock );
528 switch( p_pic->i_status )
530 case RESERVED_PICTURE:
531 p_pic->i_status = RESERVED_DATED_PICTURE;
533 case RESERVED_DISP_PICTURE:
534 p_pic->i_status = READY_PICTURE;
538 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
544 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
546 vlc_mutex_unlock( &p_vout->picture_lock );
549 /*****************************************************************************
550 * vout_CreatePicture: allocate a picture in the video output heap.
551 *****************************************************************************
552 * This function create a reserved image in the video output heap.
553 * A null pointer is returned if the function fails. This method provides an
554 * already allocated zone of memory in the picture data fields. It needs locking
555 * since several pictures can be created by several producers threads.
556 *****************************************************************************/
557 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
558 int i_width, int i_height )
560 int i_picture; /* picture index */
561 int i_chroma_width = 0; /* chroma width */
562 picture_t * p_free_picture = NULL; /* first free picture */
563 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
566 vlc_mutex_lock( &p_vout->picture_lock );
569 * Look for an empty place
571 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
573 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
575 /* Picture is marked for destruction, but is still allocated - note
576 * that if width and type are the same for two pictures, chroma_width
577 * should also be the same */
578 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
579 (p_vout->p_picture[i_picture].i_height == i_height) &&
580 (p_vout->p_picture[i_picture].i_width == i_width) )
582 /* Memory size do match : memory will not be reallocated, and function
583 * can end immediately - this is the best possible case, since no
584 * memory allocation needs to be done */
585 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
586 p_vout->i_pictures++;
588 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
589 &p_vout->p_picture[i_picture] );
591 vlc_mutex_unlock( &p_vout->picture_lock );
592 return( &p_vout->p_picture[i_picture] );
594 else if( p_destroyed_picture == NULL )
596 /* Memory size do not match, but picture index will be kept in
597 * case no other place are left */
598 p_destroyed_picture = &p_vout->p_picture[i_picture];
601 else if( (p_free_picture == NULL) &&
602 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
604 /* Picture is empty and ready for allocation */
605 p_free_picture = &p_vout->p_picture[i_picture];
609 /* If no free picture is available, use a destroyed picture */
610 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
612 /* No free picture or matching destroyed picture has been found, but
613 * a destroyed picture is still avalaible */
614 free( p_destroyed_picture->p_data );
615 p_free_picture = p_destroyed_picture;
621 if( p_free_picture != NULL )
623 /* Allocate memory */
626 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
627 i_chroma_width = i_width / 2;
628 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
629 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
630 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
631 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
633 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
634 i_chroma_width = i_width / 2;
635 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
636 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
637 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
638 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
640 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
641 i_chroma_width = i_width;
642 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
643 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
644 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
645 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
649 intf_DbgMsg("error: unknown picture type %d\n", i_type );
650 p_free_picture->p_data = NULL;
655 if( p_free_picture->p_data != NULL )
657 /* Copy picture informations, set some default values */
658 p_free_picture->i_type = i_type;
659 p_free_picture->i_status = RESERVED_PICTURE;
660 p_free_picture->i_matrix_coefficients = 1;
661 p_free_picture->i_width = i_width;
662 p_free_picture->i_height = i_height;
663 p_free_picture->i_chroma_width = i_chroma_width;
664 p_free_picture->i_display_horizontal_offset = 0;
665 p_free_picture->i_display_vertical_offset = 0;
666 p_free_picture->i_display_width = i_width;
667 p_free_picture->i_display_height = i_height;
668 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
669 p_free_picture->i_refcount = 0;
670 p_vout->i_pictures++;
674 /* Memory allocation failed : set picture as empty */
675 p_free_picture->i_type = EMPTY_PICTURE;
676 p_free_picture->i_status = FREE_PICTURE;
677 p_free_picture = NULL;
678 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
682 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
684 vlc_mutex_unlock( &p_vout->picture_lock );
685 return( p_free_picture );
688 /* No free or destroyed picture could be found */
689 intf_DbgMsg( "warning: heap is full\n" );
690 vlc_mutex_unlock( &p_vout->picture_lock );
694 /*****************************************************************************
695 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
696 *****************************************************************************
697 * This function frees a previously reserved picture or a permanent
698 * picture. It is meant to be used when the construction of a picture aborted.
699 * Note that the picture will be destroyed even if it is linked !
700 *****************************************************************************/
701 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
703 vlc_mutex_lock( &p_vout->picture_lock );
706 /* Check if picture status is valid */
707 if( (p_pic->i_status != RESERVED_PICTURE) &&
708 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
709 (p_pic->i_status != RESERVED_DISP_PICTURE) )
711 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
715 p_pic->i_status = DESTROYED_PICTURE;
716 p_vout->i_pictures--;
719 intf_DbgMsg("picture %p\n", p_pic);
721 vlc_mutex_unlock( &p_vout->picture_lock );
724 /*****************************************************************************
725 * vout_LinkPicture: increment reference counter of a picture
726 *****************************************************************************
727 * This function increment the reference counter of a picture in the video
728 * heap. It needs a lock since several producer threads can access the picture.
729 *****************************************************************************/
730 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
732 vlc_mutex_lock( &p_vout->picture_lock );
736 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
739 vlc_mutex_unlock( &p_vout->picture_lock );
742 /*****************************************************************************
743 * vout_UnlinkPicture: decrement reference counter of a picture
744 *****************************************************************************
745 * This function decrement the reference counter of a picture in the video heap.
746 *****************************************************************************/
747 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
749 vlc_mutex_lock( &p_vout->picture_lock );
753 if( p_pic->i_refcount < 0 )
755 intf_DbgMsg("error: refcount < 0\n");
756 p_pic->i_refcount = 0;
760 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
762 p_pic->i_status = DESTROYED_PICTURE;
763 p_vout->i_pictures--;
767 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
770 vlc_mutex_unlock( &p_vout->picture_lock );
773 /*****************************************************************************
774 * vout_SetBuffers: set buffers adresses
775 *****************************************************************************
776 * This function is called by system drivers to set buffers video memory
778 *****************************************************************************/
779 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
781 /* No picture previously */
782 p_vout->p_buffer[0].i_pic_x = 0;
783 p_vout->p_buffer[0].i_pic_y = 0;
784 p_vout->p_buffer[0].i_pic_width = 0;
785 p_vout->p_buffer[0].i_pic_height = 0;
786 p_vout->p_buffer[1].i_pic_x = 0;
787 p_vout->p_buffer[1].i_pic_y = 0;
788 p_vout->p_buffer[1].i_pic_width = 0;
789 p_vout->p_buffer[1].i_pic_height = 0;
791 /* The first area covers all the screen */
792 p_vout->p_buffer[0].i_areas = 1;
793 p_vout->p_buffer[0].pi_area_begin[0] = 0;
794 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
795 p_vout->p_buffer[1].i_areas = 1;
796 p_vout->p_buffer[1].pi_area_begin[0] = 0;
797 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
800 p_vout->p_buffer[0].p_data = p_buf1;
801 p_vout->p_buffer[1].p_data = p_buf2;
804 /*****************************************************************************
805 * vout_Pixel2RGB: return red, green and blue from pixel value
806 *****************************************************************************
807 * Return color values, in 0-255 range, of the decomposition of a pixel. This
808 * is a slow routine and should only be used for initialization phase.
809 *****************************************************************************/
810 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
812 *pi_red = i_pixel & p_vout->i_red_mask;
813 *pi_green = i_pixel & p_vout->i_green_mask;
814 *pi_blue = i_pixel & p_vout->i_blue_mask;
817 /* following functions are local */
819 /*****************************************************************************
820 * BinaryLog: computes the base 2 log of a binary value
821 *****************************************************************************
822 * This functions is used by MaskToShift, to get a bit index from a binary
824 *****************************************************************************/
825 static int BinaryLog(u32 i)
850 if (i != ((u32)1 << i_log))
852 intf_ErrMsg("internal error: binary log overflow\n");
858 /*****************************************************************************
859 * MaskToShift: transform a color mask into right and left shifts
860 *****************************************************************************
861 * This function is used for obtaining color shifts from masks.
862 *****************************************************************************/
863 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
865 u32 i_low, i_high; /* lower hand higher bits of the mask */
868 i_low = i_mask & (- i_mask); /* lower bit of the mask */
869 i_high = i_mask + i_low; /* higher bit of the mask */
871 /* Transform bits into an index */
872 i_low = BinaryLog (i_low);
873 i_high = BinaryLog (i_high);
875 /* Update pointers and return */
877 *pi_right = (8 - i_high + i_low);
880 /*****************************************************************************
881 * InitThread: initialize video output thread
882 *****************************************************************************
883 * This function is called from RunThread and performs the second step of the
884 * initialization. It returns 0 on success. Note that the thread's flag are not
885 * modified inside this function.
886 *****************************************************************************/
887 static int InitThread( vout_thread_t *p_vout )
891 *p_vout->pi_status = THREAD_START;
893 /* Initialize output method - this function issues its own error messages */
894 if( p_vout->p_sys_init( p_vout ) )
899 /* Initialize convertion tables and functions */
900 if( vout_InitYUV( p_vout ) )
902 intf_ErrMsg("error: can't allocate YUV translation tables\n");
906 /* Mark thread as running and return */
907 p_vout->b_active = 1;
908 *p_vout->pi_status = THREAD_READY;
909 intf_DbgMsg("thread ready\n");
913 /*****************************************************************************
914 * RunThread: video output thread
915 *****************************************************************************
916 * Video output thread. This function does only returns when the thread is
917 * terminated. It handles the pictures arriving in the video heap and the
918 * display device events.
919 *****************************************************************************/
920 static void RunThread( vout_thread_t *p_vout)
922 /* XXX?? welcome to gore land */
923 static int i_trash_count = 0;
924 static mtime_t last_display_date = 0;
926 int i_index; /* index in heap */
927 mtime_t current_date; /* current date */
928 mtime_t display_date; /* display date */
929 boolean_t b_display; /* display flag */
930 picture_t * p_pic; /* picture pointer */
931 subpicture_t * p_subpic; /* subpicture pointer */
936 p_vout->b_error = InitThread( p_vout );
937 if( p_vout->b_error )
939 DestroyThread( p_vout, THREAD_ERROR );
945 * Main loop - it is not executed if an error occured during
948 while( (!p_vout->b_die) && (!p_vout->b_error) )
950 /* Initialize loop variables */
954 current_date = mdate();
957 * Find the picture to display - this operation does not need lock,
958 * since only READY_PICTUREs are handled
960 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
962 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
964 (p_vout->p_picture[i_index].date < display_date) ) )
966 p_pic = &p_vout->p_picture[i_index];
967 display_date = p_pic->date;
974 /* Computes FPS rate */
975 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
979 //fprintf( stderr, "gap : %Ld\n", display_date-last_display_date );
980 last_display_date = display_date;
982 if( display_date < current_date && i_trash_count > 4 )
984 /* Picture is late: it will be destroyed and the thread will sleep and
985 * go to next picture */
987 vlc_mutex_lock( &p_vout->picture_lock );
988 if( p_pic->i_refcount )
990 p_pic->i_status = DISPLAYED_PICTURE;
994 p_pic->i_status = DESTROYED_PICTURE;
995 p_vout->i_pictures--;
997 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
998 vlc_mutex_unlock( &p_vout->picture_lock );
1000 /* Update synchronization information as if display delay
1002 Synchronize( p_vout, display_date - current_date );
1010 if( display_date > current_date + VOUT_DISPLAY_DELAY )
1012 /* A picture is ready to be rendered, but its rendering date is
1013 * far from the current one so the thread will perform an empty loop
1014 * as if no picture were found. The picture state is unchanged */
1020 /* Picture will be displayed, update synchronization
1022 Synchronize( p_vout, display_date - current_date );
1026 * Find the subpictures to display - this operation does not need
1027 * lock, since only READY_SUBPICTURE are handled. If no picture
1028 * has been selected, display_date will depend on the subpicture
1030 /* FIXME: we should find *all* subpictures to display, and
1031 * check their displaying date as well */
1032 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1034 if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
1036 p_subpic = &p_vout->p_subpicture[i_index];
1042 * Perform rendering, sleep and display rendered picture
1044 if( p_pic ) /* picture and perhaps subpicture */
1046 b_display = p_vout->b_active;
1047 p_vout->last_display_date = display_date;
1051 /* Set picture dimensions and clear buffer */
1052 SetBufferPicture( p_vout, p_pic );
1054 /* Render picture and informations */
1055 RenderPicture( p_vout, p_pic );
1056 if( p_vout->b_info )
1058 RenderPictureInfo( p_vout, p_pic );
1059 RenderInfo( p_vout );
1063 /* Remove picture from heap */
1064 vlc_mutex_lock( &p_vout->picture_lock );
1065 if( p_pic->i_refcount )
1067 p_pic->i_status = DISPLAYED_PICTURE;
1071 p_pic->i_status = DESTROYED_PICTURE;
1072 p_vout->i_pictures--;
1074 vlc_mutex_unlock( &p_vout->picture_lock );
1076 /* Render interface and subpicture */
1077 if( b_display && p_vout->b_interface )
1079 RenderInterface( p_vout );
1085 RenderSubPicture( p_vout, p_subpic );
1088 /* Remove subpicture from heap */
1089 /*vlc_mutex_lock( &p_vout->subpicture_lock );
1090 p_subpic->i_status = DESTROYED_SUBPICTURE;
1091 vlc_mutex_unlock( &p_vout->subpicture_lock );*/
1095 else if( p_subpic ) /* subpicture alone */
1097 b_display = p_vout->b_active;
1098 p_vout->last_display_date = display_date;
1103 SetBufferPicture( p_vout, NULL );
1105 /* Render informations, interface and subpicture */
1106 if( p_vout->b_info )
1108 RenderInfo( p_vout );
1110 if( p_vout->b_interface )
1112 RenderInterface( p_vout );
1114 RenderSubPicture( p_vout, p_subpic );
1117 /* Remove subpicture from heap */
1118 /*vlc_mutex_lock( &p_vout->subpicture_lock );
1119 p_subpic->i_status = DESTROYED_SUBPICTURE;
1120 vlc_mutex_unlock( &p_vout->subpicture_lock );*/
1122 else if( p_vout->b_active ) /* idle or interface screen alone */
1124 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1126 /* Interface has changed, so a new rendering is required - force
1127 * it by setting last idle date to 0 */
1128 p_vout->last_idle_date = 0;
1131 /* Render idle screen and update idle date, then render interface if
1133 b_display = RenderIdle( p_vout );
1136 p_vout->last_idle_date = current_date;
1137 if( p_vout->b_interface )
1139 RenderInterface( p_vout );
1150 * Sleep, wake up and display rendered picture
1154 /* Store render time */
1155 p_vout->render_time = mdate() - current_date;
1158 /* Give back change lock */
1159 vlc_mutex_unlock( &p_vout->change_lock );
1161 /* Sleep a while or until a given date */
1162 if( display_date != 0 )
1164 mwait( display_date );
1168 msleep( VOUT_IDLE_SLEEP );
1171 /* On awakening, take back lock and send immediately picture to display,
1172 * then swap buffers */
1173 vlc_mutex_lock( &p_vout->change_lock );
1175 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1176 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1178 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1180 p_vout->p_sys_display( p_vout );
1181 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1185 * Check events and manage thread
1187 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1189 /* A fatal error occured, and the thread must terminate immediately,
1190 * without displaying anything - setting b_error to 1 cause the
1191 * immediate end of the main while() loop. */
1192 p_vout->b_error = 1;
1197 * Error loop - wait until the thread destruction is requested
1199 if( p_vout->b_error )
1201 ErrorThread( p_vout );
1205 EndThread( p_vout );
1206 DestroyThread( p_vout, THREAD_OVER );
1207 intf_DbgMsg( "thread end\n" );
1210 /*****************************************************************************
1211 * ErrorThread: RunThread() error loop
1212 *****************************************************************************
1213 * This function is called when an error occured during thread main's loop. The
1214 * thread can still receive feed, but must be ready to terminate as soon as
1216 *****************************************************************************/
1217 static void ErrorThread( vout_thread_t *p_vout )
1219 /* Wait until a `die' order */
1221 while( !p_vout->b_die )
1224 msleep( VOUT_IDLE_SLEEP );
1228 /*****************************************************************************
1229 * EndThread: thread destruction
1230 *****************************************************************************
1231 * This function is called when the thread ends after a sucessful
1232 * initialization. It frees all ressources allocated by InitThread.
1233 *****************************************************************************/
1234 static void EndThread( vout_thread_t *p_vout )
1236 int i_index; /* index in heap */
1240 *p_vout->pi_status = THREAD_END;
1242 /* Destroy all remaining pictures and subpictures */
1243 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1245 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1247 free( p_vout->p_picture[i_index].p_data );
1249 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1251 free( p_vout->p_subpicture[i_index].p_data );
1255 /* Destroy translation tables */
1256 vout_EndYUV( p_vout );
1257 p_vout->p_sys_end( p_vout );
1260 /*****************************************************************************
1261 * DestroyThread: thread destruction
1262 *****************************************************************************
1263 * This function is called when the thread ends. It frees all ressources
1264 * allocated by CreateThread. Status is available at this stage.
1265 *****************************************************************************/
1266 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1268 int *pi_status; /* status adress */
1270 /* Store status adress */
1272 pi_status = p_vout->pi_status;
1274 /* Destroy thread structures allocated by Create and InitThread */
1275 vout_UnloadFont( p_vout->p_default_font );
1276 vout_UnloadFont( p_vout->p_large_font );
1277 p_vout->p_sys_destroy( p_vout );
1280 TrashPlugin( p_vout->vout_plugin );
1282 /* Free structure */
1284 *pi_status = i_status;
1287 /*****************************************************************************
1288 * Print: print simple text on a picture
1289 *****************************************************************************
1290 * This function will print a simple text on the picture. It is designed to
1291 * print debugging or general informations.
1292 *****************************************************************************/
1293 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 )
1295 int i_text_height; /* total text height */
1296 int i_text_width; /* total text width */
1298 /* Update upper left coordinates according to alignment */
1299 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1300 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1302 /* Set area and print text */
1303 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1304 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1305 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1306 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1307 p_vout->i_white_pixel, 0, 0,
1312 /*****************************************************************************
1313 * SetBufferArea: activate an area in current buffer
1314 *****************************************************************************
1315 * This function is called when something is rendered on the current buffer.
1316 * It set the area as active and prepare it to be cleared on next rendering.
1317 * Pay attention to the fact that in this functions, i_h is in fact the end y
1318 * coordinate of the new area.
1319 *****************************************************************************/
1320 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1322 vout_buffer_t * p_buffer; /* current buffer */
1323 int i_area_begin, i_area_end; /* area vertical extension */
1324 int i_area, i_area_copy; /* area index */
1325 int i_area_shift; /* shift distance for areas */
1327 /* Choose buffer and modify h to end of area position */
1328 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1332 * Remove part of the area which is inside the picture - this is done
1333 * by calling again SetBufferArea with the correct areas dimensions.
1335 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1337 i_area_begin = p_buffer->i_pic_y;
1338 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1340 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1341 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1342 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1344 /* Keep the stripe above the picture, if any */
1345 if( i_y < i_area_begin )
1347 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1349 /* Keep the stripe below the picture, if any */
1350 if( i_h > i_area_end )
1352 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1358 /* Skip some extensions until interesting areas */
1360 (i_area < p_buffer->i_areas) &&
1361 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1367 if( i_area == p_buffer->i_areas )
1369 /* New area is below all existing ones: just add it at the end of the
1370 * array, if possible - else, append it to the last one */
1371 if( i_area < VOUT_MAX_AREAS )
1373 p_buffer->pi_area_begin[i_area] = i_y;
1374 p_buffer->pi_area_end[i_area] = i_h;
1375 p_buffer->i_areas++;
1380 intf_DbgMsg("areas overflow\n");
1382 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1387 i_area_begin = p_buffer->pi_area_begin[i_area];
1388 i_area_end = p_buffer->pi_area_end[i_area];
1390 if( i_y < i_area_begin )
1392 if( i_h >= i_area_begin - 1 )
1394 /* Extend area above */
1395 p_buffer->pi_area_begin[i_area] = i_y;
1399 /* Create a new area above : merge last area if overflow, then
1400 * move all old areas down */
1401 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1404 intf_DbgMsg("areas overflow\n");
1406 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1410 p_buffer->i_areas++;
1412 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1414 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1415 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1417 p_buffer->pi_area_begin[i_area] = i_y;
1418 p_buffer->pi_area_end[i_area] = i_h;
1422 if( i_h > i_area_end )
1424 /* Find further areas which can be merged with the new one */
1425 for( i_area_copy = i_area + 1;
1426 (i_area_copy < p_buffer->i_areas) &&
1427 (p_buffer->pi_area_begin[i_area] <= i_h);
1434 if( i_area_copy != i_area )
1436 /* Merge with last possible areas */
1437 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1439 /* Shift lower areas upward */
1440 i_area_shift = i_area_copy - i_area;
1441 p_buffer->i_areas -= i_area_shift;
1442 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1444 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1445 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1450 /* Extend area below */
1451 p_buffer->pi_area_end[i_area] = i_h;
1457 /*****************************************************************************
1458 * SetBufferPicture: clear buffer and set picture area
1459 *****************************************************************************
1460 * This function is called before any rendering. It clears the current
1461 * rendering buffer and set the new picture area. If the picture pointer is
1462 * NULL, then no picture area is defined. Floating operations are avoided since
1463 * some MMX calculations may follow.
1464 *****************************************************************************/
1465 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1467 vout_buffer_t * p_buffer; /* current buffer */
1468 int i_pic_x, i_pic_y; /* picture position */
1469 int i_pic_width, i_pic_height; /* picture dimensions */
1470 int i_old_pic_y, i_old_pic_height; /* old picture area */
1471 int i_vout_width, i_vout_height; /* display dimensions */
1472 int i_area; /* area index */
1473 int i_data_index; /* area data index */
1474 int i_data_size; /* area data size, in 256 bytes blocs */
1475 u64 * p_data; /* area data, for clearing */
1476 byte_t * p_data8; /* area data, for clearing (slow) */
1478 /* Choose buffer and set display dimensions */
1479 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1480 i_vout_width = p_vout->i_width;
1481 i_vout_height = p_vout->i_height;
1484 * Computes new picture size
1488 /* Try horizontal scaling first - width must be a mutiple of 16 */
1489 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1490 i_vout_width : p_pic->i_width) & ~0xf;
1491 switch( p_pic->i_aspect_ratio )
1493 case AR_3_4_PICTURE:
1494 i_pic_height = i_pic_width * 3 / 4;
1496 case AR_16_9_PICTURE:
1497 i_pic_height = i_pic_width * 9 / 16;
1499 case AR_221_1_PICTURE:
1500 i_pic_height = i_pic_width * 100 / 221;
1502 case AR_SQUARE_PICTURE:
1504 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1508 /* If picture dimensions using horizontal scaling are too large, use
1509 * vertical scaling. Since width must be a multiple of 16, height is
1510 * adjusted again after. */
1511 if( i_pic_height > i_vout_height )
1513 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1514 i_vout_height : p_pic->i_height;
1515 switch( p_pic->i_aspect_ratio )
1517 case AR_3_4_PICTURE:
1518 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1519 i_pic_height = i_pic_width * 3 / 4;
1521 case AR_16_9_PICTURE:
1522 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1523 i_pic_height = i_pic_width * 9 / 16;
1525 case AR_221_1_PICTURE:
1526 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1527 i_pic_height = i_pic_width * 100 / 221;
1529 case AR_SQUARE_PICTURE:
1531 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1532 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1537 /* Set picture position */
1538 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1539 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1543 /* No picture: size is 0 */
1551 * Set new picture size - if is is smaller than the previous one, clear
1552 * around it. Since picture are centered, only their size is tested.
1554 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1556 i_old_pic_y = p_buffer->i_pic_y;
1557 i_old_pic_height = p_buffer->i_pic_height;
1558 p_buffer->i_pic_x = i_pic_x;
1559 p_buffer->i_pic_y = i_pic_y;
1560 p_buffer->i_pic_width = i_pic_width;
1561 p_buffer->i_pic_height = i_pic_height;
1562 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1566 p_buffer->i_pic_x = i_pic_x;
1567 p_buffer->i_pic_y = i_pic_y;
1568 p_buffer->i_pic_width = i_pic_width;
1569 p_buffer->i_pic_height = i_pic_height;
1575 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1578 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1579 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1581 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1582 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1583 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1585 /* Clear 256 bytes block */
1586 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1587 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1588 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1589 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1590 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1591 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1592 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1593 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1595 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1597 /* Clear remaining 16 bytes blocks */
1598 *p_data++ = 0; *p_data++ = 0;
1600 p_data8 = (byte_t *)p_data;
1601 for( i_data_index = i_data_size % 16; i_data_index--; )
1603 /* Clear remaining bytes */
1611 p_buffer->i_areas = 0;
1614 /*****************************************************************************
1615 * RenderPicture: render a picture
1616 *****************************************************************************
1617 * This function convert a picture from a video heap to a pixel-encoded image
1618 * and copy it to the current rendering buffer. No lock is required, since the
1619 * rendered picture has been determined as existant, and will only be destroyed
1620 * by the vout thread later.
1621 *****************************************************************************/
1622 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1625 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1626 mtime_t render_time; /* picture rendering time */
1628 vout_buffer_t * p_buffer; /* rendering buffer */
1629 byte_t * p_pic_data; /* convertion destination */
1631 /* Get and set rendering informations */
1632 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1633 p_pic_data = p_buffer->p_data +
1634 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1635 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1637 render_time = mdate();
1641 * Choose appropriate rendering function and render picture
1643 switch( p_pic->i_type )
1645 case YUV_420_PICTURE:
1646 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1647 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1648 p_pic->i_width, p_pic->i_height,
1649 p_buffer->i_pic_width, p_buffer->i_pic_height,
1650 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1651 p_pic->i_matrix_coefficients );
1653 case YUV_422_PICTURE:
1654 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1655 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1656 p_pic->i_width, p_pic->i_height,
1657 p_buffer->i_pic_width, p_buffer->i_pic_height,
1658 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1659 p_pic->i_matrix_coefficients );
1661 case YUV_444_PICTURE:
1662 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1663 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1664 p_pic->i_width, p_pic->i_height,
1665 p_buffer->i_pic_width, p_buffer->i_pic_height,
1666 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1667 p_pic->i_matrix_coefficients );
1671 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1677 /* Print picture date and rendering time */
1678 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1679 p_vout->i_buffer_index, (long) (mdate() - render_time),
1680 mstrtime( psz_date, p_pic->date ));
1684 /*****************************************************************************
1685 * RenderPictureInfo: print additionnal informations on a picture
1686 *****************************************************************************
1687 * This function will print informations such as fps and other picture
1688 * dependant informations.
1689 *****************************************************************************/
1690 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1692 #if defined(STATS) || defined(DEBUG)
1693 char psz_buffer[256]; /* string buffer */
1698 * Print FPS rate in upper right corner
1700 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1702 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1703 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1704 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1705 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1709 * Print frames count and loop time in upper left corner
1711 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1712 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1713 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1718 * Print picture information in lower right corner
1720 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1721 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1722 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1723 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1724 p_pic->i_width, p_pic->i_height,
1725 p_pic->i_display_width, p_pic->i_display_height,
1726 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1727 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1728 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1729 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1730 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1731 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1732 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1733 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1734 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1735 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1739 /*****************************************************************************
1740 * RenderIdle: render idle picture
1741 *****************************************************************************
1742 * This function will print something on the screen. It will return 0 if
1743 * nothing has been rendered, or 1 if something has been changed on the screen.
1744 * Note that if you absolutely want something to be printed, you will have
1745 * to force it by setting the last idle date to 0.
1746 * Unlike other rendering functions, this one calls the SetBufferPicture
1747 * function when needed.
1748 *****************************************************************************/
1749 static int RenderIdle( vout_thread_t *p_vout )
1751 int i_x = 0, i_y = 0; /* text position */
1752 int i_width, i_height; /* text size */
1753 mtime_t current_date; /* current date */
1754 const char *psz_text = "waiting for stream ..."; /* text to display */
1757 current_date = mdate();
1758 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1759 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1761 SetBufferPicture( p_vout, NULL );
1762 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1763 &i_width, &i_height );
1764 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1766 vout_Print( p_vout->p_large_font,
1767 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1768 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1769 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1770 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1771 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1772 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1779 /*****************************************************************************
1780 * RenderInfo: render additionnal informations
1781 *****************************************************************************
1782 * This function render informations which do not depend of the current picture
1784 *****************************************************************************/
1785 static void RenderInfo( vout_thread_t *p_vout )
1788 char psz_buffer[256]; /* string buffer */
1789 int i_ready_pic = 0; /* ready pictures */
1790 int i_reserved_pic = 0; /* reserved pictures */
1791 int i_picture; /* picture index */
1796 * Print thread state in lower left corner
1798 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1800 switch( p_vout->p_picture[i_picture].i_status )
1802 case RESERVED_PICTURE:
1803 case RESERVED_DATED_PICTURE:
1804 case RESERVED_DISP_PICTURE:
1812 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1813 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1814 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1818 /*****************************************************************************
1819 * RenderSubPicture: render a subpicture
1820 *****************************************************************************
1821 * This function render a sub picture unit.
1822 *****************************************************************************/
1823 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1825 p_vout_font_t p_font; /* text font */
1826 int i_width, i_height; /* subpicture dimensions */
1828 switch( p_subpic->i_type )
1830 case DVD_SUBPICTURE: /* DVD subpicture unit */
1831 vout_RenderSPU( p_subpic->p_data, p_subpic->type.spu.i_offset,
1832 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data,
1833 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line );
1835 case TEXT_SUBPICTURE: /* single line text */
1836 /* Select default font if not specified */
1837 p_font = p_subpic->type.text.p_font;
1838 if( p_font == NULL )
1840 p_font = p_vout->p_default_font;
1843 /* Compute text size (width and height fields are ignored)
1845 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1846 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1847 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1849 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1850 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1851 p_subpic->i_y * p_vout->i_bytes_per_line,
1852 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1853 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1854 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1856 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1862 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1867 /*****************************************************************************
1868 * RenderInterface: render the interface
1869 *****************************************************************************
1870 * This function render the interface, if any.
1871 *****************************************************************************/
1872 static void RenderInterface( vout_thread_t *p_vout )
1874 int i_height, i_text_height; /* total and text height */
1875 int i_width_1, i_width_2; /* text width */
1876 int i_byte; /* byte index */
1877 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1878 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1881 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1882 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1883 i_height += i_text_height;
1885 /* Render background */
1886 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1887 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1891 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1894 /* Render text, if not larger than screen */
1895 if( i_width_1 < p_vout->i_width )
1897 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1898 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1899 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1900 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1901 OUTLINED_TEXT, psz_text_1 );
1903 if( i_width_2 < p_vout->i_width )
1905 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1906 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1907 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1908 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1909 OUTLINED_TEXT, psz_text_2 );
1912 /* Activate modified area */
1913 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1916 /*****************************************************************************
1917 * Synchronize: update synchro level depending of heap state
1918 *****************************************************************************
1919 * This function is called during the main vout loop.
1920 *****************************************************************************/
1921 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1923 int i_synchro_inc = 0;
1924 /* XXX?? gore following */
1925 static int i_panic_count = 0;
1926 static int i_last_synchro_inc = 0;
1927 static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1928 static int i_truc = 10;
1932 //fprintf( stderr, "PANIC %d\n", i_panic_count );
1938 if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1941 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1946 if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1949 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1953 if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1954 i_synchro_inc*i_last_synchro_inc < 0 )
1959 if( i_delay < 6000 )
1964 else if( i_delay < 70000 )
1966 i_truc = 24+(24*i_delay)/70000;
1969 i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
1971 else if( i_delay > 100000 )
1973 r_synchro_level += 1;
1974 if( i_delay > 130000 )
1975 r_synchro_level += 1;
1978 r_synchro_level += (float)i_synchro_inc / i_truc;
1979 p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
1981 if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
1983 r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
1986 //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
1987 // p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
1988 i_last_synchro_inc = i_synchro_inc;
1991 /*****************************************************************************
1992 * Manage: manage thread
1993 *****************************************************************************
1994 * This function will handle changes in thread configuration.
1995 *****************************************************************************/
1996 static int Manage( vout_thread_t *p_vout )
1999 if( p_vout->i_changes )
2001 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
2002 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
2006 /* On gamma or grayscale change, rebuild tables */
2007 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2010 if( vout_ResetYUV( p_vout ) )
2012 intf_ErrMsg("error: can't rebuild convertion tables\n");
2017 /* Clear changes flags which does not need management or have been
2019 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2020 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
2021 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
2023 /* Detect unauthorized changes */
2024 if( p_vout->i_changes )
2026 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
2027 * function, it means they should not be authorized */
2028 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2035 /*****************************************************************************
2036 * Align: align a subpicture in the screen
2037 *****************************************************************************
2038 * This function is used for rendering text or subpictures. It returns non 0
2039 * it the final aera is not fully included in display area. Return coordinates
2041 *****************************************************************************/
2042 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2043 int i_width, int i_height, int i_h_align, int i_v_align )
2045 /* Align horizontally */
2049 *pi_x -= i_width / 2;
2052 *pi_x += (p_vout->i_width - i_width) / 2;
2058 *pi_x += p_vout->i_width - i_width;
2062 /* Align vertically */
2066 *pi_y -= i_height / 2;
2069 *pi_y += (p_vout->i_height - i_height) / 2;
2075 *pi_y += p_vout->i_height - i_height;
2077 case SUBTITLE_RALIGN:
2078 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2079 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2083 /* Return non 0 if clipping failed */
2084 return( (*pi_x < 0) || (*pi_y < 0) ||
2085 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2088 /*****************************************************************************
2089 * SetPalette: sets an 8 bpp palette
2090 *****************************************************************************
2091 * This function is just a prototype that does nothing. Architectures that
2092 * support palette allocation should override it.
2093 *****************************************************************************/
2094 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2095 u16 *green, u16 *blue, u16 *transp )
2097 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );