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
8 * $Id: video_output.c,v 1.129 2001/05/30 17:03:13 sam Exp $
10 * Authors: Vincent Seguin <seguin@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <errno.h> /* ENOMEM */
33 #include <stdlib.h> /* free() */
34 #include <stdio.h> /* sprintf() */
35 #include <string.h> /* strerror() */
38 # include <sys/times.h>
48 #include "video_output.h"
49 #include "video_text.h"
50 #include "video_spu.h"
51 #include "video_yuv.h"
57 /*****************************************************************************
59 *****************************************************************************/
60 static int BinaryLog ( u32 i );
61 static void MaskToShift ( int *pi_left, int *pi_right, u32 i_mask );
62 static int InitThread ( vout_thread_t *p_vout );
63 static void RunThread ( vout_thread_t *p_vout );
64 static void ErrorThread ( vout_thread_t *p_vout );
65 static void EndThread ( vout_thread_t *p_vout );
66 static void DestroyThread ( vout_thread_t *p_vout, int i_status );
67 static void Print ( vout_thread_t *p_vout, int i_x, int i_y,
68 int i_h_align, int i_v_align,
69 unsigned char *psz_text );
70 static void SetBuffers ( vout_thread_t *p_vout, void *, void * );
71 static void SetBufferArea ( vout_thread_t *p_vout, int i_x, int i_y,
73 static void SetBufferPicture ( vout_thread_t *p_vout, picture_t *p_pic );
74 static void RenderPicture ( vout_thread_t *p_vout, picture_t *p_pic );
75 static void RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
76 static void RenderSubPicture ( vout_thread_t *p_vout, picture_t *p_pic,
77 subpicture_t *p_subpic );
78 static int RenderIdle ( vout_thread_t *p_vout );
79 static void RenderInfo ( vout_thread_t *p_vout );
80 static int Manage ( vout_thread_t *p_vout );
81 static int Align ( vout_thread_t *p_vout, int *pi_x,
82 int *pi_y, int i_width, int i_height,
83 int i_h_align, int i_v_align );
84 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
85 u16 *green, u16 *blue, u16 *transp );
87 /*****************************************************************************
88 * vout_InitBank: initialize the video output bank.
89 *****************************************************************************/
90 void vout_InitBank ( void )
92 p_vout_bank->i_count = 0;
94 vlc_mutex_init( &p_vout_bank->lock );
97 /*****************************************************************************
98 * vout_EndBank: empty the video output bank.
99 *****************************************************************************
100 * This function ends all unused video outputs and empties the bank in
102 *****************************************************************************/
103 void vout_EndBank ( void )
105 /* Ask all remaining video outputs to die */
106 while( p_vout_bank->i_count )
109 p_vout_bank->pp_vout[ --p_vout_bank->i_count ], NULL );
112 vlc_mutex_destroy( &p_vout_bank->lock );
115 /*****************************************************************************
116 * vout_CreateThread: creates a new video output thread
117 *****************************************************************************
118 * This function creates a new video output thread, and returns a pointer
119 * to its description. On error, it returns NULL.
120 * If pi_status is NULL, then the function will block until the thread is ready.
121 * If not, it will be updated using one of the THREAD_* constants.
122 *****************************************************************************/
123 vout_thread_t * vout_CreateThread ( int *pi_status )
125 vout_thread_t * p_vout; /* thread descriptor */
126 int i_status; /* thread status */
127 int i_index; /* index for array initialization */
129 /* Allocate descriptor */
130 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
133 intf_ErrMsg( "vout error: vout thread creation returned %s",
138 /* Choose the best module */
139 p_vout->p_module = module_Need( MODULE_CAPABILITY_VOUT, NULL );
141 if( p_vout->p_module == NULL )
143 intf_ErrMsg( "vout error: no suitable vout module" );
148 #define f p_vout->p_module->p_functions->vout.functions.vout
149 p_vout->pf_create = f.pf_create;
150 p_vout->pf_init = f.pf_init;
151 p_vout->pf_end = f.pf_end;
152 p_vout->pf_destroy = f.pf_destroy;
153 p_vout->pf_manage = f.pf_manage;
154 p_vout->pf_display = f.pf_display;
155 p_vout->pf_setpalette = f.pf_setpalette;
158 /* Initialize callbacks */
159 p_vout->pf_setbuffers = SetBuffers;
161 if( p_vout->pf_setpalette == NULL )
163 p_vout->pf_setpalette = SetPalette;
166 /* Initialize thread properties - thread id and locks will be initialized
170 p_vout->b_active = 0;
171 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
172 *p_vout->pi_status = THREAD_CREATE;
174 /* Initialize some fields used by the system-dependant method - these
175 * fields will probably be modified by the method, and are only
177 p_vout->i_changes = 0;
178 p_vout->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
179 VOUT_WIDTH_DEFAULT );
180 p_vout->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
181 VOUT_HEIGHT_DEFAULT );
182 p_vout->i_bytes_per_line = p_vout->i_width * 2;
183 p_vout->i_screen_depth = main_GetIntVariable( VOUT_DEPTH_VAR,
184 VOUT_DEPTH_DEFAULT );
185 p_vout->i_bytes_per_pixel = 2;
186 p_vout->f_gamma = VOUT_GAMMA_DEFAULT; // FIXME: replace with
188 p_vout->b_need_render = 1;
191 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
192 VOUT_GRAYSCALE_DEFAULT );
194 p_vout->b_interface = 0;
196 p_vout->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
197 VOUT_FULLSCREEN_DEFAULT );
199 intf_WarnMsg( 3, "wished configuration: %dx%d, %d/%d bpp (%d Bpl)",
200 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
201 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
203 /* Initialize idle screen */
204 p_vout->last_display_date = 0;
205 p_vout->last_idle_date = 0;
206 p_vout->init_display_date = mdate();
207 p_vout->render_time = 10000;
209 /* Initialize statistics fields */
210 p_vout->c_fps_samples = 0;
212 /* Initialize buffer index */
213 p_vout->i_buffer_index = 0;
215 /* Initialize pictures and subpictures - translation tables and functions
216 * will be initialized later in InitThread */
217 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
219 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
220 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
223 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
225 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
226 p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
229 p_vout->i_pictures = 0;
231 /* Create and initialize system-dependant method - this function issues its
232 * own error messages */
233 if( p_vout->pf_create( p_vout ) )
235 module_Unneed( p_vout->p_module );
240 intf_WarnMsg( 3, "actual configuration: %dx%d, %d/%d bpp (%d Bpl), "
241 "masks: 0x%x/0x%x/0x%x",
242 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
243 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
244 p_vout->i_red_mask, p_vout->i_green_mask,
245 p_vout->i_blue_mask );
247 /* Calculate shifts from system-updated masks */
248 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift,
249 p_vout->i_red_mask );
250 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift,
251 p_vout->i_green_mask );
252 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift,
253 p_vout->i_blue_mask );
255 /* Set some useful colors */
256 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
257 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
258 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
259 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
261 /* Load fonts - fonts must be initialized after the system method since
262 * they may be dependant on screen depth and other thread properties */
263 p_vout->p_default_font = vout_LoadFont( VOUT_DEFAULT_FONT );
264 if( p_vout->p_default_font == NULL )
266 intf_ErrMsg( "vout error: could not load default font" );
269 p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
270 if( p_vout->p_large_font == NULL )
272 intf_ErrMsg( "vout error: could not load large font" );
275 /* Create thread and set locks */
276 vlc_mutex_init( &p_vout->picture_lock );
277 vlc_mutex_init( &p_vout->subpicture_lock );
278 vlc_mutex_init( &p_vout->change_lock );
280 if( vlc_thread_create( &p_vout->thread_id, "video output",
281 (void *) RunThread, (void *) p_vout) )
283 intf_ErrMsg("vout error: %s", strerror(ENOMEM));
284 vout_UnloadFont( p_vout->p_default_font );
285 vout_UnloadFont( p_vout->p_large_font );
286 p_vout->pf_destroy( p_vout );
291 intf_WarnMsg( 1, "vout: video display initialized (%dx%d, %d/%d bpp)",
292 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
293 p_vout->i_bytes_per_pixel * 8 );
295 /* If status is NULL, wait until the thread is created */
296 if( pi_status == NULL )
300 msleep( THREAD_SLEEP );
301 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
302 && (i_status != THREAD_FATAL) );
303 if( i_status != THREAD_READY )
311 /*****************************************************************************
312 * vout_DestroyThread: destroys a previously created thread
313 *****************************************************************************
314 * Destroy a terminated thread.
315 * The function will request a destruction of the specified thread. If pi_error
316 * is NULL, it will return once the thread is destroyed. Else, it will be
317 * update using one of the THREAD_* constants.
318 *****************************************************************************/
319 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
321 int i_status; /* thread status */
325 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
326 *p_vout->pi_status = THREAD_DESTROY;
328 /* Request thread destruction */
331 /* If status is NULL, wait until thread has been destroyed */
332 if( pi_status == NULL )
336 msleep( THREAD_SLEEP );
337 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
338 && (i_status != THREAD_FATAL) );
342 /*****************************************************************************
343 * vout_DisplaySubPicture: display a subpicture unit
344 *****************************************************************************
345 * Remove the reservation flag of a subpicture, which will cause it to be ready
346 * for display. The picture does not need to be locked, since it is ignored by
347 * the output thread if is reserved.
348 *****************************************************************************/
349 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
352 char psz_start[ MSTRTIME_MAX_SIZE ]; /* buffer for date string */
353 char psz_stop[ MSTRTIME_MAX_SIZE ]; /* buffer for date string */
357 /* Check if status is valid */
358 if( p_subpic->i_status != RESERVED_SUBPICTURE )
360 intf_ErrMsg("error: subpicture %p has invalid status %d", p_subpic,
361 p_subpic->i_status );
365 /* Remove reservation flag */
366 p_subpic->i_status = READY_SUBPICTURE;
369 /* Send subpicture information */
370 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s",
371 p_subpic, p_subpic->i_type,
372 mstrtime( psz_start, p_subpic->i_start ),
373 mstrtime( psz_stop, p_subpic->i_stop ) );
377 /*****************************************************************************
378 * vout_CreateSubPicture: allocate a subpicture in the video output heap.
379 *****************************************************************************
380 * This function create a reserved subpicture in the video output heap.
381 * A null pointer is returned if the function fails. This method provides an
382 * already allocated zone of memory in the spu data fields. It needs locking
383 * since several pictures can be created by several producers threads.
384 *****************************************************************************/
385 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
388 int i_subpic; /* subpicture index */
389 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
390 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
393 vlc_mutex_lock( &p_vout->subpicture_lock );
396 * Look for an empty place
398 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
400 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
402 /* Subpicture is marked for destruction, but is still allocated */
403 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
404 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
406 /* Memory size do match or is smaller : memory will not be
407 * reallocated, and function can end immediately - this is
408 * the best possible case, since no memory allocation needs
410 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
412 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)",
413 &p_vout->p_subpicture[i_subpic] );
415 vlc_mutex_unlock( &p_vout->subpicture_lock );
416 return( &p_vout->p_subpicture[i_subpic] );
418 else if( p_destroyed_subpic == NULL )
420 /* Memory size do not match, but subpicture index will be kept
421 * in case we find no other place */
422 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
425 else if( (p_free_subpic == NULL) &&
426 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
428 /* Subpicture is empty and ready for allocation */
429 p_free_subpic = &p_vout->p_subpicture[i_subpic];
433 /* If no free subpictures are available, use a destroyed subpicture */
434 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
436 /* No free subpicture or matching destroyed subpictures have been
437 * found, but a destroyed subpicture is still avalaible */
438 free( p_destroyed_subpic->p_data );
439 p_free_subpic = p_destroyed_subpic;
445 if( p_free_subpic != NULL )
447 /* Allocate memory */
450 case TEXT_SUBPICTURE: /* text subpicture */
451 p_free_subpic->p_data = malloc( i_size + 1 );
453 case DVD_SUBPICTURE: /* DVD subpicture unit */
454 p_free_subpic->p_data = malloc( i_size );
458 intf_ErrMsg("error: unknown subpicture type %d", i_type );
459 p_free_subpic->p_data = NULL;
464 if( p_free_subpic->p_data != NULL )
466 /* Copy subpicture information, set some default values */
467 p_free_subpic->i_type = i_type;
468 p_free_subpic->i_status = RESERVED_SUBPICTURE;
469 p_free_subpic->i_size = i_size;
470 p_free_subpic->i_x = 0;
471 p_free_subpic->i_y = 0;
472 p_free_subpic->i_width = 0;
473 p_free_subpic->i_height = 0;
474 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
475 p_free_subpic->i_vertical_align = CENTER_RALIGN;
479 /* Memory allocation failed : set subpicture as empty */
480 p_free_subpic->i_type = EMPTY_SUBPICTURE;
481 p_free_subpic->i_status = FREE_SUBPICTURE;
482 p_free_subpic = NULL;
483 intf_ErrMsg( "vout error: spu allocation returned %s",
484 strerror( ENOMEM ) );
488 intf_DbgMsg("subpicture %p (in free subpicture slot)", p_free_subpic );
490 vlc_mutex_unlock( &p_vout->subpicture_lock );
491 return( p_free_subpic );
494 /* No free or destroyed subpicture could be found */
495 intf_DbgMsg( "warning: subpicture heap is full" );
496 vlc_mutex_unlock( &p_vout->subpicture_lock );
500 /*****************************************************************************
501 * vout_DestroySubPicture: remove a subpicture from the heap
502 *****************************************************************************
503 * This function frees a previously reserved subpicture.
504 * It is meant to be used when the construction of a picture aborted.
505 * This function does not need locking since reserved subpictures are ignored
506 * by the output thread.
507 *****************************************************************************/
508 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
511 /* Check if status is valid */
512 if( p_subpic->i_status != RESERVED_SUBPICTURE )
514 intf_ErrMsg("error: subpicture %p has invalid status %d",
515 p_subpic, p_subpic->i_status );
519 p_subpic->i_status = DESTROYED_SUBPICTURE;
522 intf_DbgMsg("subpicture %p", p_subpic);
526 /*****************************************************************************
527 * vout_DisplayPicture: display a picture
528 *****************************************************************************
529 * Remove the reservation flag of a picture, which will cause it to be ready for
530 * display. The picture won't be displayed until vout_DatePicture has been
532 *****************************************************************************/
533 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
535 vlc_mutex_lock( &p_vout->picture_lock );
536 switch( p_pic->i_status )
538 case RESERVED_PICTURE:
539 p_pic->i_status = RESERVED_DISP_PICTURE;
541 case RESERVED_DATED_PICTURE:
542 p_pic->i_status = READY_PICTURE;
546 intf_ErrMsg("error: picture %p has invalid status %d", p_pic, p_pic->i_status );
552 intf_DbgMsg("picture %p", p_pic);
554 vlc_mutex_unlock( &p_vout->picture_lock );
557 /*****************************************************************************
558 * vout_DatePicture: date a picture
559 *****************************************************************************
560 * Remove the reservation flag of a picture, which will cause it to be ready for
561 * display. The picture won't be displayed until vout_DisplayPicture has been
563 *****************************************************************************/
564 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
567 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
570 vlc_mutex_lock( &p_vout->picture_lock );
572 switch( p_pic->i_status )
574 case RESERVED_PICTURE:
575 p_pic->i_status = RESERVED_DATED_PICTURE;
577 case RESERVED_DISP_PICTURE:
578 p_pic->i_status = READY_PICTURE;
582 intf_ErrMsg("error: picture %p has invalid status %d", p_pic, p_pic->i_status );
588 intf_DbgMsg("picture %p, display date: %s", p_pic, mstrtime( psz_date, p_pic->date) );
590 vlc_mutex_unlock( &p_vout->picture_lock );
593 /*****************************************************************************
594 * vout_CreatePicture: allocate a picture in the video output heap.
595 *****************************************************************************
596 * This function create a reserved image in the video output heap.
597 * A null pointer is returned if the function fails. This method provides an
598 * already allocated zone of memory in the picture data fields. It needs locking
599 * since several pictures can be created by several producers threads.
600 *****************************************************************************/
601 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
602 int i_width, int i_height )
604 int i_picture; /* picture index */
605 int i_chroma_width = 0; /* chroma width */
606 picture_t * p_free_picture = NULL; /* first free picture */
607 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
610 vlc_mutex_lock( &p_vout->picture_lock );
613 * Look for an empty place
615 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
617 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
619 /* Picture is marked for destruction, but is still allocated - note
620 * that if width and type are the same for two pictures, chroma_width
621 * should also be the same */
622 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
623 (p_vout->p_picture[i_picture].i_height == i_height) &&
624 (p_vout->p_picture[i_picture].i_width == i_width) )
626 /* Memory size do match : memory will not be reallocated, and function
627 * can end immediately - this is the best possible case, since no
628 * memory allocation needs to be done */
629 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
630 p_vout->i_pictures++;
632 intf_DbgMsg("picture %p (in destroyed picture slot)",
633 &p_vout->p_picture[i_picture] );
635 vlc_mutex_unlock( &p_vout->picture_lock );
636 return( &p_vout->p_picture[i_picture] );
638 else if( p_destroyed_picture == NULL )
640 /* Memory size do not match, but picture index will be kept in
641 * case no other place are left */
642 p_destroyed_picture = &p_vout->p_picture[i_picture];
645 else if( (p_free_picture == NULL) &&
646 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
648 /* Picture is empty and ready for allocation */
649 p_free_picture = &p_vout->p_picture[i_picture];
653 /* If no free picture is available, use a destroyed picture */
654 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
656 /* No free picture or matching destroyed picture has been found, but
657 * a destroyed picture is still avalaible */
658 free( p_destroyed_picture->p_data );
659 p_free_picture = p_destroyed_picture;
665 if( p_free_picture != NULL )
667 /* Allocate memory */
670 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
671 i_chroma_width = i_width / 2;
672 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
673 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
674 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
675 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
677 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
678 i_chroma_width = i_width / 2;
679 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
680 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
681 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
682 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
684 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
685 i_chroma_width = i_width;
686 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
687 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
688 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
689 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
693 intf_ErrMsg("error: unknown picture type %d", i_type );
694 p_free_picture->p_data = NULL;
699 if( p_free_picture->p_data != NULL )
701 /* Copy picture information, set some default values */
702 p_free_picture->i_type = i_type;
703 p_free_picture->i_status = RESERVED_PICTURE;
704 p_free_picture->i_matrix_coefficients = 1;
705 p_free_picture->i_width = i_width;
706 p_free_picture->i_height = i_height;
707 p_free_picture->i_chroma_width = i_chroma_width;
708 p_free_picture->i_size = i_width * i_height;
709 p_free_picture->i_chroma_size = i_chroma_width
711 p_free_picture->i_display_horizontal_offset = 0;
712 p_free_picture->i_display_vertical_offset = 0;
713 p_free_picture->i_display_width = i_width;
714 p_free_picture->i_display_height = i_height;
715 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
716 p_free_picture->i_refcount = 0;
717 p_vout->i_pictures++;
721 /* Memory allocation failed : set picture as empty */
722 p_free_picture->i_type = EMPTY_PICTURE;
723 p_free_picture->i_status = FREE_PICTURE;
724 p_free_picture = NULL;
725 intf_ErrMsg( "vout error: picture allocation returned %s",
726 strerror( ENOMEM ) );
730 intf_DbgMsg("picture %p (in free picture slot)", p_free_picture );
732 vlc_mutex_unlock( &p_vout->picture_lock );
734 /* Initialize mutex */
735 vlc_mutex_init( &(p_free_picture->lock_deccount) );
737 return( p_free_picture );
740 /* No free or destroyed picture could be found */
741 intf_DbgMsg( "warning: picture heap is full" );
742 vlc_mutex_unlock( &p_vout->picture_lock );
746 /*****************************************************************************
747 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
748 *****************************************************************************
749 * This function frees a previously reserved picture or a permanent
750 * picture. It is meant to be used when the construction of a picture aborted.
751 * Note that the picture will be destroyed even if it is linked !
752 *****************************************************************************/
753 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
755 vlc_mutex_lock( &p_vout->picture_lock );
758 /* Check if picture status is valid */
759 if( (p_pic->i_status != RESERVED_PICTURE) &&
760 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
761 (p_pic->i_status != RESERVED_DISP_PICTURE) )
763 intf_ErrMsg("error: picture %p has invalid status %d", p_pic, p_pic->i_status );
767 p_pic->i_status = DESTROYED_PICTURE;
768 p_vout->i_pictures--;
771 intf_DbgMsg("picture %p", p_pic);
774 /* destroy the lock that had been initialized in CreatePicture */
775 vlc_mutex_destroy( &(p_pic->lock_deccount) );
777 vlc_mutex_unlock( &p_vout->picture_lock );
780 /*****************************************************************************
781 * vout_LinkPicture: increment reference counter of a picture
782 *****************************************************************************
783 * This function increment the reference counter of a picture in the video
784 * heap. It needs a lock since several producer threads can access the picture.
785 *****************************************************************************/
786 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
788 vlc_mutex_lock( &p_vout->picture_lock );
792 intf_DbgMsg("picture %p refcount=%d", p_pic, p_pic->i_refcount );
795 vlc_mutex_unlock( &p_vout->picture_lock );
798 /*****************************************************************************
799 * vout_UnlinkPicture: decrement reference counter of a picture
800 *****************************************************************************
801 * This function decrement the reference counter of a picture in the video heap.
802 *****************************************************************************/
803 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
805 vlc_mutex_lock( &p_vout->picture_lock );
809 if( p_pic->i_refcount < 0 )
811 intf_DbgMsg("error: refcount < 0");
812 p_pic->i_refcount = 0;
816 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
818 p_pic->i_status = DESTROYED_PICTURE;
819 p_vout->i_pictures--;
823 intf_DbgMsg("picture %p refcount=%d", p_pic, p_pic->i_refcount );
826 vlc_mutex_unlock( &p_vout->picture_lock );
829 /*****************************************************************************
830 * vout_Pixel2RGB: return red, green and blue from pixel value
831 *****************************************************************************
832 * Return color values, in 0-255 range, of the decomposition of a pixel. This
833 * is a slow routine and should only be used for initialization phase.
834 *****************************************************************************/
835 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
837 *pi_red = i_pixel & p_vout->i_red_mask;
838 *pi_green = i_pixel & p_vout->i_green_mask;
839 *pi_blue = i_pixel & p_vout->i_blue_mask;
842 /* following functions are local */
844 /*****************************************************************************
845 * BinaryLog: computes the base 2 log of a binary value
846 *****************************************************************************
847 * This functions is used by MaskToShift, to get a bit index from a binary
849 *****************************************************************************/
850 static int BinaryLog(u32 i)
875 if (i != ((u32)1 << i_log))
877 intf_DbgMsg("internal error: binary log overflow");
883 /*****************************************************************************
884 * MaskToShift: transform a color mask into right and left shifts
885 *****************************************************************************
886 * This function is used for obtaining color shifts from masks.
887 *****************************************************************************/
888 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
890 u32 i_low, i_high; /* lower hand higher bits of the mask */
893 i_low = i_mask & (- i_mask); /* lower bit of the mask */
894 i_high = i_mask + i_low; /* higher bit of the mask */
896 /* Transform bits into an index */
897 i_low = BinaryLog (i_low);
898 i_high = BinaryLog (i_high);
900 /* Update pointers and return */
902 *pi_right = (8 - i_high + i_low);
905 /*****************************************************************************
906 * InitThread: initialize video output thread
907 *****************************************************************************
908 * This function is called from RunThread and performs the second step of the
909 * initialization. It returns 0 on success. Note that the thread's flag are not
910 * modified inside this function.
911 *****************************************************************************/
912 static int InitThread( vout_thread_t *p_vout )
915 *p_vout->pi_status = THREAD_START;
917 vlc_mutex_lock( &p_vout->change_lock );
923 /* Initialize output method - this function issues its own error messages */
924 if( p_vout->pf_init( p_vout ) )
929 /* Initialize convertion tables and functions */
930 if( vout_InitYUV( p_vout ) )
932 intf_ErrMsg("vout error: can't allocate YUV translation tables");
936 /* Mark thread as running and return */
937 p_vout->b_active = 1;
938 *p_vout->pi_status = THREAD_READY;
941 intf_DbgMsg("thread ready");
945 /*****************************************************************************
946 * RunThread: video output thread
947 *****************************************************************************
948 * Video output thread. This function does only returns when the thread is
949 * terminated. It handles the pictures arriving in the video heap and the
950 * display device events.
951 *****************************************************************************/
952 static void RunThread( vout_thread_t *p_vout)
954 int i_index; /* index in heap */
955 mtime_t current_date; /* current date */
956 mtime_t display_date; /* display date */
957 boolean_t b_display; /* display flag */
959 picture_t * p_pic; /* picture pointer */
961 subpicture_t * p_subpic; /* subpicture pointer */
962 subpicture_t * p_ephemer; /* youngest ephemer subpicture pointer */
963 mtime_t ephemer_date; /* earliest subpicture date */
968 p_vout->b_error = InitThread( p_vout );
969 if( p_vout->b_error )
971 DestroyThread( p_vout, THREAD_ERROR );
976 * Main loop - it is not executed if an error occured during
979 while( (!p_vout->b_die) && (!p_vout->b_error) )
981 /* Initialize loop variables */
987 current_date = mdate();
990 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
992 intf_Msg("vout stats: picture heap: %d/%d",
993 p_vout->i_pictures, VOUT_MAX_PICTURES);
998 * Find the picture to display - this operation does not need lock,
999 * since only READY_PICTUREs are handled
1001 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1003 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
1004 ( (p_pic == NULL) ||
1005 (p_vout->p_picture[i_index].date < display_date) ) )
1007 p_pic = &p_vout->p_picture[i_index];
1008 display_date = p_pic->date;
1014 /* Computes FPS rate */
1015 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
1017 if( display_date < current_date - p_vout->render_time )
1019 /* Picture is late: it will be destroyed and the thread
1020 * will sleep and go to next picture */
1022 vlc_mutex_lock( &p_vout->picture_lock );
1023 if( p_pic->i_refcount )
1025 p_pic->i_status = DISPLAYED_PICTURE;
1029 p_pic->i_status = DESTROYED_PICTURE;
1030 p_vout->i_pictures--;
1033 "warning: late picture skipped (%p)", p_pic );
1034 vlc_mutex_unlock( &p_vout->picture_lock );
1038 else if( display_date > current_date + VOUT_DISPLAY_DELAY )
1040 /* A picture is ready to be rendered, but its rendering date
1041 * is far from the current one so the thread will perform an
1042 * empty loop as if no picture were found. The picture state
1050 * Find the subpictures to display - this operation does not need
1051 * lock, since only READY_SUBPICTURE are handled. If no picture
1052 * has been selected, display_date will depend on the subpicture.
1054 * We also check for ephemer DVD subpictures (subpictures that have
1055 * to be removed if a newer one is available), which makes it a lot
1056 * more difficult to guess if a subpicture has to be rendered or not.
1058 * We get an easily parsable chained list of subpictures which
1059 * ends with NULL since p_subpic was initialized to NULL.
1061 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1063 if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
1065 /* If it is a DVD subpicture, check its date */
1066 if( p_vout->p_subpicture[i_index].i_type == DVD_SUBPICTURE )
1068 if( display_date > p_vout->p_subpicture[i_index].i_stop )
1070 /* Too late, destroy the subpic */
1071 vout_DestroySubPicture( p_vout,
1072 &p_vout->p_subpicture[i_index] );
1076 if( display_date < p_vout->p_subpicture[i_index].i_start )
1078 /* Too early, come back next monday */
1082 /* If this is an ephemer subpic, see if it's the
1083 * youngest we have */
1084 if( p_vout->p_subpicture[i_index].b_ephemer )
1086 if( p_ephemer == NULL )
1088 p_ephemer = &p_vout->p_subpicture[i_index];
1092 if( p_vout->p_subpicture[i_index].i_start
1093 < p_ephemer->i_start )
1095 /* Link the previous ephemer subpicture and
1096 * replace it with the current one */
1097 p_ephemer->p_next = p_subpic;
1098 p_subpic = p_ephemer;
1099 p_ephemer = &p_vout->p_subpicture[i_index];
1101 /* If it's the 2nd youngest subpicture,
1102 * register its date */
1104 || ephemer_date > p_subpic->i_start )
1106 ephemer_date = p_subpic->i_start;
1113 p_vout->p_subpicture[i_index].p_next = p_subpic;
1114 p_subpic = &p_vout->p_subpicture[i_index];
1116 /* If it's the 2nd youngest subpicture, register its date */
1117 if( !ephemer_date || ephemer_date > p_subpic->i_start )
1119 ephemer_date = p_subpic->i_start;
1122 /* If it's not a DVD subpicture, just register it */
1125 p_vout->p_subpicture[i_index].p_next = p_subpic;
1126 p_subpic = &p_vout->p_subpicture[i_index];
1131 /* If we found an ephemer subpicture, check if it has to be
1133 if( p_ephemer != NULL )
1135 if( p_ephemer->i_start < ephemer_date )
1137 /* Ephemer subpicture has lived too long */
1138 vout_DestroySubPicture( p_vout, p_ephemer );
1142 /* Ephemer subpicture can still live a bit */
1143 p_ephemer->p_next = p_subpic;
1144 p_subpic = p_ephemer;
1149 * Perform rendering, sleep and display rendered picture
1151 if( p_pic ) /* picture and perhaps subpicture */
1153 b_display = p_vout->b_active;
1154 p_vout->last_display_date = display_date;
1155 p_vout->p_rendered_pic = p_pic;
1157 /* Set picture dimensions and clear buffer */
1158 SetBufferPicture( p_vout, p_pic );
1160 /* FIXME: if b_need_render == 0 we need to do something with
1161 * the subpictures one day. */
1163 if( p_vout->b_need_render && b_display )
1165 /* Render picture and information */
1166 RenderPicture( p_vout, p_pic );
1167 if( p_vout->b_info )
1169 RenderPictureInfo( p_vout, p_pic );
1170 RenderInfo( p_vout );
1173 if( b_display ) /* XXX: quick HACK */
1177 RenderSubPicture( p_vout, p_pic, p_subpic );
1181 else if( p_vout->b_active && p_vout->b_need_render
1182 && p_vout->init_display_date == 0)
1184 /* Idle or interface screen alone */
1186 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1188 /* Interface has changed, so a new rendering is required - force
1189 * it by setting last idle date to 0 */
1190 p_vout->last_idle_date = 0;
1193 /* Render idle screen and update idle date, then render interface if
1195 b_display = RenderIdle( p_vout );
1198 p_vout->last_idle_date = current_date;
1209 * Check for the current time and
1210 * display splash screen if everything is on time
1212 if( p_vout->init_display_date > 0 && p_vout->b_need_render )
1214 p_vout->init_display_date = 0;
1219 * Sleep, wake up and display rendered picture
1222 if( display_date != 0 )
1224 /* Store render time using Bresenham algorithm */
1225 p_vout->render_time += mdate() - current_date;
1226 p_vout->render_time >>= 1;
1229 /* Give back change lock */
1230 vlc_mutex_unlock( &p_vout->change_lock );
1232 /* Sleep a while or until a given date */
1233 if( display_date != 0 )
1235 mwait( display_date - VOUT_MWAIT_TOLERANCE );
1239 msleep( VOUT_IDLE_SLEEP );
1242 /* On awakening, take back lock and send immediately picture to display,
1243 * then swap buffers */
1244 vlc_mutex_lock( &p_vout->change_lock );
1246 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d", p_pic, p_subpic,
1247 p_vout->i_buffer_index, b_display /* && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) */ );
1249 if( b_display /* && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) */ )
1251 p_vout->pf_display( p_vout );
1253 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1259 /* Remove picture from heap */
1260 vlc_mutex_lock( &p_vout->picture_lock );
1261 if( p_pic->i_refcount )
1263 p_pic->i_status = DISPLAYED_PICTURE;
1267 p_pic->i_status = DESTROYED_PICTURE;
1268 p_vout->i_pictures--;
1270 vlc_mutex_unlock( &p_vout->picture_lock );
1275 * Check events and manage thread
1277 if( p_vout->pf_manage( p_vout ) | Manage( p_vout ) )
1279 /* A fatal error occured, and the thread must terminate immediately,
1280 * without displaying anything - setting b_error to 1 cause the
1281 * immediate end of the main while() loop. */
1282 p_vout->b_error = 1;
1287 * Error loop - wait until the thread destruction is requested
1289 if( p_vout->b_error )
1291 ErrorThread( p_vout );
1295 EndThread( p_vout );
1296 DestroyThread( p_vout, THREAD_OVER );
1297 intf_DbgMsg( "thread end" );
1300 /*****************************************************************************
1301 * ErrorThread: RunThread() error loop
1302 *****************************************************************************
1303 * This function is called when an error occured during thread main's loop. The
1304 * thread can still receive feed, but must be ready to terminate as soon as
1306 *****************************************************************************/
1307 static void ErrorThread( vout_thread_t *p_vout )
1309 /* Wait until a `die' order */
1310 while( !p_vout->b_die )
1313 msleep( VOUT_IDLE_SLEEP );
1317 /*****************************************************************************
1318 * EndThread: thread destruction
1319 *****************************************************************************
1320 * This function is called when the thread ends after a sucessful
1321 * initialization. It frees all ressources allocated by InitThread.
1322 *****************************************************************************/
1323 static void EndThread( vout_thread_t *p_vout )
1325 int i_index; /* index in heap */
1328 *p_vout->pi_status = THREAD_END;
1332 struct tms cpu_usage;
1333 times( &cpu_usage );
1335 intf_Msg( "vout stats: cpu usage (user: %d, system: %d)",
1336 cpu_usage.tms_utime, cpu_usage.tms_stime );
1340 /* Destroy all remaining pictures and subpictures */
1341 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1343 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1345 free( p_vout->p_picture[i_index].p_data );
1349 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1351 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1353 free( p_vout->p_subpicture[i_index].p_data );
1357 /* Destroy translation tables */
1358 vout_EndYUV( p_vout );
1359 p_vout->pf_end( p_vout );
1361 /* Release the change lock */
1362 vlc_mutex_unlock( &p_vout->change_lock );
1365 /*****************************************************************************
1366 * DestroyThread: thread destruction
1367 *****************************************************************************
1368 * This function is called when the thread ends. It frees all ressources
1369 * allocated by CreateThread. Status is available at this stage.
1370 *****************************************************************************/
1371 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1373 int *pi_status; /* status adress */
1375 /* Store status adress */
1376 pi_status = p_vout->pi_status;
1378 /* Destroy thread structures allocated by Create and InitThread */
1379 vout_UnloadFont( p_vout->p_default_font );
1380 vout_UnloadFont( p_vout->p_large_font );
1381 p_vout->pf_destroy( p_vout );
1383 /* Destroy the locks */
1384 vlc_mutex_destroy( &p_vout->picture_lock );
1385 vlc_mutex_destroy( &p_vout->subpicture_lock );
1386 vlc_mutex_destroy( &p_vout->change_lock );
1388 /* Release the module */
1389 module_Unneed( p_vout->p_module );
1391 /* Free structure */
1393 *pi_status = i_status;
1396 /*****************************************************************************
1397 * Print: print simple text on a picture
1398 *****************************************************************************
1399 * This function will print a simple text on the picture. It is designed to
1400 * print debugging or general information.
1401 *****************************************************************************/
1402 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 )
1404 int i_text_height; /* total text height */
1405 int i_text_width; /* total text width */
1407 /* Update upper left coordinates according to alignment */
1408 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1409 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1411 /* Set area and print text */
1412 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1413 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1414 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1415 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1416 p_vout->i_white_pixel, 0, 0,
1421 /*****************************************************************************
1422 * SetBuffers: set buffers adresses
1423 *****************************************************************************
1424 * This function is called by system drivers to set buffers video memory
1426 *****************************************************************************/
1427 static void SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
1429 /* No picture previously */
1430 p_vout->p_buffer[0].i_pic_x = 0;
1431 p_vout->p_buffer[0].i_pic_y = 0;
1432 p_vout->p_buffer[0].i_pic_width = 0;
1433 p_vout->p_buffer[0].i_pic_height = 0;
1434 p_vout->p_buffer[1].i_pic_x = 0;
1435 p_vout->p_buffer[1].i_pic_y = 0;
1436 p_vout->p_buffer[1].i_pic_width = 0;
1437 p_vout->p_buffer[1].i_pic_height = 0;
1439 /* The first area covers all the screen */
1440 p_vout->p_buffer[0].i_areas = 1;
1441 p_vout->p_buffer[0].pi_area_begin[0] = 0;
1442 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
1443 p_vout->p_buffer[1].i_areas = 1;
1444 p_vout->p_buffer[1].pi_area_begin[0] = 0;
1445 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
1448 p_vout->p_buffer[0].p_data = p_buf1;
1449 p_vout->p_buffer[1].p_data = p_buf2;
1452 /*****************************************************************************
1453 * SetBufferArea: activate an area in current buffer
1454 *****************************************************************************
1455 * This function is called when something is rendered on the current buffer.
1456 * It set the area as active and prepare it to be cleared on next rendering.
1457 * Pay attention to the fact that in this functions, i_h is in fact the end y
1458 * coordinate of the new area.
1459 *****************************************************************************/
1460 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1462 vout_buffer_t * p_buffer; /* current buffer */
1463 int i_area_begin, i_area_end; /* area vertical extension */
1464 int i_area, i_area_copy; /* area index */
1465 int i_area_shift; /* shift distance for areas */
1467 /* Choose buffer and modify h to end of area position */
1468 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1472 * Remove part of the area which is inside the picture - this is done
1473 * by calling again SetBufferArea with the correct areas dimensions.
1475 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1477 i_area_begin = p_buffer->i_pic_y;
1478 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1480 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1481 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1482 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1484 /* Keep the stripe above the picture, if any */
1485 if( i_y < i_area_begin )
1487 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1489 /* Keep the stripe below the picture, if any */
1490 if( i_h > i_area_end )
1492 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1498 /* Skip some extensions until interesting areas */
1500 (i_area < p_buffer->i_areas) &&
1501 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1507 if( i_area == p_buffer->i_areas )
1509 /* New area is below all existing ones: just add it at the end of the
1510 * array, if possible - otherwise, append it to the last one */
1511 if( i_area < VOUT_MAX_AREAS )
1513 p_buffer->pi_area_begin[i_area] = i_y;
1514 p_buffer->pi_area_end[i_area] = i_h;
1515 p_buffer->i_areas++;
1520 intf_DbgMsg("area overflow");
1522 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1527 i_area_begin = p_buffer->pi_area_begin[i_area];
1528 i_area_end = p_buffer->pi_area_end[i_area];
1530 if( i_y < i_area_begin )
1532 if( i_h >= i_area_begin - 1 )
1534 /* Extend area above */
1535 p_buffer->pi_area_begin[i_area] = i_y;
1539 /* Create a new area above : merge last area if overflow, then
1540 * move all old areas down */
1541 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1544 intf_DbgMsg("areas overflow");
1546 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1550 p_buffer->i_areas++;
1552 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1554 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1555 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1557 p_buffer->pi_area_begin[i_area] = i_y;
1558 p_buffer->pi_area_end[i_area] = i_h;
1562 if( i_h > i_area_end )
1564 /* Find further areas which can be merged with the new one */
1565 for( i_area_copy = i_area + 1;
1566 (i_area_copy < p_buffer->i_areas) &&
1567 (p_buffer->pi_area_begin[i_area] <= i_h);
1574 if( i_area_copy != i_area )
1576 /* Merge with last possible areas */
1577 //p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1579 /* Shift lower areas upward */
1580 i_area_shift = i_area_copy - i_area;
1581 p_buffer->i_areas -= i_area_shift;
1582 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1584 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1585 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1590 /* Extend area below */
1591 p_buffer->pi_area_end[i_area] = i_h;
1597 /*****************************************************************************
1598 * SetBufferPicture: clear buffer and set picture area
1599 *****************************************************************************
1600 * This function is called before any rendering. It clears the current
1601 * rendering buffer and set the new picture area. If the picture pointer is
1602 * NULL, then no picture area is defined. Floating operations are avoided since
1603 * some MMX calculations may follow.
1604 *****************************************************************************/
1605 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1607 vout_buffer_t * p_buffer; /* current buffer */
1608 int i_pic_x, i_pic_y; /* picture position */
1609 int i_pic_width, i_pic_height; /* picture dimensions */
1610 int i_old_pic_y, i_old_pic_height; /* old picture area */
1611 int i_vout_width, i_vout_height; /* display dimensions */
1612 int i_area; /* area index */
1613 int i_data_index; /* area data index */
1614 int i_data_size; /* area data size, in 256 bytes blocs */
1615 u64 * p_data; /* area data, for clearing */
1616 byte_t * p_data8; /* area data, for clearing (slow) */
1618 /* Choose buffer and set display dimensions */
1619 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1620 i_vout_width = p_vout->i_width;
1621 i_vout_height = p_vout->i_height;
1624 * Computes new picture size
1628 /* Try horizontal scaling first - width must be a mutiple of 16 */
1629 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1630 i_vout_width : p_pic->i_width) & ~0xf;
1631 switch( p_pic->i_aspect_ratio )
1633 case AR_3_4_PICTURE:
1634 i_pic_height = i_pic_width * 3 / 4;
1636 case AR_16_9_PICTURE:
1637 i_pic_height = i_pic_width * 9 / 16;
1639 case AR_221_1_PICTURE:
1640 i_pic_height = i_pic_width * 100 / 221;
1642 case AR_SQUARE_PICTURE:
1644 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1648 /* If picture dimensions using horizontal scaling are too large, use
1649 * vertical scaling. Since width must be a multiple of 16, height is
1650 * adjusted again after. */
1651 if( i_pic_height > i_vout_height )
1653 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1654 i_vout_height : p_pic->i_height;
1655 switch( p_pic->i_aspect_ratio )
1657 case AR_3_4_PICTURE:
1658 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1659 i_pic_height = i_pic_width * 3 / 4;
1661 case AR_16_9_PICTURE:
1662 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1663 i_pic_height = i_pic_width * 9 / 16;
1665 case AR_221_1_PICTURE:
1666 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1667 i_pic_height = i_pic_width * 100 / 221;
1669 case AR_SQUARE_PICTURE:
1671 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1672 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1677 /* Set picture position */
1678 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1679 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1684 /* No picture: size is 0 */
1692 * Set new picture size - if it is smaller than the previous one, clear
1693 * around it. Since picture are centered, only their size is tested.
1695 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1697 i_old_pic_y = p_buffer->i_pic_y;
1698 i_old_pic_height = p_buffer->i_pic_height;
1699 p_buffer->i_pic_x = i_pic_x;
1700 p_buffer->i_pic_y = i_pic_y;
1701 p_buffer->i_pic_width = i_pic_width;
1702 p_buffer->i_pic_height = i_pic_height;
1703 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1707 p_buffer->i_pic_x = i_pic_x;
1708 p_buffer->i_pic_y = i_pic_y;
1709 p_buffer->i_pic_width = i_pic_width;
1710 p_buffer->i_pic_height = i_pic_height;
1716 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1719 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d", p_pic,
1720 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1722 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1723 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1725 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1727 /* Clear 256 bytes block */
1728 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1729 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1730 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1731 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1732 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1733 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1734 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1735 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1737 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1739 /* Clear remaining 16 bytes blocks */
1740 *p_data++ = 0; *p_data++ = 0;
1742 p_data8 = (byte_t *)p_data;
1743 for( i_data_index = i_data_size % 16; i_data_index--; )
1745 /* Clear remaining bytes */
1753 p_buffer->i_areas = 0;
1756 /*****************************************************************************
1757 * RenderPicture: render a picture
1758 *****************************************************************************
1759 * This function converts a picture from a video heap to a pixel-encoded image
1760 * and copies it to the current rendering buffer. No lock is required, since
1761 * the * rendered picture has been determined as existant, and will only be
1762 * destroyed by the vout thread later.
1763 *****************************************************************************/
1764 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1767 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1768 mtime_t render_time; /* picture rendering time */
1770 vout_buffer_t * p_buffer; /* rendering buffer */
1771 byte_t * p_pic_data; /* convertion destination */
1773 /* Get and set rendering information */
1774 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1775 p_pic_data = p_buffer->p_data +
1776 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1777 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1779 render_time = mdate();
1785 * Choose appropriate rendering function and render picture
1787 switch( p_pic->i_type )
1789 case YUV_420_PICTURE:
1790 p_vout->yuv.pf_yuv420( p_vout, p_pic_data,
1791 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1792 p_pic->i_width, p_pic->i_height,
1793 p_buffer->i_pic_width, p_buffer->i_pic_height,
1794 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1795 p_pic->i_matrix_coefficients );
1797 case YUV_422_PICTURE:
1798 p_vout->yuv.pf_yuv422( p_vout, p_pic_data,
1799 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1800 p_pic->i_width, p_pic->i_height,
1801 p_buffer->i_pic_width, p_buffer->i_pic_height,
1802 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1803 p_pic->i_matrix_coefficients );
1805 case YUV_444_PICTURE:
1806 p_vout->yuv.pf_yuv444( p_vout, p_pic_data,
1807 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1808 p_pic->i_width, p_pic->i_height,
1809 p_buffer->i_pic_width, p_buffer->i_pic_height,
1810 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1811 p_pic->i_matrix_coefficients );
1815 intf_ErrMsg("error: unknown picture type %d", p_pic->i_type );
1821 /* Print picture date and rendering time */
1822 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s", p_pic,
1823 p_vout->i_buffer_index, (long) (mdate() - render_time),
1824 mstrtime( psz_date, p_pic->date ));
1828 /*****************************************************************************
1829 * RenderPictureInfo: print additionnal information on a picture
1830 *****************************************************************************
1831 * This function will print information such as fps and other picture
1832 * dependant information.
1833 *****************************************************************************/
1834 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1836 char psz_buffer[256]; /* string buffer */
1839 * Print FPS rate in upper right corner
1841 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1843 long i_fps = VOUT_FPS_SAMPLES * 1000000 * 10 /
1844 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1)
1845 % VOUT_FPS_SAMPLES ] -
1846 p_vout->p_fps_sample[ p_vout->c_fps_samples
1847 % VOUT_FPS_SAMPLES ] );
1848 sprintf( psz_buffer, "%li.%i fps", i_fps / 10, (int)i_fps % 10 );
1849 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1853 * Print frames count and loop time in upper left corner
1855 sprintf( psz_buffer, "%ld frames, render: %ldus",
1856 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1857 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1861 * Print picture information in lower right corner
1863 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1864 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1865 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1866 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1867 p_pic->i_width, p_pic->i_height,
1868 p_pic->i_display_width, p_pic->i_display_height,
1869 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1870 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1871 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1872 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1873 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1874 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1875 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1876 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1877 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1878 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1882 /*****************************************************************************
1883 * RenderIdle: render idle picture
1884 *****************************************************************************
1885 * This function will print something on the screen. It will return 0 if
1886 * nothing has been rendered, or 1 if something has been changed on the screen.
1887 * Note that if you absolutely want something to be printed, you will have
1888 * to force it by setting the last idle date to 0.
1889 * Unlike other rendering functions, this one calls the SetBufferPicture
1890 * function when needed.
1891 *****************************************************************************/
1892 int RenderIdle( vout_thread_t *p_vout )
1895 int i_x = 0, i_y = 0; /* text position */
1896 int i_width, i_height; /* text size */
1897 int i_amount = 0; /* amount to draw */
1898 char *psz_text = "Waiting for stream"; /* text to display */
1899 char *psz_wtext = "[................]";
1901 mtime_t current_date; /* current date */
1904 memset( p_vout->p_buffer[ p_vout->i_buffer_index ].p_data,
1905 p_vout->i_bytes_per_line * p_vout->i_height, 12);
1908 current_date = mdate();
1909 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY
1910 // && (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY
1913 /* FIXME: idle screen disabled */
1915 SetBufferPicture( p_vout, NULL );
1916 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1917 &i_width, &i_height );
1918 if( !Align( p_vout, &i_x, &i_y, i_width, i_height * 2, CENTER_RALIGN, CENTER_RALIGN ) )
1920 i_amount = (int) ((current_date - p_vout->last_display_date ) / 5000LL);
1921 vout_Print( p_vout->p_large_font,
1922 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1923 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1924 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1925 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1926 WIDE_TEXT | OUTLINED_TEXT, psz_text, (i_amount / 3 ) %110);
1928 vout_Print( p_vout->p_large_font,
1929 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1930 i_x * p_vout->i_bytes_per_pixel + (i_y + 16) * p_vout->i_bytes_per_line,
1931 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1932 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1933 WIDE_TEXT | OUTLINED_TEXT, psz_wtext, (i_amount/5)%110 );
1936 SetBufferArea( p_vout, i_x, i_y, i_width, i_height * 2 );
1944 /*****************************************************************************
1945 * RenderInfo: render additionnal information
1946 *****************************************************************************
1947 * This function renders information which do not depend on the current
1949 *****************************************************************************/
1950 static void RenderInfo( vout_thread_t *p_vout )
1953 char psz_buffer[256]; /* string buffer */
1954 int i_ready_pic = 0; /* ready pictures */
1955 int i_reserved_pic = 0; /* reserved pictures */
1956 int i_picture; /* picture index */
1961 * Print thread state in lower left corner
1963 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1965 switch( p_vout->p_picture[i_picture].i_status )
1967 case RESERVED_PICTURE:
1968 case RESERVED_DATED_PICTURE:
1969 case RESERVED_DISP_PICTURE:
1977 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1978 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1979 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1983 /*****************************************************************************
1984 * RenderSubPicture: render a subpicture
1985 *****************************************************************************
1986 * This function renders a sub picture unit.
1987 *****************************************************************************/
1988 static void RenderSubPicture( vout_thread_t *p_vout, picture_t *p_pic,
1989 subpicture_t *p_subpic )
1991 p_vout_font_t p_font; /* text font */
1992 int i_width, i_height; /* subpicture dimensions */
1994 while( p_subpic != NULL )
1996 switch( p_subpic->i_type )
1998 case DVD_SUBPICTURE: /* DVD subpicture unit */
1999 vout_RenderRGBSPU( p_pic, p_subpic,
2000 &p_vout->p_buffer[ p_vout->i_buffer_index ],
2001 p_vout->i_bytes_per_pixel,
2002 p_vout->i_bytes_per_line );
2003 /* vout_RenderYUVSPU( p_pic, p_subpic ); */
2006 case TEXT_SUBPICTURE: /* single line text */
2007 /* Select default font if not specified */
2008 p_font = p_subpic->type.text.p_font;
2009 if( p_font == NULL )
2011 p_font = p_vout->p_default_font;
2014 /* Compute text size (width and height fields are ignored)
2016 vout_TextSize( p_font, p_subpic->type.text.i_style,
2017 p_subpic->p_data, &i_width, &i_height );
2018 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y,
2019 i_width, i_height, p_subpic->i_horizontal_align,
2020 p_subpic->i_vertical_align ) )
2023 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
2024 p_subpic->i_x * p_vout->i_bytes_per_pixel +
2025 p_subpic->i_y * p_vout->i_bytes_per_line,
2026 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
2027 p_subpic->type.text.i_char_color,
2028 p_subpic->type.text.i_border_color,
2029 p_subpic->type.text.i_bg_color,
2030 p_subpic->type.text.i_style, p_subpic->p_data, 100 );
2031 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y,
2032 i_width, i_height );
2038 intf_ErrMsg( "error: unknown subpicture %p type %d",
2039 p_subpic, p_subpic->i_type );
2044 p_subpic = p_subpic->p_next;
2048 /*****************************************************************************
2049 * Manage: manage thread
2050 *****************************************************************************
2051 * This function will handle changes in thread configuration.
2052 *****************************************************************************/
2053 static int Manage( vout_thread_t *p_vout )
2056 if( p_vout->i_changes )
2058 intf_DbgMsg("changes: 0x%x (no display: 0x%x)", p_vout->i_changes,
2059 0 /* p_vout->i_changes & VOUT_NODISPLAY_CHANGE */ );
2063 /* On gamma or grayscale change, rebuild tables */
2064 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2067 if( vout_ResetYUV( p_vout ) )
2069 intf_ErrMsg( "vout error: can't rebuild conversion tables" );
2074 /* Clear changes flags which does not need management or have been
2076 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2077 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
2078 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE |
2079 VOUT_CURSOR_CHANGE | VOUT_FULLSCREEN_CHANGE );
2081 /* Detect unauthorized changes */
2082 if( p_vout->i_changes )
2084 /* Some changes were not acknowledged by p_vout->pf_manage or this
2085 * function, it means they should not be authorized */
2086 intf_ErrMsg( "vout error: unauthorized changes in the vout thread" );
2093 /*****************************************************************************
2094 * Align: align a subpicture in the screen
2095 *****************************************************************************
2096 * This function is used for rendering text or subpictures. It returns non 0
2097 * it the final aera is not fully included in display area. Return coordinates
2099 *****************************************************************************/
2100 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2101 int i_width, int i_height, int i_h_align, int i_v_align )
2103 /* Align horizontally */
2107 *pi_x -= i_width / 2;
2110 *pi_x += (p_vout->i_width - i_width) / 2;
2116 *pi_x += p_vout->i_width - i_width;
2120 /* Align vertically */
2124 *pi_y -= i_height / 2;
2127 *pi_y += (p_vout->i_height - i_height) / 2;
2133 *pi_y += p_vout->i_height - i_height;
2135 case SUBTITLE_RALIGN:
2136 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2137 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2141 /* Return non 0 if clipping failed */
2142 return( (*pi_x < 0) || (*pi_y < 0) ||
2143 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2146 /*****************************************************************************
2147 * SetPalette: sets an 8 bpp palette
2148 *****************************************************************************
2149 * This function is just a prototype that does nothing. Architectures that
2150 * support palette allocation should override it.
2151 *****************************************************************************/
2152 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2153 u16 *green, u16 *blue, u16 *transp )
2155 intf_ErrMsg( "vout error: method does not support palette changing" );