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_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, int i_method )
92 vout_thread_t * p_vout; /* thread descriptor */
93 int i_status; /* thread status */
94 int i_index; /* index for array initialization */
97 /* Allocate descriptor */
99 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
102 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
106 /* Request an interface plugin */
107 psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
109 if( RequestPlugin( &p_vout->vout_plugin, "vout", psz_method ) < 0 )
111 intf_ErrMsg( "error: could not open video plugin vout_%s.so\n", psz_method );
117 p_vout->p_sys_create = GetPluginFunction( p_vout->vout_plugin, "vout_SysCreate" );
118 p_vout->p_sys_init = GetPluginFunction( p_vout->vout_plugin, "vout_SysInit" );
119 p_vout->p_sys_end = GetPluginFunction( p_vout->vout_plugin, "vout_SysEnd" );
120 p_vout->p_sys_destroy = GetPluginFunction( p_vout->vout_plugin, "vout_SysDestroy" );
121 p_vout->p_sys_manage = GetPluginFunction( p_vout->vout_plugin, "vout_SysManage" );
122 p_vout->p_sys_display = GetPluginFunction( p_vout->vout_plugin, "vout_SysDisplay" );
124 /* Initialize thread properties - thread id and locks will be initialized
128 p_vout->b_active = 0;
129 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
130 *p_vout->pi_status = THREAD_CREATE;
132 /* Initialize some fields used by the system-dependant method - these fields will
133 * probably be modified by the method, and are only preferences */
134 p_vout->i_changes = 0;
135 p_vout->i_width = i_width;
136 p_vout->i_height = i_height;
137 p_vout->i_bytes_per_line = i_width * 2;
138 p_vout->i_screen_depth = 15;
139 p_vout->i_bytes_per_pixel = 2;
140 p_vout->f_gamma = VOUT_GAMMA;
142 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
144 p_vout->b_interface = 0;
147 p_vout->p_set_palette = SetPalette;
149 intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
150 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
151 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
153 /* Initialize idle screen */
154 p_vout->last_display_date = mdate();
155 p_vout->last_display_date = 0;
156 p_vout->last_idle_date = 0;
159 /* Initialize statistics fields */
160 p_vout->render_time = 0;
161 p_vout->c_fps_samples = 0;
164 /* Initialize buffer index */
165 p_vout->i_buffer_index = 0;
167 /* Initialize pictures and subpictures - translation tables and functions
168 * will be initialized later in InitThread */
169 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
171 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
172 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
173 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
174 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
176 p_vout->i_pictures = 0;
178 /* Initialize synchronization informations */
179 p_vout->i_synchro_level = VOUT_SYNCHRO_LEVEL_START;
181 /* Create and initialize system-dependant method - this function issues its
182 * own error messages */
183 if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
185 TrashPlugin( p_vout->vout_plugin );
189 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
190 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
191 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
192 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
194 /* Calculate shifts from system-updated masks */
195 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
196 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
197 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
199 /* Set some useful colors */
200 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
201 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
202 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
203 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
205 /* Load fonts - fonts must be initialized after the system method since
206 * they may be dependant on screen depth and other thread properties */
207 p_vout->p_default_font = vout_LoadFont( DATA_PATH "/" VOUT_DEFAULT_FONT );
208 if( p_vout->p_default_font == NULL )
210 p_vout->p_default_font = vout_LoadFont( "share/" VOUT_DEFAULT_FONT );
212 if( p_vout->p_default_font == NULL )
214 p_vout->p_sys_destroy( p_vout );
215 TrashPlugin( p_vout->vout_plugin );
219 p_vout->p_large_font = vout_LoadFont( DATA_PATH "/" VOUT_LARGE_FONT );
220 if( p_vout->p_large_font == NULL )
222 p_vout->p_large_font = vout_LoadFont( "share/" VOUT_LARGE_FONT );
224 if( p_vout->p_large_font == NULL )
226 vout_UnloadFont( p_vout->p_default_font );
227 p_vout->p_sys_destroy( p_vout );
228 TrashPlugin( p_vout->vout_plugin );
233 /* Create thread and set locks */
234 vlc_mutex_init( &p_vout->picture_lock );
235 vlc_mutex_init( &p_vout->subpicture_lock );
236 vlc_mutex_init( &p_vout->change_lock );
237 vlc_mutex_lock( &p_vout->change_lock );
238 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
240 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
241 vout_UnloadFont( p_vout->p_default_font );
242 vout_UnloadFont( p_vout->p_large_font );
243 p_vout->p_sys_destroy( p_vout );
244 TrashPlugin( p_vout->vout_plugin );
249 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
250 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
252 /* If status is NULL, wait until the thread is created */
253 if( pi_status == NULL )
257 msleep( THREAD_SLEEP );
258 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
259 && (i_status != THREAD_FATAL) );
260 if( i_status != THREAD_READY )
268 /*****************************************************************************
269 * vout_DestroyThread: destroys a previously created thread
270 *****************************************************************************
271 * Destroy a terminated thread.
272 * The function will request a destruction of the specified thread. If pi_error
273 * is NULL, it will return once the thread is destroyed. Else, it will be
274 * update using one of the THREAD_* constants.
275 *****************************************************************************/
276 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
278 int i_status; /* thread status */
282 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
283 *p_vout->pi_status = THREAD_DESTROY;
285 /* Request thread destruction */
288 /* If status is NULL, wait until thread has been destroyed */
289 if( pi_status == NULL )
293 msleep( THREAD_SLEEP );
294 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
295 && (i_status != THREAD_FATAL) );
299 /*****************************************************************************
300 * vout_DisplaySubPicture: display a subpicture unit
301 *****************************************************************************
302 * Remove the reservation flag of an subpicture, which will cause it to be ready
303 * for display. The picture does not need to be locked, since it is ignored by
304 * the output thread if is reserved.
305 *****************************************************************************/
306 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
309 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
310 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
314 /* Check if status is valid */
315 if( p_subpic->i_status != RESERVED_SUBPICTURE )
317 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
318 p_subpic->i_status );
322 /* Remove reservation flag */
323 p_subpic->i_status = READY_SUBPICTURE;
326 /* Send subpicture informations */
327 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
328 p_subpic, p_subpic->i_type,
329 mstrtime( psz_begin_date, p_subpic->begin_date ),
330 mstrtime( psz_end_date, p_subpic->end_date ) );
334 /*****************************************************************************
335 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
336 *****************************************************************************
337 * This function create a reserved subpicture in the video output heap.
338 * A null pointer is returned if the function fails. This method provides an
339 * already allocated zone of memory in the spu data fields. It needs locking
340 * since several pictures can be created by several producers threads.
341 *****************************************************************************/
342 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
345 int i_subpic; /* subpicture index */
346 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
347 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
350 vlc_mutex_lock( &p_vout->subpicture_lock );
353 * Look for an empty place
355 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
357 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
359 /* Subpicture is marked for destruction, but is still allocated */
360 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
361 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
363 /* Memory size do match or is smaller : memory will not be reallocated,
364 * and function can end immediately - this is the best possible case,
365 * since no memory allocation needs to be done */
366 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
368 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
369 &p_vout->p_subpicture[i_subpic] );
371 vlc_mutex_unlock( &p_vout->subpicture_lock );
372 return( &p_vout->p_subpicture[i_subpic] );
374 else if( p_destroyed_subpic == NULL )
376 /* Memory size do not match, but subpicture index will be kept in
377 * case no other place are left */
378 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
381 else if( (p_free_subpic == NULL) &&
382 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
384 /* Subpicture is empty and ready for allocation */
385 p_free_subpic = &p_vout->p_subpicture[i_subpic];
389 /* If no free subpicture is available, use a destroyed subpicture */
390 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
392 /* No free subpicture or matching destroyed subpicture has been found, but
393 * a destroyed subpicture is still avalaible */
394 free( p_destroyed_subpic->p_data );
395 p_free_subpic = p_destroyed_subpic;
401 if( p_free_subpic != NULL )
403 /* Allocate memory */
406 case TEXT_SUBPICTURE: /* text subpicture */
407 p_free_subpic->p_data = malloc( i_size + 1 );
409 case DVD_SUBPICTURE: /* DVD subpicture unit */
410 p_free_subpic->p_data = malloc( i_size );
414 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
415 p_free_subpic->p_data = NULL;
420 if( p_free_subpic->p_data != NULL )
421 { /* Copy subpicture informations, set some default values */
422 p_free_subpic->i_type = i_type;
423 p_free_subpic->i_status = RESERVED_SUBPICTURE;
424 p_free_subpic->i_size = i_size;
425 p_free_subpic->i_x = 0;
426 p_free_subpic->i_y = 0;
427 p_free_subpic->i_width = 0;
428 p_free_subpic->i_height = 0;
429 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
430 p_free_subpic->i_vertical_align = CENTER_RALIGN;
434 /* Memory allocation failed : set subpicture as empty */
435 p_free_subpic->i_type = EMPTY_SUBPICTURE;
436 p_free_subpic->i_status = FREE_SUBPICTURE;
437 p_free_subpic = NULL;
438 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
442 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
444 vlc_mutex_unlock( &p_vout->subpicture_lock );
445 return( p_free_subpic );
448 /* No free or destroyed subpicture could be found */
449 intf_DbgMsg( "warning: heap is full\n" );
450 vlc_mutex_unlock( &p_vout->subpicture_lock );
454 /*****************************************************************************
455 * vout_DestroySubPicture: remove a subpicture from the heap
456 *****************************************************************************
457 * This function frees a previously reserved subpicture.
458 * It is meant to be used when the construction of a picture aborted.
459 * This function does not need locking since reserved subpictures are ignored
460 * by the output thread.
461 *****************************************************************************/
462 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
465 /* Check if status is valid */
466 if( p_subpic->i_status != RESERVED_SUBPICTURE )
468 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
469 p_subpic, p_subpic->i_status );
473 p_subpic->i_status = DESTROYED_SUBPICTURE;
476 intf_DbgMsg("subpicture %p\n", p_subpic);
480 /*****************************************************************************
481 * vout_DisplayPicture: display a picture
482 *****************************************************************************
483 * Remove the reservation flag of a picture, which will cause it to be ready for
484 * display. The picture won't be displayed until vout_DatePicture has been
486 *****************************************************************************/
487 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
489 vlc_mutex_lock( &p_vout->picture_lock );
490 switch( p_pic->i_status )
492 case RESERVED_PICTURE:
493 p_pic->i_status = RESERVED_DISP_PICTURE;
495 case RESERVED_DATED_PICTURE:
496 p_pic->i_status = READY_PICTURE;
500 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
506 intf_DbgMsg("picture %p\n", p_pic);
508 vlc_mutex_unlock( &p_vout->picture_lock );
511 /*****************************************************************************
512 * vout_DatePicture: date a picture
513 *****************************************************************************
514 * Remove the reservation flag of a picture, which will cause it to be ready for
515 * display. The picture won't be displayed until vout_DisplayPicture has been
517 *****************************************************************************/
518 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
521 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
524 vlc_mutex_lock( &p_vout->picture_lock );
526 switch( p_pic->i_status )
528 case RESERVED_PICTURE:
529 p_pic->i_status = RESERVED_DATED_PICTURE;
531 case RESERVED_DISP_PICTURE:
532 p_pic->i_status = READY_PICTURE;
536 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
542 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
544 vlc_mutex_unlock( &p_vout->picture_lock );
547 /*****************************************************************************
548 * vout_CreatePicture: allocate a picture in the video output heap.
549 *****************************************************************************
550 * This function create a reserved image in the video output heap.
551 * A null pointer is returned if the function fails. This method provides an
552 * already allocated zone of memory in the picture data fields. It needs locking
553 * since several pictures can be created by several producers threads.
554 *****************************************************************************/
555 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
556 int i_width, int i_height )
558 int i_picture; /* picture index */
559 int i_chroma_width = 0; /* chroma width */
560 picture_t * p_free_picture = NULL; /* first free picture */
561 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
564 vlc_mutex_lock( &p_vout->picture_lock );
567 * Look for an empty place
569 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
571 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
573 /* Picture is marked for destruction, but is still allocated - note
574 * that if width and type are the same for two pictures, chroma_width
575 * should also be the same */
576 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
577 (p_vout->p_picture[i_picture].i_height == i_height) &&
578 (p_vout->p_picture[i_picture].i_width == i_width) )
580 /* Memory size do match : memory will not be reallocated, and function
581 * can end immediately - this is the best possible case, since no
582 * memory allocation needs to be done */
583 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
584 p_vout->i_pictures++;
586 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
587 &p_vout->p_picture[i_picture] );
589 vlc_mutex_unlock( &p_vout->picture_lock );
590 return( &p_vout->p_picture[i_picture] );
592 else if( p_destroyed_picture == NULL )
594 /* Memory size do not match, but picture index will be kept in
595 * case no other place are left */
596 p_destroyed_picture = &p_vout->p_picture[i_picture];
599 else if( (p_free_picture == NULL) &&
600 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
602 /* Picture is empty and ready for allocation */
603 p_free_picture = &p_vout->p_picture[i_picture];
607 /* If no free picture is available, use a destroyed picture */
608 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
610 /* No free picture or matching destroyed picture has been found, but
611 * a destroyed picture is still avalaible */
612 free( p_destroyed_picture->p_data );
613 p_free_picture = p_destroyed_picture;
619 if( p_free_picture != NULL )
621 /* Allocate memory */
624 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
625 i_chroma_width = i_width / 2;
626 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
627 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
628 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
629 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
631 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
632 i_chroma_width = i_width / 2;
633 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
634 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
635 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
636 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
638 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
639 i_chroma_width = i_width;
640 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
641 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
642 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
643 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
647 intf_DbgMsg("error: unknown picture type %d\n", i_type );
648 p_free_picture->p_data = NULL;
653 if( p_free_picture->p_data != NULL )
655 /* Copy picture informations, set some default values */
656 p_free_picture->i_type = i_type;
657 p_free_picture->i_status = RESERVED_PICTURE;
658 p_free_picture->i_matrix_coefficients = 1;
659 p_free_picture->i_width = i_width;
660 p_free_picture->i_height = i_height;
661 p_free_picture->i_chroma_width = i_chroma_width;
662 p_free_picture->i_display_horizontal_offset = 0;
663 p_free_picture->i_display_vertical_offset = 0;
664 p_free_picture->i_display_width = i_width;
665 p_free_picture->i_display_height = i_height;
666 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
667 p_free_picture->i_refcount = 0;
668 p_vout->i_pictures++;
672 /* Memory allocation failed : set picture as empty */
673 p_free_picture->i_type = EMPTY_PICTURE;
674 p_free_picture->i_status = FREE_PICTURE;
675 p_free_picture = NULL;
676 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
680 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
682 vlc_mutex_unlock( &p_vout->picture_lock );
683 return( p_free_picture );
686 /* No free or destroyed picture could be found */
687 intf_DbgMsg( "warning: heap is full\n" );
688 vlc_mutex_unlock( &p_vout->picture_lock );
692 /*****************************************************************************
693 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
694 *****************************************************************************
695 * This function frees a previously reserved picture or a permanent
696 * picture. It is meant to be used when the construction of a picture aborted.
697 * Note that the picture will be destroyed even if it is linked !
698 *****************************************************************************/
699 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
701 vlc_mutex_lock( &p_vout->picture_lock );
704 /* Check if picture status is valid */
705 if( (p_pic->i_status != RESERVED_PICTURE) &&
706 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
707 (p_pic->i_status != RESERVED_DISP_PICTURE) )
709 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
713 p_pic->i_status = DESTROYED_PICTURE;
714 p_vout->i_pictures--;
717 intf_DbgMsg("picture %p\n", p_pic);
719 vlc_mutex_unlock( &p_vout->picture_lock );
722 /*****************************************************************************
723 * vout_LinkPicture: increment reference counter of a picture
724 *****************************************************************************
725 * This function increment the reference counter of a picture in the video
726 * heap. It needs a lock since several producer threads can access the picture.
727 *****************************************************************************/
728 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
730 vlc_mutex_lock( &p_vout->picture_lock );
734 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
737 vlc_mutex_unlock( &p_vout->picture_lock );
740 /*****************************************************************************
741 * vout_UnlinkPicture: decrement reference counter of a picture
742 *****************************************************************************
743 * This function decrement the reference counter of a picture in the video heap.
744 *****************************************************************************/
745 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
747 vlc_mutex_lock( &p_vout->picture_lock );
751 if( p_pic->i_refcount < 0 )
753 intf_DbgMsg("error: refcount < 0\n");
754 p_pic->i_refcount = 0;
758 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
760 p_pic->i_status = DESTROYED_PICTURE;
761 p_vout->i_pictures--;
765 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
768 vlc_mutex_unlock( &p_vout->picture_lock );
771 /*****************************************************************************
772 * vout_SetBuffers: set buffers adresses
773 *****************************************************************************
774 * This function is called by system drivers to set buffers video memory
776 *****************************************************************************/
777 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
779 /* No picture previously */
780 p_vout->p_buffer[0].i_pic_x = 0;
781 p_vout->p_buffer[0].i_pic_y = 0;
782 p_vout->p_buffer[0].i_pic_width = 0;
783 p_vout->p_buffer[0].i_pic_height = 0;
784 p_vout->p_buffer[1].i_pic_x = 0;
785 p_vout->p_buffer[1].i_pic_y = 0;
786 p_vout->p_buffer[1].i_pic_width = 0;
787 p_vout->p_buffer[1].i_pic_height = 0;
789 /* The first area covers all the screen */
790 p_vout->p_buffer[0].i_areas = 1;
791 p_vout->p_buffer[0].pi_area_begin[0] = 0;
792 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
793 p_vout->p_buffer[1].i_areas = 1;
794 p_vout->p_buffer[1].pi_area_begin[0] = 0;
795 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
798 p_vout->p_buffer[0].p_data = p_buf1;
799 p_vout->p_buffer[1].p_data = p_buf2;
802 /*****************************************************************************
803 * vout_Pixel2RGB: return red, green and blue from pixel value
804 *****************************************************************************
805 * Return color values, in 0-255 range, of the decomposition of a pixel. This
806 * is a slow routine and should only be used for initialization phase.
807 *****************************************************************************/
808 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
810 *pi_red = i_pixel & p_vout->i_red_mask;
811 *pi_green = i_pixel & p_vout->i_green_mask;
812 *pi_blue = i_pixel & p_vout->i_blue_mask;
815 /* following functions are local */
817 /*****************************************************************************
818 * BinaryLog: computes the base 2 log of a binary value
819 *****************************************************************************
820 * This functions is used by MaskToShift, to get a bit index from a binary
822 *****************************************************************************/
823 static int BinaryLog(u32 i)
848 if (i != ((u32)1 << i_log))
850 intf_ErrMsg("internal error: binary log overflow\n");
856 /*****************************************************************************
857 * MaskToShift: transform a color mask into right and left shifts
858 *****************************************************************************
859 * This function is used for obtaining color shifts from masks.
860 *****************************************************************************/
861 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
863 u32 i_low, i_high; /* lower hand higher bits of the mask */
866 i_low = i_mask & (- i_mask); /* lower bit of the mask */
867 i_high = i_mask + i_low; /* higher bit of the mask */
869 /* Transform bits into an index */
870 i_low = BinaryLog (i_low);
871 i_high = BinaryLog (i_high);
873 /* Update pointers and return */
875 *pi_right = (8 - i_high + i_low);
878 /*****************************************************************************
879 * InitThread: initialize video output thread
880 *****************************************************************************
881 * This function is called from RunThread and performs the second step of the
882 * initialization. It returns 0 on success. Note that the thread's flag are not
883 * modified inside this function.
884 *****************************************************************************/
885 static int InitThread( vout_thread_t *p_vout )
889 *p_vout->pi_status = THREAD_START;
891 /* Initialize output method - this function issues its own error messages */
892 if( p_vout->p_sys_init( p_vout ) )
897 /* Initialize convertion tables and functions */
898 if( vout_InitYUV( p_vout ) )
900 intf_ErrMsg("error: can't allocate YUV translation tables\n");
904 /* Mark thread as running and return */
905 p_vout->b_active = 1;
906 *p_vout->pi_status = THREAD_READY;
907 intf_DbgMsg("thread ready\n");
911 /*****************************************************************************
912 * RunThread: video output thread
913 *****************************************************************************
914 * Video output thread. This function does only returns when the thread is
915 * terminated. It handles the pictures arriving in the video heap and the
916 * display device events.
917 *****************************************************************************/
918 static void RunThread( vout_thread_t *p_vout)
920 /* XXX?? welcome to gore land */
921 static int i_trash_count = 0;
922 static mtime_t last_display_date = 0;
924 int i_index; /* index in heap */
925 mtime_t current_date; /* current date */
926 mtime_t display_date; /* display date */
927 boolean_t b_display; /* display flag */
928 picture_t * p_pic; /* picture pointer */
929 subpicture_t * p_subpic; /* subpicture pointer */
934 p_vout->b_error = InitThread( p_vout );
935 if( p_vout->b_error )
937 DestroyThread( p_vout, THREAD_ERROR );
943 * Main loop - it is not executed if an error occured during
946 while( (!p_vout->b_die) && (!p_vout->b_error) )
948 /* Initialize loop variables */
952 current_date = mdate();
955 * Find the picture to display - this operation does not need lock,
956 * since only READY_PICTUREs are handled
958 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
960 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
962 (p_vout->p_picture[i_index].date < display_date) ) )
964 p_pic = &p_vout->p_picture[i_index];
965 display_date = p_pic->date;
972 /* Computes FPS rate */
973 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
977 //fprintf( stderr, "gap : %Ld\n", display_date-last_display_date );
978 last_display_date = display_date;
980 if( display_date < current_date && i_trash_count > 4 )
982 /* Picture is late: it will be destroyed and the thread will sleep and
983 * go to next picture */
985 vlc_mutex_lock( &p_vout->picture_lock );
986 if( p_pic->i_refcount )
988 p_pic->i_status = DISPLAYED_PICTURE;
992 p_pic->i_status = DESTROYED_PICTURE;
993 p_vout->i_pictures--;
995 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
996 vlc_mutex_unlock( &p_vout->picture_lock );
998 /* Update synchronization information as if display delay
1000 Synchronize( p_vout, display_date - current_date );
1008 if( display_date > current_date + VOUT_DISPLAY_DELAY )
1010 /* A picture is ready to be rendered, but its rendering date is
1011 * far from the current one so the thread will perform an empty loop
1012 * as if no picture were found. The picture state is unchanged */
1018 /* Picture will be displayed, update synchronization
1020 Synchronize( p_vout, display_date - current_date );
1024 * Find the subpicture to display - this operation does not need lock, since
1025 * only READY_SUBPICTURES are handled. If no picture has been selected,
1026 * display_date will depend on the subpicture
1031 * Perform rendering, sleep and display rendered picture
1033 if( p_pic ) /* picture and perhaps subpicture */
1035 b_display = p_vout->b_active;
1036 p_vout->last_display_date = display_date;
1040 /* Set picture dimensions and clear buffer */
1041 SetBufferPicture( p_vout, p_pic );
1043 /* Render picture and informations */
1044 RenderPicture( p_vout, p_pic );
1045 if( p_vout->b_info )
1047 RenderPictureInfo( p_vout, p_pic );
1048 RenderInfo( p_vout );
1052 /* Remove picture from heap */
1053 vlc_mutex_lock( &p_vout->picture_lock );
1054 if( p_pic->i_refcount )
1056 p_pic->i_status = DISPLAYED_PICTURE;
1060 p_pic->i_status = DESTROYED_PICTURE;
1061 p_vout->i_pictures--;
1063 vlc_mutex_unlock( &p_vout->picture_lock );
1065 /* Render interface and subpicture */
1066 if( b_display && p_vout->b_interface )
1068 RenderInterface( p_vout );
1074 RenderSubPicture( p_vout, p_subpic );
1077 /* Remove subpicture from heap */
1078 vlc_mutex_lock( &p_vout->subpicture_lock );
1079 p_subpic->i_status = DESTROYED_SUBPICTURE;
1080 vlc_mutex_unlock( &p_vout->subpicture_lock );
1084 else if( p_subpic ) /* subpicture alone */
1086 b_display = p_vout->b_active;
1087 p_vout->last_display_date = display_date;
1092 SetBufferPicture( p_vout, NULL );
1094 /* Render informations, interface and subpicture */
1095 if( p_vout->b_info )
1097 RenderInfo( p_vout );
1099 if( p_vout->b_interface )
1101 RenderInterface( p_vout );
1103 RenderSubPicture( p_vout, p_subpic );
1106 /* Remove subpicture from heap */
1107 vlc_mutex_lock( &p_vout->subpicture_lock );
1108 p_subpic->i_status = DESTROYED_SUBPICTURE;
1109 vlc_mutex_unlock( &p_vout->subpicture_lock );
1111 else if( p_vout->b_active ) /* idle or interface screen alone */
1113 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1115 /* Interface has changed, so a new rendering is required - force
1116 * it by setting last idle date to 0 */
1117 p_vout->last_idle_date = 0;
1120 /* Render idle screen and update idle date, then render interface if
1122 b_display = RenderIdle( p_vout );
1125 p_vout->last_idle_date = current_date;
1126 if( p_vout->b_interface )
1128 RenderInterface( p_vout );
1139 * Sleep, wake up and display rendered picture
1143 /* Store render time */
1144 p_vout->render_time = mdate() - current_date;
1147 /* Give back change lock */
1148 vlc_mutex_unlock( &p_vout->change_lock );
1150 /* Sleep a while or until a given date */
1151 if( display_date != 0 )
1153 mwait( display_date );
1157 msleep( VOUT_IDLE_SLEEP );
1160 /* On awakening, take back lock and send immediately picture to display,
1161 * then swap buffers */
1162 vlc_mutex_lock( &p_vout->change_lock );
1164 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1165 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1167 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1169 p_vout->p_sys_display( p_vout );
1170 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1174 * Check events and manage thread
1176 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1178 /* A fatal error occured, and the thread must terminate immediately,
1179 * without displaying anything - setting b_error to 1 cause the
1180 * immediate end of the main while() loop. */
1181 p_vout->b_error = 1;
1186 * Error loop - wait until the thread destruction is requested
1188 if( p_vout->b_error )
1190 ErrorThread( p_vout );
1194 EndThread( p_vout );
1195 DestroyThread( p_vout, THREAD_OVER );
1196 intf_DbgMsg( "thread end\n" );
1199 /*****************************************************************************
1200 * ErrorThread: RunThread() error loop
1201 *****************************************************************************
1202 * This function is called when an error occured during thread main's loop. The
1203 * thread can still receive feed, but must be ready to terminate as soon as
1205 *****************************************************************************/
1206 static void ErrorThread( vout_thread_t *p_vout )
1208 /* Wait until a `die' order */
1210 while( !p_vout->b_die )
1213 msleep( VOUT_IDLE_SLEEP );
1217 /*****************************************************************************
1218 * EndThread: thread destruction
1219 *****************************************************************************
1220 * This function is called when the thread ends after a sucessful
1221 * initialization. It frees all ressources allocated by InitThread.
1222 *****************************************************************************/
1223 static void EndThread( vout_thread_t *p_vout )
1225 int i_index; /* index in heap */
1229 *p_vout->pi_status = THREAD_END;
1231 /* Destroy all remaining pictures and subpictures */
1232 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1234 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1236 free( p_vout->p_picture[i_index].p_data );
1238 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1240 free( p_vout->p_subpicture[i_index].p_data );
1244 /* Destroy translation tables */
1245 vout_EndYUV( p_vout );
1246 p_vout->p_sys_end( p_vout );
1249 /*****************************************************************************
1250 * DestroyThread: thread destruction
1251 *****************************************************************************
1252 * This function is called when the thread ends. It frees all ressources
1253 * allocated by CreateThread. Status is available at this stage.
1254 *****************************************************************************/
1255 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1257 int *pi_status; /* status adress */
1259 /* Store status adress */
1261 pi_status = p_vout->pi_status;
1263 /* Destroy thread structures allocated by Create and InitThread */
1264 vout_UnloadFont( p_vout->p_default_font );
1265 vout_UnloadFont( p_vout->p_large_font );
1266 p_vout->p_sys_destroy( p_vout );
1269 TrashPlugin( p_vout->vout_plugin );
1271 /* Free structure */
1273 *pi_status = i_status;
1276 /*****************************************************************************
1277 * Print: print simple text on a picture
1278 *****************************************************************************
1279 * This function will print a simple text on the picture. It is designed to
1280 * print debugging or general informations.
1281 *****************************************************************************/
1282 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 )
1284 int i_text_height; /* total text height */
1285 int i_text_width; /* total text width */
1287 /* Update upper left coordinates according to alignment */
1288 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1289 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1291 /* Set area and print text */
1292 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1293 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1294 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1295 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1296 p_vout->i_white_pixel, 0, 0,
1301 /*****************************************************************************
1302 * SetBufferArea: activate an area in current buffer
1303 *****************************************************************************
1304 * This function is called when something is rendered on the current buffer.
1305 * It set the area as active and prepare it to be cleared on next rendering.
1306 * Pay attention to the fact that in this functions, i_h is in fact the end y
1307 * coordinate of the new area.
1308 *****************************************************************************/
1309 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1311 vout_buffer_t * p_buffer; /* current buffer */
1312 int i_area_begin, i_area_end; /* area vertical extension */
1313 int i_area, i_area_copy; /* area index */
1314 int i_area_shift; /* shift distance for areas */
1316 /* Choose buffer and modify h to end of area position */
1317 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1321 * Remove part of the area which is inside the picture - this is done
1322 * by calling again SetBufferArea with the correct areas dimensions.
1324 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1326 i_area_begin = p_buffer->i_pic_y;
1327 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1329 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1330 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1331 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1333 /* Keep the stripe above the picture, if any */
1334 if( i_y < i_area_begin )
1336 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1338 /* Keep the stripe below the picture, if any */
1339 if( i_h > i_area_end )
1341 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1347 /* Skip some extensions until interesting areas */
1349 (i_area < p_buffer->i_areas) &&
1350 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1356 if( i_area == p_buffer->i_areas )
1358 /* New area is below all existing ones: just add it at the end of the
1359 * array, if possible - else, append it to the last one */
1360 if( i_area < VOUT_MAX_AREAS )
1362 p_buffer->pi_area_begin[i_area] = i_y;
1363 p_buffer->pi_area_end[i_area] = i_h;
1364 p_buffer->i_areas++;
1369 intf_DbgMsg("areas overflow\n");
1371 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1376 i_area_begin = p_buffer->pi_area_begin[i_area];
1377 i_area_end = p_buffer->pi_area_end[i_area];
1379 if( i_y < i_area_begin )
1381 if( i_h >= i_area_begin - 1 )
1383 /* Extend area above */
1384 p_buffer->pi_area_begin[i_area] = i_y;
1388 /* Create a new area above : merge last area if overflow, then
1389 * move all old areas down */
1390 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1393 intf_DbgMsg("areas overflow\n");
1395 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1399 p_buffer->i_areas++;
1401 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1403 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1404 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1406 p_buffer->pi_area_begin[i_area] = i_y;
1407 p_buffer->pi_area_end[i_area] = i_h;
1411 if( i_h > i_area_end )
1413 /* Find further areas which can be merged with the new one */
1414 for( i_area_copy = i_area + 1;
1415 (i_area_copy < p_buffer->i_areas) &&
1416 (p_buffer->pi_area_begin[i_area] <= i_h);
1423 if( i_area_copy != i_area )
1425 /* Merge with last possible areas */
1426 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1428 /* Shift lower areas upward */
1429 i_area_shift = i_area_copy - i_area;
1430 p_buffer->i_areas -= i_area_shift;
1431 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1433 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1434 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1439 /* Extend area below */
1440 p_buffer->pi_area_end[i_area] = i_h;
1446 /*****************************************************************************
1447 * SetBufferPicture: clear buffer and set picture area
1448 *****************************************************************************
1449 * This function is called before any rendering. It clears the current
1450 * rendering buffer and set the new picture area. If the picture pointer is
1451 * NULL, then no picture area is defined. Floating operations are avoided since
1452 * some MMX calculations may follow.
1453 *****************************************************************************/
1454 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1456 vout_buffer_t * p_buffer; /* current buffer */
1457 int i_pic_x, i_pic_y; /* picture position */
1458 int i_pic_width, i_pic_height; /* picture dimensions */
1459 int i_old_pic_y, i_old_pic_height; /* old picture area */
1460 int i_vout_width, i_vout_height; /* display dimensions */
1461 int i_area; /* area index */
1462 int i_data_index; /* area data index */
1463 int i_data_size; /* area data size, in 256 bytes blocs */
1464 u64 * p_data; /* area data, for clearing */
1465 byte_t * p_data8; /* area data, for clearing (slow) */
1467 /* Choose buffer and set display dimensions */
1468 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1469 i_vout_width = p_vout->i_width;
1470 i_vout_height = p_vout->i_height;
1473 * Computes new picture size
1477 /* Try horizontal scaling first - width must be a mutiple of 16 */
1478 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1479 i_vout_width : p_pic->i_width) & ~0xf;
1480 switch( p_pic->i_aspect_ratio )
1482 case AR_3_4_PICTURE:
1483 i_pic_height = i_pic_width * 3 / 4;
1485 case AR_16_9_PICTURE:
1486 i_pic_height = i_pic_width * 9 / 16;
1488 case AR_221_1_PICTURE:
1489 i_pic_height = i_pic_width * 100 / 221;
1491 case AR_SQUARE_PICTURE:
1493 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1497 /* If picture dimensions using horizontal scaling are too large, use
1498 * vertical scaling. Since width must be a multiple of 16, height is
1499 * adjusted again after. */
1500 if( i_pic_height > i_vout_height )
1502 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1503 i_vout_height : p_pic->i_height;
1504 switch( p_pic->i_aspect_ratio )
1506 case AR_3_4_PICTURE:
1507 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1508 i_pic_height = i_pic_width * 3 / 4;
1510 case AR_16_9_PICTURE:
1511 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1512 i_pic_height = i_pic_width * 9 / 16;
1514 case AR_221_1_PICTURE:
1515 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1516 i_pic_height = i_pic_width * 100 / 221;
1518 case AR_SQUARE_PICTURE:
1520 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1521 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1526 /* Set picture position */
1527 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1528 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1532 /* No picture: size is 0 */
1540 * Set new picture size - if is is smaller than the previous one, clear
1541 * around it. Since picture are centered, only their size is tested.
1543 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1545 i_old_pic_y = p_buffer->i_pic_y;
1546 i_old_pic_height = p_buffer->i_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;
1551 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1555 p_buffer->i_pic_x = i_pic_x;
1556 p_buffer->i_pic_y = i_pic_y;
1557 p_buffer->i_pic_width = i_pic_width;
1558 p_buffer->i_pic_height = i_pic_height;
1564 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1567 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1568 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1570 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1571 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1572 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1574 /* Clear 256 bytes block */
1575 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1576 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1577 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1578 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1579 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1580 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1581 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1582 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1584 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1586 /* Clear remaining 16 bytes blocks */
1587 *p_data++ = 0; *p_data++ = 0;
1589 p_data8 = (byte_t *)p_data;
1590 for( i_data_index = i_data_size % 16; i_data_index--; )
1592 /* Clear remaining bytes */
1600 p_buffer->i_areas = 0;
1603 /*****************************************************************************
1604 * RenderPicture: render a picture
1605 *****************************************************************************
1606 * This function convert a picture from a video heap to a pixel-encoded image
1607 * and copy it to the current rendering buffer. No lock is required, since the
1608 * rendered picture has been determined as existant, and will only be destroyed
1609 * by the vout thread later.
1610 *****************************************************************************/
1611 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1614 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1615 mtime_t render_time; /* picture rendering time */
1617 vout_buffer_t * p_buffer; /* rendering buffer */
1618 byte_t * p_pic_data; /* convertion destination */
1620 /* Get and set rendering informations */
1621 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1622 p_pic_data = p_buffer->p_data +
1623 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1624 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1626 render_time = mdate();
1630 * Choose appropriate rendering function and render picture
1632 switch( p_pic->i_type )
1634 case YUV_420_PICTURE:
1635 p_vout->yuv.p_Convert420( 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_422_PICTURE:
1643 p_vout->yuv.p_Convert422( 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 );
1650 case YUV_444_PICTURE:
1651 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1652 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1653 p_pic->i_width, p_pic->i_height,
1654 p_buffer->i_pic_width, p_buffer->i_pic_height,
1655 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1656 p_pic->i_matrix_coefficients );
1660 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1666 /* Print picture date and rendering time */
1667 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1668 p_vout->i_buffer_index, (long) (mdate() - render_time),
1669 mstrtime( psz_date, p_pic->date ));
1673 /*****************************************************************************
1674 * RenderPictureInfo: print additionnal informations on a picture
1675 *****************************************************************************
1676 * This function will print informations such as fps and other picture
1677 * dependant informations.
1678 *****************************************************************************/
1679 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1681 #if defined(STATS) || defined(DEBUG)
1682 char psz_buffer[256]; /* string buffer */
1687 * Print FPS rate in upper right corner
1689 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1691 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1692 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1693 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1694 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1698 * Print frames count and loop time in upper left corner
1700 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1701 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1702 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1707 * Print picture information in lower right corner
1709 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1710 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1711 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1712 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1713 p_pic->i_width, p_pic->i_height,
1714 p_pic->i_display_width, p_pic->i_display_height,
1715 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1716 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1717 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1718 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1719 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1720 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1721 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1722 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1723 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1724 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1728 /*****************************************************************************
1729 * RenderIdle: render idle picture
1730 *****************************************************************************
1731 * This function will print something on the screen. It will return 0 if
1732 * nothing has been rendered, or 1 if something has been changed on the screen.
1733 * Note that if you absolutely want something to be printed, you will have
1734 * to force it by setting the last idle date to 0.
1735 * Unlike other rendering functions, this one calls the SetBufferPicture
1736 * function when needed.
1737 *****************************************************************************/
1738 static int RenderIdle( vout_thread_t *p_vout )
1740 int i_x = 0, i_y = 0; /* text position */
1741 int i_width, i_height; /* text size */
1742 mtime_t current_date; /* current date */
1743 const char *psz_text = "waiting for stream ..."; /* text to display */
1746 current_date = mdate();
1747 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1748 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1750 SetBufferPicture( p_vout, NULL );
1751 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1752 &i_width, &i_height );
1753 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1755 vout_Print( p_vout->p_large_font,
1756 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1757 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1758 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1759 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1760 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1761 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1768 /*****************************************************************************
1769 * RenderInfo: render additionnal informations
1770 *****************************************************************************
1771 * This function render informations which do not depend of the current picture
1773 *****************************************************************************/
1774 static void RenderInfo( vout_thread_t *p_vout )
1777 char psz_buffer[256]; /* string buffer */
1778 int i_ready_pic = 0; /* ready pictures */
1779 int i_reserved_pic = 0; /* reserved pictures */
1780 int i_picture; /* picture index */
1785 * Print thread state in lower left corner
1787 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1789 switch( p_vout->p_picture[i_picture].i_status )
1791 case RESERVED_PICTURE:
1792 case RESERVED_DATED_PICTURE:
1793 case RESERVED_DISP_PICTURE:
1801 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1802 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1803 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1807 /*****************************************************************************
1808 * RenderSubPicture: render a subpicture
1809 *****************************************************************************
1810 * This function render a sub picture unit.
1811 *****************************************************************************/
1812 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1814 p_vout_font_t p_font; /* text font */
1815 int i_width, i_height; /* subpicture dimensions */
1817 switch( p_subpic->i_type )
1819 case TEXT_SUBPICTURE: /* single line text */
1820 /* Select default font if not specified */
1821 p_font = p_subpic->type.text.p_font;
1822 if( p_font == NULL )
1824 p_font = p_vout->p_default_font;
1827 /* Computes text size (width and height fields are ignored) and print it */
1828 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1829 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1830 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1832 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1833 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1834 p_subpic->i_y * p_vout->i_bytes_per_line,
1835 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1836 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1837 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1839 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1845 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1850 /*****************************************************************************
1851 * RenderInterface: render the interface
1852 *****************************************************************************
1853 * This function render the interface, if any.
1854 *****************************************************************************/
1855 static void RenderInterface( vout_thread_t *p_vout )
1857 int i_height, i_text_height; /* total and text height */
1858 int i_width_1, i_width_2; /* text width */
1859 int i_byte; /* byte index */
1860 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1861 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1864 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1865 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1866 i_height += i_text_height;
1868 /* Render background */
1869 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1870 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1874 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1877 /* Render text, if not larger than screen */
1878 if( i_width_1 < p_vout->i_width )
1880 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1881 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1882 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1883 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1884 OUTLINED_TEXT, psz_text_1 );
1886 if( i_width_2 < p_vout->i_width )
1888 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1889 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1890 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1891 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1892 OUTLINED_TEXT, psz_text_2 );
1895 /* Activate modified area */
1896 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1899 /*****************************************************************************
1900 * Synchronize: update synchro level depending of heap state
1901 *****************************************************************************
1902 * This function is called during the main vout loop.
1903 *****************************************************************************/
1904 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1906 int i_synchro_inc = 0;
1907 /* XXX?? gore following */
1908 static int i_panic_count = 0;
1909 static int i_last_synchro_inc = 0;
1910 static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1911 static int i_truc = 10;
1915 //fprintf( stderr, "PANIC %d\n", i_panic_count );
1921 if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1924 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1929 if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1932 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1936 if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1937 i_synchro_inc*i_last_synchro_inc < 0 )
1942 if( i_delay < 6000 )
1947 else if( i_delay < 70000 )
1949 i_truc = 24+(24*i_delay)/70000;
1952 i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
1954 else if( i_delay > 100000 )
1956 r_synchro_level += 1;
1957 if( i_delay > 130000 )
1958 r_synchro_level += 1;
1961 r_synchro_level += (float)i_synchro_inc / i_truc;
1962 p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
1964 if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
1966 r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
1969 //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
1970 // p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
1971 i_last_synchro_inc = i_synchro_inc;
1974 /*****************************************************************************
1975 * Manage: manage thread
1976 *****************************************************************************
1977 * This function will handle changes in thread configuration.
1978 *****************************************************************************/
1979 static int Manage( vout_thread_t *p_vout )
1982 if( p_vout->i_changes )
1984 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1985 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1989 /* On gamma or grayscale change, rebuild tables */
1990 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1993 if( vout_ResetYUV( p_vout ) )
1995 intf_ErrMsg("error: can't rebuild convertion tables\n");
2000 /* Clear changes flags which does not need management or have been
2002 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2003 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
2004 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
2006 /* Detect unauthorized changes */
2007 if( p_vout->i_changes )
2009 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
2010 * function, it means they should not be authorized */
2011 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2018 /*****************************************************************************
2019 * Align: align a subpicture in the screen
2020 *****************************************************************************
2021 * This function is used for rendering text or subpictures. It returns non 0
2022 * it the final aera is not fully included in display area. Return coordinates
2024 *****************************************************************************/
2025 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2026 int i_width, int i_height, int i_h_align, int i_v_align )
2028 /* Align horizontally */
2032 *pi_x -= i_width / 2;
2035 *pi_x += (p_vout->i_width - i_width) / 2;
2041 *pi_x += p_vout->i_width - i_width;
2045 /* Align vertically */
2049 *pi_y -= i_height / 2;
2052 *pi_y += (p_vout->i_height - i_height) / 2;
2058 *pi_y += p_vout->i_height - i_height;
2060 case SUBTITLE_RALIGN:
2061 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2062 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2066 /* Return non 0 if clipping failed */
2067 return( (*pi_x < 0) || (*pi_y < 0) ||
2068 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2071 /*****************************************************************************
2072 * SetPalette: sets an 8 bpp palette
2073 *****************************************************************************
2074 * This function is just a prototype that does nothing. Architectures that
2075 * support palette allocation should override it.
2076 *****************************************************************************/
2077 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2078 u16 *green, u16 *blue, u16 *transp )
2080 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );