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 );
107 if( RequestPlugin( &p_vout->vout_plugin, "vout", psz_method ) < 0 )
109 intf_ErrMsg( "error: could not open video plugin vout_%s.so\n", psz_method );
115 p_vout->p_sys_create = GetPluginFunction( p_vout->vout_plugin, "vout_SysCreate" );
116 p_vout->p_sys_init = GetPluginFunction( p_vout->vout_plugin, "vout_SysInit" );
117 p_vout->p_sys_end = GetPluginFunction( p_vout->vout_plugin, "vout_SysEnd" );
118 p_vout->p_sys_destroy = GetPluginFunction( p_vout->vout_plugin, "vout_SysDestroy" );
119 p_vout->p_sys_manage = GetPluginFunction( p_vout->vout_plugin, "vout_SysManage" );
120 p_vout->p_sys_display = GetPluginFunction( p_vout->vout_plugin, "vout_SysDisplay" );
122 /* Initialize thread properties - thread id and locks will be initialized
126 p_vout->b_active = 0;
127 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
128 *p_vout->pi_status = THREAD_CREATE;
130 /* Initialize some fields used by the system-dependant method - these fields will
131 * probably be modified by the method, and are only preferences */
132 p_vout->i_changes = 0;
133 p_vout->i_width = i_width;
134 p_vout->i_height = i_height;
135 p_vout->i_bytes_per_line = i_width * 2;
136 p_vout->i_screen_depth = 15;
137 p_vout->i_bytes_per_pixel = 2;
138 p_vout->f_gamma = VOUT_GAMMA;
140 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
142 p_vout->b_interface = 0;
145 p_vout->p_set_palette = SetPalette;
147 intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
148 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
149 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
151 /* Initialize idle screen */
152 p_vout->last_display_date = mdate();
153 p_vout->last_display_date = 0;
154 p_vout->last_idle_date = 0;
157 /* Initialize statistics fields */
158 p_vout->render_time = 0;
159 p_vout->c_fps_samples = 0;
162 /* Initialize buffer index */
163 p_vout->i_buffer_index = 0;
165 /* Initialize pictures and subpictures - translation tables and functions
166 * will be initialized later in InitThread */
167 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
169 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
170 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
171 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
172 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
174 p_vout->i_pictures = 0;
176 /* Initialize synchronization informations */
177 p_vout->i_synchro_level = VOUT_SYNCHRO_LEVEL_START;
179 /* Create and initialize system-dependant method - this function issues its
180 * own error messages */
181 if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
183 TrashPlugin( p_vout->vout_plugin );
187 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
188 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
189 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
190 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
192 /* Calculate shifts from system-updated masks */
193 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
194 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
195 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
197 /* Set some useful colors */
198 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
199 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
200 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
201 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
203 /* Load fonts - fonts must be initialized after the system method since
204 * they may be dependant on screen depth and other thread properties */
205 p_vout->p_default_font = vout_LoadFont( VOUT_DEFAULT_FONT );
206 if( p_vout->p_default_font == NULL )
208 p_vout->p_sys_destroy( p_vout );
209 TrashPlugin( p_vout->vout_plugin );
213 p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
214 if( p_vout->p_large_font == NULL )
216 vout_UnloadFont( p_vout->p_default_font );
217 p_vout->p_sys_destroy( p_vout );
218 TrashPlugin( p_vout->vout_plugin );
223 /* Create thread and set locks */
224 vlc_mutex_init( &p_vout->picture_lock );
225 vlc_mutex_init( &p_vout->subpicture_lock );
226 vlc_mutex_init( &p_vout->change_lock );
227 vlc_mutex_lock( &p_vout->change_lock );
228 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
230 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
231 vout_UnloadFont( p_vout->p_default_font );
232 vout_UnloadFont( p_vout->p_large_font );
233 p_vout->p_sys_destroy( p_vout );
234 TrashPlugin( p_vout->vout_plugin );
239 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
240 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
242 /* If status is NULL, wait until the thread is created */
243 if( pi_status == NULL )
247 msleep( THREAD_SLEEP );
248 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
249 && (i_status != THREAD_FATAL) );
250 if( i_status != THREAD_READY )
258 /*****************************************************************************
259 * vout_DestroyThread: destroys a previously created thread
260 *****************************************************************************
261 * Destroy a terminated thread.
262 * The function will request a destruction of the specified thread. If pi_error
263 * is NULL, it will return once the thread is destroyed. Else, it will be
264 * update using one of the THREAD_* constants.
265 *****************************************************************************/
266 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
268 int i_status; /* thread status */
272 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
273 *p_vout->pi_status = THREAD_DESTROY;
275 /* Request thread destruction */
278 /* If status is NULL, wait until thread has been destroyed */
279 if( pi_status == NULL )
283 msleep( THREAD_SLEEP );
284 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
285 && (i_status != THREAD_FATAL) );
289 /*****************************************************************************
290 * vout_DisplaySubPicture: display a subpicture unit
291 *****************************************************************************
292 * Remove the reservation flag of an subpicture, which will cause it to be ready
293 * for display. The picture does not need to be locked, since it is ignored by
294 * the output thread if is reserved.
295 *****************************************************************************/
296 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
299 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
300 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
304 /* Check if status is valid */
305 if( p_subpic->i_status != RESERVED_SUBPICTURE )
307 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
308 p_subpic->i_status );
312 /* Remove reservation flag */
313 p_subpic->i_status = READY_SUBPICTURE;
316 /* Send subpicture informations */
317 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
318 p_subpic, p_subpic->i_type,
319 mstrtime( psz_begin_date, p_subpic->begin_date ),
320 mstrtime( psz_end_date, p_subpic->end_date ) );
324 /*****************************************************************************
325 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
326 *****************************************************************************
327 * This function create a reserved subpicture in the video output heap.
328 * A null pointer is returned if the function fails. This method provides an
329 * already allocated zone of memory in the spu data fields. It needs locking
330 * since several pictures can be created by several producers threads.
331 *****************************************************************************/
332 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
335 int i_subpic; /* subpicture index */
336 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
337 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
340 vlc_mutex_lock( &p_vout->subpicture_lock );
343 * Look for an empty place
345 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
347 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
349 /* Subpicture is marked for destruction, but is still allocated */
350 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
351 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
353 /* Memory size do match or is smaller : memory will not be reallocated,
354 * and function can end immediately - this is the best possible case,
355 * since no memory allocation needs to be done */
356 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
358 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
359 &p_vout->p_subpicture[i_subpic] );
361 vlc_mutex_unlock( &p_vout->subpicture_lock );
362 return( &p_vout->p_subpicture[i_subpic] );
364 else if( p_destroyed_subpic == NULL )
366 /* Memory size do not match, but subpicture index will be kept in
367 * case no other place are left */
368 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
371 else if( (p_free_subpic == NULL) &&
372 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
374 /* Subpicture is empty and ready for allocation */
375 p_free_subpic = &p_vout->p_subpicture[i_subpic];
379 /* If no free subpicture is available, use a destroyed subpicture */
380 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
382 /* No free subpicture or matching destroyed subpicture has been found, but
383 * a destroyed subpicture is still avalaible */
384 free( p_destroyed_subpic->p_data );
385 p_free_subpic = p_destroyed_subpic;
391 if( p_free_subpic != NULL )
393 /* Allocate memory */
396 case TEXT_SUBPICTURE: /* text subpicture */
397 p_free_subpic->p_data = malloc( i_size + 1 );
401 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
402 p_free_subpic->p_data = NULL;
407 if( p_free_subpic->p_data != NULL )
408 { /* Copy subpicture informations, set some default values */
409 p_free_subpic->i_type = i_type;
410 p_free_subpic->i_status = RESERVED_SUBPICTURE;
411 p_free_subpic->i_size = i_size;
412 p_free_subpic->i_x = 0;
413 p_free_subpic->i_y = 0;
414 p_free_subpic->i_width = 0;
415 p_free_subpic->i_height = 0;
416 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
417 p_free_subpic->i_vertical_align = CENTER_RALIGN;
421 /* Memory allocation failed : set subpicture as empty */
422 p_free_subpic->i_type = EMPTY_SUBPICTURE;
423 p_free_subpic->i_status = FREE_SUBPICTURE;
424 p_free_subpic = NULL;
425 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
429 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
431 vlc_mutex_unlock( &p_vout->subpicture_lock );
432 return( p_free_subpic );
435 /* No free or destroyed subpicture could be found */
436 intf_DbgMsg( "warning: heap is full\n" );
437 vlc_mutex_unlock( &p_vout->subpicture_lock );
441 /*****************************************************************************
442 * vout_DestroySubPicture: remove a subpicture from the heap
443 *****************************************************************************
444 * This function frees a previously reserved subpicture.
445 * It is meant to be used when the construction of a picture aborted.
446 * This function does not need locking since reserved subpictures are ignored
447 * by the output thread.
448 *****************************************************************************/
449 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
452 /* Check if status is valid */
453 if( p_subpic->i_status != RESERVED_SUBPICTURE )
455 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
456 p_subpic, p_subpic->i_status );
460 p_subpic->i_status = DESTROYED_SUBPICTURE;
463 intf_DbgMsg("subpicture %p\n", p_subpic);
467 /*****************************************************************************
468 * vout_DisplayPicture: display a picture
469 *****************************************************************************
470 * Remove the reservation flag of a picture, which will cause it to be ready for
471 * display. The picture won't be displayed until vout_DatePicture has been
473 *****************************************************************************/
474 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
476 vlc_mutex_lock( &p_vout->picture_lock );
477 switch( p_pic->i_status )
479 case RESERVED_PICTURE:
480 p_pic->i_status = RESERVED_DISP_PICTURE;
482 case RESERVED_DATED_PICTURE:
483 p_pic->i_status = READY_PICTURE;
487 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
493 intf_DbgMsg("picture %p\n", p_pic);
495 vlc_mutex_unlock( &p_vout->picture_lock );
498 /*****************************************************************************
499 * vout_DatePicture: date a picture
500 *****************************************************************************
501 * Remove the reservation flag of a picture, which will cause it to be ready for
502 * display. The picture won't be displayed until vout_DisplayPicture has been
504 *****************************************************************************/
505 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
508 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
511 vlc_mutex_lock( &p_vout->picture_lock );
513 switch( p_pic->i_status )
515 case RESERVED_PICTURE:
516 p_pic->i_status = RESERVED_DATED_PICTURE;
518 case RESERVED_DISP_PICTURE:
519 p_pic->i_status = READY_PICTURE;
523 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
529 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
531 vlc_mutex_unlock( &p_vout->picture_lock );
534 /*****************************************************************************
535 * vout_CreatePicture: allocate a picture in the video output heap.
536 *****************************************************************************
537 * This function create a reserved image in the video output heap.
538 * A null pointer is returned if the function fails. This method provides an
539 * already allocated zone of memory in the picture data fields. It needs locking
540 * since several pictures can be created by several producers threads.
541 *****************************************************************************/
542 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
543 int i_width, int i_height )
545 int i_picture; /* picture index */
546 int i_chroma_width = 0; /* chroma width */
547 picture_t * p_free_picture = NULL; /* first free picture */
548 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
551 vlc_mutex_lock( &p_vout->picture_lock );
554 * Look for an empty place
556 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
558 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
560 /* Picture is marked for destruction, but is still allocated - note
561 * that if width and type are the same for two pictures, chroma_width
562 * should also be the same */
563 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
564 (p_vout->p_picture[i_picture].i_height == i_height) &&
565 (p_vout->p_picture[i_picture].i_width == i_width) )
567 /* Memory size do match : memory will not be reallocated, and function
568 * can end immediately - this is the best possible case, since no
569 * memory allocation needs to be done */
570 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
571 p_vout->i_pictures++;
573 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
574 &p_vout->p_picture[i_picture] );
576 vlc_mutex_unlock( &p_vout->picture_lock );
577 return( &p_vout->p_picture[i_picture] );
579 else if( p_destroyed_picture == NULL )
581 /* Memory size do not match, but picture index will be kept in
582 * case no other place are left */
583 p_destroyed_picture = &p_vout->p_picture[i_picture];
586 else if( (p_free_picture == NULL) &&
587 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
589 /* Picture is empty and ready for allocation */
590 p_free_picture = &p_vout->p_picture[i_picture];
594 /* If no free picture is available, use a destroyed picture */
595 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
597 /* No free picture or matching destroyed picture has been found, but
598 * a destroyed picture is still avalaible */
599 free( p_destroyed_picture->p_data );
600 p_free_picture = p_destroyed_picture;
606 if( p_free_picture != NULL )
608 /* Allocate memory */
611 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
612 i_chroma_width = i_width / 2;
613 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
614 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
615 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
616 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
618 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
619 i_chroma_width = i_width / 2;
620 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
621 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
622 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
623 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
625 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
626 i_chroma_width = i_width;
627 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
628 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
629 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
630 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
634 intf_DbgMsg("error: unknown picture type %d\n", i_type );
635 p_free_picture->p_data = NULL;
640 if( p_free_picture->p_data != NULL )
642 /* Copy picture informations, set some default values */
643 p_free_picture->i_type = i_type;
644 p_free_picture->i_status = RESERVED_PICTURE;
645 p_free_picture->i_matrix_coefficients = 1;
646 p_free_picture->i_width = i_width;
647 p_free_picture->i_height = i_height;
648 p_free_picture->i_chroma_width = i_chroma_width;
649 p_free_picture->i_display_horizontal_offset = 0;
650 p_free_picture->i_display_vertical_offset = 0;
651 p_free_picture->i_display_width = i_width;
652 p_free_picture->i_display_height = i_height;
653 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
654 p_free_picture->i_refcount = 0;
655 p_vout->i_pictures++;
659 /* Memory allocation failed : set picture as empty */
660 p_free_picture->i_type = EMPTY_PICTURE;
661 p_free_picture->i_status = FREE_PICTURE;
662 p_free_picture = NULL;
663 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
667 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
669 vlc_mutex_unlock( &p_vout->picture_lock );
670 return( p_free_picture );
673 /* No free or destroyed picture could be found */
674 intf_DbgMsg( "warning: heap is full\n" );
675 vlc_mutex_unlock( &p_vout->picture_lock );
679 /*****************************************************************************
680 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
681 *****************************************************************************
682 * This function frees a previously reserved picture or a permanent
683 * picture. It is meant to be used when the construction of a picture aborted.
684 * Note that the picture will be destroyed even if it is linked !
685 *****************************************************************************/
686 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
688 vlc_mutex_lock( &p_vout->picture_lock );
691 /* Check if picture status is valid */
692 if( (p_pic->i_status != RESERVED_PICTURE) &&
693 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
694 (p_pic->i_status != RESERVED_DISP_PICTURE) )
696 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
700 p_pic->i_status = DESTROYED_PICTURE;
701 p_vout->i_pictures--;
704 intf_DbgMsg("picture %p\n", p_pic);
706 vlc_mutex_unlock( &p_vout->picture_lock );
709 /*****************************************************************************
710 * vout_LinkPicture: increment reference counter of a picture
711 *****************************************************************************
712 * This function increment the reference counter of a picture in the video
713 * heap. It needs a lock since several producer threads can access the picture.
714 *****************************************************************************/
715 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
717 vlc_mutex_lock( &p_vout->picture_lock );
721 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
724 vlc_mutex_unlock( &p_vout->picture_lock );
727 /*****************************************************************************
728 * vout_UnlinkPicture: decrement reference counter of a picture
729 *****************************************************************************
730 * This function decrement the reference counter of a picture in the video heap.
731 *****************************************************************************/
732 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
734 vlc_mutex_lock( &p_vout->picture_lock );
738 if( p_pic->i_refcount < 0 )
740 intf_DbgMsg("error: refcount < 0\n");
741 p_pic->i_refcount = 0;
745 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
747 p_pic->i_status = DESTROYED_PICTURE;
748 p_vout->i_pictures--;
752 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
755 vlc_mutex_unlock( &p_vout->picture_lock );
758 /*****************************************************************************
759 * vout_SetBuffers: set buffers adresses
760 *****************************************************************************
761 * This function is called by system drivers to set buffers video memory
763 *****************************************************************************/
764 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
766 /* No picture previously */
767 p_vout->p_buffer[0].i_pic_x = 0;
768 p_vout->p_buffer[0].i_pic_y = 0;
769 p_vout->p_buffer[0].i_pic_width = 0;
770 p_vout->p_buffer[0].i_pic_height = 0;
771 p_vout->p_buffer[1].i_pic_x = 0;
772 p_vout->p_buffer[1].i_pic_y = 0;
773 p_vout->p_buffer[1].i_pic_width = 0;
774 p_vout->p_buffer[1].i_pic_height = 0;
776 /* The first area covers all the screen */
777 p_vout->p_buffer[0].i_areas = 1;
778 p_vout->p_buffer[0].pi_area_begin[0] = 0;
779 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
780 p_vout->p_buffer[1].i_areas = 1;
781 p_vout->p_buffer[1].pi_area_begin[0] = 0;
782 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
785 p_vout->p_buffer[0].p_data = p_buf1;
786 p_vout->p_buffer[1].p_data = p_buf2;
789 /*****************************************************************************
790 * vout_Pixel2RGB: return red, green and blue from pixel value
791 *****************************************************************************
792 * Return color values, in 0-255 range, of the decomposition of a pixel. This
793 * is a slow routine and should only be used for initialization phase.
794 *****************************************************************************/
795 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
797 *pi_red = i_pixel & p_vout->i_red_mask;
798 *pi_green = i_pixel & p_vout->i_green_mask;
799 *pi_blue = i_pixel & p_vout->i_blue_mask;
802 /* following functions are local */
804 /*****************************************************************************
805 * BinaryLog: computes the base 2 log of a binary value
806 *****************************************************************************
807 * This functions is used by MaskToShift, to get a bit index from a binary
809 *****************************************************************************/
810 static int BinaryLog(u32 i)
835 if (i != ((u32)1 << i_log))
837 intf_ErrMsg("internal error: binary log overflow\n");
843 /*****************************************************************************
844 * MaskToShift: transform a color mask into right and left shifts
845 *****************************************************************************
846 * This function is used for obtaining color shifts from masks.
847 *****************************************************************************/
848 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
850 u32 i_low, i_high; /* lower hand higher bits of the mask */
853 i_low = i_mask & (- i_mask); /* lower bit of the mask */
854 i_high = i_mask + i_low; /* higher bit of the mask */
856 /* Transform bits into an index */
857 i_low = BinaryLog (i_low);
858 i_high = BinaryLog (i_high);
860 /* Update pointers and return */
862 *pi_right = (8 - i_high + i_low);
865 /*****************************************************************************
866 * InitThread: initialize video output thread
867 *****************************************************************************
868 * This function is called from RunThread and performs the second step of the
869 * initialization. It returns 0 on success. Note that the thread's flag are not
870 * modified inside this function.
871 *****************************************************************************/
872 static int InitThread( vout_thread_t *p_vout )
876 *p_vout->pi_status = THREAD_START;
878 /* Initialize output method - this function issues its own error messages */
879 if( p_vout->p_sys_init( p_vout ) )
884 /* Initialize convertion tables and functions */
885 if( vout_InitYUV( p_vout ) )
887 intf_ErrMsg("error: can't allocate YUV translation tables\n");
891 /* Mark thread as running and return */
892 p_vout->b_active = 1;
893 *p_vout->pi_status = THREAD_READY;
894 intf_DbgMsg("thread ready\n");
898 /*****************************************************************************
899 * RunThread: video output thread
900 *****************************************************************************
901 * Video output thread. This function does only returns when the thread is
902 * terminated. It handles the pictures arriving in the video heap and the
903 * display device events.
904 *****************************************************************************/
905 static void RunThread( vout_thread_t *p_vout)
907 /* XXX?? welcome to gore land */
908 static int i_trash_count = 0;
909 static mtime_t last_display_date = 0;
911 int i_index; /* index in heap */
912 mtime_t current_date; /* current date */
913 mtime_t display_date; /* display date */
914 boolean_t b_display; /* display flag */
915 picture_t * p_pic; /* picture pointer */
916 subpicture_t * p_subpic; /* subpicture pointer */
921 p_vout->b_error = InitThread( p_vout );
922 if( p_vout->b_error )
924 DestroyThread( p_vout, THREAD_ERROR );
930 * Main loop - it is not executed if an error occured during
933 while( (!p_vout->b_die) && (!p_vout->b_error) )
935 /* Initialize loop variables */
939 current_date = mdate();
942 * Find the picture to display - this operation does not need lock,
943 * since only READY_PICTUREs are handled
945 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
947 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
949 (p_vout->p_picture[i_index].date < display_date) ) )
951 p_pic = &p_vout->p_picture[i_index];
952 display_date = p_pic->date;
959 /* Computes FPS rate */
960 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
964 //fprintf( stderr, "gap : %Ld\n", display_date-last_display_date );
965 last_display_date = display_date;
967 if( display_date < current_date && i_trash_count > 4 )
969 /* Picture is late: it will be destroyed and the thread will sleep and
970 * go to next picture */
972 vlc_mutex_lock( &p_vout->picture_lock );
973 if( p_pic->i_refcount )
975 p_pic->i_status = DISPLAYED_PICTURE;
979 p_pic->i_status = DESTROYED_PICTURE;
980 p_vout->i_pictures--;
982 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
983 vlc_mutex_unlock( &p_vout->picture_lock );
985 /* Update synchronization information as if display delay
987 Synchronize( p_vout, display_date - current_date );
995 if( display_date > current_date + VOUT_DISPLAY_DELAY )
997 /* A picture is ready to be rendered, but its rendering date is
998 * far from the current one so the thread will perform an empty loop
999 * as if no picture were found. The picture state is unchanged */
1005 /* Picture will be displayed, update synchronization
1007 Synchronize( p_vout, display_date - current_date );
1011 * Find the subpicture to display - this operation does not need lock, since
1012 * only READY_SUBPICTURES are handled. If no picture has been selected,
1013 * display_date will depend on the subpicture
1018 * Perform rendering, sleep and display rendered picture
1020 if( p_pic ) /* picture and perhaps subpicture */
1022 b_display = p_vout->b_active;
1023 p_vout->last_display_date = display_date;
1027 /* Set picture dimensions and clear buffer */
1028 SetBufferPicture( p_vout, p_pic );
1030 /* Render picture and informations */
1031 RenderPicture( p_vout, p_pic );
1032 if( p_vout->b_info )
1034 RenderPictureInfo( p_vout, p_pic );
1035 RenderInfo( p_vout );
1039 /* Remove picture from heap */
1040 vlc_mutex_lock( &p_vout->picture_lock );
1041 if( p_pic->i_refcount )
1043 p_pic->i_status = DISPLAYED_PICTURE;
1047 p_pic->i_status = DESTROYED_PICTURE;
1048 p_vout->i_pictures--;
1050 vlc_mutex_unlock( &p_vout->picture_lock );
1052 /* Render interface and subpicture */
1053 if( b_display && p_vout->b_interface )
1055 RenderInterface( p_vout );
1061 RenderSubPicture( p_vout, p_subpic );
1064 /* Remove subpicture from heap */
1065 vlc_mutex_lock( &p_vout->subpicture_lock );
1066 p_subpic->i_status = DESTROYED_SUBPICTURE;
1067 vlc_mutex_unlock( &p_vout->subpicture_lock );
1071 else if( p_subpic ) /* subpicture alone */
1073 b_display = p_vout->b_active;
1074 p_vout->last_display_date = display_date;
1079 SetBufferPicture( p_vout, NULL );
1081 /* Render informations, interface and subpicture */
1082 if( p_vout->b_info )
1084 RenderInfo( p_vout );
1086 if( p_vout->b_interface )
1088 RenderInterface( p_vout );
1090 RenderSubPicture( p_vout, p_subpic );
1093 /* Remove subpicture from heap */
1094 vlc_mutex_lock( &p_vout->subpicture_lock );
1095 p_subpic->i_status = DESTROYED_SUBPICTURE;
1096 vlc_mutex_unlock( &p_vout->subpicture_lock );
1098 else if( p_vout->b_active ) /* idle or interface screen alone */
1100 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1102 /* Interface has changed, so a new rendering is required - force
1103 * it by setting last idle date to 0 */
1104 p_vout->last_idle_date = 0;
1107 /* Render idle screen and update idle date, then render interface if
1109 b_display = RenderIdle( p_vout );
1112 p_vout->last_idle_date = current_date;
1113 if( p_vout->b_interface )
1115 RenderInterface( p_vout );
1126 * Sleep, wake up and display rendered picture
1130 /* Store render time */
1131 p_vout->render_time = mdate() - current_date;
1134 /* Give back change lock */
1135 vlc_mutex_unlock( &p_vout->change_lock );
1137 /* Sleep a while or until a given date */
1138 if( display_date != 0 )
1140 mwait( display_date );
1144 msleep( VOUT_IDLE_SLEEP );
1147 /* On awakening, take back lock and send immediately picture to display,
1148 * then swap buffers */
1149 vlc_mutex_lock( &p_vout->change_lock );
1151 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1152 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1154 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1156 p_vout->p_sys_display( p_vout );
1157 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1161 * Check events and manage thread
1163 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1165 /* A fatal error occured, and the thread must terminate immediately,
1166 * without displaying anything - setting b_error to 1 cause the
1167 * immediate end of the main while() loop. */
1168 p_vout->b_error = 1;
1173 * Error loop - wait until the thread destruction is requested
1175 if( p_vout->b_error )
1177 ErrorThread( p_vout );
1181 EndThread( p_vout );
1182 DestroyThread( p_vout, THREAD_OVER );
1183 intf_DbgMsg( "thread end\n" );
1186 /*****************************************************************************
1187 * ErrorThread: RunThread() error loop
1188 *****************************************************************************
1189 * This function is called when an error occured during thread main's loop. The
1190 * thread can still receive feed, but must be ready to terminate as soon as
1192 *****************************************************************************/
1193 static void ErrorThread( vout_thread_t *p_vout )
1195 /* Wait until a `die' order */
1197 while( !p_vout->b_die )
1200 msleep( VOUT_IDLE_SLEEP );
1204 /*****************************************************************************
1205 * EndThread: thread destruction
1206 *****************************************************************************
1207 * This function is called when the thread ends after a sucessfull
1208 * initialization. It frees all ressources allocated by InitThread.
1209 *****************************************************************************/
1210 static void EndThread( vout_thread_t *p_vout )
1212 int i_index; /* index in heap */
1216 *p_vout->pi_status = THREAD_END;
1218 /* Destroy all remaining pictures and subpictures */
1219 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1221 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1223 free( p_vout->p_picture[i_index].p_data );
1225 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1227 free( p_vout->p_subpicture[i_index].p_data );
1231 /* Destroy translation tables */
1232 vout_EndYUV( p_vout );
1233 p_vout->p_sys_end( p_vout );
1236 /*****************************************************************************
1237 * DestroyThread: thread destruction
1238 *****************************************************************************
1239 * This function is called when the thread ends. It frees all ressources
1240 * allocated by CreateThread. Status is available at this stage.
1241 *****************************************************************************/
1242 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1244 int *pi_status; /* status adress */
1246 /* Store status adress */
1248 pi_status = p_vout->pi_status;
1250 /* Destroy thread structures allocated by Create and InitThread */
1251 vout_UnloadFont( p_vout->p_default_font );
1252 vout_UnloadFont( p_vout->p_large_font );
1253 p_vout->p_sys_destroy( p_vout );
1256 TrashPlugin( p_vout->vout_plugin );
1258 /* Free structure */
1260 *pi_status = i_status;
1263 /*****************************************************************************
1264 * Print: print simple text on a picture
1265 *****************************************************************************
1266 * This function will print a simple text on the picture. It is designed to
1267 * print debugging or general informations.
1268 *****************************************************************************/
1269 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 )
1271 int i_text_height; /* total text height */
1272 int i_text_width; /* total text width */
1274 /* Update upper left coordinates according to alignment */
1275 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1276 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1278 /* Set area and print text */
1279 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1280 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1281 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1282 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1283 p_vout->i_white_pixel, 0, 0,
1288 /*****************************************************************************
1289 * SetBufferArea: activate an area in current buffer
1290 *****************************************************************************
1291 * This function is called when something is rendered on the current buffer.
1292 * It set the area as active and prepare it to be cleared on next rendering.
1293 * Pay attention to the fact that in this functions, i_h is in fact the end y
1294 * coordinate of the new area.
1295 *****************************************************************************/
1296 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1298 vout_buffer_t * p_buffer; /* current buffer */
1299 int i_area_begin, i_area_end; /* area vertical extension */
1300 int i_area, i_area_copy; /* area index */
1301 int i_area_shift; /* shift distance for areas */
1303 /* Choose buffer and modify h to end of area position */
1304 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1308 * Remove part of the area which is inside the picture - this is done
1309 * by calling again SetBufferArea with the correct areas dimensions.
1311 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1313 i_area_begin = p_buffer->i_pic_y;
1314 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1316 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1317 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1318 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1320 /* Keep the stripe above the picture, if any */
1321 if( i_y < i_area_begin )
1323 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1325 /* Keep the stripe below the picture, if any */
1326 if( i_h > i_area_end )
1328 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1334 /* Skip some extensions until interesting areas */
1336 (i_area < p_buffer->i_areas) &&
1337 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1343 if( i_area == p_buffer->i_areas )
1345 /* New area is below all existing ones: just add it at the end of the
1346 * array, if possible - else, append it to the last one */
1347 if( i_area < VOUT_MAX_AREAS )
1349 p_buffer->pi_area_begin[i_area] = i_y;
1350 p_buffer->pi_area_end[i_area] = i_h;
1351 p_buffer->i_areas++;
1356 intf_DbgMsg("areas overflow\n");
1358 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1363 i_area_begin = p_buffer->pi_area_begin[i_area];
1364 i_area_end = p_buffer->pi_area_end[i_area];
1366 if( i_y < i_area_begin )
1368 if( i_h >= i_area_begin - 1 )
1370 /* Extend area above */
1371 p_buffer->pi_area_begin[i_area] = i_y;
1375 /* Create a new area above : merge last area if overflow, then
1376 * move all old areas down */
1377 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1380 intf_DbgMsg("areas overflow\n");
1382 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1386 p_buffer->i_areas++;
1388 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1390 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1391 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1393 p_buffer->pi_area_begin[i_area] = i_y;
1394 p_buffer->pi_area_end[i_area] = i_h;
1398 if( i_h > i_area_end )
1400 /* Find further areas which can be merged with the new one */
1401 for( i_area_copy = i_area + 1;
1402 (i_area_copy < p_buffer->i_areas) &&
1403 (p_buffer->pi_area_begin[i_area] <= i_h);
1410 if( i_area_copy != i_area )
1412 /* Merge with last possible areas */
1413 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1415 /* Shift lower areas upward */
1416 i_area_shift = i_area_copy - i_area;
1417 p_buffer->i_areas -= i_area_shift;
1418 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1420 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1421 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1426 /* Extend area below */
1427 p_buffer->pi_area_end[i_area] = i_h;
1433 /*****************************************************************************
1434 * SetBufferPicture: clear buffer and set picture area
1435 *****************************************************************************
1436 * This function is called before any rendering. It clears the current
1437 * rendering buffer and set the new picture area. If the picture pointer is
1438 * NULL, then no picture area is defined. Floating operations are avoided since
1439 * some MMX calculations may follow.
1440 *****************************************************************************/
1441 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1443 vout_buffer_t * p_buffer; /* current buffer */
1444 int i_pic_x, i_pic_y; /* picture position */
1445 int i_pic_width, i_pic_height; /* picture dimensions */
1446 int i_old_pic_y, i_old_pic_height; /* old picture area */
1447 int i_vout_width, i_vout_height; /* display dimensions */
1448 int i_area; /* area index */
1449 int i_data_index; /* area data index */
1450 int i_data_size; /* area data size, in 256 bytes blocs */
1451 u64 * p_data; /* area data, for clearing */
1452 byte_t * p_data8; /* area data, for clearing (slow) */
1454 /* Choose buffer and set display dimensions */
1455 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1456 i_vout_width = p_vout->i_width;
1457 i_vout_height = p_vout->i_height;
1460 * Computes new picture size
1464 /* Try horizontal scaling first - width must be a mutiple of 16 */
1465 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1466 i_vout_width : p_pic->i_width) & ~0xf;
1467 switch( p_pic->i_aspect_ratio )
1469 case AR_3_4_PICTURE:
1470 i_pic_height = i_pic_width * 3 / 4;
1472 case AR_16_9_PICTURE:
1473 i_pic_height = i_pic_width * 9 / 16;
1475 case AR_221_1_PICTURE:
1476 i_pic_height = i_pic_width * 100 / 221;
1478 case AR_SQUARE_PICTURE:
1480 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1484 /* If picture dimensions using horizontal scaling are too large, use
1485 * vertical scaling. Since width must be a multiple of 16, height is
1486 * adjusted again after. */
1487 if( i_pic_height > i_vout_height )
1489 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1490 i_vout_height : p_pic->i_height;
1491 switch( p_pic->i_aspect_ratio )
1493 case AR_3_4_PICTURE:
1494 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1495 i_pic_height = i_pic_width * 3 / 4;
1497 case AR_16_9_PICTURE:
1498 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1499 i_pic_height = i_pic_width * 9 / 16;
1501 case AR_221_1_PICTURE:
1502 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1503 i_pic_height = i_pic_width * 100 / 221;
1505 case AR_SQUARE_PICTURE:
1507 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1508 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1513 /* Set picture position */
1514 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1515 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1519 /* No picture: size is 0 */
1527 * Set new picture size - if is is smaller than the previous one, clear
1528 * around it. Since picture are centered, only their size is tested.
1530 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1532 i_old_pic_y = p_buffer->i_pic_y;
1533 i_old_pic_height = p_buffer->i_pic_height;
1534 p_buffer->i_pic_x = i_pic_x;
1535 p_buffer->i_pic_y = i_pic_y;
1536 p_buffer->i_pic_width = i_pic_width;
1537 p_buffer->i_pic_height = i_pic_height;
1538 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1542 p_buffer->i_pic_x = i_pic_x;
1543 p_buffer->i_pic_y = i_pic_y;
1544 p_buffer->i_pic_width = i_pic_width;
1545 p_buffer->i_pic_height = i_pic_height;
1551 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1554 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1555 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1557 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1558 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1559 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1561 /* Clear 256 bytes block */
1562 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
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;
1571 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1573 /* Clear remaining 16 bytes blocks */
1574 *p_data++ = 0; *p_data++ = 0;
1576 p_data8 = (byte_t *)p_data;
1577 for( i_data_index = i_data_size % 16; i_data_index--; )
1579 /* Clear remaining bytes */
1587 p_buffer->i_areas = 0;
1590 /*****************************************************************************
1591 * RenderPicture: render a picture
1592 *****************************************************************************
1593 * This function convert a picture from a video heap to a pixel-encoded image
1594 * and copy it to the current rendering buffer. No lock is required, since the
1595 * rendered picture has been determined as existant, and will only be destroyed
1596 * by the vout thread later.
1597 *****************************************************************************/
1598 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1601 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1602 mtime_t render_time; /* picture rendering time */
1604 vout_buffer_t * p_buffer; /* rendering buffer */
1605 byte_t * p_pic_data; /* convertion destination */
1607 /* Get and set rendering informations */
1608 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1609 p_pic_data = p_buffer->p_data +
1610 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1611 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1613 render_time = mdate();
1617 * Choose appropriate rendering function and render picture
1619 switch( p_pic->i_type )
1621 case YUV_420_PICTURE:
1622 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1623 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1624 p_pic->i_width, p_pic->i_height,
1625 p_buffer->i_pic_width, p_buffer->i_pic_height,
1626 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1627 p_pic->i_matrix_coefficients );
1629 case YUV_422_PICTURE:
1630 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1631 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1632 p_pic->i_width, p_pic->i_height,
1633 p_buffer->i_pic_width, p_buffer->i_pic_height,
1634 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1635 p_pic->i_matrix_coefficients );
1637 case YUV_444_PICTURE:
1638 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1639 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1640 p_pic->i_width, p_pic->i_height,
1641 p_buffer->i_pic_width, p_buffer->i_pic_height,
1642 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1643 p_pic->i_matrix_coefficients );
1647 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1653 /* Print picture date and rendering time */
1654 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1655 p_vout->i_buffer_index, (long) (mdate() - render_time),
1656 mstrtime( psz_date, p_pic->date ));
1660 /*****************************************************************************
1661 * RenderPictureInfo: print additionnal informations on a picture
1662 *****************************************************************************
1663 * This function will print informations such as fps and other picture
1664 * dependant informations.
1665 *****************************************************************************/
1666 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1668 #if defined(STATS) || defined(DEBUG)
1669 char psz_buffer[256]; /* string buffer */
1674 * Print FPS rate in upper right corner
1676 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1678 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1679 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1680 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1681 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1685 * Print frames count and loop time in upper left corner
1687 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1688 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1689 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1694 * Print picture information in lower right corner
1696 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1697 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1698 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1699 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1700 p_pic->i_width, p_pic->i_height,
1701 p_pic->i_display_width, p_pic->i_display_height,
1702 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1703 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1704 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1705 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1706 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1707 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1708 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1709 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1710 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1711 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1715 /*****************************************************************************
1716 * RenderIdle: render idle picture
1717 *****************************************************************************
1718 * This function will print something on the screen. It will return 0 if
1719 * nothing has been rendered, or 1 if something has been changed on the screen.
1720 * Note that if you absolutely want something to be printed, you will have
1721 * to force it by setting the last idle date to 0.
1722 * Unlike other rendering functions, this one calls the SetBufferPicture
1723 * function when needed.
1724 *****************************************************************************/
1725 static int RenderIdle( vout_thread_t *p_vout )
1727 int i_x = 0, i_y = 0; /* text position */
1728 int i_width, i_height; /* text size */
1729 mtime_t current_date; /* current date */
1730 const char *psz_text = "waiting for stream ..."; /* text to display */
1733 current_date = mdate();
1734 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1735 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1737 SetBufferPicture( p_vout, NULL );
1738 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1739 &i_width, &i_height );
1740 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1742 vout_Print( p_vout->p_large_font,
1743 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1744 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1745 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1746 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1747 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1748 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1755 /*****************************************************************************
1756 * RenderInfo: render additionnal informations
1757 *****************************************************************************
1758 * This function render informations which do not depend of the current picture
1760 *****************************************************************************/
1761 static void RenderInfo( vout_thread_t *p_vout )
1764 char psz_buffer[256]; /* string buffer */
1765 int i_ready_pic = 0; /* ready pictures */
1766 int i_reserved_pic = 0; /* reserved pictures */
1767 int i_picture; /* picture index */
1772 * Print thread state in lower left corner
1774 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1776 switch( p_vout->p_picture[i_picture].i_status )
1778 case RESERVED_PICTURE:
1779 case RESERVED_DATED_PICTURE:
1780 case RESERVED_DISP_PICTURE:
1788 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1789 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1790 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1794 /*****************************************************************************
1795 * RenderSubPicture: render a subpicture
1796 *****************************************************************************
1797 * This function render a sub picture unit.
1798 *****************************************************************************/
1799 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1801 p_vout_font_t p_font; /* text font */
1802 int i_width, i_height; /* subpicture dimensions */
1804 switch( p_subpic->i_type )
1806 case TEXT_SUBPICTURE: /* single line text */
1807 /* Select default font if not specified */
1808 p_font = p_subpic->type.text.p_font;
1809 if( p_font == NULL )
1811 p_font = p_vout->p_default_font;
1814 /* Computes text size (width and height fields are ignored) and print it */
1815 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1816 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1817 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1819 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1820 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1821 p_subpic->i_y * p_vout->i_bytes_per_line,
1822 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1823 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1824 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1826 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1832 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1837 /*****************************************************************************
1838 * RenderInterface: render the interface
1839 *****************************************************************************
1840 * This function render the interface, if any.
1841 *****************************************************************************/
1842 static void RenderInterface( vout_thread_t *p_vout )
1844 int i_height, i_text_height; /* total and text height */
1845 int i_width_1, i_width_2; /* text width */
1846 int i_byte; /* byte index */
1847 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1848 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1851 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1852 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1853 i_height += i_text_height;
1855 /* Render background */
1856 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1857 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1861 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1864 /* Render text, if not larger than screen */
1865 if( i_width_1 < p_vout->i_width )
1867 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1868 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1869 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1870 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1871 OUTLINED_TEXT, psz_text_1 );
1873 if( i_width_2 < p_vout->i_width )
1875 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1876 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1877 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1878 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1879 OUTLINED_TEXT, psz_text_2 );
1882 /* Activate modified area */
1883 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1886 /*****************************************************************************
1887 * Synchronize: update synchro level depending of heap state
1888 *****************************************************************************
1889 * This function is called during the main vout loop.
1890 *****************************************************************************/
1891 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1893 int i_synchro_inc = 0;
1894 /* XXX?? gore following */
1895 static int i_panic_count = 0;
1896 static int i_last_synchro_inc = 0;
1897 static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1898 static int i_truc = 10;
1902 //fprintf( stderr, "PANIC %d\n", i_panic_count );
1908 if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1911 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1916 if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1919 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1923 if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1924 i_synchro_inc*i_last_synchro_inc < 0 )
1929 if( i_delay < 6000 )
1934 else if( i_delay < 70000 )
1936 i_truc = 24+(24*i_delay)/70000;
1939 i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
1941 else if( i_delay > 100000 )
1943 r_synchro_level += 1;
1944 if( i_delay > 130000 )
1945 r_synchro_level += 1;
1948 r_synchro_level += (float)i_synchro_inc / i_truc;
1949 p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
1951 if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
1953 r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
1956 //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
1957 // p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
1958 i_last_synchro_inc = i_synchro_inc;
1961 /*****************************************************************************
1962 * Manage: manage thread
1963 *****************************************************************************
1964 * This function will handle changes in thread configuration.
1965 *****************************************************************************/
1966 static int Manage( vout_thread_t *p_vout )
1969 if( p_vout->i_changes )
1971 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1972 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1976 /* On gamma or grayscale change, rebuild tables */
1977 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1980 if( vout_ResetYUV( p_vout ) )
1982 intf_ErrMsg("error: can't rebuild convertion tables\n");
1987 /* Clear changes flags which does not need management or have been
1989 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1990 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
1991 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
1993 /* Detect unauthorized changes */
1994 if( p_vout->i_changes )
1996 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
1997 * function, it means they should not be authorized */
1998 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2005 /*****************************************************************************
2006 * Align: align a subpicture in the screen
2007 *****************************************************************************
2008 * This function is used for rendering text or subpictures. It returns non 0
2009 * it the final aera is not fully included in display area. Return coordinates
2011 *****************************************************************************/
2012 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2013 int i_width, int i_height, int i_h_align, int i_v_align )
2015 /* Align horizontally */
2019 *pi_x -= i_width / 2;
2022 *pi_x += (p_vout->i_width - i_width) / 2;
2028 *pi_x += p_vout->i_width - i_width;
2032 /* Align vertically */
2036 *pi_y -= i_height / 2;
2039 *pi_y += (p_vout->i_height - i_height) / 2;
2045 *pi_y += p_vout->i_height - i_height;
2047 case SUBTITLE_RALIGN:
2048 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2049 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2053 /* Return non 0 if clipping failed */
2054 return( (*pi_x < 0) || (*pi_y < 0) ||
2055 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2058 /*****************************************************************************
2059 * SetPalette: sets an 8 bpp palette
2060 *****************************************************************************
2061 * This function is just a prototype that does nothing. Architectures that
2062 * support palette allocation should override it.
2063 *****************************************************************************/
2064 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2065 u16 *green, u16 *blue, u16 *transp )
2067 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );