1 /*****************************************************************************
2 * video_output.c : video output thread
3 * This module describes the programming interface for video output threads.
4 * It includes functions allowing to open a new thread, send pictures to a
5 * thread, and destroy a previously oppened video output thread.
6 *****************************************************************************
7 * Copyright (C) 2000 VideoLAN
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <errno.h> /* ENOMEM */
32 #include <stdlib.h> /* free() */
33 #include <stdio.h> /* sprintf() */
34 #include <string.h> /* strerror() */
42 #include "video_output.h"
43 #include "video_text.h"
44 #include "video_spu.h"
45 #include "video_yuv.h"
50 #include "vdec_idct.h"
51 #include "video_decoder.h"
53 /*****************************************************************************
55 *****************************************************************************/
56 static int BinaryLog ( u32 i );
57 static void MaskToShift ( int *pi_left, int *pi_right, u32 i_mask );
58 static int InitThread ( vout_thread_t *p_vout );
59 static void RunThread ( vout_thread_t *p_vout );
60 static void ErrorThread ( vout_thread_t *p_vout );
61 static void EndThread ( vout_thread_t *p_vout );
62 static void DestroyThread ( vout_thread_t *p_vout, int i_status );
63 static void Print ( vout_thread_t *p_vout, int i_x, int i_y,
64 int i_h_align, int i_v_align,
65 unsigned char *psz_text );
66 static void SetBufferArea ( vout_thread_t *p_vout, int i_x, int i_y,
68 static void SetBufferPicture ( vout_thread_t *p_vout, picture_t *p_pic );
69 static void RenderPicture ( vout_thread_t *p_vout, picture_t *p_pic );
70 static void RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
71 static void RenderSubPicture ( vout_thread_t *p_vout,
72 subpicture_t *p_subpic );
73 static void RenderInterface ( vout_thread_t *p_vout );
74 static int RenderIdle ( vout_thread_t *p_vout );
75 static int RenderSplash ( vout_thread_t *p_vout );
76 static void RenderInfo ( vout_thread_t *p_vout );
77 static int Manage ( vout_thread_t *p_vout );
78 static int Align ( vout_thread_t *p_vout, int *pi_x,
79 int *pi_y, int i_width, int i_height,
80 int i_h_align, int i_v_align );
81 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
82 u16 *green, u16 *blue, u16 *transp );
84 /*****************************************************************************
85 * vout_CreateThread: creates a new video output thread
86 *****************************************************************************
87 * This function creates a new video output thread, and returns a pointer
88 * to its description. On error, it returns NULL.
89 * If pi_status is NULL, then the function will block until the thread is ready.
90 * If not, it will be updated using one of the THREAD_* constants.
91 *****************************************************************************/
92 vout_thread_t * vout_CreateThread ( char *psz_display, int i_root_window,
93 int i_width, int i_height, int *pi_status,
94 int i_method, void *p_data )
96 vout_thread_t * p_vout; /* thread descriptor */
97 typedef void ( vout_getplugin_t ) ( vout_thread_t * p_vout );
98 int i_status; /* thread status */
99 int i_index; /* index for array initialization */
100 int i_best_index = 0, i_best_score = 0;
102 /* Allocate descriptor */
104 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
107 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
111 p_vout->p_set_palette = SetPalette;
113 /* Get a suitable video plugin */
114 for( i_index = 0 ; i_index < p_main->p_bank->i_plugin_count ; i_index++ )
116 /* If there's a plugin in p_info ... */
117 if( p_main->p_bank->p_info[ i_index ] != NULL )
119 /* ... and if this plugin provides the functions we want ... */
120 if( p_main->p_bank->p_info[ i_index ]->vout_GetPlugin != NULL )
122 /* ... and if this plugin has a good score ... */
123 if( p_main->p_bank->p_info[ i_index ]->i_score > i_best_score )
125 /* ... then take it */
126 i_best_score = p_main->p_bank->p_info[ i_index ]->i_score;
127 i_best_index = i_index;
133 if( i_best_score == 0 )
139 /* Get the plugin functions */
140 ( (vout_getplugin_t *)
141 p_main->p_bank->p_info[ i_best_index ]->vout_GetPlugin )( p_vout );
143 /* Initialize thread properties - thread id and locks will be initialized
147 p_vout->b_active = 0;
148 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
149 *p_vout->pi_status = THREAD_CREATE;
151 /* Initialize some fields used by the system-dependant method - these
152 * fields will probably be modified by the method, and are only
154 p_vout->i_changes = 0;
155 p_vout->i_width = i_width;
156 p_vout->i_height = i_height;
157 p_vout->i_bytes_per_line = i_width * 2;
158 p_vout->i_screen_depth = 15;
159 p_vout->i_bytes_per_pixel = 2;
160 p_vout->f_gamma = VOUT_GAMMA;
162 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
163 VOUT_GRAYSCALE_DEFAULT );
164 p_vout->vdec_DecodeMacroblock = vdec_DecodeMacroblockC;
166 p_vout->b_interface = 0;
169 intf_DbgMsg( "wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
170 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
171 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
173 /* Initialize idle screen */
174 p_vout->last_display_date = 0;
175 p_vout->last_idle_date = 0;
176 p_vout->init_display_date = mdate();
177 p_vout->render_time = 10000;
180 /* Initialize statistics fields */
181 p_vout->c_fps_samples = 0;
184 /* Initialize buffer index */
185 p_vout->i_buffer_index = 0;
187 /* Initialize pictures and subpictures - translation tables and functions
188 * will be initialized later in InitThread */
189 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
191 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
192 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
194 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
196 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
197 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
199 p_vout->i_pictures = 0;
201 /* Create and initialize system-dependant method - this function issues its
202 * own error messages */
203 if( p_vout->p_sys_create( p_vout, psz_display, i_root_window, p_data ) )
208 intf_DbgMsg( "actual configuration: %dx%d, %d/%d bpp (%d Bpl), "
209 "masks: 0x%x/0x%x/0x%x\n",
210 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
211 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
212 p_vout->i_red_mask, p_vout->i_green_mask,
213 p_vout->i_blue_mask );
215 /* Calculate shifts from system-updated masks */
216 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift,
217 p_vout->i_red_mask );
218 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift,
219 p_vout->i_green_mask );
220 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift,
221 p_vout->i_blue_mask );
223 /* Set some useful colors */
224 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
225 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
226 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
227 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
229 /* Load fonts - fonts must be initialized after the system method since
230 * they may be dependant on screen depth and other thread properties */
231 p_vout->p_default_font = vout_LoadFont( DATA_PATH "/" VOUT_DEFAULT_FONT );
232 if( p_vout->p_default_font == NULL )
234 p_vout->p_default_font = vout_LoadFont( "share/" VOUT_DEFAULT_FONT );
236 if( p_vout->p_default_font == NULL )
238 intf_ErrMsg( "vout error: could not load default font\n" );
239 p_vout->p_sys_destroy( p_vout );
243 p_vout->p_large_font = vout_LoadFont( DATA_PATH "/" VOUT_LARGE_FONT );
244 if( p_vout->p_large_font == NULL )
246 p_vout->p_large_font = vout_LoadFont( "share/" VOUT_LARGE_FONT );
248 if( p_vout->p_large_font == NULL )
250 intf_ErrMsg( "vout error: could not load large font\n" );
251 vout_UnloadFont( p_vout->p_default_font );
252 p_vout->p_sys_destroy( p_vout );
257 /* Create thread and set locks */
258 vlc_mutex_init( &p_vout->picture_lock );
259 vlc_mutex_init( &p_vout->subpicture_lock );
260 vlc_mutex_init( &p_vout->change_lock );
261 vlc_mutex_lock( &p_vout->change_lock );
262 if( vlc_thread_create( &p_vout->thread_id, "video output",
263 (void *) RunThread, (void *) p_vout) )
265 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
266 vout_UnloadFont( p_vout->p_default_font );
267 vout_UnloadFont( p_vout->p_large_font );
268 p_vout->p_sys_destroy( p_vout );
273 intf_Msg( "Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
274 p_vout->i_height, p_vout->i_screen_depth,
275 p_vout->i_bytes_per_pixel * 8 );
277 /* If status is NULL, wait until the thread is created */
278 if( pi_status == NULL )
282 msleep( THREAD_SLEEP );
283 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
284 && (i_status != THREAD_FATAL) );
285 if( i_status != THREAD_READY )
293 /*****************************************************************************
294 * vout_DestroyThread: destroys a previously created thread
295 *****************************************************************************
296 * Destroy a terminated thread.
297 * The function will request a destruction of the specified thread. If pi_error
298 * is NULL, it will return once the thread is destroyed. Else, it will be
299 * update using one of the THREAD_* constants.
300 *****************************************************************************/
301 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
303 int i_status; /* thread status */
307 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
308 *p_vout->pi_status = THREAD_DESTROY;
310 /* Request thread destruction */
313 /* If status is NULL, wait until thread has been destroyed */
314 if( pi_status == NULL )
318 msleep( THREAD_SLEEP );
319 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
320 && (i_status != THREAD_FATAL) );
324 /*****************************************************************************
325 * vout_DisplaySubPicture: display a subpicture unit
326 *****************************************************************************
327 * Remove the reservation flag of an subpicture, which will cause it to be ready
328 * for display. The picture does not need to be locked, since it is ignored by
329 * the output thread if is reserved.
330 *****************************************************************************/
331 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
334 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
335 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
339 /* Check if status is valid */
340 if( p_subpic->i_status != RESERVED_SUBPICTURE )
342 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
343 p_subpic->i_status );
347 /* Remove reservation flag */
348 p_subpic->i_status = READY_SUBPICTURE;
351 /* Send subpicture information */
352 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
353 p_subpic, p_subpic->i_type,
354 mstrtime( psz_begin_date, p_subpic->begin_date ),
355 mstrtime( psz_end_date, p_subpic->end_date ) );
359 /*****************************************************************************
360 * vout_CreateSubPicture: allocate a subpicture in the video output heap.
361 *****************************************************************************
362 * This function create a reserved subpicture in the video output heap.
363 * A null pointer is returned if the function fails. This method provides an
364 * already allocated zone of memory in the spu data fields. It needs locking
365 * since several pictures can be created by several producers threads.
366 *****************************************************************************/
367 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
370 int i_subpic; /* subpicture index */
371 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
372 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
375 vlc_mutex_lock( &p_vout->subpicture_lock );
378 * Look for an empty place
380 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
382 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
384 /* Subpicture is marked for destruction, but is still allocated */
385 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
386 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
388 /* Memory size do match or is smaller : memory will not be
389 * reallocated, and function can end immediately - this is
390 * the best possible case, since no memory allocation needs
392 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
394 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
395 &p_vout->p_subpicture[i_subpic] );
397 vlc_mutex_unlock( &p_vout->subpicture_lock );
398 return( &p_vout->p_subpicture[i_subpic] );
400 else if( p_destroyed_subpic == NULL )
402 /* Memory size do not match, but subpicture index will be kept in
403 * case no other place are left */
404 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
407 else if( (p_free_subpic == NULL) &&
408 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
410 /* Subpicture is empty and ready for allocation */
411 p_free_subpic = &p_vout->p_subpicture[i_subpic];
415 /* If no free subpictures are available, use a destroyed subpicture */
416 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
418 /* No free subpicture or matching destroyed subpictures have been
419 * found, but a destroyed subpicture is still avalaible */
420 free( p_destroyed_subpic->p_data );
421 p_free_subpic = p_destroyed_subpic;
427 if( p_free_subpic != NULL )
429 /* Allocate memory */
432 case TEXT_SUBPICTURE: /* text subpicture */
433 p_free_subpic->p_data = malloc( i_size + 1 );
435 case DVD_SUBPICTURE: /* DVD subpicture unit */
436 p_free_subpic->p_data = malloc( i_size );
440 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
441 p_free_subpic->p_data = NULL;
446 if( p_free_subpic->p_data != NULL )
448 /* Copy subpicture information, set some default values */
449 p_free_subpic->i_type = i_type;
450 p_free_subpic->i_status = RESERVED_SUBPICTURE;
451 p_free_subpic->i_size = i_size;
452 p_free_subpic->i_x = 0;
453 p_free_subpic->i_y = 0;
454 p_free_subpic->i_width = 0;
455 p_free_subpic->i_height = 0;
456 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
457 p_free_subpic->i_vertical_align = CENTER_RALIGN;
461 /* Memory allocation failed : set subpicture as empty */
462 p_free_subpic->i_type = EMPTY_SUBPICTURE;
463 p_free_subpic->i_status = FREE_SUBPICTURE;
464 p_free_subpic = NULL;
465 intf_ErrMsg("spu warning: %s\n", strerror( ENOMEM ) );
469 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
471 vlc_mutex_unlock( &p_vout->subpicture_lock );
472 return( p_free_subpic );
475 /* No free or destroyed subpicture could be found */
476 intf_DbgMsg( "warning: subpicture heap is full\n" );
477 vlc_mutex_unlock( &p_vout->subpicture_lock );
481 /*****************************************************************************
482 * vout_DestroySubPicture: remove a subpicture from the heap
483 *****************************************************************************
484 * This function frees a previously reserved subpicture.
485 * It is meant to be used when the construction of a picture aborted.
486 * This function does not need locking since reserved subpictures are ignored
487 * by the output thread.
488 *****************************************************************************/
489 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
492 /* Check if status is valid */
493 if( p_subpic->i_status != RESERVED_SUBPICTURE )
495 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
496 p_subpic, p_subpic->i_status );
500 p_subpic->i_status = DESTROYED_SUBPICTURE;
503 intf_DbgMsg("subpicture %p\n", p_subpic);
507 /*****************************************************************************
508 * vout_DisplayPicture: display a picture
509 *****************************************************************************
510 * Remove the reservation flag of a picture, which will cause it to be ready for
511 * display. The picture won't be displayed until vout_DatePicture has been
513 *****************************************************************************/
514 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
516 vlc_mutex_lock( &p_vout->picture_lock );
517 switch( p_pic->i_status )
519 case RESERVED_PICTURE:
520 p_pic->i_status = RESERVED_DISP_PICTURE;
522 case RESERVED_DATED_PICTURE:
523 p_pic->i_status = READY_PICTURE;
527 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
533 intf_DbgMsg("picture %p\n", p_pic);
535 vlc_mutex_unlock( &p_vout->picture_lock );
538 /*****************************************************************************
539 * vout_DatePicture: date a picture
540 *****************************************************************************
541 * Remove the reservation flag of a picture, which will cause it to be ready for
542 * display. The picture won't be displayed until vout_DisplayPicture has been
544 *****************************************************************************/
545 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
548 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
551 vlc_mutex_lock( &p_vout->picture_lock );
553 switch( p_pic->i_status )
555 case RESERVED_PICTURE:
556 p_pic->i_status = RESERVED_DATED_PICTURE;
558 case RESERVED_DISP_PICTURE:
559 p_pic->i_status = READY_PICTURE;
563 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
569 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
571 vlc_mutex_unlock( &p_vout->picture_lock );
574 /*****************************************************************************
575 * vout_CreatePicture: allocate a picture in the video output heap.
576 *****************************************************************************
577 * This function create a reserved image in the video output heap.
578 * A null pointer is returned if the function fails. This method provides an
579 * already allocated zone of memory in the picture data fields. It needs locking
580 * since several pictures can be created by several producers threads.
581 *****************************************************************************/
582 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
583 int i_width, int i_height )
585 int i_picture; /* picture index */
586 int i_chroma_width = 0; /* chroma width */
587 picture_t * p_free_picture = NULL; /* first free picture */
588 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
591 vlc_mutex_lock( &p_vout->picture_lock );
594 * Look for an empty place
596 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
598 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
600 /* Picture is marked for destruction, but is still allocated - note
601 * that if width and type are the same for two pictures, chroma_width
602 * should also be the same */
603 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
604 (p_vout->p_picture[i_picture].i_height == i_height) &&
605 (p_vout->p_picture[i_picture].i_width == i_width) )
607 /* Memory size do match : memory will not be reallocated, and function
608 * can end immediately - this is the best possible case, since no
609 * memory allocation needs to be done */
610 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
611 p_vout->i_pictures++;
613 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
614 &p_vout->p_picture[i_picture] );
616 vlc_mutex_unlock( &p_vout->picture_lock );
617 return( &p_vout->p_picture[i_picture] );
619 else if( p_destroyed_picture == NULL )
621 /* Memory size do not match, but picture index will be kept in
622 * case no other place are left */
623 p_destroyed_picture = &p_vout->p_picture[i_picture];
626 else if( (p_free_picture == NULL) &&
627 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
629 /* Picture is empty and ready for allocation */
630 p_free_picture = &p_vout->p_picture[i_picture];
634 /* If no free picture is available, use a destroyed picture */
635 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
637 /* No free picture or matching destroyed picture has been found, but
638 * a destroyed picture is still avalaible */
639 free( p_destroyed_picture->p_data );
640 p_free_picture = p_destroyed_picture;
646 if( p_free_picture != NULL )
648 /* Allocate memory */
651 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
652 i_chroma_width = i_width / 2;
653 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
654 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
655 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
656 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
658 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
659 i_chroma_width = i_width / 2;
660 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
661 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
662 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
663 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
665 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
666 i_chroma_width = i_width;
667 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
668 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
669 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
670 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
674 intf_DbgMsg("error: unknown picture type %d\n", i_type );
675 p_free_picture->p_data = NULL;
680 if( p_free_picture->p_data != NULL )
682 /* Copy picture information, set some default values */
683 p_free_picture->i_type = i_type;
684 p_free_picture->i_status = RESERVED_PICTURE;
685 p_free_picture->i_matrix_coefficients = 1;
686 p_free_picture->i_width = i_width;
687 p_free_picture->i_height = i_height;
688 p_free_picture->i_chroma_width = i_chroma_width;
689 p_free_picture->i_display_horizontal_offset = 0;
690 p_free_picture->i_display_vertical_offset = 0;
691 p_free_picture->i_display_width = i_width;
692 p_free_picture->i_display_height = i_height;
693 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
694 p_free_picture->i_refcount = 0;
695 p_vout->i_pictures++;
699 /* Memory allocation failed : set picture as empty */
700 p_free_picture->i_type = EMPTY_PICTURE;
701 p_free_picture->i_status = FREE_PICTURE;
702 p_free_picture = NULL;
703 intf_ErrMsg( "vout warning: %s\n", strerror( ENOMEM ) );
707 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
709 vlc_mutex_unlock( &p_vout->picture_lock );
710 return( p_free_picture );
713 /* No free or destroyed picture could be found */
714 intf_DbgMsg( "warning: picture heap is full\n" );
715 vlc_mutex_unlock( &p_vout->picture_lock );
719 /*****************************************************************************
720 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
721 *****************************************************************************
722 * This function frees a previously reserved picture or a permanent
723 * picture. It is meant to be used when the construction of a picture aborted.
724 * Note that the picture will be destroyed even if it is linked !
725 *****************************************************************************/
726 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
728 vlc_mutex_lock( &p_vout->picture_lock );
731 /* Check if picture status is valid */
732 if( (p_pic->i_status != RESERVED_PICTURE) &&
733 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
734 (p_pic->i_status != RESERVED_DISP_PICTURE) )
736 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
740 p_pic->i_status = DESTROYED_PICTURE;
741 p_vout->i_pictures--;
744 intf_DbgMsg("picture %p\n", p_pic);
746 vlc_mutex_unlock( &p_vout->picture_lock );
749 /*****************************************************************************
750 * vout_LinkPicture: increment reference counter of a picture
751 *****************************************************************************
752 * This function increment the reference counter of a picture in the video
753 * heap. It needs a lock since several producer threads can access the picture.
754 *****************************************************************************/
755 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
757 vlc_mutex_lock( &p_vout->picture_lock );
761 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
764 vlc_mutex_unlock( &p_vout->picture_lock );
767 /*****************************************************************************
768 * vout_UnlinkPicture: decrement reference counter of a picture
769 *****************************************************************************
770 * This function decrement the reference counter of a picture in the video heap.
771 *****************************************************************************/
772 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
774 vlc_mutex_lock( &p_vout->picture_lock );
778 if( p_pic->i_refcount < 0 )
780 intf_DbgMsg("error: refcount < 0\n");
781 p_pic->i_refcount = 0;
785 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
787 p_pic->i_status = DESTROYED_PICTURE;
788 p_vout->i_pictures--;
792 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
795 vlc_mutex_unlock( &p_vout->picture_lock );
798 /*****************************************************************************
799 * vout_SetBuffers: set buffers adresses
800 *****************************************************************************
801 * This function is called by system drivers to set buffers video memory
803 *****************************************************************************/
804 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
806 /* No picture previously */
807 p_vout->p_buffer[0].i_pic_x = 0;
808 p_vout->p_buffer[0].i_pic_y = 0;
809 p_vout->p_buffer[0].i_pic_width = 0;
810 p_vout->p_buffer[0].i_pic_height = 0;
811 p_vout->p_buffer[1].i_pic_x = 0;
812 p_vout->p_buffer[1].i_pic_y = 0;
813 p_vout->p_buffer[1].i_pic_width = 0;
814 p_vout->p_buffer[1].i_pic_height = 0;
816 /* The first area covers all the screen */
817 p_vout->p_buffer[0].i_areas = 1;
818 p_vout->p_buffer[0].pi_area_begin[0] = 0;
819 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
820 p_vout->p_buffer[1].i_areas = 1;
821 p_vout->p_buffer[1].pi_area_begin[0] = 0;
822 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
825 p_vout->p_buffer[0].p_data = p_buf1;
826 p_vout->p_buffer[1].p_data = p_buf2;
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\n");
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 )
916 *p_vout->pi_status = THREAD_START;
918 /* Initialize output method - this function issues its own error messages */
919 if( p_vout->p_sys_init( p_vout ) )
924 /* Initialize convertion tables and functions */
925 if( vout_InitYUV( p_vout ) )
927 intf_ErrMsg("error: can't allocate YUV translation tables\n");
931 /* Mark thread as running and return */
932 p_vout->b_active = 1;
933 *p_vout->pi_status = THREAD_READY;
936 intf_DbgMsg("thread ready\n");
940 /*****************************************************************************
941 * RunThread: video output thread
942 *****************************************************************************
943 * Video output thread. This function does only returns when the thread is
944 * terminated. It handles the pictures arriving in the video heap and the
945 * display device events.
946 *****************************************************************************/
947 static void RunThread( vout_thread_t *p_vout)
949 int i_index; /* index in heap */
950 mtime_t current_date; /* current date */
951 mtime_t display_date; /* display date */
952 boolean_t b_display; /* display flag */
953 picture_t * p_pic; /* picture pointer */
954 subpicture_t * p_subpic; /* subpicture pointer */
959 p_vout->b_error = InitThread( p_vout );
960 if( p_vout->b_error )
962 DestroyThread( p_vout, THREAD_ERROR );
968 * Main loop - it is not executed if an error occured during
971 while( (!p_vout->b_die) && (!p_vout->b_error) )
973 /* Initialize loop variables */
977 current_date = mdate();
980 * Find the picture to display - this operation does not need lock,
981 * since only READY_PICTUREs are handled
983 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
985 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
987 (p_vout->p_picture[i_index].date < display_date) ) )
989 p_pic = &p_vout->p_picture[i_index];
990 display_date = p_pic->date;
997 /* Computes FPS rate */
998 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
1000 if( display_date < current_date )
1002 /* Picture is late: it will be destroyed and the thread
1003 * will sleep and go to next picture */
1005 vlc_mutex_lock( &p_vout->picture_lock );
1006 if( p_pic->i_refcount )
1008 p_pic->i_status = DISPLAYED_PICTURE;
1012 p_pic->i_status = DESTROYED_PICTURE;
1013 p_vout->i_pictures--;
1015 intf_DbgMsg( "warning: late picture skipped (%p)\n", p_pic );
1016 vlc_mutex_unlock( &p_vout->picture_lock );
1020 else if( display_date > current_date + VOUT_DISPLAY_DELAY )
1022 /* A picture is ready to be rendered, but its rendering date
1023 * is far from the current one so the thread will perform an
1024 * empty loop as if no picture were found. The picture state
1031 * Find the subpictures to display - this operation does not need
1032 * lock, since only READY_SUBPICTURE are handled. If no picture
1033 * has been selected, display_date will depend on the subpicture.
1034 * We get an easily parsable chained list of subpictures which
1035 * ends with NULL since p_subpic was initialized to NULL.
1037 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1039 if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
1041 p_vout->p_subpicture[i_index].p_next = p_subpic;
1042 p_subpic = &p_vout->p_subpicture[i_index];
1047 * Perform rendering, sleep and display rendered picture
1049 if( p_pic ) /* picture and perhaps subpicture */
1051 b_display = p_vout->b_active;
1052 p_vout->last_display_date = display_date;
1056 /* Set picture dimensions and clear buffer */
1057 SetBufferPicture( p_vout, p_pic );
1059 /* Render picture and information */
1060 RenderPicture( p_vout, p_pic );
1061 if( p_vout->b_info )
1063 RenderPictureInfo( p_vout, p_pic );
1064 RenderInfo( p_vout );
1068 RenderSubPicture( p_vout, p_subpic );
1072 /* Remove picture from heap */
1073 vlc_mutex_lock( &p_vout->picture_lock );
1074 if( p_pic->i_refcount )
1076 p_pic->i_status = DISPLAYED_PICTURE;
1080 p_pic->i_status = DESTROYED_PICTURE;
1081 p_vout->i_pictures--;
1083 vlc_mutex_unlock( &p_vout->picture_lock );
1085 /* Render interface and subpicture */
1086 if( b_display && p_vout->b_interface )
1088 RenderInterface( p_vout );
1092 else if( p_vout->b_active && p_vout->init_display_date == 0) /* idle or interface screen alone */
1094 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1096 /* Interface has changed, so a new rendering is required - force
1097 * it by setting last idle date to 0 */
1098 p_vout->last_idle_date = 0;
1101 /* Render idle screen and update idle date, then render interface if
1103 b_display = RenderIdle( p_vout );
1106 p_vout->last_idle_date = current_date;
1107 if( p_vout->b_interface )
1109 RenderInterface( p_vout );
1121 * chech for the current time and
1122 * display splash screen if everything is on time
1124 if( p_vout->init_display_date > 0)
1126 if( p_vout->b_active &&
1127 mdate()-p_vout->init_display_date < 5000000)
1129 /* there is something to display ! */
1131 RenderSplash( p_vout );
1134 /* no splash screen ! */
1135 intf_ErrMsgImm("End of splash screen\n");
1136 p_vout->init_display_date=0;
1144 * Sleep, wake up and display rendered picture
1147 if( display_date != 0 )
1149 /* Store render time */
1150 p_vout->render_time += mdate() - current_date;
1151 p_vout->render_time >>= 1;
1154 /* Give back change lock */
1155 vlc_mutex_unlock( &p_vout->change_lock );
1157 /* Sleep a while or until a given date */
1158 if( display_date != 0 )
1160 mwait( display_date - VOUT_MWAIT_TOLERANCE );
1164 msleep( VOUT_IDLE_SLEEP );
1167 /* On awakening, take back lock and send immediately picture to display,
1168 * then swap buffers */
1169 vlc_mutex_lock( &p_vout->change_lock );
1171 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1172 p_vout->i_buffer_index, b_display /* && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) */ );
1174 if( b_display /* && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) */ )
1176 p_vout->p_sys_display( p_vout );
1178 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1183 * Check events and manage thread
1185 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1187 /* A fatal error occured, and the thread must terminate immediately,
1188 * without displaying anything - setting b_error to 1 cause the
1189 * immediate end of the main while() loop. */
1190 p_vout->b_error = 1;
1195 * Error loop - wait until the thread destruction is requested
1197 if( p_vout->b_error )
1199 ErrorThread( p_vout );
1203 EndThread( p_vout );
1204 DestroyThread( p_vout, THREAD_OVER );
1205 intf_DbgMsg( "thread end\n" );
1208 /*****************************************************************************
1209 * ErrorThread: RunThread() error loop
1210 *****************************************************************************
1211 * This function is called when an error occured during thread main's loop. The
1212 * thread can still receive feed, but must be ready to terminate as soon as
1214 *****************************************************************************/
1215 static void ErrorThread( vout_thread_t *p_vout )
1217 /* Wait until a `die' order */
1219 while( !p_vout->b_die )
1222 msleep( VOUT_IDLE_SLEEP );
1226 /*****************************************************************************
1227 * EndThread: thread destruction
1228 *****************************************************************************
1229 * This function is called when the thread ends after a sucessful
1230 * initialization. It frees all ressources allocated by InitThread.
1231 *****************************************************************************/
1232 static void EndThread( vout_thread_t *p_vout )
1234 int i_index; /* index in heap */
1238 *p_vout->pi_status = THREAD_END;
1240 /* Destroy all remaining pictures and subpictures */
1241 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1243 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1245 free( p_vout->p_picture[i_index].p_data );
1248 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1250 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1252 free( p_vout->p_subpicture[i_index].p_data );
1256 /* Destroy translation tables */
1257 vout_EndYUV( p_vout );
1258 p_vout->p_sys_end( p_vout );
1261 /*****************************************************************************
1262 * DestroyThread: thread destruction
1263 *****************************************************************************
1264 * This function is called when the thread ends. It frees all ressources
1265 * allocated by CreateThread. Status is available at this stage.
1266 *****************************************************************************/
1267 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1269 int *pi_status; /* status adress */
1271 /* Store status adress */
1273 pi_status = p_vout->pi_status;
1275 /* Destroy thread structures allocated by Create and InitThread */
1276 vout_UnloadFont( p_vout->p_default_font );
1277 vout_UnloadFont( p_vout->p_large_font );
1278 p_vout->p_sys_destroy( p_vout );
1280 /* Free structure */
1282 *pi_status = i_status;
1285 /*****************************************************************************
1286 * Print: print simple text on a picture
1287 *****************************************************************************
1288 * This function will print a simple text on the picture. It is designed to
1289 * print debugging or general information.
1290 *****************************************************************************/
1291 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 )
1293 int i_text_height; /* total text height */
1294 int i_text_width; /* total text width */
1296 /* Update upper left coordinates according to alignment */
1297 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1298 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1300 /* Set area and print text */
1301 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1302 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1303 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1304 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1305 p_vout->i_white_pixel, 0, 0,
1310 /*****************************************************************************
1311 * SetBufferArea: activate an area in current buffer
1312 *****************************************************************************
1313 * This function is called when something is rendered on the current buffer.
1314 * It set the area as active and prepare it to be cleared on next rendering.
1315 * Pay attention to the fact that in this functions, i_h is in fact the end y
1316 * coordinate of the new area.
1317 *****************************************************************************/
1318 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1320 vout_buffer_t * p_buffer; /* current buffer */
1321 int i_area_begin, i_area_end; /* area vertical extension */
1322 int i_area, i_area_copy; /* area index */
1323 int i_area_shift; /* shift distance for areas */
1325 /* Choose buffer and modify h to end of area position */
1326 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1330 * Remove part of the area which is inside the picture - this is done
1331 * by calling again SetBufferArea with the correct areas dimensions.
1333 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1335 i_area_begin = p_buffer->i_pic_y;
1336 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1338 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1339 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1340 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1342 /* Keep the stripe above the picture, if any */
1343 if( i_y < i_area_begin )
1345 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1347 /* Keep the stripe below the picture, if any */
1348 if( i_h > i_area_end )
1350 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1356 /* Skip some extensions until interesting areas */
1358 (i_area < p_buffer->i_areas) &&
1359 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1365 if( i_area == p_buffer->i_areas )
1367 /* New area is below all existing ones: just add it at the end of the
1368 * array, if possible - otherwise, append it to the last one */
1369 if( i_area < VOUT_MAX_AREAS )
1371 p_buffer->pi_area_begin[i_area] = i_y;
1372 p_buffer->pi_area_end[i_area] = i_h;
1373 p_buffer->i_areas++;
1378 intf_DbgMsg("area overflow\n");
1380 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1385 i_area_begin = p_buffer->pi_area_begin[i_area];
1386 i_area_end = p_buffer->pi_area_end[i_area];
1388 if( i_y < i_area_begin )
1390 if( i_h >= i_area_begin - 1 )
1392 /* Extend area above */
1393 p_buffer->pi_area_begin[i_area] = i_y;
1397 /* Create a new area above : merge last area if overflow, then
1398 * move all old areas down */
1399 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1402 intf_DbgMsg("areas overflow\n");
1404 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1408 p_buffer->i_areas++;
1410 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1412 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1413 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1415 p_buffer->pi_area_begin[i_area] = i_y;
1416 p_buffer->pi_area_end[i_area] = i_h;
1420 if( i_h > i_area_end )
1422 /* Find further areas which can be merged with the new one */
1423 for( i_area_copy = i_area + 1;
1424 (i_area_copy < p_buffer->i_areas) &&
1425 (p_buffer->pi_area_begin[i_area] <= i_h);
1432 if( i_area_copy != i_area )
1434 /* Merge with last possible areas */
1435 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1437 /* Shift lower areas upward */
1438 i_area_shift = i_area_copy - i_area;
1439 p_buffer->i_areas -= i_area_shift;
1440 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1442 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1443 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1448 /* Extend area below */
1449 p_buffer->pi_area_end[i_area] = i_h;
1455 /*****************************************************************************
1456 * SetBufferPicture: clear buffer and set picture area
1457 *****************************************************************************
1458 * This function is called before any rendering. It clears the current
1459 * rendering buffer and set the new picture area. If the picture pointer is
1460 * NULL, then no picture area is defined. Floating operations are avoided since
1461 * some MMX calculations may follow.
1462 *****************************************************************************/
1463 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1465 vout_buffer_t * p_buffer; /* current buffer */
1466 int i_pic_x, i_pic_y; /* picture position */
1467 int i_pic_width, i_pic_height; /* picture dimensions */
1468 int i_old_pic_y, i_old_pic_height; /* old picture area */
1469 int i_vout_width, i_vout_height; /* display dimensions */
1470 int i_area; /* area index */
1471 int i_data_index; /* area data index */
1472 int i_data_size; /* area data size, in 256 bytes blocs */
1473 u64 * p_data; /* area data, for clearing */
1474 byte_t * p_data8; /* area data, for clearing (slow) */
1476 /* Choose buffer and set display dimensions */
1477 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1478 i_vout_width = p_vout->i_width;
1479 i_vout_height = p_vout->i_height;
1482 * Computes new picture size
1486 /* Try horizontal scaling first - width must be a mutiple of 16 */
1487 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1488 i_vout_width : p_pic->i_width) & ~0xf;
1489 switch( p_pic->i_aspect_ratio )
1491 case AR_3_4_PICTURE:
1492 i_pic_height = i_pic_width * 3 / 4;
1494 case AR_16_9_PICTURE:
1495 i_pic_height = i_pic_width * 9 / 16;
1497 case AR_221_1_PICTURE:
1498 i_pic_height = i_pic_width * 100 / 221;
1500 case AR_SQUARE_PICTURE:
1502 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1506 /* If picture dimensions using horizontal scaling are too large, use
1507 * vertical scaling. Since width must be a multiple of 16, height is
1508 * adjusted again after. */
1509 if( i_pic_height > i_vout_height )
1511 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1512 i_vout_height : p_pic->i_height;
1513 switch( p_pic->i_aspect_ratio )
1515 case AR_3_4_PICTURE:
1516 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1517 i_pic_height = i_pic_width * 3 / 4;
1519 case AR_16_9_PICTURE:
1520 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1521 i_pic_height = i_pic_width * 9 / 16;
1523 case AR_221_1_PICTURE:
1524 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1525 i_pic_height = i_pic_width * 100 / 221;
1527 case AR_SQUARE_PICTURE:
1529 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1530 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1535 /* Set picture position */
1536 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1537 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1541 /* No picture: size is 0 */
1549 * Set new picture size - if it is smaller than the previous one, clear
1550 * around it. Since picture are centered, only their size is tested.
1552 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1554 i_old_pic_y = p_buffer->i_pic_y;
1555 i_old_pic_height = p_buffer->i_pic_height;
1556 p_buffer->i_pic_x = i_pic_x;
1557 p_buffer->i_pic_y = i_pic_y;
1558 p_buffer->i_pic_width = i_pic_width;
1559 p_buffer->i_pic_height = i_pic_height;
1560 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1564 p_buffer->i_pic_x = i_pic_x;
1565 p_buffer->i_pic_y = i_pic_y;
1566 p_buffer->i_pic_width = i_pic_width;
1567 p_buffer->i_pic_height = i_pic_height;
1573 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1576 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1577 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1579 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1580 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1581 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1583 /* Clear 256 bytes block */
1584 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1585 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1586 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1587 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1588 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1589 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1590 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1591 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1593 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1595 /* Clear remaining 16 bytes blocks */
1596 *p_data++ = 0; *p_data++ = 0;
1598 p_data8 = (byte_t *)p_data;
1599 for( i_data_index = i_data_size % 16; i_data_index--; )
1601 /* Clear remaining bytes */
1609 p_buffer->i_areas = 0;
1612 /*****************************************************************************
1613 * RenderPicture: render a picture
1614 *****************************************************************************
1615 * This function converts a picture from a video heap to a pixel-encoded image
1616 * and copies it to the current rendering buffer. No lock is required, since
1617 * the * rendered picture has been determined as existant, and will only be
1618 * destroyed by the vout thread later.
1619 *****************************************************************************/
1620 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1623 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1624 mtime_t render_time; /* picture rendering time */
1626 vout_buffer_t * p_buffer; /* rendering buffer */
1627 byte_t * p_pic_data; /* convertion destination */
1629 /* Get and set rendering information */
1630 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1631 p_pic_data = p_buffer->p_data +
1632 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1633 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1635 render_time = mdate();
1639 * Choose appropriate rendering function and render picture
1641 switch( p_pic->i_type )
1643 case YUV_420_PICTURE:
1644 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1645 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1646 p_pic->i_width, p_pic->i_height,
1647 p_buffer->i_pic_width, p_buffer->i_pic_height,
1648 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1649 p_pic->i_matrix_coefficients );
1651 case YUV_422_PICTURE:
1652 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1653 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1654 p_pic->i_width, p_pic->i_height,
1655 p_buffer->i_pic_width, p_buffer->i_pic_height,
1656 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1657 p_pic->i_matrix_coefficients );
1659 case YUV_444_PICTURE:
1660 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1661 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1662 p_pic->i_width, p_pic->i_height,
1663 p_buffer->i_pic_width, p_buffer->i_pic_height,
1664 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1665 p_pic->i_matrix_coefficients );
1669 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1675 /* Print picture date and rendering time */
1676 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1677 p_vout->i_buffer_index, (long) (mdate() - render_time),
1678 mstrtime( psz_date, p_pic->date ));
1682 /*****************************************************************************
1683 * RenderPictureInfo: print additionnal information on a picture
1684 *****************************************************************************
1685 * This function will print information such as fps and other picture
1686 * dependant information.
1687 *****************************************************************************/
1688 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1690 #if defined(STATS) || defined(DEBUG)
1691 char psz_buffer[256]; /* string buffer */
1696 * Print FPS rate in upper right corner
1698 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1700 long i_fps = VOUT_FPS_SAMPLES * 1000000 * 10 /
1701 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1)
1702 % VOUT_FPS_SAMPLES ] -
1703 p_vout->p_fps_sample[ p_vout->c_fps_samples
1704 % VOUT_FPS_SAMPLES ] );
1705 sprintf( psz_buffer, "%li.%i fps", i_fps / 10, (int)i_fps % 10 );
1706 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1710 * Print frames count and loop time in upper left corner
1712 sprintf( psz_buffer, "%ld frames, render: %ldus",
1713 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1714 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1719 * Print picture information in lower right corner
1721 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1722 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1723 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1724 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1725 p_pic->i_width, p_pic->i_height,
1726 p_pic->i_display_width, p_pic->i_display_height,
1727 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1728 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1729 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1730 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1731 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1732 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1733 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1734 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1735 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1736 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1740 /*****************************************************************************
1741 * RenderSplash: render splash picture
1742 *****************************************************************************
1743 * This function will print something on the screen. It will return 0 if
1744 * nothing has been rendered, or 1 if something has been changed on the screen.
1745 * Note that if you absolutely want something to be printed, you will have
1746 * to force it by setting the last idle date to 0.
1747 * Unlike other rendering functions, this one calls the SetBufferPicture
1748 * function when needed.
1749 *****************************************************************************/
1750 int RenderSplash( vout_thread_t *p_vout )
1752 int i_x = 0, i_y = 0; /* text position */
1753 int i_width, i_height; /* text size */
1754 char *psz_text = "VideoLAN Client (" VERSION ")"; /* text to display */
1756 memset( p_vout->p_buffer[ p_vout->i_buffer_index ].p_data,
1757 p_vout->i_bytes_per_line * p_vout->i_height, 12);
1759 // SetBufferPicture( p_vout, NULL );
1760 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1761 &i_width, &i_height );
1762 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1764 vout_Print( p_vout->p_large_font,
1765 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1766 i_x * p_vout->i_bytes_per_pixel + (i_y - 16 ) * p_vout->i_bytes_per_line,
1767 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1768 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1769 WIDE_TEXT | OUTLINED_TEXT, psz_text, 100);
1770 SetBufferArea( p_vout, i_x, i_y, i_width, i_height);
1776 /*****************************************************************************
1777 * RenderIdle: render idle picture
1778 *****************************************************************************
1779 * This function will print something on the screen. It will return 0 if
1780 * nothing has been rendered, or 1 if something has been changed on the screen.
1781 * Note that if you absolutely want something to be printed, you will have
1782 * to force it by setting the last idle date to 0.
1783 * Unlike other rendering functions, this one calls the SetBufferPicture
1784 * function when needed.
1785 *****************************************************************************/
1786 int RenderIdle( vout_thread_t *p_vout )
1788 int i_x = 0, i_y = 0; /* text position */
1789 int i_width, i_height; /* text size */
1790 mtime_t current_date; /* current date */
1791 int i_amount = 0; /* amount to draw */
1792 char *psz_text = "Waiting for stream"; /* text to display */
1793 char *psz_wtext = "[................]";
1796 memset( p_vout->p_buffer[ p_vout->i_buffer_index ].p_data,
1797 p_vout->i_bytes_per_line * p_vout->i_height, 12);
1800 current_date = mdate();
1801 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY
1802 // && (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY
1805 SetBufferPicture( p_vout, NULL );
1806 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1807 &i_width, &i_height );
1808 if( !Align( p_vout, &i_x, &i_y, i_width, i_height * 2, CENTER_RALIGN, CENTER_RALIGN ) )
1810 i_amount = (int) ((current_date - p_vout->last_display_date ) / 5000LL);
1811 vout_Print( p_vout->p_large_font,
1812 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1813 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1814 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1815 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1816 WIDE_TEXT | OUTLINED_TEXT, psz_text, (i_amount / 3 ) %110);
1818 vout_Print( p_vout->p_large_font,
1819 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1820 i_x * p_vout->i_bytes_per_pixel + (i_y + 16) * p_vout->i_bytes_per_line,
1821 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1822 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1823 WIDE_TEXT | OUTLINED_TEXT, psz_wtext, (i_amount/5)%110 );
1826 SetBufferArea( p_vout, i_x, i_y, i_width, i_height * 2 );
1833 /*****************************************************************************
1834 * RenderInfo: render additionnal information
1835 *****************************************************************************
1836 * This function renders information which do not depend on the current
1838 *****************************************************************************/
1839 static void RenderInfo( vout_thread_t *p_vout )
1842 char psz_buffer[256]; /* string buffer */
1843 int i_ready_pic = 0; /* ready pictures */
1844 int i_reserved_pic = 0; /* reserved pictures */
1845 int i_picture; /* picture index */
1850 * Print thread state in lower left corner
1852 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1854 switch( p_vout->p_picture[i_picture].i_status )
1856 case RESERVED_PICTURE:
1857 case RESERVED_DATED_PICTURE:
1858 case RESERVED_DISP_PICTURE:
1866 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1867 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1868 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1872 /*****************************************************************************
1873 * RenderSubPicture: render a subpicture
1874 *****************************************************************************
1875 * This function renders a sub picture unit.
1876 *****************************************************************************/
1877 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1879 p_vout_font_t p_font; /* text font */
1880 int i_width, i_height; /* subpicture dimensions */
1882 while( p_subpic != NULL )
1884 switch( p_subpic->i_type )
1886 case DVD_SUBPICTURE: /* DVD subpicture unit */
1887 /* test if the picture really has to be displayed */
1888 if( mdate() < p_subpic->begin_date )
1890 /* not yet, see you later */
1893 if( mdate() > p_subpic->end_date )
1895 /* too late, destroying the subpic */
1896 vout_DestroySubPicture( p_vout, p_subpic );
1899 vout_RenderSPU( &p_vout->p_buffer[ p_vout->i_buffer_index ],
1900 p_subpic, p_vout->i_bytes_per_pixel,
1901 p_vout->i_bytes_per_line );
1903 case TEXT_SUBPICTURE: /* single line text */
1904 /* Select default font if not specified */
1905 p_font = p_subpic->type.text.p_font;
1906 if( p_font == NULL )
1908 p_font = p_vout->p_default_font;
1911 /* Compute text size (width and height fields are ignored)
1913 vout_TextSize( p_font, p_subpic->type.text.i_style,
1914 p_subpic->p_data, &i_width, &i_height );
1915 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y,
1916 i_width, i_height, p_subpic->i_horizontal_align,
1917 p_subpic->i_vertical_align ) )
1920 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1921 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1922 p_subpic->i_y * p_vout->i_bytes_per_line,
1923 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1924 p_subpic->type.text.i_char_color,
1925 p_subpic->type.text.i_border_color,
1926 p_subpic->type.text.i_bg_color,
1927 p_subpic->type.text.i_style, p_subpic->p_data, 100 );
1928 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y,
1929 i_width, i_height );
1935 intf_DbgMsg( "error: unknown subpicture %p type %d\n",
1936 p_subpic, p_subpic->i_type );
1940 p_subpic = p_subpic->p_next;
1944 /*****************************************************************************
1945 * RenderInterface: render the interface
1946 *****************************************************************************
1947 * This function renders the interface, if any.
1948 *****************************************************************************/
1949 static void RenderInterface( vout_thread_t *p_vout )
1951 int i_height, i_text_height; /* total and text height */
1952 int i_width_1, i_width_2; /* text width */
1953 int i_byte; /* byte index */
1954 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1955 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1958 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1959 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1960 i_height += i_text_height;
1962 /* Render background */
1963 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1964 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1968 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1971 /* Render text, if not larger than screen */
1972 if( i_width_1 < p_vout->i_width )
1974 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1975 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1976 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1977 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1978 OUTLINED_TEXT, psz_text_1, 100 );
1980 if( i_width_2 < p_vout->i_width )
1982 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1983 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1984 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1985 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1986 OUTLINED_TEXT, psz_text_2, 100 );
1989 /* Activate modified area */
1990 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1993 /*****************************************************************************
1994 * Manage: manage thread
1995 *****************************************************************************
1996 * This function will handle changes in thread configuration.
1997 *****************************************************************************/
1998 static int Manage( vout_thread_t *p_vout )
2001 if( p_vout->i_changes )
2003 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
2004 0 /* p_vout->i_changes & VOUT_NODISPLAY_CHANGE */ );
2008 /* On gamma or grayscale change, rebuild tables */
2009 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2012 /* Change vdec_DecodeMacroblock when switching between BW and C */
2013 if( !p_vout->b_grayscale )
2015 p_vout->vdec_DecodeMacroblock = vdec_DecodeMacroblockC;
2017 else if( p_vout->b_grayscale)
2019 p_vout->vdec_DecodeMacroblock = vdec_DecodeMacroblockBW;
2021 if( vout_ResetYUV( p_vout ) )
2023 intf_ErrMsg("error: can't rebuild convertion tables\n");
2028 /* Clear changes flags which does not need management or have been
2030 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2031 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
2032 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
2034 /* Detect unauthorized changes */
2035 if( p_vout->i_changes )
2037 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
2038 * function, it means they should not be authorized */
2039 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2046 /*****************************************************************************
2047 * Align: align a subpicture in the screen
2048 *****************************************************************************
2049 * This function is used for rendering text or subpictures. It returns non 0
2050 * it the final aera is not fully included in display area. Return coordinates
2052 *****************************************************************************/
2053 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2054 int i_width, int i_height, int i_h_align, int i_v_align )
2056 /* Align horizontally */
2060 *pi_x -= i_width / 2;
2063 *pi_x += (p_vout->i_width - i_width) / 2;
2069 *pi_x += p_vout->i_width - i_width;
2073 /* Align vertically */
2077 *pi_y -= i_height / 2;
2080 *pi_y += (p_vout->i_height - i_height) / 2;
2086 *pi_y += p_vout->i_height - i_height;
2088 case SUBTITLE_RALIGN:
2089 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2090 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2094 /* Return non 0 if clipping failed */
2095 return( (*pi_x < 0) || (*pi_y < 0) ||
2096 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2099 /*****************************************************************************
2100 * SetPalette: sets an 8 bpp palette
2101 *****************************************************************************
2102 * This function is just a prototype that does nothing. Architectures that
2103 * support palette allocation should override it.
2104 *****************************************************************************/
2105 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2106 u16 *green, u16 *blue, u16 *transp )
2108 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );