1 /*****************************************************************************
2 * video_output.c : video output thread
3 * This module describes the programming interface for video output threads.
4 * It includes functions allowing to open a new thread, send pictures to a
5 * thread, and destroy a previously oppened video output thread.
6 *****************************************************************************
7 * Copyright (C) 2000 VideoLAN
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <stdio.h> /* sprintf() */
33 #include <string.h> /* strerror() */
41 #include "video_output.h"
42 #include "video_text.h"
43 #include "video_yuv.h"
48 /*****************************************************************************
50 *****************************************************************************/
51 static int BinaryLog ( u32 i );
52 static void MaskToShift ( int *pi_left, int *pi_right, u32 i_mask );
53 static int InitThread ( vout_thread_t *p_vout );
54 static void RunThread ( vout_thread_t *p_vout );
55 static void ErrorThread ( vout_thread_t *p_vout );
56 static void EndThread ( vout_thread_t *p_vout );
57 static void DestroyThread ( vout_thread_t *p_vout, int i_status );
58 static void Print ( vout_thread_t *p_vout, int i_x, int i_y,
59 int i_h_align, int i_v_align,
60 unsigned char *psz_text );
61 static void SetBufferArea ( vout_thread_t *p_vout, int i_x, int i_y,
63 static void SetBufferPicture ( vout_thread_t *p_vout, picture_t *p_pic );
64 static void RenderPicture ( vout_thread_t *p_vout, picture_t *p_pic );
65 static void RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
66 static void RenderSubPicture ( vout_thread_t *p_vout,
67 subpicture_t *p_subpic );
68 static void RenderInterface ( vout_thread_t *p_vout );
69 static int RenderIdle ( vout_thread_t *p_vout );
70 static void RenderInfo ( vout_thread_t *p_vout );
71 static void Synchronize ( vout_thread_t *p_vout, s64 i_delay );
72 static int Manage ( vout_thread_t *p_vout );
73 static int Align ( vout_thread_t *p_vout, int *pi_x,
74 int *pi_y, int i_width, int i_height,
75 int i_h_align, int i_v_align );
76 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
77 u16 *green, u16 *blue, u16 *transp );
79 /*****************************************************************************
80 * vout_CreateThread: creates a new video output thread
81 *****************************************************************************
82 * This function creates a new video output thread, and returns a pointer
83 * to its description. On error, it returns NULL.
84 * If pi_status is NULL, then the function will block until the thread is ready.
85 * If not, it will be updated using one of the THREAD_* constants.
86 *****************************************************************************/
87 vout_thread_t * vout_CreateThread ( char *psz_display, int i_root_window,
88 int i_width, int i_height, int *pi_status, int i_method )
90 vout_thread_t * p_vout; /* thread descriptor */
91 int i_status; /* thread status */
92 int i_index; /* index for array initialization */
95 /* Allocate descriptor */
97 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
100 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
104 /* Request an interface plugin */
105 psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
106 p_vout->p_vout_plugin = RequestPlugin( "vout", psz_method );
108 if( !p_vout->p_vout_plugin )
110 intf_ErrMsg( "error: could not open video plugin vout_%s.so\n", psz_method );
116 p_vout->p_sys_create = GetPluginFunction( p_vout->p_vout_plugin, "vout_SysCreate" );
117 p_vout->p_sys_init = GetPluginFunction( p_vout->p_vout_plugin, "vout_SysInit" );
118 p_vout->p_sys_end = GetPluginFunction( p_vout->p_vout_plugin, "vout_SysEnd" );
119 p_vout->p_sys_destroy = GetPluginFunction( p_vout->p_vout_plugin, "vout_SysDestroy" );
120 p_vout->p_sys_manage = GetPluginFunction( p_vout->p_vout_plugin, "vout_SysManage" );
121 p_vout->p_sys_display = GetPluginFunction( p_vout->p_vout_plugin, "vout_SysDisplay" );
123 /* Initialize thread properties - thread id and locks will be initialized
127 p_vout->b_active = 0;
128 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
129 *p_vout->pi_status = THREAD_CREATE;
131 /* Initialize some fields used by the system-dependant method - these fields will
132 * probably be modified by the method, and are only preferences */
133 p_vout->i_changes = 0;
134 p_vout->i_width = i_width;
135 p_vout->i_height = i_height;
136 p_vout->i_bytes_per_line = i_width * 2;
137 p_vout->i_screen_depth = 15;
138 p_vout->i_bytes_per_pixel = 2;
139 p_vout->f_gamma = VOUT_GAMMA;
141 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
143 p_vout->b_interface = 0;
146 p_vout->p_set_palette = SetPalette;
148 intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
149 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
150 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
152 /* Initialize idle screen */
153 p_vout->last_display_date = mdate();
154 p_vout->last_display_date = 0;
155 p_vout->last_idle_date = 0;
158 /* Initialize statistics fields */
159 p_vout->render_time = 0;
160 p_vout->c_fps_samples = 0;
163 /* Initialize buffer index */
164 p_vout->i_buffer_index = 0;
166 /* Initialize pictures and subpictures - translation tables and functions
167 * will be initialized later in InitThread */
168 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
170 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
171 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
172 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
173 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
175 p_vout->i_pictures = 0;
177 /* Initialize synchronization informations */
178 p_vout->i_synchro_level = VOUT_SYNCHRO_LEVEL_START;
180 /* Create and initialize system-dependant method - this function issues its
181 * own error messages */
182 if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
184 TrashPlugin( p_vout->p_vout_plugin );
188 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
189 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
190 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
191 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
193 /* Calculate shifts from system-updated masks */
194 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
195 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
196 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
198 /* Set some useful colors */
199 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
200 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
201 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
202 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
204 /* Load fonts - fonts must be initialized after the system method since
205 * they may be dependant on screen depth and other thread properties */
206 p_vout->p_default_font = vout_LoadFont( VOUT_DEFAULT_FONT );
207 if( p_vout->p_default_font == NULL )
209 p_vout->p_sys_destroy( p_vout );
210 TrashPlugin( p_vout->p_vout_plugin );
214 p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
215 if( p_vout->p_large_font == NULL )
217 vout_UnloadFont( p_vout->p_default_font );
218 p_vout->p_sys_destroy( p_vout );
219 TrashPlugin( p_vout->p_vout_plugin );
224 /* Create thread and set locks */
225 vlc_mutex_init( &p_vout->picture_lock );
226 vlc_mutex_init( &p_vout->subpicture_lock );
227 vlc_mutex_init( &p_vout->change_lock );
228 vlc_mutex_lock( &p_vout->change_lock );
229 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
231 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
232 vout_UnloadFont( p_vout->p_default_font );
233 vout_UnloadFont( p_vout->p_large_font );
234 p_vout->p_sys_destroy( p_vout );
235 TrashPlugin( p_vout->p_vout_plugin );
240 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
241 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
243 /* If status is NULL, wait until the thread is created */
244 if( pi_status == NULL )
248 msleep( THREAD_SLEEP );
249 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
250 && (i_status != THREAD_FATAL) );
251 if( i_status != THREAD_READY )
259 /*****************************************************************************
260 * vout_DestroyThread: destroys a previously created thread
261 *****************************************************************************
262 * Destroy a terminated thread.
263 * The function will request a destruction of the specified thread. If pi_error
264 * is NULL, it will return once the thread is destroyed. Else, it will be
265 * update using one of the THREAD_* constants.
266 *****************************************************************************/
267 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
269 int i_status; /* thread status */
273 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
274 *p_vout->pi_status = THREAD_DESTROY;
276 /* Request thread destruction */
279 /* If status is NULL, wait until thread has been destroyed */
280 if( pi_status == NULL )
284 msleep( THREAD_SLEEP );
285 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
286 && (i_status != THREAD_FATAL) );
290 /*****************************************************************************
291 * vout_DisplaySubPicture: display a subpicture unit
292 *****************************************************************************
293 * Remove the reservation flag of an subpicture, which will cause it to be ready
294 * for display. The picture does not need to be locked, since it is ignored by
295 * the output thread if is reserved.
296 *****************************************************************************/
297 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
300 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
301 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
305 /* Check if status is valid */
306 if( p_subpic->i_status != RESERVED_SUBPICTURE )
308 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
309 p_subpic->i_status );
313 /* Remove reservation flag */
314 p_subpic->i_status = READY_SUBPICTURE;
317 /* Send subpicture informations */
318 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
319 p_subpic, p_subpic->i_type,
320 mstrtime( psz_begin_date, p_subpic->begin_date ),
321 mstrtime( psz_end_date, p_subpic->end_date ) );
325 /*****************************************************************************
326 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
327 *****************************************************************************
328 * This function create a reserved subpicture in the video output heap.
329 * A null pointer is returned if the function fails. This method provides an
330 * already allocated zone of memory in the spu data fields. It needs locking
331 * since several pictures can be created by several producers threads.
332 *****************************************************************************/
333 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
336 int i_subpic; /* subpicture index */
337 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
338 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
341 vlc_mutex_lock( &p_vout->subpicture_lock );
344 * Look for an empty place
346 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
348 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
350 /* Subpicture is marked for destruction, but is still allocated */
351 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
352 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
354 /* Memory size do match or is smaller : memory will not be reallocated,
355 * and function can end immediately - this is the best possible case,
356 * since no memory allocation needs to be done */
357 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
359 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
360 &p_vout->p_subpicture[i_subpic] );
362 vlc_mutex_unlock( &p_vout->subpicture_lock );
363 return( &p_vout->p_subpicture[i_subpic] );
365 else if( p_destroyed_subpic == NULL )
367 /* Memory size do not match, but subpicture index will be kept in
368 * case no other place are left */
369 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
372 else if( (p_free_subpic == NULL) &&
373 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
375 /* Subpicture is empty and ready for allocation */
376 p_free_subpic = &p_vout->p_subpicture[i_subpic];
380 /* If no free subpicture is available, use a destroyed subpicture */
381 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
383 /* No free subpicture or matching destroyed subpicture has been found, but
384 * a destroyed subpicture is still avalaible */
385 free( p_destroyed_subpic->p_data );
386 p_free_subpic = p_destroyed_subpic;
392 if( p_free_subpic != NULL )
394 /* Allocate memory */
397 case TEXT_SUBPICTURE: /* text subpicture */
398 p_free_subpic->p_data = malloc( i_size + 1 );
402 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
403 p_free_subpic->p_data = NULL;
408 if( p_free_subpic->p_data != NULL )
409 { /* Copy subpicture informations, set some default values */
410 p_free_subpic->i_type = i_type;
411 p_free_subpic->i_status = RESERVED_SUBPICTURE;
412 p_free_subpic->i_size = i_size;
413 p_free_subpic->i_x = 0;
414 p_free_subpic->i_y = 0;
415 p_free_subpic->i_width = 0;
416 p_free_subpic->i_height = 0;
417 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
418 p_free_subpic->i_vertical_align = CENTER_RALIGN;
422 /* Memory allocation failed : set subpicture as empty */
423 p_free_subpic->i_type = EMPTY_SUBPICTURE;
424 p_free_subpic->i_status = FREE_SUBPICTURE;
425 p_free_subpic = NULL;
426 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
430 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
432 vlc_mutex_unlock( &p_vout->subpicture_lock );
433 return( p_free_subpic );
436 /* No free or destroyed subpicture could be found */
437 intf_DbgMsg( "warning: heap is full\n" );
438 vlc_mutex_unlock( &p_vout->subpicture_lock );
442 /*****************************************************************************
443 * vout_DestroySubPicture: remove a subpicture from the heap
444 *****************************************************************************
445 * This function frees a previously reserved subpicture.
446 * It is meant to be used when the construction of a picture aborted.
447 * This function does not need locking since reserved subpictures are ignored
448 * by the output thread.
449 *****************************************************************************/
450 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
453 /* Check if status is valid */
454 if( p_subpic->i_status != RESERVED_SUBPICTURE )
456 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
457 p_subpic, p_subpic->i_status );
461 p_subpic->i_status = DESTROYED_SUBPICTURE;
464 intf_DbgMsg("subpicture %p\n", p_subpic);
468 /*****************************************************************************
469 * vout_DisplayPicture: display a picture
470 *****************************************************************************
471 * Remove the reservation flag of a picture, which will cause it to be ready for
472 * display. The picture won't be displayed until vout_DatePicture has been
474 *****************************************************************************/
475 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
477 vlc_mutex_lock( &p_vout->picture_lock );
478 switch( p_pic->i_status )
480 case RESERVED_PICTURE:
481 p_pic->i_status = RESERVED_DISP_PICTURE;
483 case RESERVED_DATED_PICTURE:
484 p_pic->i_status = READY_PICTURE;
488 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
494 intf_DbgMsg("picture %p\n", p_pic);
496 vlc_mutex_unlock( &p_vout->picture_lock );
499 /*****************************************************************************
500 * vout_DatePicture: date a picture
501 *****************************************************************************
502 * Remove the reservation flag of a picture, which will cause it to be ready for
503 * display. The picture won't be displayed until vout_DisplayPicture has been
505 *****************************************************************************/
506 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
509 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
512 vlc_mutex_lock( &p_vout->picture_lock );
514 switch( p_pic->i_status )
516 case RESERVED_PICTURE:
517 p_pic->i_status = RESERVED_DATED_PICTURE;
519 case RESERVED_DISP_PICTURE:
520 p_pic->i_status = READY_PICTURE;
524 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
530 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
532 vlc_mutex_unlock( &p_vout->picture_lock );
535 /*****************************************************************************
536 * vout_CreatePicture: allocate a picture in the video output heap.
537 *****************************************************************************
538 * This function create a reserved image in the video output heap.
539 * A null pointer is returned if the function fails. This method provides an
540 * already allocated zone of memory in the picture data fields. It needs locking
541 * since several pictures can be created by several producers threads.
542 *****************************************************************************/
543 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
544 int i_width, int i_height )
546 int i_picture; /* picture index */
547 int i_chroma_width = 0; /* chroma width */
548 picture_t * p_free_picture = NULL; /* first free picture */
549 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
552 vlc_mutex_lock( &p_vout->picture_lock );
555 * Look for an empty place
557 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
559 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
561 /* Picture is marked for destruction, but is still allocated - note
562 * that if width and type are the same for two pictures, chroma_width
563 * should also be the same */
564 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
565 (p_vout->p_picture[i_picture].i_height == i_height) &&
566 (p_vout->p_picture[i_picture].i_width == i_width) )
568 /* Memory size do match : memory will not be reallocated, and function
569 * can end immediately - this is the best possible case, since no
570 * memory allocation needs to be done */
571 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
572 p_vout->i_pictures++;
574 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
575 &p_vout->p_picture[i_picture] );
577 vlc_mutex_unlock( &p_vout->picture_lock );
578 return( &p_vout->p_picture[i_picture] );
580 else if( p_destroyed_picture == NULL )
582 /* Memory size do not match, but picture index will be kept in
583 * case no other place are left */
584 p_destroyed_picture = &p_vout->p_picture[i_picture];
587 else if( (p_free_picture == NULL) &&
588 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
590 /* Picture is empty and ready for allocation */
591 p_free_picture = &p_vout->p_picture[i_picture];
595 /* If no free picture is available, use a destroyed picture */
596 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
598 /* No free picture or matching destroyed picture has been found, but
599 * a destroyed picture is still avalaible */
600 free( p_destroyed_picture->p_data );
601 p_free_picture = p_destroyed_picture;
607 if( p_free_picture != NULL )
609 /* Allocate memory */
612 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
613 i_chroma_width = i_width / 2;
614 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
615 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
616 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
617 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
619 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
620 i_chroma_width = i_width / 2;
621 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
622 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
623 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
624 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
626 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
627 i_chroma_width = i_width;
628 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
629 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
630 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
631 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
635 intf_DbgMsg("error: unknown picture type %d\n", i_type );
636 p_free_picture->p_data = NULL;
641 if( p_free_picture->p_data != NULL )
643 /* Copy picture informations, set some default values */
644 p_free_picture->i_type = i_type;
645 p_free_picture->i_status = RESERVED_PICTURE;
646 p_free_picture->i_matrix_coefficients = 1;
647 p_free_picture->i_width = i_width;
648 p_free_picture->i_height = i_height;
649 p_free_picture->i_chroma_width = i_chroma_width;
650 p_free_picture->i_display_horizontal_offset = 0;
651 p_free_picture->i_display_vertical_offset = 0;
652 p_free_picture->i_display_width = i_width;
653 p_free_picture->i_display_height = i_height;
654 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
655 p_free_picture->i_refcount = 0;
656 p_vout->i_pictures++;
660 /* Memory allocation failed : set picture as empty */
661 p_free_picture->i_type = EMPTY_PICTURE;
662 p_free_picture->i_status = FREE_PICTURE;
663 p_free_picture = NULL;
664 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
668 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
670 vlc_mutex_unlock( &p_vout->picture_lock );
671 return( p_free_picture );
674 /* No free or destroyed picture could be found */
675 intf_DbgMsg( "warning: heap is full\n" );
676 vlc_mutex_unlock( &p_vout->picture_lock );
680 /*****************************************************************************
681 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
682 *****************************************************************************
683 * This function frees a previously reserved picture or a permanent
684 * picture. It is meant to be used when the construction of a picture aborted.
685 * Note that the picture will be destroyed even if it is linked !
686 *****************************************************************************/
687 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
689 vlc_mutex_lock( &p_vout->picture_lock );
692 /* Check if picture status is valid */
693 if( (p_pic->i_status != RESERVED_PICTURE) &&
694 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
695 (p_pic->i_status != RESERVED_DISP_PICTURE) )
697 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
701 p_pic->i_status = DESTROYED_PICTURE;
702 p_vout->i_pictures--;
705 intf_DbgMsg("picture %p\n", p_pic);
707 vlc_mutex_unlock( &p_vout->picture_lock );
710 /*****************************************************************************
711 * vout_LinkPicture: increment reference counter of a picture
712 *****************************************************************************
713 * This function increment the reference counter of a picture in the video
714 * heap. It needs a lock since several producer threads can access the picture.
715 *****************************************************************************/
716 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
718 vlc_mutex_lock( &p_vout->picture_lock );
722 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
725 vlc_mutex_unlock( &p_vout->picture_lock );
728 /*****************************************************************************
729 * vout_UnlinkPicture: decrement reference counter of a picture
730 *****************************************************************************
731 * This function decrement the reference counter of a picture in the video heap.
732 *****************************************************************************/
733 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
735 vlc_mutex_lock( &p_vout->picture_lock );
739 if( p_pic->i_refcount < 0 )
741 intf_DbgMsg("error: refcount < 0\n");
742 p_pic->i_refcount = 0;
746 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
748 p_pic->i_status = DESTROYED_PICTURE;
749 p_vout->i_pictures--;
753 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
756 vlc_mutex_unlock( &p_vout->picture_lock );
759 /*****************************************************************************
760 * vout_SetBuffers: set buffers adresses
761 *****************************************************************************
762 * This function is called by system drivers to set buffers video memory
764 *****************************************************************************/
765 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
767 /* No picture previously */
768 p_vout->p_buffer[0].i_pic_x = 0;
769 p_vout->p_buffer[0].i_pic_y = 0;
770 p_vout->p_buffer[0].i_pic_width = 0;
771 p_vout->p_buffer[0].i_pic_height = 0;
772 p_vout->p_buffer[1].i_pic_x = 0;
773 p_vout->p_buffer[1].i_pic_y = 0;
774 p_vout->p_buffer[1].i_pic_width = 0;
775 p_vout->p_buffer[1].i_pic_height = 0;
777 /* The first area covers all the screen */
778 p_vout->p_buffer[0].i_areas = 1;
779 p_vout->p_buffer[0].pi_area_begin[0] = 0;
780 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
781 p_vout->p_buffer[1].i_areas = 1;
782 p_vout->p_buffer[1].pi_area_begin[0] = 0;
783 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
786 p_vout->p_buffer[0].p_data = p_buf1;
787 p_vout->p_buffer[1].p_data = p_buf2;
790 /*****************************************************************************
791 * vout_Pixel2RGB: return red, green and blue from pixel value
792 *****************************************************************************
793 * Return color values, in 0-255 range, of the decomposition of a pixel. This
794 * is a slow routine and should only be used for initialization phase.
795 *****************************************************************************/
796 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
798 *pi_red = i_pixel & p_vout->i_red_mask;
799 *pi_green = i_pixel & p_vout->i_green_mask;
800 *pi_blue = i_pixel & p_vout->i_blue_mask;
803 /* following functions are local */
805 /*****************************************************************************
806 * BinaryLog: computes the base 2 log of a binary value
807 *****************************************************************************
808 * This functions is used by MaskToShift, to get a bit index from a binary
810 *****************************************************************************/
811 static int BinaryLog(u32 i)
836 if (i != ((u32)1 << i_log))
838 intf_ErrMsg("internal error: binary log overflow\n");
844 /*****************************************************************************
845 * MaskToShift: transform a color mask into right and left shifts
846 *****************************************************************************
847 * This function is used for obtaining color shifts from masks.
848 *****************************************************************************/
849 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
851 u32 i_low, i_high; /* lower hand higher bits of the mask */
854 i_low = i_mask & (- i_mask); /* lower bit of the mask */
855 i_high = i_mask + i_low; /* higher bit of the mask */
857 /* Transform bits into an index */
858 i_low = BinaryLog (i_low);
859 i_high = BinaryLog (i_high);
861 /* Update pointers and return */
863 *pi_right = (8 - i_high + i_low);
866 /*****************************************************************************
867 * InitThread: initialize video output thread
868 *****************************************************************************
869 * This function is called from RunThread and performs the second step of the
870 * initialization. It returns 0 on success. Note that the thread's flag are not
871 * modified inside this function.
872 *****************************************************************************/
873 static int InitThread( vout_thread_t *p_vout )
877 *p_vout->pi_status = THREAD_START;
879 /* Initialize output method - this function issues its own error messages */
880 if( p_vout->p_sys_init( p_vout ) )
885 /* Initialize convertion tables and functions */
886 if( vout_InitYUV( p_vout ) )
888 intf_ErrMsg("error: can't allocate YUV translation tables\n");
892 /* Mark thread as running and return */
893 p_vout->b_active = 1;
894 *p_vout->pi_status = THREAD_READY;
895 intf_DbgMsg("thread ready\n");
899 /*****************************************************************************
900 * RunThread: video output thread
901 *****************************************************************************
902 * Video output thread. This function does only returns when the thread is
903 * terminated. It handles the pictures arriving in the video heap and the
904 * display device events.
905 *****************************************************************************/
906 static void RunThread( vout_thread_t *p_vout)
908 /* XXX?? welcome to gore land */
909 static int i_trash_count = 0;
910 static mtime_t last_display_date = 0;
912 int i_index; /* index in heap */
913 mtime_t current_date; /* current date */
914 mtime_t display_date; /* display date */
915 boolean_t b_display; /* display flag */
916 picture_t * p_pic; /* picture pointer */
917 subpicture_t * p_subpic; /* subpicture pointer */
922 p_vout->b_error = InitThread( p_vout );
923 if( p_vout->b_error )
925 DestroyThread( p_vout, THREAD_ERROR );
931 * Main loop - it is not executed if an error occured during
934 while( (!p_vout->b_die) && (!p_vout->b_error) )
936 /* Initialize loop variables */
940 current_date = mdate();
943 * Find the picture to display - this operation does not need lock,
944 * since only READY_PICTUREs are handled
946 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
948 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
950 (p_vout->p_picture[i_index].date < display_date) ) )
952 p_pic = &p_vout->p_picture[i_index];
953 display_date = p_pic->date;
960 /* Computes FPS rate */
961 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
965 //fprintf( stderr, "gap : %Ld\n", display_date-last_display_date );
966 last_display_date = display_date;
968 if( display_date < current_date && i_trash_count > 4 )
970 /* Picture is late: it will be destroyed and the thread will sleep and
971 * go to next picture */
973 vlc_mutex_lock( &p_vout->picture_lock );
974 if( p_pic->i_refcount )
976 p_pic->i_status = DISPLAYED_PICTURE;
980 p_pic->i_status = DESTROYED_PICTURE;
981 p_vout->i_pictures--;
983 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
984 vlc_mutex_unlock( &p_vout->picture_lock );
986 /* Update synchronization information as if display delay
988 Synchronize( p_vout, display_date - current_date );
996 if( display_date > current_date + VOUT_DISPLAY_DELAY )
998 /* A picture is ready to be rendered, but its rendering date is
999 * far from the current one so the thread will perform an empty loop
1000 * as if no picture were found. The picture state is unchanged */
1006 /* Picture will be displayed, update synchronization
1008 Synchronize( p_vout, display_date - current_date );
1012 * Find the subpicture to display - this operation does not need lock, since
1013 * only READY_SUBPICTURES are handled. If no picture has been selected,
1014 * display_date will depend on the subpicture
1019 * Perform rendering, sleep and display rendered picture
1021 if( p_pic ) /* picture and perhaps subpicture */
1023 b_display = p_vout->b_active;
1024 p_vout->last_display_date = display_date;
1028 /* Set picture dimensions and clear buffer */
1029 SetBufferPicture( p_vout, p_pic );
1031 /* Render picture and informations */
1032 RenderPicture( p_vout, p_pic );
1033 if( p_vout->b_info )
1035 RenderPictureInfo( p_vout, p_pic );
1036 RenderInfo( p_vout );
1040 /* Remove picture from heap */
1041 vlc_mutex_lock( &p_vout->picture_lock );
1042 if( p_pic->i_refcount )
1044 p_pic->i_status = DISPLAYED_PICTURE;
1048 p_pic->i_status = DESTROYED_PICTURE;
1049 p_vout->i_pictures--;
1051 vlc_mutex_unlock( &p_vout->picture_lock );
1053 /* Render interface and subpicture */
1054 if( b_display && p_vout->b_interface )
1056 RenderInterface( p_vout );
1062 RenderSubPicture( p_vout, p_subpic );
1065 /* Remove subpicture from heap */
1066 vlc_mutex_lock( &p_vout->subpicture_lock );
1067 p_subpic->i_status = DESTROYED_SUBPICTURE;
1068 vlc_mutex_unlock( &p_vout->subpicture_lock );
1072 else if( p_subpic ) /* subpicture alone */
1074 b_display = p_vout->b_active;
1075 p_vout->last_display_date = display_date;
1080 SetBufferPicture( p_vout, NULL );
1082 /* Render informations, interface and subpicture */
1083 if( p_vout->b_info )
1085 RenderInfo( p_vout );
1087 if( p_vout->b_interface )
1089 RenderInterface( p_vout );
1091 RenderSubPicture( p_vout, p_subpic );
1094 /* Remove subpicture from heap */
1095 vlc_mutex_lock( &p_vout->subpicture_lock );
1096 p_subpic->i_status = DESTROYED_SUBPICTURE;
1097 vlc_mutex_unlock( &p_vout->subpicture_lock );
1099 else if( p_vout->b_active ) /* idle or interface screen alone */
1101 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1103 /* Interface has changed, so a new rendering is required - force
1104 * it by setting last idle date to 0 */
1105 p_vout->last_idle_date = 0;
1108 /* Render idle screen and update idle date, then render interface if
1110 b_display = RenderIdle( p_vout );
1113 p_vout->last_idle_date = current_date;
1114 if( p_vout->b_interface )
1116 RenderInterface( p_vout );
1127 * Sleep, wake up and display rendered picture
1131 /* Store render time */
1132 p_vout->render_time = mdate() - current_date;
1135 /* Give back change lock */
1136 vlc_mutex_unlock( &p_vout->change_lock );
1138 /* Sleep a while or until a given date */
1139 if( display_date != 0 )
1141 mwait( display_date );
1145 msleep( VOUT_IDLE_SLEEP );
1148 /* On awakening, take back lock and send immediately picture to display,
1149 * then swap buffers */
1150 vlc_mutex_lock( &p_vout->change_lock );
1152 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1153 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1155 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1157 p_vout->p_sys_display( p_vout );
1158 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1162 * Check events and manage thread
1164 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1166 /* A fatal error occured, and the thread must terminate immediately,
1167 * without displaying anything - setting b_error to 1 cause the
1168 * immediate end of the main while() loop. */
1169 p_vout->b_error = 1;
1174 * Error loop - wait until the thread destruction is requested
1176 if( p_vout->b_error )
1178 ErrorThread( p_vout );
1182 EndThread( p_vout );
1183 DestroyThread( p_vout, THREAD_OVER );
1184 intf_DbgMsg( "thread end\n" );
1187 /*****************************************************************************
1188 * ErrorThread: RunThread() error loop
1189 *****************************************************************************
1190 * This function is called when an error occured during thread main's loop. The
1191 * thread can still receive feed, but must be ready to terminate as soon as
1193 *****************************************************************************/
1194 static void ErrorThread( vout_thread_t *p_vout )
1196 /* Wait until a `die' order */
1198 while( !p_vout->b_die )
1201 msleep( VOUT_IDLE_SLEEP );
1205 /*****************************************************************************
1206 * EndThread: thread destruction
1207 *****************************************************************************
1208 * This function is called when the thread ends after a sucessfull
1209 * initialization. It frees all ressources allocated by InitThread.
1210 *****************************************************************************/
1211 static void EndThread( vout_thread_t *p_vout )
1213 int i_index; /* index in heap */
1217 *p_vout->pi_status = THREAD_END;
1219 /* Destroy all remaining pictures and subpictures */
1220 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1222 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1224 free( p_vout->p_picture[i_index].p_data );
1226 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1228 free( p_vout->p_subpicture[i_index].p_data );
1232 /* Destroy translation tables */
1233 vout_EndYUV( p_vout );
1234 p_vout->p_sys_end( p_vout );
1237 /*****************************************************************************
1238 * DestroyThread: thread destruction
1239 *****************************************************************************
1240 * This function is called when the thread ends. It frees all ressources
1241 * allocated by CreateThread. Status is available at this stage.
1242 *****************************************************************************/
1243 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1245 int *pi_status; /* status adress */
1247 /* Store status adress */
1249 pi_status = p_vout->pi_status;
1251 /* Destroy thread structures allocated by Create and InitThread */
1252 vout_UnloadFont( p_vout->p_default_font );
1253 vout_UnloadFont( p_vout->p_large_font );
1254 p_vout->p_sys_destroy( p_vout );
1257 TrashPlugin( p_vout->p_vout_plugin );
1259 /* Free structure */
1261 *pi_status = i_status;
1264 /*****************************************************************************
1265 * Print: print simple text on a picture
1266 *****************************************************************************
1267 * This function will print a simple text on the picture. It is designed to
1268 * print debugging or general informations.
1269 *****************************************************************************/
1270 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 )
1272 int i_text_height; /* total text height */
1273 int i_text_width; /* total text width */
1275 /* Update upper left coordinates according to alignment */
1276 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1277 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1279 /* Set area and print text */
1280 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1281 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1282 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1283 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1284 p_vout->i_white_pixel, 0, 0,
1289 /*****************************************************************************
1290 * SetBufferArea: activate an area in current buffer
1291 *****************************************************************************
1292 * This function is called when something is rendered on the current buffer.
1293 * It set the area as active and prepare it to be cleared on next rendering.
1294 * Pay attention to the fact that in this functions, i_h is in fact the end y
1295 * coordinate of the new area.
1296 *****************************************************************************/
1297 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1299 vout_buffer_t * p_buffer; /* current buffer */
1300 int i_area_begin, i_area_end; /* area vertical extension */
1301 int i_area, i_area_copy; /* area index */
1302 int i_area_shift; /* shift distance for areas */
1304 /* Choose buffer and modify h to end of area position */
1305 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1309 * Remove part of the area which is inside the picture - this is done
1310 * by calling again SetBufferArea with the correct areas dimensions.
1312 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1314 i_area_begin = p_buffer->i_pic_y;
1315 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1317 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1318 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1319 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1321 /* Keep the stripe above the picture, if any */
1322 if( i_y < i_area_begin )
1324 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1326 /* Keep the stripe below the picture, if any */
1327 if( i_h > i_area_end )
1329 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1335 /* Skip some extensions until interesting areas */
1337 (i_area < p_buffer->i_areas) &&
1338 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1344 if( i_area == p_buffer->i_areas )
1346 /* New area is below all existing ones: just add it at the end of the
1347 * array, if possible - else, append it to the last one */
1348 if( i_area < VOUT_MAX_AREAS )
1350 p_buffer->pi_area_begin[i_area] = i_y;
1351 p_buffer->pi_area_end[i_area] = i_h;
1352 p_buffer->i_areas++;
1357 intf_DbgMsg("areas overflow\n");
1359 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1364 i_area_begin = p_buffer->pi_area_begin[i_area];
1365 i_area_end = p_buffer->pi_area_end[i_area];
1367 if( i_y < i_area_begin )
1369 if( i_h >= i_area_begin - 1 )
1371 /* Extend area above */
1372 p_buffer->pi_area_begin[i_area] = i_y;
1376 /* Create a new area above : merge last area if overflow, then
1377 * move all old areas down */
1378 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1381 intf_DbgMsg("areas overflow\n");
1383 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1387 p_buffer->i_areas++;
1389 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1391 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1392 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1394 p_buffer->pi_area_begin[i_area] = i_y;
1395 p_buffer->pi_area_end[i_area] = i_h;
1399 if( i_h > i_area_end )
1401 /* Find further areas which can be merged with the new one */
1402 for( i_area_copy = i_area + 1;
1403 (i_area_copy < p_buffer->i_areas) &&
1404 (p_buffer->pi_area_begin[i_area] <= i_h);
1411 if( i_area_copy != i_area )
1413 /* Merge with last possible areas */
1414 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1416 /* Shift lower areas upward */
1417 i_area_shift = i_area_copy - i_area;
1418 p_buffer->i_areas -= i_area_shift;
1419 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1421 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1422 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1427 /* Extend area below */
1428 p_buffer->pi_area_end[i_area] = i_h;
1434 /*****************************************************************************
1435 * SetBufferPicture: clear buffer and set picture area
1436 *****************************************************************************
1437 * This function is called before any rendering. It clears the current
1438 * rendering buffer and set the new picture area. If the picture pointer is
1439 * NULL, then no picture area is defined. Floating operations are avoided since
1440 * some MMX calculations may follow.
1441 *****************************************************************************/
1442 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1444 vout_buffer_t * p_buffer; /* current buffer */
1445 int i_pic_x, i_pic_y; /* picture position */
1446 int i_pic_width, i_pic_height; /* picture dimensions */
1447 int i_old_pic_y, i_old_pic_height; /* old picture area */
1448 int i_vout_width, i_vout_height; /* display dimensions */
1449 int i_area; /* area index */
1450 int i_data_index; /* area data index */
1451 int i_data_size; /* area data size, in 256 bytes blocs */
1452 u64 * p_data; /* area data, for clearing */
1453 byte_t * p_data8; /* area data, for clearing (slow) */
1455 /* Choose buffer and set display dimensions */
1456 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1457 i_vout_width = p_vout->i_width;
1458 i_vout_height = p_vout->i_height;
1461 * Computes new picture size
1465 /* Try horizontal scaling first - width must be a mutiple of 16 */
1466 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1467 i_vout_width : p_pic->i_width) & ~0xf;
1468 switch( p_pic->i_aspect_ratio )
1470 case AR_3_4_PICTURE:
1471 i_pic_height = i_pic_width * 3 / 4;
1473 case AR_16_9_PICTURE:
1474 i_pic_height = i_pic_width * 9 / 16;
1476 case AR_221_1_PICTURE:
1477 i_pic_height = i_pic_width * 100 / 221;
1479 case AR_SQUARE_PICTURE:
1481 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1485 /* If picture dimensions using horizontal scaling are too large, use
1486 * vertical scaling. Since width must be a multiple of 16, height is
1487 * adjusted again after. */
1488 if( i_pic_height > i_vout_height )
1490 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1491 i_vout_height : p_pic->i_height;
1492 switch( p_pic->i_aspect_ratio )
1494 case AR_3_4_PICTURE:
1495 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1496 i_pic_height = i_pic_width * 3 / 4;
1498 case AR_16_9_PICTURE:
1499 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1500 i_pic_height = i_pic_width * 9 / 16;
1502 case AR_221_1_PICTURE:
1503 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1504 i_pic_height = i_pic_width * 100 / 221;
1506 case AR_SQUARE_PICTURE:
1508 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1509 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1514 /* Set picture position */
1515 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1516 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1520 /* No picture: size is 0 */
1528 * Set new picture size - if is is smaller than the previous one, clear
1529 * around it. Since picture are centered, only their size is tested.
1531 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1533 i_old_pic_y = p_buffer->i_pic_y;
1534 i_old_pic_height = p_buffer->i_pic_height;
1535 p_buffer->i_pic_x = i_pic_x;
1536 p_buffer->i_pic_y = i_pic_y;
1537 p_buffer->i_pic_width = i_pic_width;
1538 p_buffer->i_pic_height = i_pic_height;
1539 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1543 p_buffer->i_pic_x = i_pic_x;
1544 p_buffer->i_pic_y = i_pic_y;
1545 p_buffer->i_pic_width = i_pic_width;
1546 p_buffer->i_pic_height = i_pic_height;
1552 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1555 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1556 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1558 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1559 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1560 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1562 /* Clear 256 bytes block */
1563 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1564 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1565 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1566 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1567 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1568 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1569 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1570 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1572 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1574 /* Clear remaining 16 bytes blocks */
1575 *p_data++ = 0; *p_data++ = 0;
1577 p_data8 = (byte_t *)p_data;
1578 for( i_data_index = i_data_size % 16; i_data_index--; )
1580 /* Clear remaining bytes */
1588 p_buffer->i_areas = 0;
1591 /*****************************************************************************
1592 * RenderPicture: render a picture
1593 *****************************************************************************
1594 * This function convert a picture from a video heap to a pixel-encoded image
1595 * and copy it to the current rendering buffer. No lock is required, since the
1596 * rendered picture has been determined as existant, and will only be destroyed
1597 * by the vout thread later.
1598 *****************************************************************************/
1599 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1602 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1603 mtime_t render_time; /* picture rendering time */
1605 vout_buffer_t * p_buffer; /* rendering buffer */
1606 byte_t * p_pic_data; /* convertion destination */
1608 /* Get and set rendering informations */
1609 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1610 p_pic_data = p_buffer->p_data +
1611 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1612 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1614 render_time = mdate();
1618 * Choose appropriate rendering function and render picture
1620 switch( p_pic->i_type )
1622 case YUV_420_PICTURE:
1623 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1624 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1625 p_pic->i_width, p_pic->i_height,
1626 p_buffer->i_pic_width, p_buffer->i_pic_height,
1627 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1628 p_pic->i_matrix_coefficients );
1630 case YUV_422_PICTURE:
1631 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1632 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1633 p_pic->i_width, p_pic->i_height,
1634 p_buffer->i_pic_width, p_buffer->i_pic_height,
1635 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1636 p_pic->i_matrix_coefficients );
1638 case YUV_444_PICTURE:
1639 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1640 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1641 p_pic->i_width, p_pic->i_height,
1642 p_buffer->i_pic_width, p_buffer->i_pic_height,
1643 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1644 p_pic->i_matrix_coefficients );
1648 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1654 /* Print picture date and rendering time */
1655 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1656 p_vout->i_buffer_index, (long) (mdate() - render_time),
1657 mstrtime( psz_date, p_pic->date ));
1661 /*****************************************************************************
1662 * RenderPictureInfo: print additionnal informations on a picture
1663 *****************************************************************************
1664 * This function will print informations such as fps and other picture
1665 * dependant informations.
1666 *****************************************************************************/
1667 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1669 #if defined(STATS) || defined(DEBUG)
1670 char psz_buffer[256]; /* string buffer */
1675 * Print FPS rate in upper right corner
1677 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1679 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1680 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1681 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1682 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1686 * Print frames count and loop time in upper left corner
1688 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1689 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1690 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1695 * Print picture information in lower right corner
1697 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1698 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1699 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1700 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1701 p_pic->i_width, p_pic->i_height,
1702 p_pic->i_display_width, p_pic->i_display_height,
1703 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1704 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1705 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1706 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1707 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1708 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1709 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1710 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1711 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1712 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1716 /*****************************************************************************
1717 * RenderIdle: render idle picture
1718 *****************************************************************************
1719 * This function will print something on the screen. It will return 0 if
1720 * nothing has been rendered, or 1 if something has been changed on the screen.
1721 * Note that if you absolutely want something to be printed, you will have
1722 * to force it by setting the last idle date to 0.
1723 * Unlike other rendering functions, this one calls the SetBufferPicture
1724 * function when needed.
1725 *****************************************************************************/
1726 static int RenderIdle( vout_thread_t *p_vout )
1728 int i_x = 0, i_y = 0; /* text position */
1729 int i_width, i_height; /* text size */
1730 mtime_t current_date; /* current date */
1731 const char *psz_text = "waiting for stream ..."; /* text to display */
1734 current_date = mdate();
1735 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1736 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1738 SetBufferPicture( p_vout, NULL );
1739 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1740 &i_width, &i_height );
1741 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1743 vout_Print( p_vout->p_large_font,
1744 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1745 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1746 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1747 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1748 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1749 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1756 /*****************************************************************************
1757 * RenderInfo: render additionnal informations
1758 *****************************************************************************
1759 * This function render informations which do not depend of the current picture
1761 *****************************************************************************/
1762 static void RenderInfo( vout_thread_t *p_vout )
1765 char psz_buffer[256]; /* string buffer */
1766 int i_ready_pic = 0; /* ready pictures */
1767 int i_reserved_pic = 0; /* reserved pictures */
1768 int i_picture; /* picture index */
1773 * Print thread state in lower left corner
1775 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1777 switch( p_vout->p_picture[i_picture].i_status )
1779 case RESERVED_PICTURE:
1780 case RESERVED_DATED_PICTURE:
1781 case RESERVED_DISP_PICTURE:
1789 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1790 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1791 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1795 /*****************************************************************************
1796 * RenderSubPicture: render a subpicture
1797 *****************************************************************************
1798 * This function render a sub picture unit.
1799 *****************************************************************************/
1800 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1802 p_vout_font_t p_font; /* text font */
1803 int i_width, i_height; /* subpicture dimensions */
1805 switch( p_subpic->i_type )
1807 case TEXT_SUBPICTURE: /* single line text */
1808 /* Select default font if not specified */
1809 p_font = p_subpic->type.text.p_font;
1810 if( p_font == NULL )
1812 p_font = p_vout->p_default_font;
1815 /* Computes text size (width and height fields are ignored) and print it */
1816 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1817 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1818 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1820 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1821 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1822 p_subpic->i_y * p_vout->i_bytes_per_line,
1823 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1824 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1825 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1827 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1833 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1838 /*****************************************************************************
1839 * RenderInterface: render the interface
1840 *****************************************************************************
1841 * This function render the interface, if any.
1842 *****************************************************************************/
1843 static void RenderInterface( vout_thread_t *p_vout )
1845 int i_height, i_text_height; /* total and text height */
1846 int i_width_1, i_width_2; /* text width */
1847 int i_byte; /* byte index */
1848 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1849 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1852 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1853 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1854 i_height += i_text_height;
1856 /* Render background */
1857 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1858 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1862 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1865 /* Render text, if not larger than screen */
1866 if( i_width_1 < p_vout->i_width )
1868 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1869 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1870 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1871 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1872 OUTLINED_TEXT, psz_text_1 );
1874 if( i_width_2 < p_vout->i_width )
1876 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1877 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1878 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1879 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1880 OUTLINED_TEXT, psz_text_2 );
1883 /* Activate modified area */
1884 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1887 /*****************************************************************************
1888 * Synchronize: update synchro level depending of heap state
1889 *****************************************************************************
1890 * This function is called during the main vout loop.
1891 *****************************************************************************/
1892 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1894 int i_synchro_inc = 0;
1895 /* XXX?? gore following */
1896 static int i_panic_count = 0;
1897 static int i_last_synchro_inc = 0;
1898 static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1899 static int i_truc = 10;
1903 //fprintf( stderr, "PANIC %d\n", i_panic_count );
1909 if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1912 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1917 if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1920 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1924 if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1925 i_synchro_inc*i_last_synchro_inc < 0 )
1930 if( i_delay < 6000 )
1935 else if( i_delay < 70000 )
1937 i_truc = 24+(24*i_delay)/70000;
1940 i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
1942 else if( i_delay > 100000 )
1944 r_synchro_level += 1;
1945 if( i_delay > 130000 )
1946 r_synchro_level += 1;
1949 r_synchro_level += (float)i_synchro_inc / i_truc;
1950 p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
1952 if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
1954 r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
1957 //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
1958 // p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
1959 i_last_synchro_inc = i_synchro_inc;
1962 /*****************************************************************************
1963 * Manage: manage thread
1964 *****************************************************************************
1965 * This function will handle changes in thread configuration.
1966 *****************************************************************************/
1967 static int Manage( vout_thread_t *p_vout )
1970 if( p_vout->i_changes )
1972 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1973 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1977 /* On gamma or grayscale change, rebuild tables */
1978 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1981 if( vout_ResetYUV( p_vout ) )
1983 intf_ErrMsg("error: can't rebuild convertion tables\n");
1988 /* Clear changes flags which does not need management or have been
1990 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1991 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
1992 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
1994 /* Detect unauthorized changes */
1995 if( p_vout->i_changes )
1997 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
1998 * function, it means they should not be authorized */
1999 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2006 /*****************************************************************************
2007 * Align: align a subpicture in the screen
2008 *****************************************************************************
2009 * This function is used for rendering text or subpictures. It returns non 0
2010 * it the final aera is not fully included in display area. Return coordinates
2012 *****************************************************************************/
2013 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2014 int i_width, int i_height, int i_h_align, int i_v_align )
2016 /* Align horizontally */
2020 *pi_x -= i_width / 2;
2023 *pi_x += (p_vout->i_width - i_width) / 2;
2029 *pi_x += p_vout->i_width - i_width;
2033 /* Align vertically */
2037 *pi_y -= i_height / 2;
2040 *pi_y += (p_vout->i_height - i_height) / 2;
2046 *pi_y += p_vout->i_height - i_height;
2048 case SUBTITLE_RALIGN:
2049 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2050 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2054 /* Return non 0 if clipping failed */
2055 return( (*pi_x < 0) || (*pi_y < 0) ||
2056 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2059 /*****************************************************************************
2060 * SetPalette: sets an 8 bpp palette
2061 *****************************************************************************
2062 * This function is just a prototype that does nothing. Architectures that
2063 * support palette allocation should override it.
2064 *****************************************************************************/
2065 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2066 u16 *green, u16 *blue, u16 *transp )
2068 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );