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( VOUT_DEFAULT_FONT );
208 if( p_vout->p_default_font == NULL )
210 p_vout->p_sys_destroy( p_vout );
211 TrashPlugin( p_vout->vout_plugin );
215 p_vout->p_large_font = vout_LoadFont( VOUT_LARGE_FONT );
216 if( p_vout->p_large_font == NULL )
218 vout_UnloadFont( p_vout->p_default_font );
219 p_vout->p_sys_destroy( p_vout );
220 TrashPlugin( p_vout->vout_plugin );
225 /* Create thread and set locks */
226 vlc_mutex_init( &p_vout->picture_lock );
227 vlc_mutex_init( &p_vout->subpicture_lock );
228 vlc_mutex_init( &p_vout->change_lock );
229 vlc_mutex_lock( &p_vout->change_lock );
230 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
232 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
233 vout_UnloadFont( p_vout->p_default_font );
234 vout_UnloadFont( p_vout->p_large_font );
235 p_vout->p_sys_destroy( p_vout );
236 TrashPlugin( p_vout->vout_plugin );
241 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
242 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
244 /* If status is NULL, wait until the thread is created */
245 if( pi_status == NULL )
249 msleep( THREAD_SLEEP );
250 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
251 && (i_status != THREAD_FATAL) );
252 if( i_status != THREAD_READY )
260 /*****************************************************************************
261 * vout_DestroyThread: destroys a previously created thread
262 *****************************************************************************
263 * Destroy a terminated thread.
264 * The function will request a destruction of the specified thread. If pi_error
265 * is NULL, it will return once the thread is destroyed. Else, it will be
266 * update using one of the THREAD_* constants.
267 *****************************************************************************/
268 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
270 int i_status; /* thread status */
274 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
275 *p_vout->pi_status = THREAD_DESTROY;
277 /* Request thread destruction */
280 /* If status is NULL, wait until thread has been destroyed */
281 if( pi_status == NULL )
285 msleep( THREAD_SLEEP );
286 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
287 && (i_status != THREAD_FATAL) );
291 /*****************************************************************************
292 * vout_DisplaySubPicture: display a subpicture unit
293 *****************************************************************************
294 * Remove the reservation flag of an subpicture, which will cause it to be ready
295 * for display. The picture does not need to be locked, since it is ignored by
296 * the output thread if is reserved.
297 *****************************************************************************/
298 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
301 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
302 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
306 /* Check if status is valid */
307 if( p_subpic->i_status != RESERVED_SUBPICTURE )
309 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
310 p_subpic->i_status );
314 /* Remove reservation flag */
315 p_subpic->i_status = READY_SUBPICTURE;
318 /* Send subpicture informations */
319 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
320 p_subpic, p_subpic->i_type,
321 mstrtime( psz_begin_date, p_subpic->begin_date ),
322 mstrtime( psz_end_date, p_subpic->end_date ) );
326 /*****************************************************************************
327 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
328 *****************************************************************************
329 * This function create a reserved subpicture in the video output heap.
330 * A null pointer is returned if the function fails. This method provides an
331 * already allocated zone of memory in the spu data fields. It needs locking
332 * since several pictures can be created by several producers threads.
333 *****************************************************************************/
334 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
337 int i_subpic; /* subpicture index */
338 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
339 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
342 vlc_mutex_lock( &p_vout->subpicture_lock );
345 * Look for an empty place
347 for( i_subpic = 0; i_subpic < VOUT_MAX_PICTURES; i_subpic++ )
349 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
351 /* Subpicture is marked for destruction, but is still allocated */
352 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
353 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
355 /* Memory size do match or is smaller : memory will not be reallocated,
356 * and function can end immediately - this is the best possible case,
357 * since no memory allocation needs to be done */
358 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
360 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
361 &p_vout->p_subpicture[i_subpic] );
363 vlc_mutex_unlock( &p_vout->subpicture_lock );
364 return( &p_vout->p_subpicture[i_subpic] );
366 else if( p_destroyed_subpic == NULL )
368 /* Memory size do not match, but subpicture index will be kept in
369 * case no other place are left */
370 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
373 else if( (p_free_subpic == NULL) &&
374 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
376 /* Subpicture is empty and ready for allocation */
377 p_free_subpic = &p_vout->p_subpicture[i_subpic];
381 /* If no free subpicture is available, use a destroyed subpicture */
382 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
384 /* No free subpicture or matching destroyed subpicture has been found, but
385 * a destroyed subpicture is still avalaible */
386 free( p_destroyed_subpic->p_data );
387 p_free_subpic = p_destroyed_subpic;
393 if( p_free_subpic != NULL )
395 /* Allocate memory */
398 case TEXT_SUBPICTURE: /* text subpicture */
399 p_free_subpic->p_data = malloc( i_size + 1 );
403 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
404 p_free_subpic->p_data = NULL;
409 if( p_free_subpic->p_data != NULL )
410 { /* Copy subpicture informations, set some default values */
411 p_free_subpic->i_type = i_type;
412 p_free_subpic->i_status = RESERVED_SUBPICTURE;
413 p_free_subpic->i_size = i_size;
414 p_free_subpic->i_x = 0;
415 p_free_subpic->i_y = 0;
416 p_free_subpic->i_width = 0;
417 p_free_subpic->i_height = 0;
418 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
419 p_free_subpic->i_vertical_align = CENTER_RALIGN;
423 /* Memory allocation failed : set subpicture as empty */
424 p_free_subpic->i_type = EMPTY_SUBPICTURE;
425 p_free_subpic->i_status = FREE_SUBPICTURE;
426 p_free_subpic = NULL;
427 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
431 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
433 vlc_mutex_unlock( &p_vout->subpicture_lock );
434 return( p_free_subpic );
437 /* No free or destroyed subpicture could be found */
438 intf_DbgMsg( "warning: heap is full\n" );
439 vlc_mutex_unlock( &p_vout->subpicture_lock );
443 /*****************************************************************************
444 * vout_DestroySubPicture: remove a subpicture from the heap
445 *****************************************************************************
446 * This function frees a previously reserved subpicture.
447 * It is meant to be used when the construction of a picture aborted.
448 * This function does not need locking since reserved subpictures are ignored
449 * by the output thread.
450 *****************************************************************************/
451 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
454 /* Check if status is valid */
455 if( p_subpic->i_status != RESERVED_SUBPICTURE )
457 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
458 p_subpic, p_subpic->i_status );
462 p_subpic->i_status = DESTROYED_SUBPICTURE;
465 intf_DbgMsg("subpicture %p\n", p_subpic);
469 /*****************************************************************************
470 * vout_DisplayPicture: display a picture
471 *****************************************************************************
472 * Remove the reservation flag of a picture, which will cause it to be ready for
473 * display. The picture won't be displayed until vout_DatePicture has been
475 *****************************************************************************/
476 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
478 vlc_mutex_lock( &p_vout->picture_lock );
479 switch( p_pic->i_status )
481 case RESERVED_PICTURE:
482 p_pic->i_status = RESERVED_DISP_PICTURE;
484 case RESERVED_DATED_PICTURE:
485 p_pic->i_status = READY_PICTURE;
489 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
495 intf_DbgMsg("picture %p\n", p_pic);
497 vlc_mutex_unlock( &p_vout->picture_lock );
500 /*****************************************************************************
501 * vout_DatePicture: date a picture
502 *****************************************************************************
503 * Remove the reservation flag of a picture, which will cause it to be ready for
504 * display. The picture won't be displayed until vout_DisplayPicture has been
506 *****************************************************************************/
507 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
510 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
513 vlc_mutex_lock( &p_vout->picture_lock );
515 switch( p_pic->i_status )
517 case RESERVED_PICTURE:
518 p_pic->i_status = RESERVED_DATED_PICTURE;
520 case RESERVED_DISP_PICTURE:
521 p_pic->i_status = READY_PICTURE;
525 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
531 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
533 vlc_mutex_unlock( &p_vout->picture_lock );
536 /*****************************************************************************
537 * vout_CreatePicture: allocate a picture in the video output heap.
538 *****************************************************************************
539 * This function create a reserved image in the video output heap.
540 * A null pointer is returned if the function fails. This method provides an
541 * already allocated zone of memory in the picture data fields. It needs locking
542 * since several pictures can be created by several producers threads.
543 *****************************************************************************/
544 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
545 int i_width, int i_height )
547 int i_picture; /* picture index */
548 int i_chroma_width = 0; /* chroma width */
549 picture_t * p_free_picture = NULL; /* first free picture */
550 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
553 vlc_mutex_lock( &p_vout->picture_lock );
556 * Look for an empty place
558 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
560 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
562 /* Picture is marked for destruction, but is still allocated - note
563 * that if width and type are the same for two pictures, chroma_width
564 * should also be the same */
565 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
566 (p_vout->p_picture[i_picture].i_height == i_height) &&
567 (p_vout->p_picture[i_picture].i_width == i_width) )
569 /* Memory size do match : memory will not be reallocated, and function
570 * can end immediately - this is the best possible case, since no
571 * memory allocation needs to be done */
572 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
573 p_vout->i_pictures++;
575 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
576 &p_vout->p_picture[i_picture] );
578 vlc_mutex_unlock( &p_vout->picture_lock );
579 return( &p_vout->p_picture[i_picture] );
581 else if( p_destroyed_picture == NULL )
583 /* Memory size do not match, but picture index will be kept in
584 * case no other place are left */
585 p_destroyed_picture = &p_vout->p_picture[i_picture];
588 else if( (p_free_picture == NULL) &&
589 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
591 /* Picture is empty and ready for allocation */
592 p_free_picture = &p_vout->p_picture[i_picture];
596 /* If no free picture is available, use a destroyed picture */
597 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
599 /* No free picture or matching destroyed picture has been found, but
600 * a destroyed picture is still avalaible */
601 free( p_destroyed_picture->p_data );
602 p_free_picture = p_destroyed_picture;
608 if( p_free_picture != NULL )
610 /* Allocate memory */
613 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
614 i_chroma_width = i_width / 2;
615 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
616 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
617 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*4/2;
618 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
620 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
621 i_chroma_width = i_width / 2;
622 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
623 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
624 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
625 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
627 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
628 i_chroma_width = i_width;
629 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
630 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
631 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
632 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
636 intf_DbgMsg("error: unknown picture type %d\n", i_type );
637 p_free_picture->p_data = NULL;
642 if( p_free_picture->p_data != NULL )
644 /* Copy picture informations, set some default values */
645 p_free_picture->i_type = i_type;
646 p_free_picture->i_status = RESERVED_PICTURE;
647 p_free_picture->i_matrix_coefficients = 1;
648 p_free_picture->i_width = i_width;
649 p_free_picture->i_height = i_height;
650 p_free_picture->i_chroma_width = i_chroma_width;
651 p_free_picture->i_display_horizontal_offset = 0;
652 p_free_picture->i_display_vertical_offset = 0;
653 p_free_picture->i_display_width = i_width;
654 p_free_picture->i_display_height = i_height;
655 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
656 p_free_picture->i_refcount = 0;
657 p_vout->i_pictures++;
661 /* Memory allocation failed : set picture as empty */
662 p_free_picture->i_type = EMPTY_PICTURE;
663 p_free_picture->i_status = FREE_PICTURE;
664 p_free_picture = NULL;
665 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
669 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
671 vlc_mutex_unlock( &p_vout->picture_lock );
672 return( p_free_picture );
675 /* No free or destroyed picture could be found */
676 intf_DbgMsg( "warning: heap is full\n" );
677 vlc_mutex_unlock( &p_vout->picture_lock );
681 /*****************************************************************************
682 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
683 *****************************************************************************
684 * This function frees a previously reserved picture or a permanent
685 * picture. It is meant to be used when the construction of a picture aborted.
686 * Note that the picture will be destroyed even if it is linked !
687 *****************************************************************************/
688 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
690 vlc_mutex_lock( &p_vout->picture_lock );
693 /* Check if picture status is valid */
694 if( (p_pic->i_status != RESERVED_PICTURE) &&
695 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
696 (p_pic->i_status != RESERVED_DISP_PICTURE) )
698 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
702 p_pic->i_status = DESTROYED_PICTURE;
703 p_vout->i_pictures--;
706 intf_DbgMsg("picture %p\n", p_pic);
708 vlc_mutex_unlock( &p_vout->picture_lock );
711 /*****************************************************************************
712 * vout_LinkPicture: increment reference counter of a picture
713 *****************************************************************************
714 * This function increment the reference counter of a picture in the video
715 * heap. It needs a lock since several producer threads can access the picture.
716 *****************************************************************************/
717 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
719 vlc_mutex_lock( &p_vout->picture_lock );
723 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
726 vlc_mutex_unlock( &p_vout->picture_lock );
729 /*****************************************************************************
730 * vout_UnlinkPicture: decrement reference counter of a picture
731 *****************************************************************************
732 * This function decrement the reference counter of a picture in the video heap.
733 *****************************************************************************/
734 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
736 vlc_mutex_lock( &p_vout->picture_lock );
740 if( p_pic->i_refcount < 0 )
742 intf_DbgMsg("error: refcount < 0\n");
743 p_pic->i_refcount = 0;
747 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
749 p_pic->i_status = DESTROYED_PICTURE;
750 p_vout->i_pictures--;
754 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
757 vlc_mutex_unlock( &p_vout->picture_lock );
760 /*****************************************************************************
761 * vout_SetBuffers: set buffers adresses
762 *****************************************************************************
763 * This function is called by system drivers to set buffers video memory
765 *****************************************************************************/
766 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
768 /* No picture previously */
769 p_vout->p_buffer[0].i_pic_x = 0;
770 p_vout->p_buffer[0].i_pic_y = 0;
771 p_vout->p_buffer[0].i_pic_width = 0;
772 p_vout->p_buffer[0].i_pic_height = 0;
773 p_vout->p_buffer[1].i_pic_x = 0;
774 p_vout->p_buffer[1].i_pic_y = 0;
775 p_vout->p_buffer[1].i_pic_width = 0;
776 p_vout->p_buffer[1].i_pic_height = 0;
778 /* The first area covers all the screen */
779 p_vout->p_buffer[0].i_areas = 1;
780 p_vout->p_buffer[0].pi_area_begin[0] = 0;
781 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
782 p_vout->p_buffer[1].i_areas = 1;
783 p_vout->p_buffer[1].pi_area_begin[0] = 0;
784 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
787 p_vout->p_buffer[0].p_data = p_buf1;
788 p_vout->p_buffer[1].p_data = p_buf2;
791 /*****************************************************************************
792 * vout_Pixel2RGB: return red, green and blue from pixel value
793 *****************************************************************************
794 * Return color values, in 0-255 range, of the decomposition of a pixel. This
795 * is a slow routine and should only be used for initialization phase.
796 *****************************************************************************/
797 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
799 *pi_red = i_pixel & p_vout->i_red_mask;
800 *pi_green = i_pixel & p_vout->i_green_mask;
801 *pi_blue = i_pixel & p_vout->i_blue_mask;
804 /* following functions are local */
806 /*****************************************************************************
807 * BinaryLog: computes the base 2 log of a binary value
808 *****************************************************************************
809 * This functions is used by MaskToShift, to get a bit index from a binary
811 *****************************************************************************/
812 static int BinaryLog(u32 i)
837 if (i != ((u32)1 << i_log))
839 intf_ErrMsg("internal error: binary log overflow\n");
845 /*****************************************************************************
846 * MaskToShift: transform a color mask into right and left shifts
847 *****************************************************************************
848 * This function is used for obtaining color shifts from masks.
849 *****************************************************************************/
850 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
852 u32 i_low, i_high; /* lower hand higher bits of the mask */
855 i_low = i_mask & (- i_mask); /* lower bit of the mask */
856 i_high = i_mask + i_low; /* higher bit of the mask */
858 /* Transform bits into an index */
859 i_low = BinaryLog (i_low);
860 i_high = BinaryLog (i_high);
862 /* Update pointers and return */
864 *pi_right = (8 - i_high + i_low);
867 /*****************************************************************************
868 * InitThread: initialize video output thread
869 *****************************************************************************
870 * This function is called from RunThread and performs the second step of the
871 * initialization. It returns 0 on success. Note that the thread's flag are not
872 * modified inside this function.
873 *****************************************************************************/
874 static int InitThread( vout_thread_t *p_vout )
878 *p_vout->pi_status = THREAD_START;
880 /* Initialize output method - this function issues its own error messages */
881 if( p_vout->p_sys_init( p_vout ) )
886 /* Initialize convertion tables and functions */
887 if( vout_InitYUV( p_vout ) )
889 intf_ErrMsg("error: can't allocate YUV translation tables\n");
893 /* Mark thread as running and return */
894 p_vout->b_active = 1;
895 *p_vout->pi_status = THREAD_READY;
896 intf_DbgMsg("thread ready\n");
900 /*****************************************************************************
901 * RunThread: video output thread
902 *****************************************************************************
903 * Video output thread. This function does only returns when the thread is
904 * terminated. It handles the pictures arriving in the video heap and the
905 * display device events.
906 *****************************************************************************/
907 static void RunThread( vout_thread_t *p_vout)
909 /* XXX?? welcome to gore land */
910 static int i_trash_count = 0;
911 static mtime_t last_display_date = 0;
913 int i_index; /* index in heap */
914 mtime_t current_date; /* current date */
915 mtime_t display_date; /* display date */
916 boolean_t b_display; /* display flag */
917 picture_t * p_pic; /* picture pointer */
918 subpicture_t * p_subpic; /* subpicture pointer */
923 p_vout->b_error = InitThread( p_vout );
924 if( p_vout->b_error )
926 DestroyThread( p_vout, THREAD_ERROR );
932 * Main loop - it is not executed if an error occured during
935 while( (!p_vout->b_die) && (!p_vout->b_error) )
937 /* Initialize loop variables */
941 current_date = mdate();
944 * Find the picture to display - this operation does not need lock,
945 * since only READY_PICTUREs are handled
947 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
949 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
951 (p_vout->p_picture[i_index].date < display_date) ) )
953 p_pic = &p_vout->p_picture[i_index];
954 display_date = p_pic->date;
961 /* Computes FPS rate */
962 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
966 //fprintf( stderr, "gap : %Ld\n", display_date-last_display_date );
967 last_display_date = display_date;
969 if( display_date < current_date && i_trash_count > 4 )
971 /* Picture is late: it will be destroyed and the thread will sleep and
972 * go to next picture */
974 vlc_mutex_lock( &p_vout->picture_lock );
975 if( p_pic->i_refcount )
977 p_pic->i_status = DISPLAYED_PICTURE;
981 p_pic->i_status = DESTROYED_PICTURE;
982 p_vout->i_pictures--;
984 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
985 vlc_mutex_unlock( &p_vout->picture_lock );
987 /* Update synchronization information as if display delay
989 Synchronize( p_vout, display_date - current_date );
997 if( display_date > current_date + VOUT_DISPLAY_DELAY )
999 /* A picture is ready to be rendered, but its rendering date is
1000 * far from the current one so the thread will perform an empty loop
1001 * as if no picture were found. The picture state is unchanged */
1007 /* Picture will be displayed, update synchronization
1009 Synchronize( p_vout, display_date - current_date );
1013 * Find the subpicture to display - this operation does not need lock, since
1014 * only READY_SUBPICTURES are handled. If no picture has been selected,
1015 * display_date will depend on the subpicture
1020 * Perform rendering, sleep and display rendered picture
1022 if( p_pic ) /* picture and perhaps subpicture */
1024 b_display = p_vout->b_active;
1025 p_vout->last_display_date = display_date;
1029 /* Set picture dimensions and clear buffer */
1030 SetBufferPicture( p_vout, p_pic );
1032 /* Render picture and informations */
1033 RenderPicture( p_vout, p_pic );
1034 if( p_vout->b_info )
1036 RenderPictureInfo( p_vout, p_pic );
1037 RenderInfo( p_vout );
1041 /* Remove picture from heap */
1042 vlc_mutex_lock( &p_vout->picture_lock );
1043 if( p_pic->i_refcount )
1045 p_pic->i_status = DISPLAYED_PICTURE;
1049 p_pic->i_status = DESTROYED_PICTURE;
1050 p_vout->i_pictures--;
1052 vlc_mutex_unlock( &p_vout->picture_lock );
1054 /* Render interface and subpicture */
1055 if( b_display && p_vout->b_interface )
1057 RenderInterface( p_vout );
1063 RenderSubPicture( p_vout, p_subpic );
1066 /* Remove subpicture from heap */
1067 vlc_mutex_lock( &p_vout->subpicture_lock );
1068 p_subpic->i_status = DESTROYED_SUBPICTURE;
1069 vlc_mutex_unlock( &p_vout->subpicture_lock );
1073 else if( p_subpic ) /* subpicture alone */
1075 b_display = p_vout->b_active;
1076 p_vout->last_display_date = display_date;
1081 SetBufferPicture( p_vout, NULL );
1083 /* Render informations, interface and subpicture */
1084 if( p_vout->b_info )
1086 RenderInfo( p_vout );
1088 if( p_vout->b_interface )
1090 RenderInterface( p_vout );
1092 RenderSubPicture( p_vout, p_subpic );
1095 /* Remove subpicture from heap */
1096 vlc_mutex_lock( &p_vout->subpicture_lock );
1097 p_subpic->i_status = DESTROYED_SUBPICTURE;
1098 vlc_mutex_unlock( &p_vout->subpicture_lock );
1100 else if( p_vout->b_active ) /* idle or interface screen alone */
1102 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1104 /* Interface has changed, so a new rendering is required - force
1105 * it by setting last idle date to 0 */
1106 p_vout->last_idle_date = 0;
1109 /* Render idle screen and update idle date, then render interface if
1111 b_display = RenderIdle( p_vout );
1114 p_vout->last_idle_date = current_date;
1115 if( p_vout->b_interface )
1117 RenderInterface( p_vout );
1128 * Sleep, wake up and display rendered picture
1132 /* Store render time */
1133 p_vout->render_time = mdate() - current_date;
1136 /* Give back change lock */
1137 vlc_mutex_unlock( &p_vout->change_lock );
1139 /* Sleep a while or until a given date */
1140 if( display_date != 0 )
1142 mwait( display_date );
1146 msleep( VOUT_IDLE_SLEEP );
1149 /* On awakening, take back lock and send immediately picture to display,
1150 * then swap buffers */
1151 vlc_mutex_lock( &p_vout->change_lock );
1153 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1154 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1156 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1158 p_vout->p_sys_display( p_vout );
1159 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1163 * Check events and manage thread
1165 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1167 /* A fatal error occured, and the thread must terminate immediately,
1168 * without displaying anything - setting b_error to 1 cause the
1169 * immediate end of the main while() loop. */
1170 p_vout->b_error = 1;
1175 * Error loop - wait until the thread destruction is requested
1177 if( p_vout->b_error )
1179 ErrorThread( p_vout );
1183 EndThread( p_vout );
1184 DestroyThread( p_vout, THREAD_OVER );
1185 intf_DbgMsg( "thread end\n" );
1188 /*****************************************************************************
1189 * ErrorThread: RunThread() error loop
1190 *****************************************************************************
1191 * This function is called when an error occured during thread main's loop. The
1192 * thread can still receive feed, but must be ready to terminate as soon as
1194 *****************************************************************************/
1195 static void ErrorThread( vout_thread_t *p_vout )
1197 /* Wait until a `die' order */
1199 while( !p_vout->b_die )
1202 msleep( VOUT_IDLE_SLEEP );
1206 /*****************************************************************************
1207 * EndThread: thread destruction
1208 *****************************************************************************
1209 * This function is called when the thread ends after a sucessfull
1210 * initialization. It frees all ressources allocated by InitThread.
1211 *****************************************************************************/
1212 static void EndThread( vout_thread_t *p_vout )
1214 int i_index; /* index in heap */
1218 *p_vout->pi_status = THREAD_END;
1220 /* Destroy all remaining pictures and subpictures */
1221 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1223 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1225 free( p_vout->p_picture[i_index].p_data );
1227 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1229 free( p_vout->p_subpicture[i_index].p_data );
1233 /* Destroy translation tables */
1234 vout_EndYUV( p_vout );
1235 p_vout->p_sys_end( p_vout );
1238 /*****************************************************************************
1239 * DestroyThread: thread destruction
1240 *****************************************************************************
1241 * This function is called when the thread ends. It frees all ressources
1242 * allocated by CreateThread. Status is available at this stage.
1243 *****************************************************************************/
1244 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1246 int *pi_status; /* status adress */
1248 /* Store status adress */
1250 pi_status = p_vout->pi_status;
1252 /* Destroy thread structures allocated by Create and InitThread */
1253 vout_UnloadFont( p_vout->p_default_font );
1254 vout_UnloadFont( p_vout->p_large_font );
1255 p_vout->p_sys_destroy( p_vout );
1258 TrashPlugin( p_vout->vout_plugin );
1260 /* Free structure */
1262 *pi_status = i_status;
1265 /*****************************************************************************
1266 * Print: print simple text on a picture
1267 *****************************************************************************
1268 * This function will print a simple text on the picture. It is designed to
1269 * print debugging or general informations.
1270 *****************************************************************************/
1271 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 )
1273 int i_text_height; /* total text height */
1274 int i_text_width; /* total text width */
1276 /* Update upper left coordinates according to alignment */
1277 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1278 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1280 /* Set area and print text */
1281 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1282 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1283 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1284 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1285 p_vout->i_white_pixel, 0, 0,
1290 /*****************************************************************************
1291 * SetBufferArea: activate an area in current buffer
1292 *****************************************************************************
1293 * This function is called when something is rendered on the current buffer.
1294 * It set the area as active and prepare it to be cleared on next rendering.
1295 * Pay attention to the fact that in this functions, i_h is in fact the end y
1296 * coordinate of the new area.
1297 *****************************************************************************/
1298 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1300 vout_buffer_t * p_buffer; /* current buffer */
1301 int i_area_begin, i_area_end; /* area vertical extension */
1302 int i_area, i_area_copy; /* area index */
1303 int i_area_shift; /* shift distance for areas */
1305 /* Choose buffer and modify h to end of area position */
1306 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1310 * Remove part of the area which is inside the picture - this is done
1311 * by calling again SetBufferArea with the correct areas dimensions.
1313 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1315 i_area_begin = p_buffer->i_pic_y;
1316 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1318 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1319 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1320 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1322 /* Keep the stripe above the picture, if any */
1323 if( i_y < i_area_begin )
1325 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1327 /* Keep the stripe below the picture, if any */
1328 if( i_h > i_area_end )
1330 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1336 /* Skip some extensions until interesting areas */
1338 (i_area < p_buffer->i_areas) &&
1339 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1345 if( i_area == p_buffer->i_areas )
1347 /* New area is below all existing ones: just add it at the end of the
1348 * array, if possible - else, append it to the last one */
1349 if( i_area < VOUT_MAX_AREAS )
1351 p_buffer->pi_area_begin[i_area] = i_y;
1352 p_buffer->pi_area_end[i_area] = i_h;
1353 p_buffer->i_areas++;
1358 intf_DbgMsg("areas overflow\n");
1360 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1365 i_area_begin = p_buffer->pi_area_begin[i_area];
1366 i_area_end = p_buffer->pi_area_end[i_area];
1368 if( i_y < i_area_begin )
1370 if( i_h >= i_area_begin - 1 )
1372 /* Extend area above */
1373 p_buffer->pi_area_begin[i_area] = i_y;
1377 /* Create a new area above : merge last area if overflow, then
1378 * move all old areas down */
1379 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1382 intf_DbgMsg("areas overflow\n");
1384 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1388 p_buffer->i_areas++;
1390 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1392 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1393 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1395 p_buffer->pi_area_begin[i_area] = i_y;
1396 p_buffer->pi_area_end[i_area] = i_h;
1400 if( i_h > i_area_end )
1402 /* Find further areas which can be merged with the new one */
1403 for( i_area_copy = i_area + 1;
1404 (i_area_copy < p_buffer->i_areas) &&
1405 (p_buffer->pi_area_begin[i_area] <= i_h);
1412 if( i_area_copy != i_area )
1414 /* Merge with last possible areas */
1415 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1417 /* Shift lower areas upward */
1418 i_area_shift = i_area_copy - i_area;
1419 p_buffer->i_areas -= i_area_shift;
1420 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1422 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1423 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1428 /* Extend area below */
1429 p_buffer->pi_area_end[i_area] = i_h;
1435 /*****************************************************************************
1436 * SetBufferPicture: clear buffer and set picture area
1437 *****************************************************************************
1438 * This function is called before any rendering. It clears the current
1439 * rendering buffer and set the new picture area. If the picture pointer is
1440 * NULL, then no picture area is defined. Floating operations are avoided since
1441 * some MMX calculations may follow.
1442 *****************************************************************************/
1443 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1445 vout_buffer_t * p_buffer; /* current buffer */
1446 int i_pic_x, i_pic_y; /* picture position */
1447 int i_pic_width, i_pic_height; /* picture dimensions */
1448 int i_old_pic_y, i_old_pic_height; /* old picture area */
1449 int i_vout_width, i_vout_height; /* display dimensions */
1450 int i_area; /* area index */
1451 int i_data_index; /* area data index */
1452 int i_data_size; /* area data size, in 256 bytes blocs */
1453 u64 * p_data; /* area data, for clearing */
1454 byte_t * p_data8; /* area data, for clearing (slow) */
1456 /* Choose buffer and set display dimensions */
1457 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1458 i_vout_width = p_vout->i_width;
1459 i_vout_height = p_vout->i_height;
1462 * Computes new picture size
1466 /* Try horizontal scaling first - width must be a mutiple of 16 */
1467 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1468 i_vout_width : p_pic->i_width) & ~0xf;
1469 switch( p_pic->i_aspect_ratio )
1471 case AR_3_4_PICTURE:
1472 i_pic_height = i_pic_width * 3 / 4;
1474 case AR_16_9_PICTURE:
1475 i_pic_height = i_pic_width * 9 / 16;
1477 case AR_221_1_PICTURE:
1478 i_pic_height = i_pic_width * 100 / 221;
1480 case AR_SQUARE_PICTURE:
1482 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1486 /* If picture dimensions using horizontal scaling are too large, use
1487 * vertical scaling. Since width must be a multiple of 16, height is
1488 * adjusted again after. */
1489 if( i_pic_height > i_vout_height )
1491 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1492 i_vout_height : p_pic->i_height;
1493 switch( p_pic->i_aspect_ratio )
1495 case AR_3_4_PICTURE:
1496 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1497 i_pic_height = i_pic_width * 3 / 4;
1499 case AR_16_9_PICTURE:
1500 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1501 i_pic_height = i_pic_width * 9 / 16;
1503 case AR_221_1_PICTURE:
1504 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1505 i_pic_height = i_pic_width * 100 / 221;
1507 case AR_SQUARE_PICTURE:
1509 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1510 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1515 /* Set picture position */
1516 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1517 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1521 /* No picture: size is 0 */
1529 * Set new picture size - if is is smaller than the previous one, clear
1530 * around it. Since picture are centered, only their size is tested.
1532 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1534 i_old_pic_y = p_buffer->i_pic_y;
1535 i_old_pic_height = p_buffer->i_pic_height;
1536 p_buffer->i_pic_x = i_pic_x;
1537 p_buffer->i_pic_y = i_pic_y;
1538 p_buffer->i_pic_width = i_pic_width;
1539 p_buffer->i_pic_height = i_pic_height;
1540 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_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;
1553 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1556 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1557 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1559 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1560 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1561 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1563 /* Clear 256 bytes block */
1564 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1565 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1566 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1567 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1568 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1569 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1570 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1571 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1573 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1575 /* Clear remaining 16 bytes blocks */
1576 *p_data++ = 0; *p_data++ = 0;
1578 p_data8 = (byte_t *)p_data;
1579 for( i_data_index = i_data_size % 16; i_data_index--; )
1581 /* Clear remaining bytes */
1589 p_buffer->i_areas = 0;
1592 /*****************************************************************************
1593 * RenderPicture: render a picture
1594 *****************************************************************************
1595 * This function convert a picture from a video heap to a pixel-encoded image
1596 * and copy it to the current rendering buffer. No lock is required, since the
1597 * rendered picture has been determined as existant, and will only be destroyed
1598 * by the vout thread later.
1599 *****************************************************************************/
1600 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1603 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1604 mtime_t render_time; /* picture rendering time */
1606 vout_buffer_t * p_buffer; /* rendering buffer */
1607 byte_t * p_pic_data; /* convertion destination */
1609 /* Get and set rendering informations */
1610 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1611 p_pic_data = p_buffer->p_data +
1612 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1613 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1615 render_time = mdate();
1619 * Choose appropriate rendering function and render picture
1621 switch( p_pic->i_type )
1623 case YUV_420_PICTURE:
1624 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1625 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1626 p_pic->i_width, p_pic->i_height,
1627 p_buffer->i_pic_width, p_buffer->i_pic_height,
1628 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1629 p_pic->i_matrix_coefficients );
1631 case YUV_422_PICTURE:
1632 p_vout->yuv.p_Convert422( 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_444_PICTURE:
1640 p_vout->yuv.p_Convert444( 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 );
1649 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1655 /* Print picture date and rendering time */
1656 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1657 p_vout->i_buffer_index, (long) (mdate() - render_time),
1658 mstrtime( psz_date, p_pic->date ));
1662 /*****************************************************************************
1663 * RenderPictureInfo: print additionnal informations on a picture
1664 *****************************************************************************
1665 * This function will print informations such as fps and other picture
1666 * dependant informations.
1667 *****************************************************************************/
1668 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1670 #if defined(STATS) || defined(DEBUG)
1671 char psz_buffer[256]; /* string buffer */
1676 * Print FPS rate in upper right corner
1678 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1680 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1681 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1682 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1683 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1687 * Print frames count and loop time in upper left corner
1689 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1690 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1691 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1696 * Print picture information in lower right corner
1698 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1699 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1700 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1701 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1702 p_pic->i_width, p_pic->i_height,
1703 p_pic->i_display_width, p_pic->i_display_height,
1704 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1705 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1706 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1707 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1708 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1709 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1710 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1711 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1712 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1713 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1717 /*****************************************************************************
1718 * RenderIdle: render idle picture
1719 *****************************************************************************
1720 * This function will print something on the screen. It will return 0 if
1721 * nothing has been rendered, or 1 if something has been changed on the screen.
1722 * Note that if you absolutely want something to be printed, you will have
1723 * to force it by setting the last idle date to 0.
1724 * Unlike other rendering functions, this one calls the SetBufferPicture
1725 * function when needed.
1726 *****************************************************************************/
1727 static int RenderIdle( vout_thread_t *p_vout )
1729 int i_x = 0, i_y = 0; /* text position */
1730 int i_width, i_height; /* text size */
1731 mtime_t current_date; /* current date */
1732 const char *psz_text = "waiting for stream ..."; /* text to display */
1735 current_date = mdate();
1736 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1737 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1739 SetBufferPicture( p_vout, NULL );
1740 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1741 &i_width, &i_height );
1742 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1744 vout_Print( p_vout->p_large_font,
1745 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1746 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1747 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1748 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1749 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1750 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1757 /*****************************************************************************
1758 * RenderInfo: render additionnal informations
1759 *****************************************************************************
1760 * This function render informations which do not depend of the current picture
1762 *****************************************************************************/
1763 static void RenderInfo( vout_thread_t *p_vout )
1766 char psz_buffer[256]; /* string buffer */
1767 int i_ready_pic = 0; /* ready pictures */
1768 int i_reserved_pic = 0; /* reserved pictures */
1769 int i_picture; /* picture index */
1774 * Print thread state in lower left corner
1776 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1778 switch( p_vout->p_picture[i_picture].i_status )
1780 case RESERVED_PICTURE:
1781 case RESERVED_DATED_PICTURE:
1782 case RESERVED_DISP_PICTURE:
1790 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1791 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1792 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1796 /*****************************************************************************
1797 * RenderSubPicture: render a subpicture
1798 *****************************************************************************
1799 * This function render a sub picture unit.
1800 *****************************************************************************/
1801 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1803 p_vout_font_t p_font; /* text font */
1804 int i_width, i_height; /* subpicture dimensions */
1806 switch( p_subpic->i_type )
1808 case TEXT_SUBPICTURE: /* single line text */
1809 /* Select default font if not specified */
1810 p_font = p_subpic->type.text.p_font;
1811 if( p_font == NULL )
1813 p_font = p_vout->p_default_font;
1816 /* Computes text size (width and height fields are ignored) and print it */
1817 vout_TextSize( p_font, p_subpic->type.text.i_style, p_subpic->p_data, &i_width, &i_height );
1818 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y, i_width, i_height,
1819 p_subpic->i_horizontal_align, p_subpic->i_vertical_align ) )
1821 vout_Print( p_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1822 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1823 p_subpic->i_y * p_vout->i_bytes_per_line,
1824 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1825 p_subpic->type.text.i_char_color, p_subpic->type.text.i_border_color,
1826 p_subpic->type.text.i_bg_color, p_subpic->type.text.i_style,
1828 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y, i_width, i_height );
1834 intf_DbgMsg("error: unknown subpicture %p type %d\n", p_subpic, p_subpic->i_type );
1839 /*****************************************************************************
1840 * RenderInterface: render the interface
1841 *****************************************************************************
1842 * This function render the interface, if any.
1843 *****************************************************************************/
1844 static void RenderInterface( vout_thread_t *p_vout )
1846 int i_height, i_text_height; /* total and text height */
1847 int i_width_1, i_width_2; /* text width */
1848 int i_byte; /* byte index */
1849 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1850 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1853 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1854 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1855 i_height += i_text_height;
1857 /* Render background */
1858 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1859 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1863 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1866 /* Render text, if not larger than screen */
1867 if( i_width_1 < p_vout->i_width )
1869 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1870 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1871 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1872 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1873 OUTLINED_TEXT, psz_text_1 );
1875 if( i_width_2 < 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 + i_text_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_2 );
1884 /* Activate modified area */
1885 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1888 /*****************************************************************************
1889 * Synchronize: update synchro level depending of heap state
1890 *****************************************************************************
1891 * This function is called during the main vout loop.
1892 *****************************************************************************/
1893 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1895 int i_synchro_inc = 0;
1896 /* XXX?? gore following */
1897 static int i_panic_count = 0;
1898 static int i_last_synchro_inc = 0;
1899 static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1900 static int i_truc = 10;
1904 //fprintf( stderr, "PANIC %d\n", i_panic_count );
1910 if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1913 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1918 if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1921 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1925 if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1926 i_synchro_inc*i_last_synchro_inc < 0 )
1931 if( i_delay < 6000 )
1936 else if( i_delay < 70000 )
1938 i_truc = 24+(24*i_delay)/70000;
1941 i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
1943 else if( i_delay > 100000 )
1945 r_synchro_level += 1;
1946 if( i_delay > 130000 )
1947 r_synchro_level += 1;
1950 r_synchro_level += (float)i_synchro_inc / i_truc;
1951 p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
1953 if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
1955 r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
1958 //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
1959 // p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
1960 i_last_synchro_inc = i_synchro_inc;
1963 /*****************************************************************************
1964 * Manage: manage thread
1965 *****************************************************************************
1966 * This function will handle changes in thread configuration.
1967 *****************************************************************************/
1968 static int Manage( vout_thread_t *p_vout )
1971 if( p_vout->i_changes )
1973 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
1974 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
1978 /* On gamma or grayscale change, rebuild tables */
1979 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1982 if( vout_ResetYUV( p_vout ) )
1984 intf_ErrMsg("error: can't rebuild convertion tables\n");
1989 /* Clear changes flags which does not need management or have been
1991 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
1992 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
1993 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
1995 /* Detect unauthorized changes */
1996 if( p_vout->i_changes )
1998 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
1999 * function, it means they should not be authorized */
2000 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2007 /*****************************************************************************
2008 * Align: align a subpicture in the screen
2009 *****************************************************************************
2010 * This function is used for rendering text or subpictures. It returns non 0
2011 * it the final aera is not fully included in display area. Return coordinates
2013 *****************************************************************************/
2014 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2015 int i_width, int i_height, int i_h_align, int i_v_align )
2017 /* Align horizontally */
2021 *pi_x -= i_width / 2;
2024 *pi_x += (p_vout->i_width - i_width) / 2;
2030 *pi_x += p_vout->i_width - i_width;
2034 /* Align vertically */
2038 *pi_y -= i_height / 2;
2041 *pi_y += (p_vout->i_height - i_height) / 2;
2047 *pi_y += p_vout->i_height - i_height;
2049 case SUBTITLE_RALIGN:
2050 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2051 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2055 /* Return non 0 if clipping failed */
2056 return( (*pi_x < 0) || (*pi_y < 0) ||
2057 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2060 /*****************************************************************************
2061 * SetPalette: sets an 8 bpp palette
2062 *****************************************************************************
2063 * This function is just a prototype that does nothing. Architectures that
2064 * support palette allocation should override it.
2065 *****************************************************************************/
2066 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2067 u16 *green, u16 *blue, u16 *transp )
2069 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );