1 /*****************************************************************************
2 * video_output.c : video output thread
3 * This module describes the programming interface for video output threads.
4 * It includes functions allowing to open a new thread, send pictures to a
5 * thread, and destroy a previously oppened video output thread.
6 *****************************************************************************
7 * Copyright (C) 2000 VideoLAN
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <errno.h> /* ENOMEM */
33 #include <stdlib.h> /* free() */
34 #include <stdio.h> /* sprintf() */
35 #include <string.h> /* strerror() */
43 #include "video_output.h"
44 #include "video_text.h"
45 #include "video_yuv.h"
50 /*****************************************************************************
52 *****************************************************************************/
53 static int BinaryLog ( u32 i );
54 static void MaskToShift ( int *pi_left, int *pi_right, u32 i_mask );
55 static int InitThread ( vout_thread_t *p_vout );
56 static void RunThread ( vout_thread_t *p_vout );
57 static void ErrorThread ( vout_thread_t *p_vout );
58 static void EndThread ( vout_thread_t *p_vout );
59 static void DestroyThread ( vout_thread_t *p_vout, int i_status );
60 static void Print ( vout_thread_t *p_vout, int i_x, int i_y,
61 int i_h_align, int i_v_align,
62 unsigned char *psz_text );
63 static void SetBufferArea ( vout_thread_t *p_vout, int i_x, int i_y,
65 static void SetBufferPicture ( vout_thread_t *p_vout, picture_t *p_pic );
66 static void RenderPicture ( vout_thread_t *p_vout, picture_t *p_pic );
67 static void RenderPictureInfo ( vout_thread_t *p_vout, picture_t *p_pic );
68 static void RenderSubPicture ( vout_thread_t *p_vout,
69 subpicture_t *p_subpic );
70 static void RenderInterface ( vout_thread_t *p_vout );
71 static int RenderIdle ( vout_thread_t *p_vout );
72 static void RenderInfo ( vout_thread_t *p_vout );
73 static void Synchronize ( vout_thread_t *p_vout, s64 i_delay );
74 static int Manage ( vout_thread_t *p_vout );
75 static int Align ( vout_thread_t *p_vout, int *pi_x,
76 int *pi_y, int i_width, int i_height,
77 int i_h_align, int i_v_align );
78 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
79 u16 *green, u16 *blue, u16 *transp );
81 /*****************************************************************************
82 * vout_CreateThread: creates a new video output thread
83 *****************************************************************************
84 * This function creates a new video output thread, and returns a pointer
85 * to its description. On error, it returns NULL.
86 * If pi_status is NULL, then the function will block until the thread is ready.
87 * If not, it will be updated using one of the THREAD_* constants.
88 *****************************************************************************/
89 vout_thread_t * vout_CreateThread ( char *psz_display, int i_root_window,
90 int i_width, int i_height, int *pi_status, int i_method )
92 vout_thread_t * p_vout; /* thread descriptor */
93 int i_status; /* thread status */
94 int i_index; /* index for array initialization */
97 /* Allocate descriptor */
99 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
102 intf_ErrMsg( "error: %s\n", strerror(ENOMEM) );
106 /* Request an interface plugin */
107 psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
109 if( RequestPlugin( &p_vout->vout_plugin, "vout", psz_method ) < 0 )
111 intf_ErrMsg( "error: could not open video plugin vout_%s.so\n", psz_method );
117 p_vout->p_sys_create = GetPluginFunction( p_vout->vout_plugin, "vout_SysCreate" );
118 p_vout->p_sys_init = GetPluginFunction( p_vout->vout_plugin, "vout_SysInit" );
119 p_vout->p_sys_end = GetPluginFunction( p_vout->vout_plugin, "vout_SysEnd" );
120 p_vout->p_sys_destroy = GetPluginFunction( p_vout->vout_plugin, "vout_SysDestroy" );
121 p_vout->p_sys_manage = GetPluginFunction( p_vout->vout_plugin, "vout_SysManage" );
122 p_vout->p_sys_display = GetPluginFunction( p_vout->vout_plugin, "vout_SysDisplay" );
124 /* Initialize thread properties - thread id and locks will be initialized
128 p_vout->b_active = 0;
129 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
130 *p_vout->pi_status = THREAD_CREATE;
132 /* Initialize some fields used by the system-dependant method - these fields will
133 * probably be modified by the method, and are only preferences */
134 p_vout->i_changes = 0;
135 p_vout->i_width = i_width;
136 p_vout->i_height = i_height;
137 p_vout->i_bytes_per_line = i_width * 2;
138 p_vout->i_screen_depth = 15;
139 p_vout->i_bytes_per_pixel = 2;
140 p_vout->f_gamma = VOUT_GAMMA;
142 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
144 p_vout->b_interface = 0;
147 p_vout->p_set_palette = SetPalette;
149 intf_DbgMsg("wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
150 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
151 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
153 /* Initialize idle screen */
154 p_vout->last_display_date = mdate();
155 p_vout->last_display_date = 0;
156 p_vout->last_idle_date = 0;
159 /* Initialize statistics fields */
160 p_vout->render_time = 0;
161 p_vout->c_fps_samples = 0;
164 /* Initialize buffer index */
165 p_vout->i_buffer_index = 0;
167 /* Initialize pictures and subpictures - translation tables and functions
168 * will be initialized later in InitThread */
169 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
171 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
172 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
173 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
174 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
176 p_vout->i_pictures = 0;
178 /* Initialize synchronization informations */
179 p_vout->i_synchro_level = VOUT_SYNCHRO_LEVEL_START;
181 /* Create and initialize system-dependant method - this function issues its
182 * own error messages */
183 if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
185 TrashPlugin( p_vout->vout_plugin );
189 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
190 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
191 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
192 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
194 /* Calculate shifts from system-updated masks */
195 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
196 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
197 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
199 /* Set some useful colors */
200 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
201 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
202 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
203 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
205 /* Load fonts - fonts must be initialized after the system method since
206 * they may be dependant on screen depth and other thread properties */
207 p_vout->p_default_font = vout_LoadFont( DATA_PATH "/" VOUT_DEFAULT_FONT );
208 if( p_vout->p_default_font == NULL )
210 p_vout->p_default_font = vout_LoadFont( "share/" VOUT_DEFAULT_FONT );
212 if( p_vout->p_default_font == NULL )
214 p_vout->p_sys_destroy( p_vout );
215 TrashPlugin( p_vout->vout_plugin );
219 p_vout->p_large_font = vout_LoadFont( DATA_PATH "/" VOUT_LARGE_FONT );
220 if( p_vout->p_large_font == NULL )
222 p_vout->p_large_font = vout_LoadFont( "share/" VOUT_LARGE_FONT );
224 if( p_vout->p_large_font == NULL )
226 vout_UnloadFont( p_vout->p_default_font );
227 p_vout->p_sys_destroy( p_vout );
228 TrashPlugin( p_vout->vout_plugin );
233 /* Create thread and set locks */
234 vlc_mutex_init( &p_vout->picture_lock );
235 vlc_mutex_init( &p_vout->subpicture_lock );
236 vlc_mutex_init( &p_vout->change_lock );
237 vlc_mutex_lock( &p_vout->change_lock );
238 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
240 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
241 vout_UnloadFont( p_vout->p_default_font );
242 vout_UnloadFont( p_vout->p_large_font );
243 p_vout->p_sys_destroy( p_vout );
244 TrashPlugin( p_vout->vout_plugin );
249 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
250 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
252 /* If status is NULL, wait until the thread is created */
253 if( pi_status == NULL )
257 msleep( THREAD_SLEEP );
258 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
259 && (i_status != THREAD_FATAL) );
260 if( i_status != THREAD_READY )
268 /*****************************************************************************
269 * vout_DestroyThread: destroys a previously created thread
270 *****************************************************************************
271 * Destroy a terminated thread.
272 * The function will request a destruction of the specified thread. If pi_error
273 * is NULL, it will return once the thread is destroyed. Else, it will be
274 * update using one of the THREAD_* constants.
275 *****************************************************************************/
276 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
278 int i_status; /* thread status */
282 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
283 *p_vout->pi_status = THREAD_DESTROY;
285 /* Request thread destruction */
288 /* If status is NULL, wait until thread has been destroyed */
289 if( pi_status == NULL )
293 msleep( THREAD_SLEEP );
294 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
295 && (i_status != THREAD_FATAL) );
299 /*****************************************************************************
300 * vout_DisplaySubPicture: display a subpicture unit
301 *****************************************************************************
302 * Remove the reservation flag of an subpicture, which will cause it to be ready
303 * for display. The picture does not need to be locked, since it is ignored by
304 * the output thread if is reserved.
305 *****************************************************************************/
306 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
309 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
310 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
314 /* Check if status is valid */
315 if( p_subpic->i_status != RESERVED_SUBPICTURE )
317 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
318 p_subpic->i_status );
322 /* Remove reservation flag */
323 p_subpic->i_status = READY_SUBPICTURE;
326 /* Send subpicture informations */
327 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
328 p_subpic, p_subpic->i_type,
329 mstrtime( psz_begin_date, p_subpic->begin_date ),
330 mstrtime( psz_end_date, p_subpic->end_date ) );
334 /*****************************************************************************
335 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
336 *****************************************************************************
337 * This function create a reserved subpicture in the video output heap.
338 * A null pointer is returned if the function fails. This method provides an
339 * already allocated zone of memory in the spu data fields. It needs locking
340 * since several pictures can be created by several producers threads.
341 *****************************************************************************/
342 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
345 int i_subpic; /* subpicture index */
346 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
347 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
350 vlc_mutex_lock( &p_vout->subpicture_lock );
353 * Look for an empty place
355 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
357 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
359 /* Subpicture is marked for destruction, but is still allocated */
360 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
361 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
363 /* Memory size do match or is smaller : memory will not be reallocated,
364 * and function can end immediately - this is the best possible case,
365 * since no memory allocation needs to be done */
366 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
368 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
369 &p_vout->p_subpicture[i_subpic] );
371 vlc_mutex_unlock( &p_vout->subpicture_lock );
372 return( &p_vout->p_subpicture[i_subpic] );
374 else if( p_destroyed_subpic == NULL )
376 /* Memory size do not match, but subpicture index will be kept in
377 * case no other place are left */
378 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
381 else if( (p_free_subpic == NULL) &&
382 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
384 /* Subpicture is empty and ready for allocation */
385 p_free_subpic = &p_vout->p_subpicture[i_subpic];
389 /* If no free subpicture is available, use a destroyed subpicture */
390 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
392 /* No free subpicture or matching destroyed subpicture has been found, but
393 * a destroyed subpicture is still avalaible */
394 free( p_destroyed_subpic->p_data );
395 p_free_subpic = p_destroyed_subpic;
401 if( p_free_subpic != NULL )
403 /* Allocate memory */
406 case TEXT_SUBPICTURE: /* text subpicture */
407 p_free_subpic->p_data = malloc( i_size + 1 );
411 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
412 p_free_subpic->p_data = NULL;
417 if( p_free_subpic->p_data != NULL )
418 { /* Copy subpicture informations, set some default values */
419 p_free_subpic->i_type = i_type;
420 p_free_subpic->i_status = RESERVED_SUBPICTURE;
421 p_free_subpic->i_size = i_size;
422 p_free_subpic->i_x = 0;
423 p_free_subpic->i_y = 0;
424 p_free_subpic->i_width = 0;
425 p_free_subpic->i_height = 0;
426 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
427 p_free_subpic->i_vertical_align = CENTER_RALIGN;
431 /* Memory allocation failed : set subpicture as empty */
432 p_free_subpic->i_type = EMPTY_SUBPICTURE;
433 p_free_subpic->i_status = FREE_SUBPICTURE;
434 p_free_subpic = NULL;
435 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
439 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
441 vlc_mutex_unlock( &p_vout->subpicture_lock );
442 return( p_free_subpic );
445 /* No free or destroyed subpicture could be found */
446 intf_DbgMsg( "warning: heap is full\n" );
447 vlc_mutex_unlock( &p_vout->subpicture_lock );
451 /*****************************************************************************
452 * vout_DestroySubPicture: remove a subpicture from the heap
453 *****************************************************************************
454 * This function frees a previously reserved subpicture.
455 * It is meant to be used when the construction of a picture aborted.
456 * This function does not need locking since reserved subpictures are ignored
457 * by the output thread.
458 *****************************************************************************/
459 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
462 /* Check if status is valid */
463 if( p_subpic->i_status != RESERVED_SUBPICTURE )
465 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
466 p_subpic, p_subpic->i_status );
470 p_subpic->i_status = DESTROYED_SUBPICTURE;
473 intf_DbgMsg("subpicture %p\n", p_subpic);
477 /*****************************************************************************
478 * vout_DisplayPicture: display a picture
479 *****************************************************************************
480 * Remove the reservation flag of a picture, which will cause it to be ready for
481 * display. The picture won't be displayed until vout_DatePicture has been
483 *****************************************************************************/
484 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
486 vlc_mutex_lock( &p_vout->picture_lock );
487 switch( p_pic->i_status )
489 case RESERVED_PICTURE:
490 p_pic->i_status = RESERVED_DISP_PICTURE;
492 case RESERVED_DATED_PICTURE:
493 p_pic->i_status = READY_PICTURE;
497 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
503 intf_DbgMsg("picture %p\n", p_pic);
505 vlc_mutex_unlock( &p_vout->picture_lock );
508 /*****************************************************************************
509 * vout_DatePicture: date a picture
510 *****************************************************************************
511 * Remove the reservation flag of a picture, which will cause it to be ready for
512 * display. The picture won't be displayed until vout_DisplayPicture has been
514 *****************************************************************************/
515 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
518 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
521 vlc_mutex_lock( &p_vout->picture_lock );
523 switch( p_pic->i_status )
525 case RESERVED_PICTURE:
526 p_pic->i_status = RESERVED_DATED_PICTURE;
528 case RESERVED_DISP_PICTURE:
529 p_pic->i_status = READY_PICTURE;
533 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
539 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
541 vlc_mutex_unlock( &p_vout->picture_lock );
544 /*****************************************************************************
545 * vout_CreatePicture: allocate a picture in the video output heap.
546 *****************************************************************************
547 * This function create a reserved image in the video output heap.
548 * A null pointer is returned if the function fails. This method provides an
549 * already allocated zone of memory in the picture data fields. It needs locking
550 * since several pictures can be created by several producers threads.
551 *****************************************************************************/
552 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
553 int i_width, int i_height )
555 int i_picture; /* picture index */
556 int i_chroma_width = 0; /* chroma width */
557 picture_t * p_free_picture = NULL; /* first free picture */
558 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
561 vlc_mutex_lock( &p_vout->picture_lock );
564 * Look for an empty place
566 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
568 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
570 /* Picture is marked for destruction, but is still allocated - note
571 * that if width and type are the same for two pictures, chroma_width
572 * should also be the same */
573 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
574 (p_vout->p_picture[i_picture].i_height == i_height) &&
575 (p_vout->p_picture[i_picture].i_width == i_width) )
577 /* Memory size do match : memory will not be reallocated, and function
578 * can end immediately - this is the best possible case, since no
579 * memory allocation needs to be done */
580 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
581 p_vout->i_pictures++;
583 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
584 &p_vout->p_picture[i_picture] );
586 vlc_mutex_unlock( &p_vout->picture_lock );
587 return( &p_vout->p_picture[i_picture] );
589 else if( p_destroyed_picture == NULL )
591 /* Memory size do not match, but picture index will be kept in
592 * case no other place are left */
593 p_destroyed_picture = &p_vout->p_picture[i_picture];
596 else if( (p_free_picture == NULL) &&
597 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
599 /* Picture is empty and ready for allocation */
600 p_free_picture = &p_vout->p_picture[i_picture];
604 /* If no free picture is available, use a destroyed picture */
605 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
607 /* No free picture or matching destroyed picture has been found, but
608 * a destroyed picture is still avalaible */
609 free( p_destroyed_picture->p_data );
610 p_free_picture = p_destroyed_picture;
616 if( p_free_picture != NULL )
618 /* Allocate memory */
621 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
622 i_chroma_width = i_width / 2;
623 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
624 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
625 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
626 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
628 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
629 i_chroma_width = i_width / 2;
630 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
631 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
632 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
633 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
635 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
636 i_chroma_width = i_width;
637 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
638 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
639 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
640 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
644 intf_DbgMsg("error: unknown picture type %d\n", i_type );
645 p_free_picture->p_data = NULL;
650 if( p_free_picture->p_data != NULL )
652 /* Copy picture informations, set some default values */
653 p_free_picture->i_type = i_type;
654 p_free_picture->i_status = RESERVED_PICTURE;
655 p_free_picture->i_matrix_coefficients = 1;
656 p_free_picture->i_width = i_width;
657 p_free_picture->i_height = i_height;
658 p_free_picture->i_chroma_width = i_chroma_width;
659 p_free_picture->i_display_horizontal_offset = 0;
660 p_free_picture->i_display_vertical_offset = 0;
661 p_free_picture->i_display_width = i_width;
662 p_free_picture->i_display_height = i_height;
663 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
664 p_free_picture->i_refcount = 0;
665 p_vout->i_pictures++;
669 /* Memory allocation failed : set picture as empty */
670 p_free_picture->i_type = EMPTY_PICTURE;
671 p_free_picture->i_status = FREE_PICTURE;
672 p_free_picture = NULL;
673 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
677 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
679 vlc_mutex_unlock( &p_vout->picture_lock );
680 return( p_free_picture );
683 /* No free or destroyed picture could be found */
684 intf_DbgMsg( "warning: heap is full\n" );
685 vlc_mutex_unlock( &p_vout->picture_lock );
689 /*****************************************************************************
690 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
691 *****************************************************************************
692 * This function frees a previously reserved picture or a permanent
693 * picture. It is meant to be used when the construction of a picture aborted.
694 * Note that the picture will be destroyed even if it is linked !
695 *****************************************************************************/
696 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
698 vlc_mutex_lock( &p_vout->picture_lock );
701 /* Check if picture status is valid */
702 if( (p_pic->i_status != RESERVED_PICTURE) &&
703 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
704 (p_pic->i_status != RESERVED_DISP_PICTURE) )
706 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
710 p_pic->i_status = DESTROYED_PICTURE;
711 p_vout->i_pictures--;
714 intf_DbgMsg("picture %p\n", p_pic);
716 vlc_mutex_unlock( &p_vout->picture_lock );
719 /*****************************************************************************
720 * vout_LinkPicture: increment reference counter of a picture
721 *****************************************************************************
722 * This function increment the reference counter of a picture in the video
723 * heap. It needs a lock since several producer threads can access the picture.
724 *****************************************************************************/
725 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
727 vlc_mutex_lock( &p_vout->picture_lock );
731 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
734 vlc_mutex_unlock( &p_vout->picture_lock );
737 /*****************************************************************************
738 * vout_UnlinkPicture: decrement reference counter of a picture
739 *****************************************************************************
740 * This function decrement the reference counter of a picture in the video heap.
741 *****************************************************************************/
742 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
744 vlc_mutex_lock( &p_vout->picture_lock );
748 if( p_pic->i_refcount < 0 )
750 intf_DbgMsg("error: refcount < 0\n");
751 p_pic->i_refcount = 0;
755 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
757 p_pic->i_status = DESTROYED_PICTURE;
758 p_vout->i_pictures--;
762 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
765 vlc_mutex_unlock( &p_vout->picture_lock );
768 /*****************************************************************************
769 * vout_SetBuffers: set buffers adresses
770 *****************************************************************************
771 * This function is called by system drivers to set buffers video memory
773 *****************************************************************************/
774 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
776 /* No picture previously */
777 p_vout->p_buffer[0].i_pic_x = 0;
778 p_vout->p_buffer[0].i_pic_y = 0;
779 p_vout->p_buffer[0].i_pic_width = 0;
780 p_vout->p_buffer[0].i_pic_height = 0;
781 p_vout->p_buffer[1].i_pic_x = 0;
782 p_vout->p_buffer[1].i_pic_y = 0;
783 p_vout->p_buffer[1].i_pic_width = 0;
784 p_vout->p_buffer[1].i_pic_height = 0;
786 /* The first area covers all the screen */
787 p_vout->p_buffer[0].i_areas = 1;
788 p_vout->p_buffer[0].pi_area_begin[0] = 0;
789 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
790 p_vout->p_buffer[1].i_areas = 1;
791 p_vout->p_buffer[1].pi_area_begin[0] = 0;
792 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
795 p_vout->p_buffer[0].p_data = p_buf1;
796 p_vout->p_buffer[1].p_data = p_buf2;
799 /*****************************************************************************
800 * vout_Pixel2RGB: return red, green and blue from pixel value
801 *****************************************************************************
802 * Return color values, in 0-255 range, of the decomposition of a pixel. This
803 * is a slow routine and should only be used for initialization phase.
804 *****************************************************************************/
805 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
807 *pi_red = i_pixel & p_vout->i_red_mask;
808 *pi_green = i_pixel & p_vout->i_green_mask;
809 *pi_blue = i_pixel & p_vout->i_blue_mask;
812 /* following functions are local */
814 /*****************************************************************************
815 * BinaryLog: computes the base 2 log of a binary value
816 *****************************************************************************
817 * This functions is used by MaskToShift, to get a bit index from a binary
819 *****************************************************************************/
820 static int BinaryLog(u32 i)
845 if (i != ((u32)1 << i_log))
847 intf_ErrMsg("internal error: binary log overflow\n");
853 /*****************************************************************************
854 * MaskToShift: transform a color mask into right and left shifts
855 *****************************************************************************
856 * This function is used for obtaining color shifts from masks.
857 *****************************************************************************/
858 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
860 u32 i_low, i_high; /* lower hand higher bits of the mask */
863 i_low = i_mask & (- i_mask); /* lower bit of the mask */
864 i_high = i_mask + i_low; /* higher bit of the mask */
866 /* Transform bits into an index */
867 i_low = BinaryLog (i_low);
868 i_high = BinaryLog (i_high);
870 /* Update pointers and return */
872 *pi_right = (8 - i_high + i_low);
875 /*****************************************************************************
876 * InitThread: initialize video output thread
877 *****************************************************************************
878 * This function is called from RunThread and performs the second step of the
879 * initialization. It returns 0 on success. Note that the thread's flag are not
880 * modified inside this function.
881 *****************************************************************************/
882 static int InitThread( vout_thread_t *p_vout )
886 *p_vout->pi_status = THREAD_START;
888 /* Initialize output method - this function issues its own error messages */
889 if( p_vout->p_sys_init( p_vout ) )
894 /* Initialize convertion tables and functions */
895 if( vout_InitYUV( p_vout ) )
897 intf_ErrMsg("error: can't allocate YUV translation tables\n");
901 /* Mark thread as running and return */
902 p_vout->b_active = 1;
903 *p_vout->pi_status = THREAD_READY;
904 intf_DbgMsg("thread ready\n");
908 /*****************************************************************************
909 * RunThread: video output thread
910 *****************************************************************************
911 * Video output thread. This function does only returns when the thread is
912 * terminated. It handles the pictures arriving in the video heap and the
913 * display device events.
914 *****************************************************************************/
915 static void RunThread( vout_thread_t *p_vout)
917 /* XXX?? welcome to gore land */
918 static int i_trash_count = 0;
919 static mtime_t last_display_date = 0;
921 int i_index; /* index in heap */
922 mtime_t current_date; /* current date */
923 mtime_t display_date; /* display date */
924 boolean_t b_display; /* display flag */
925 picture_t * p_pic; /* picture pointer */
926 subpicture_t * p_subpic; /* subpicture pointer */
931 p_vout->b_error = InitThread( p_vout );
932 if( p_vout->b_error )
934 DestroyThread( p_vout, THREAD_ERROR );
940 * Main loop - it is not executed if an error occured during
943 while( (!p_vout->b_die) && (!p_vout->b_error) )
945 /* Initialize loop variables */
949 current_date = mdate();
952 * Find the picture to display - this operation does not need lock,
953 * since only READY_PICTUREs are handled
955 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
957 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
959 (p_vout->p_picture[i_index].date < display_date) ) )
961 p_pic = &p_vout->p_picture[i_index];
962 display_date = p_pic->date;
969 /* Computes FPS rate */
970 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
974 //fprintf( stderr, "gap : %Ld\n", display_date-last_display_date );
975 last_display_date = display_date;
977 if( display_date < current_date && i_trash_count > 4 )
979 /* Picture is late: it will be destroyed and the thread will sleep and
980 * go to next picture */
982 vlc_mutex_lock( &p_vout->picture_lock );
983 if( p_pic->i_refcount )
985 p_pic->i_status = DISPLAYED_PICTURE;
989 p_pic->i_status = DESTROYED_PICTURE;
990 p_vout->i_pictures--;
992 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
993 vlc_mutex_unlock( &p_vout->picture_lock );
995 /* Update synchronization information as if display delay
997 Synchronize( p_vout, display_date - current_date );
1005 if( display_date > current_date + VOUT_DISPLAY_DELAY )
1007 /* A picture is ready to be rendered, but its rendering date is
1008 * far from the current one so the thread will perform an empty loop
1009 * as if no picture were found. The picture state is unchanged */
1015 /* Picture will be displayed, update synchronization
1017 Synchronize( p_vout, display_date - current_date );
1021 * Find the subpicture to display - this operation does not need lock, since
1022 * only READY_SUBPICTURES are handled. If no picture has been selected,
1023 * display_date will depend on the subpicture
1028 * Perform rendering, sleep and display rendered picture
1030 if( p_pic ) /* picture and perhaps subpicture */
1032 b_display = p_vout->b_active;
1033 p_vout->last_display_date = display_date;
1037 /* Set picture dimensions and clear buffer */
1038 SetBufferPicture( p_vout, p_pic );
1040 /* Render picture and informations */
1041 RenderPicture( p_vout, p_pic );
1042 if( p_vout->b_info )
1044 RenderPictureInfo( p_vout, p_pic );
1045 RenderInfo( p_vout );
1049 /* Remove picture from heap */
1050 vlc_mutex_lock( &p_vout->picture_lock );
1051 if( p_pic->i_refcount )
1053 p_pic->i_status = DISPLAYED_PICTURE;
1057 p_pic->i_status = DESTROYED_PICTURE;
1058 p_vout->i_pictures--;
1060 vlc_mutex_unlock( &p_vout->picture_lock );
1062 /* Render interface and subpicture */
1063 if( b_display && p_vout->b_interface )
1065 RenderInterface( p_vout );
1071 RenderSubPicture( p_vout, p_subpic );
1074 /* Remove subpicture from heap */
1075 vlc_mutex_lock( &p_vout->subpicture_lock );
1076 p_subpic->i_status = DESTROYED_SUBPICTURE;
1077 vlc_mutex_unlock( &p_vout->subpicture_lock );
1081 else if( p_subpic ) /* subpicture alone */
1083 b_display = p_vout->b_active;
1084 p_vout->last_display_date = display_date;
1089 SetBufferPicture( p_vout, NULL );
1091 /* Render informations, interface and subpicture */
1092 if( p_vout->b_info )
1094 RenderInfo( p_vout );
1096 if( p_vout->b_interface )
1098 RenderInterface( p_vout );
1100 RenderSubPicture( p_vout, p_subpic );
1103 /* Remove subpicture from heap */
1104 vlc_mutex_lock( &p_vout->subpicture_lock );
1105 p_subpic->i_status = DESTROYED_SUBPICTURE;
1106 vlc_mutex_unlock( &p_vout->subpicture_lock );
1108 else if( p_vout->b_active ) /* idle or interface screen alone */
1110 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1112 /* Interface has changed, so a new rendering is required - force
1113 * it by setting last idle date to 0 */
1114 p_vout->last_idle_date = 0;
1117 /* Render idle screen and update idle date, then render interface if
1119 b_display = RenderIdle( p_vout );
1122 p_vout->last_idle_date = current_date;
1123 if( p_vout->b_interface )
1125 RenderInterface( p_vout );
1136 * Sleep, wake up and display rendered picture
1140 /* Store render time */
1141 p_vout->render_time = mdate() - current_date;
1144 /* Give back change lock */
1145 vlc_mutex_unlock( &p_vout->change_lock );
1147 /* Sleep a while or until a given date */
1148 if( display_date != 0 )
1150 mwait( display_date );
1154 msleep( VOUT_IDLE_SLEEP );
1157 /* On awakening, take back lock and send immediately picture to display,
1158 * then swap buffers */
1159 vlc_mutex_lock( &p_vout->change_lock );
1161 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1162 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1164 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1166 p_vout->p_sys_display( p_vout );
1167 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1171 * Check events and manage thread
1173 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1175 /* A fatal error occured, and the thread must terminate immediately,
1176 * without displaying anything - setting b_error to 1 cause the
1177 * immediate end of the main while() loop. */
1178 p_vout->b_error = 1;
1183 * Error loop - wait until the thread destruction is requested
1185 if( p_vout->b_error )
1187 ErrorThread( p_vout );
1191 EndThread( p_vout );
1192 DestroyThread( p_vout, THREAD_OVER );
1193 intf_DbgMsg( "thread end\n" );
1196 /*****************************************************************************
1197 * ErrorThread: RunThread() error loop
1198 *****************************************************************************
1199 * This function is called when an error occured during thread main's loop. The
1200 * thread can still receive feed, but must be ready to terminate as soon as
1202 *****************************************************************************/
1203 static void ErrorThread( vout_thread_t *p_vout )
1205 /* Wait until a `die' order */
1207 while( !p_vout->b_die )
1210 msleep( VOUT_IDLE_SLEEP );
1214 /*****************************************************************************
1215 * EndThread: thread destruction
1216 *****************************************************************************
1217 * This function is called when the thread ends after a sucessfull
1218 * initialization. It frees all ressources allocated by InitThread.
1219 *****************************************************************************/
1220 static void EndThread( vout_thread_t *p_vout )
1222 int i_index; /* index in heap */
1226 *p_vout->pi_status = THREAD_END;
1228 /* Destroy all remaining pictures and subpictures */
1229 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1231 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1233 free( p_vout->p_picture[i_index].p_data );
1235 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1237 free( p_vout->p_subpicture[i_index].p_data );
1241 /* Destroy translation tables */
1242 vout_EndYUV( p_vout );
1243 p_vout->p_sys_end( p_vout );
1246 /*****************************************************************************
1247 * DestroyThread: thread destruction
1248 *****************************************************************************
1249 * This function is called when the thread ends. It frees all ressources
1250 * allocated by CreateThread. Status is available at this stage.
1251 *****************************************************************************/
1252 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1254 int *pi_status; /* status adress */
1256 /* Store status adress */
1258 pi_status = p_vout->pi_status;
1260 /* Destroy thread structures allocated by Create and InitThread */
1261 vout_UnloadFont( p_vout->p_default_font );
1262 vout_UnloadFont( p_vout->p_large_font );
1263 p_vout->p_sys_destroy( p_vout );
1266 TrashPlugin( p_vout->vout_plugin );
1268 /* Free structure */
1270 *pi_status = i_status;
1273 /*****************************************************************************
1274 * Print: print simple text on a picture
1275 *****************************************************************************
1276 * This function will print a simple text on the picture. It is designed to
1277 * print debugging or general informations.
1278 *****************************************************************************/
1279 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 )
1281 int i_text_height; /* total text height */
1282 int i_text_width; /* total text width */
1284 /* Update upper left coordinates according to alignment */
1285 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1286 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1288 /* Set area and print text */
1289 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1290 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1291 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1292 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1293 p_vout->i_white_pixel, 0, 0,
1298 /*****************************************************************************
1299 * SetBufferArea: activate an area in current buffer
1300 *****************************************************************************
1301 * This function is called when something is rendered on the current buffer.
1302 * It set the area as active and prepare it to be cleared on next rendering.
1303 * Pay attention to the fact that in this functions, i_h is in fact the end y
1304 * coordinate of the new area.
1305 *****************************************************************************/
1306 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1308 vout_buffer_t * p_buffer; /* current buffer */
1309 int i_area_begin, i_area_end; /* area vertical extension */
1310 int i_area, i_area_copy; /* area index */
1311 int i_area_shift; /* shift distance for areas */
1313 /* Choose buffer and modify h to end of area position */
1314 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1318 * Remove part of the area which is inside the picture - this is done
1319 * by calling again SetBufferArea with the correct areas dimensions.
1321 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1323 i_area_begin = p_buffer->i_pic_y;
1324 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1326 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1327 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1328 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1330 /* Keep the stripe above the picture, if any */
1331 if( i_y < i_area_begin )
1333 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1335 /* Keep the stripe below the picture, if any */
1336 if( i_h > i_area_end )
1338 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1344 /* Skip some extensions until interesting areas */
1346 (i_area < p_buffer->i_areas) &&
1347 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1353 if( i_area == p_buffer->i_areas )
1355 /* New area is below all existing ones: just add it at the end of the
1356 * array, if possible - else, append it to the last one */
1357 if( i_area < VOUT_MAX_AREAS )
1359 p_buffer->pi_area_begin[i_area] = i_y;
1360 p_buffer->pi_area_end[i_area] = i_h;
1361 p_buffer->i_areas++;
1366 intf_DbgMsg("areas overflow\n");
1368 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1373 i_area_begin = p_buffer->pi_area_begin[i_area];
1374 i_area_end = p_buffer->pi_area_end[i_area];
1376 if( i_y < i_area_begin )
1378 if( i_h >= i_area_begin - 1 )
1380 /* Extend area above */
1381 p_buffer->pi_area_begin[i_area] = i_y;
1385 /* Create a new area above : merge last area if overflow, then
1386 * move all old areas down */
1387 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1390 intf_DbgMsg("areas overflow\n");
1392 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1396 p_buffer->i_areas++;
1398 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1400 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1401 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1403 p_buffer->pi_area_begin[i_area] = i_y;
1404 p_buffer->pi_area_end[i_area] = i_h;
1408 if( i_h > i_area_end )
1410 /* Find further areas which can be merged with the new one */
1411 for( i_area_copy = i_area + 1;
1412 (i_area_copy < p_buffer->i_areas) &&
1413 (p_buffer->pi_area_begin[i_area] <= i_h);
1420 if( i_area_copy != i_area )
1422 /* Merge with last possible areas */
1423 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1425 /* Shift lower areas upward */
1426 i_area_shift = i_area_copy - i_area;
1427 p_buffer->i_areas -= i_area_shift;
1428 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1430 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1431 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1436 /* Extend area below */
1437 p_buffer->pi_area_end[i_area] = i_h;
1443 /*****************************************************************************
1444 * SetBufferPicture: clear buffer and set picture area
1445 *****************************************************************************
1446 * This function is called before any rendering. It clears the current
1447 * rendering buffer and set the new picture area. If the picture pointer is
1448 * NULL, then no picture area is defined. Floating operations are avoided since
1449 * some MMX calculations may follow.
1450 *****************************************************************************/
1451 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1453 vout_buffer_t * p_buffer; /* current buffer */
1454 int i_pic_x, i_pic_y; /* picture position */
1455 int i_pic_width, i_pic_height; /* picture dimensions */
1456 int i_old_pic_y, i_old_pic_height; /* old picture area */
1457 int i_vout_width, i_vout_height; /* display dimensions */
1458 int i_area; /* area index */
1459 int i_data_index; /* area data index */
1460 int i_data_size; /* area data size, in 256 bytes blocs */
1461 u64 * p_data; /* area data, for clearing */
1462 byte_t * p_data8; /* area data, for clearing (slow) */
1464 /* Choose buffer and set display dimensions */
1465 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1466 i_vout_width = p_vout->i_width;
1467 i_vout_height = p_vout->i_height;
1470 * Computes new picture size
1474 /* Try horizontal scaling first - width must be a mutiple of 16 */
1475 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1476 i_vout_width : p_pic->i_width) & ~0xf;
1477 switch( p_pic->i_aspect_ratio )
1479 case AR_3_4_PICTURE:
1480 i_pic_height = i_pic_width * 3 / 4;
1482 case AR_16_9_PICTURE:
1483 i_pic_height = i_pic_width * 9 / 16;
1485 case AR_221_1_PICTURE:
1486 i_pic_height = i_pic_width * 100 / 221;
1488 case AR_SQUARE_PICTURE:
1490 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1494 /* If picture dimensions using horizontal scaling are too large, use
1495 * vertical scaling. Since width must be a multiple of 16, height is
1496 * adjusted again after. */
1497 if( i_pic_height > i_vout_height )
1499 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1500 i_vout_height : p_pic->i_height;
1501 switch( p_pic->i_aspect_ratio )
1503 case AR_3_4_PICTURE:
1504 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1505 i_pic_height = i_pic_width * 3 / 4;
1507 case AR_16_9_PICTURE:
1508 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1509 i_pic_height = i_pic_width * 9 / 16;
1511 case AR_221_1_PICTURE:
1512 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1513 i_pic_height = i_pic_width * 100 / 221;
1515 case AR_SQUARE_PICTURE:
1517 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1518 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1523 /* Set picture position */
1524 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1525 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1529 /* No picture: size is 0 */
1537 * Set new picture size - if is is smaller than the previous one, clear
1538 * around it. Since picture are centered, only their size is tested.
1540 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1542 i_old_pic_y = p_buffer->i_pic_y;
1543 i_old_pic_height = p_buffer->i_pic_height;
1544 p_buffer->i_pic_x = i_pic_x;
1545 p_buffer->i_pic_y = i_pic_y;
1546 p_buffer->i_pic_width = i_pic_width;
1547 p_buffer->i_pic_height = i_pic_height;
1548 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1552 p_buffer->i_pic_x = i_pic_x;
1553 p_buffer->i_pic_y = i_pic_y;
1554 p_buffer->i_pic_width = i_pic_width;
1555 p_buffer->i_pic_height = i_pic_height;
1561 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1564 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1565 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1567 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1568 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1569 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1571 /* Clear 256 bytes block */
1572 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1573 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1574 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1575 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1576 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1577 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1578 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1579 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1581 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1583 /* Clear remaining 16 bytes blocks */
1584 *p_data++ = 0; *p_data++ = 0;
1586 p_data8 = (byte_t *)p_data;
1587 for( i_data_index = i_data_size % 16; i_data_index--; )
1589 /* Clear remaining bytes */
1597 p_buffer->i_areas = 0;
1600 /*****************************************************************************
1601 * RenderPicture: render a picture
1602 *****************************************************************************
1603 * This function convert a picture from a video heap to a pixel-encoded image
1604 * and copy it to the current rendering buffer. No lock is required, since the
1605 * rendered picture has been determined as existant, and will only be destroyed
1606 * by the vout thread later.
1607 *****************************************************************************/
1608 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1611 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1612 mtime_t render_time; /* picture rendering time */
1614 vout_buffer_t * p_buffer; /* rendering buffer */
1615 byte_t * p_pic_data; /* convertion destination */
1617 /* Get and set rendering informations */
1618 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1619 p_pic_data = p_buffer->p_data +
1620 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1621 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1623 render_time = mdate();
1627 * Choose appropriate rendering function and render picture
1629 switch( p_pic->i_type )
1631 case YUV_420_PICTURE:
1632 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1633 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1634 p_pic->i_width, p_pic->i_height,
1635 p_buffer->i_pic_width, p_buffer->i_pic_height,
1636 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1637 p_pic->i_matrix_coefficients );
1639 case YUV_422_PICTURE:
1640 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1641 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1642 p_pic->i_width, p_pic->i_height,
1643 p_buffer->i_pic_width, p_buffer->i_pic_height,
1644 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1645 p_pic->i_matrix_coefficients );
1647 case YUV_444_PICTURE:
1648 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1649 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1650 p_pic->i_width, p_pic->i_height,
1651 p_buffer->i_pic_width, p_buffer->i_pic_height,
1652 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1653 p_pic->i_matrix_coefficients );
1657 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1663 /* Print picture date and rendering time */
1664 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1665 p_vout->i_buffer_index, (long) (mdate() - render_time),
1666 mstrtime( psz_date, p_pic->date ));
1670 /*****************************************************************************
1671 * RenderPictureInfo: print additionnal informations on a picture
1672 *****************************************************************************
1673 * This function will print informations such as fps and other picture
1674 * dependant informations.
1675 *****************************************************************************/
1676 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1678 #if defined(STATS) || defined(DEBUG)
1679 char psz_buffer[256]; /* string buffer */
1684 * Print FPS rate in upper right corner
1686 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1688 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1689 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1690 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1691 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1695 * Print frames count and loop time in upper left corner
1697 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1698 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1699 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1704 * Print picture information in lower right corner
1706 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1707 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1708 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1709 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1710 p_pic->i_width, p_pic->i_height,
1711 p_pic->i_display_width, p_pic->i_display_height,
1712 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1713 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1714 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1715 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1716 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1717 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1718 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1719 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1720 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1721 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1725 /*****************************************************************************
1726 * RenderIdle: render idle picture
1727 *****************************************************************************
1728 * This function will print something on the screen. It will return 0 if
1729 * nothing has been rendered, or 1 if something has been changed on the screen.
1730 * Note that if you absolutely want something to be printed, you will have
1731 * to force it by setting the last idle date to 0.
1732 * Unlike other rendering functions, this one calls the SetBufferPicture
1733 * function when needed.
1734 *****************************************************************************/
1735 static int RenderIdle( vout_thread_t *p_vout )
1737 int i_x = 0, i_y = 0; /* text position */
1738 int i_width, i_height; /* text size */
1739 mtime_t current_date; /* current date */
1740 const char *psz_text = "waiting for stream ..."; /* text to display */
1743 current_date = mdate();
1744 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1745 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1747 SetBufferPicture( p_vout, NULL );
1748 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1749 &i_width, &i_height );
1750 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1752 vout_Print( p_vout->p_large_font,
1753 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1754 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1755 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1756 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1757 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1758 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1765 /*****************************************************************************
1766 * RenderInfo: render additionnal informations
1767 *****************************************************************************
1768 * This function render informations which do not depend of the current picture
1770 *****************************************************************************/
1771 static void RenderInfo( vout_thread_t *p_vout )
1774 char psz_buffer[256]; /* string buffer */
1775 int i_ready_pic = 0; /* ready pictures */
1776 int i_reserved_pic = 0; /* reserved pictures */
1777 int i_picture; /* picture index */
1782 * Print thread state in lower left corner
1784 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1786 switch( p_vout->p_picture[i_picture].i_status )
1788 case RESERVED_PICTURE:
1789 case RESERVED_DATED_PICTURE:
1790 case RESERVED_DISP_PICTURE:
1798 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1799 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1800 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1804 /*****************************************************************************
1805 * RenderSubPicture: render a subpicture
1806 *****************************************************************************
1807 * This function render a sub picture unit.
1808 *****************************************************************************/
1809 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1811 p_vout_font_t p_font; /* text font */
1812 int i_width, i_height; /* subpicture dimensions */
1814 switch( p_subpic->i_type )
1816 case TEXT_SUBPICTURE: /* single line text */
1817 /* Select default font if not specified */
1818 p_font = p_subpic->type.text.p_font;
1819 if( p_font == NULL )
1821 p_font = p_vout->p_default_font;
1824 /* Computes text size (width and height fields are ignored) and print it */
1825 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1826 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1827 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1829 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1830 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1831 p_subpic->i_y * p_vout->i_bytes_per_line,
1832 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1833 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1834 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1836 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1842 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1847 /*****************************************************************************
1848 * RenderInterface: render the interface
1849 *****************************************************************************
1850 * This function render the interface, if any.
1851 *****************************************************************************/
1852 static void RenderInterface( vout_thread_t *p_vout )
1854 int i_height, i_text_height; /* total and text height */
1855 int i_width_1, i_width_2; /* text width */
1856 int i_byte; /* byte index */
1857 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1858 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1861 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1862 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1863 i_height += i_text_height;
1865 /* Render background */
1866 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1867 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1871 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1874 /* Render text, if not larger than screen */
1875 if( i_width_1 < p_vout->i_width )
1877 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1878 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1879 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1880 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1881 OUTLINED_TEXT, psz_text_1 );
1883 if( i_width_2 < p_vout->i_width )
1885 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1886 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1887 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1888 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1889 OUTLINED_TEXT, psz_text_2 );
1892 /* Activate modified area */
1893 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1896 /*****************************************************************************
1897 * Synchronize: update synchro level depending of heap state
1898 *****************************************************************************
1899 * This function is called during the main vout loop.
1900 *****************************************************************************/
1901 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1903 int i_synchro_inc = 0;
1904 /* XXX?? gore following */
1905 static int i_panic_count = 0;
1906 static int i_last_synchro_inc = 0;
1907 static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1908 static int i_truc = 10;
1912 //fprintf( stderr, "PANIC %d\n", i_panic_count );
1918 if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1921 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1926 if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1929 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1933 if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1934 i_synchro_inc*i_last_synchro_inc < 0 )
1939 if( i_delay < 6000 )
1944 else if( i_delay < 70000 )
1946 i_truc = 24+(24*i_delay)/70000;
1949 i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
1951 else if( i_delay > 100000 )
1953 r_synchro_level += 1;
1954 if( i_delay > 130000 )
1955 r_synchro_level += 1;
1958 r_synchro_level += (float)i_synchro_inc / i_truc;
1959 p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
1961 if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
1963 r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
1966 //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
1967 // p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
1968 i_last_synchro_inc = i_synchro_inc;
1971 /*****************************************************************************
1972 * Manage: manage thread
1973 *****************************************************************************
1974 * This function will handle changes in thread configuration.
1975 *****************************************************************************/
1976 static int Manage( vout_thread_t *p_vout )
1979 if( p_vout->i_changes )
1981 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1982 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1986 /* On gamma or grayscale change, rebuild tables */
1987 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1990 if( vout_ResetYUV( p_vout ) )
1992 intf_ErrMsg("error: can't rebuild convertion tables\n");
1997 /* Clear changes flags which does not need management or have been
1999 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2000 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
2001 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
2003 /* Detect unauthorized changes */
2004 if( p_vout->i_changes )
2006 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
2007 * function, it means they should not be authorized */
2008 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2015 /*****************************************************************************
2016 * Align: align a subpicture in the screen
2017 *****************************************************************************
2018 * This function is used for rendering text or subpictures. It returns non 0
2019 * it the final aera is not fully included in display area. Return coordinates
2021 *****************************************************************************/
2022 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2023 int i_width, int i_height, int i_h_align, int i_v_align )
2025 /* Align horizontally */
2029 *pi_x -= i_width / 2;
2032 *pi_x += (p_vout->i_width - i_width) / 2;
2038 *pi_x += p_vout->i_width - i_width;
2042 /* Align vertically */
2046 *pi_y -= i_height / 2;
2049 *pi_y += (p_vout->i_height - i_height) / 2;
2055 *pi_y += p_vout->i_height - i_height;
2057 case SUBTITLE_RALIGN:
2058 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2059 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2063 /* Return non 0 if clipping failed */
2064 return( (*pi_x < 0) || (*pi_y < 0) ||
2065 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2068 /*****************************************************************************
2069 * SetPalette: sets an 8 bpp palette
2070 *****************************************************************************
2071 * This function is just a prototype that does nothing. Architectures that
2072 * support palette allocation should override it.
2073 *****************************************************************************/
2074 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2075 u16 *green, u16 *blue, u16 *transp )
2077 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );