1 /*****************************************************************************
2 * video_output.c : video output thread
3 * This module describes the programming interface for video output threads.
4 * It includes functions allowing to open a new thread, send pictures to a
5 * thread, and destroy a previously oppened video output thread.
6 *****************************************************************************
7 * Copyright (C) 2000 VideoLAN
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <errno.h> /* ENOMEM */
32 #include <stdlib.h> /* free() */
33 #include <stdio.h> /* sprintf() */
34 #include <string.h> /* strerror() */
42 #include "video_output.h"
43 #include "video_text.h"
44 #include "video_spu.h"
45 #include "video_yuv.h"
50 /*****************************************************************************
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 =
118 GetPluginFunction( p_vout->vout_plugin, "vout_SysCreate" );
120 GetPluginFunction( p_vout->vout_plugin, "vout_SysInit" );
122 GetPluginFunction( p_vout->vout_plugin, "vout_SysEnd" );
123 p_vout->p_sys_destroy =
124 GetPluginFunction( p_vout->vout_plugin, "vout_SysDestroy" );
125 p_vout->p_sys_manage =
126 GetPluginFunction( p_vout->vout_plugin, "vout_SysManage" );
127 p_vout->p_sys_display =
128 GetPluginFunction( p_vout->vout_plugin, "vout_SysDisplay" );
130 /* Initialize thread properties - thread id and locks will be initialized
134 p_vout->b_active = 0;
135 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
136 *p_vout->pi_status = THREAD_CREATE;
138 /* Initialize some fields used by the system-dependant method - these fields will
139 * probably be modified by the method, and are only preferences */
140 p_vout->i_changes = 0;
141 p_vout->i_width = i_width;
142 p_vout->i_height = i_height;
143 p_vout->i_bytes_per_line = i_width * 2;
144 p_vout->i_screen_depth = 15;
145 p_vout->i_bytes_per_pixel = 2;
146 p_vout->f_gamma = VOUT_GAMMA;
148 p_vout->b_grayscale = main_GetIntVariable( VOUT_GRAYSCALE_VAR,
149 VOUT_GRAYSCALE_DEFAULT );
151 p_vout->b_interface = 0;
154 p_vout->p_set_palette = SetPalette;
156 intf_DbgMsg( "wished configuration: %dx%d, %d/%d bpp (%d Bpl)\n",
157 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
158 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line );
160 /* Initialize idle screen */
161 p_vout->last_display_date = mdate();
162 p_vout->last_display_date = 0;
163 p_vout->last_idle_date = 0;
166 /* Initialize statistics fields */
167 p_vout->render_time = 0;
168 p_vout->c_fps_samples = 0;
171 /* Initialize buffer index */
172 p_vout->i_buffer_index = 0;
174 /* Initialize pictures and subpictures - translation tables and functions
175 * will be initialized later in InitThread */
176 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
178 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
179 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
181 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
183 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
184 p_vout->p_subpicture[i_index].i_status= FREE_SUBPICTURE;
186 p_vout->i_pictures = 0;
188 /* Initialize synchronization informations */
189 p_vout->i_synchro_level = VOUT_SYNCHRO_LEVEL_START;
191 /* Create and initialize system-dependant method - this function issues its
192 * own error messages */
193 if( p_vout->p_sys_create( p_vout, psz_display, i_root_window ) )
195 TrashPlugin( p_vout->vout_plugin );
199 intf_DbgMsg("actual configuration: %dx%d, %d/%d bpp (%d Bpl), masks: 0x%x/0x%x/0x%x\n",
200 p_vout->i_width, p_vout->i_height, p_vout->i_screen_depth,
201 p_vout->i_bytes_per_pixel * 8, p_vout->i_bytes_per_line,
202 p_vout->i_red_mask, p_vout->i_green_mask, p_vout->i_blue_mask );
204 /* Calculate shifts from system-updated masks */
205 MaskToShift( &p_vout->i_red_lshift, &p_vout->i_red_rshift, p_vout->i_red_mask );
206 MaskToShift( &p_vout->i_green_lshift, &p_vout->i_green_rshift, p_vout->i_green_mask );
207 MaskToShift( &p_vout->i_blue_lshift, &p_vout->i_blue_rshift, p_vout->i_blue_mask );
209 /* Set some useful colors */
210 p_vout->i_white_pixel = RGB2PIXEL( p_vout, 255, 255, 255 );
211 p_vout->i_black_pixel = RGB2PIXEL( p_vout, 0, 0, 0 );
212 p_vout->i_gray_pixel = RGB2PIXEL( p_vout, 128, 128, 128 );
213 p_vout->i_blue_pixel = RGB2PIXEL( p_vout, 0, 0, 50 );
215 /* Load fonts - fonts must be initialized after the system method since
216 * they may be dependant on screen depth and other thread properties */
217 p_vout->p_default_font = vout_LoadFont( DATA_PATH "/" VOUT_DEFAULT_FONT );
218 if( p_vout->p_default_font == NULL )
220 p_vout->p_default_font = vout_LoadFont( "share/" VOUT_DEFAULT_FONT );
222 if( p_vout->p_default_font == NULL )
224 p_vout->p_sys_destroy( p_vout );
225 TrashPlugin( p_vout->vout_plugin );
229 p_vout->p_large_font = vout_LoadFont( DATA_PATH "/" VOUT_LARGE_FONT );
230 if( p_vout->p_large_font == NULL )
232 p_vout->p_large_font = vout_LoadFont( "share/" VOUT_LARGE_FONT );
234 if( p_vout->p_large_font == NULL )
236 vout_UnloadFont( p_vout->p_default_font );
237 p_vout->p_sys_destroy( p_vout );
238 TrashPlugin( p_vout->vout_plugin );
243 /* Create thread and set locks */
244 vlc_mutex_init( &p_vout->picture_lock );
245 vlc_mutex_init( &p_vout->subpicture_lock );
246 vlc_mutex_init( &p_vout->change_lock );
247 vlc_mutex_lock( &p_vout->change_lock );
248 if( vlc_thread_create( &p_vout->thread_id, "video output", (void *) RunThread, (void *) p_vout) )
250 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
251 vout_UnloadFont( p_vout->p_default_font );
252 vout_UnloadFont( p_vout->p_large_font );
253 p_vout->p_sys_destroy( p_vout );
254 TrashPlugin( p_vout->vout_plugin );
259 intf_Msg("Video display initialized (%dx%d, %d/%d bpp)\n", p_vout->i_width,
260 p_vout->i_height, p_vout->i_screen_depth, p_vout->i_bytes_per_pixel * 8 );
262 /* If status is NULL, wait until the thread is created */
263 if( pi_status == NULL )
267 msleep( THREAD_SLEEP );
268 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
269 && (i_status != THREAD_FATAL) );
270 if( i_status != THREAD_READY )
278 /*****************************************************************************
279 * vout_DestroyThread: destroys a previously created thread
280 *****************************************************************************
281 * Destroy a terminated thread.
282 * The function will request a destruction of the specified thread. If pi_error
283 * is NULL, it will return once the thread is destroyed. Else, it will be
284 * update using one of the THREAD_* constants.
285 *****************************************************************************/
286 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
288 int i_status; /* thread status */
292 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
293 *p_vout->pi_status = THREAD_DESTROY;
295 /* Request thread destruction */
298 /* If status is NULL, wait until thread has been destroyed */
299 if( pi_status == NULL )
303 msleep( THREAD_SLEEP );
304 }while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
305 && (i_status != THREAD_FATAL) );
309 /*****************************************************************************
310 * vout_DisplaySubPicture: display a subpicture unit
311 *****************************************************************************
312 * Remove the reservation flag of an subpicture, which will cause it to be ready
313 * for display. The picture does not need to be locked, since it is ignored by
314 * the output thread if is reserved.
315 *****************************************************************************/
316 void vout_DisplaySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
319 char psz_begin_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
320 char psz_end_date[MSTRTIME_MAX_SIZE]; /* buffer for date string */
324 /* Check if status is valid */
325 if( p_subpic->i_status != RESERVED_SUBPICTURE )
327 intf_DbgMsg("error: subpicture %p has invalid status %d\n", p_subpic,
328 p_subpic->i_status );
332 /* Remove reservation flag */
333 p_subpic->i_status = READY_SUBPICTURE;
336 /* Send subpicture informations */
337 intf_DbgMsg("subpicture %p: type=%d, begin date=%s, end date=%s\n",
338 p_subpic, p_subpic->i_type,
339 mstrtime( psz_begin_date, p_subpic->begin_date ),
340 mstrtime( psz_end_date, p_subpic->end_date ) );
344 /*****************************************************************************
345 * vout_CreateSubPicture: allocate an subpicture in the video output heap.
346 *****************************************************************************
347 * This function create a reserved subpicture in the video output heap.
348 * A null pointer is returned if the function fails. This method provides an
349 * already allocated zone of memory in the spu data fields. It needs locking
350 * since several pictures can be created by several producers threads.
351 *****************************************************************************/
352 subpicture_t *vout_CreateSubPicture( vout_thread_t *p_vout, int i_type,
355 int i_subpic; /* subpicture index */
356 subpicture_t * p_free_subpic = NULL; /* first free subpicture */
357 subpicture_t * p_destroyed_subpic = NULL; /* first destroyed subpic */
360 vlc_mutex_lock( &p_vout->subpicture_lock );
363 * Look for an empty place
365 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
367 if( p_vout->p_subpicture[i_subpic].i_status == DESTROYED_SUBPICTURE )
369 /* Subpicture is marked for destruction, but is still allocated */
370 if( (p_vout->p_subpicture[i_subpic].i_type == i_type) &&
371 (p_vout->p_subpicture[i_subpic].i_size >= i_size) )
373 /* Memory size do match or is smaller : memory will not be
374 * reallocated, and function can end immediately - this is
375 * the best possible case, since no memory allocation needs
377 p_vout->p_subpicture[i_subpic].i_status = RESERVED_SUBPICTURE;
379 intf_DbgMsg("subpicture %p (in destroyed subpicture slot)\n",
380 &p_vout->p_subpicture[i_subpic] );
382 vlc_mutex_unlock( &p_vout->subpicture_lock );
383 return( &p_vout->p_subpicture[i_subpic] );
385 else if( p_destroyed_subpic == NULL )
387 /* Memory size do not match, but subpicture index will be kept in
388 * case no other place are left */
389 p_destroyed_subpic = &p_vout->p_subpicture[i_subpic];
392 else if( (p_free_subpic == NULL) &&
393 (p_vout->p_subpicture[i_subpic].i_status == FREE_SUBPICTURE ))
395 /* Subpicture is empty and ready for allocation */
396 p_free_subpic = &p_vout->p_subpicture[i_subpic];
400 /* If no free subpicture is available, use a destroyed subpicture */
401 if( (p_free_subpic == NULL) && (p_destroyed_subpic != NULL ) )
403 /* No free subpicture or matching destroyed subpicture has been
404 * found, but a destroyed subpicture is still avalaible */
405 free( p_destroyed_subpic->p_data );
406 p_free_subpic = p_destroyed_subpic;
412 if( p_free_subpic != NULL )
414 /* Allocate memory */
417 case TEXT_SUBPICTURE: /* text subpicture */
418 p_free_subpic->p_data = malloc( i_size + 1 );
420 case DVD_SUBPICTURE: /* DVD subpicture unit */
421 p_free_subpic->p_data = malloc( i_size );
425 intf_DbgMsg("error: unknown subpicture type %d\n", i_type );
426 p_free_subpic->p_data = NULL;
431 if( p_free_subpic->p_data != NULL )
432 { /* Copy subpicture informations, set some default values */
433 p_free_subpic->i_type = i_type;
434 p_free_subpic->i_status = RESERVED_SUBPICTURE;
435 p_free_subpic->i_size = i_size;
436 p_free_subpic->i_x = 0;
437 p_free_subpic->i_y = 0;
438 p_free_subpic->i_width = 0;
439 p_free_subpic->i_height = 0;
440 p_free_subpic->i_horizontal_align = CENTER_RALIGN;
441 p_free_subpic->i_vertical_align = CENTER_RALIGN;
445 /* Memory allocation failed : set subpicture as empty */
446 p_free_subpic->i_type = EMPTY_SUBPICTURE;
447 p_free_subpic->i_status = FREE_SUBPICTURE;
448 p_free_subpic = NULL;
449 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
453 intf_DbgMsg("subpicture %p (in free subpicture slot)\n", p_free_subpic );
455 vlc_mutex_unlock( &p_vout->subpicture_lock );
456 return( p_free_subpic );
459 /* No free or destroyed subpicture could be found */
460 intf_DbgMsg( "warning: heap is full\n" );
461 vlc_mutex_unlock( &p_vout->subpicture_lock );
465 /*****************************************************************************
466 * vout_DestroySubPicture: remove a subpicture from the heap
467 *****************************************************************************
468 * This function frees a previously reserved subpicture.
469 * It is meant to be used when the construction of a picture aborted.
470 * This function does not need locking since reserved subpictures are ignored
471 * by the output thread.
472 *****************************************************************************/
473 void vout_DestroySubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
476 /* Check if status is valid */
477 if( p_subpic->i_status != RESERVED_SUBPICTURE )
479 intf_DbgMsg("error: subpicture %p has invalid status %d\n",
480 p_subpic, p_subpic->i_status );
484 p_subpic->i_status = DESTROYED_SUBPICTURE;
487 intf_DbgMsg("subpicture %p\n", p_subpic);
491 /*****************************************************************************
492 * vout_DisplayPicture: display a picture
493 *****************************************************************************
494 * Remove the reservation flag of a picture, which will cause it to be ready for
495 * display. The picture won't be displayed until vout_DatePicture has been
497 *****************************************************************************/
498 void vout_DisplayPicture( vout_thread_t *p_vout, picture_t *p_pic )
500 vlc_mutex_lock( &p_vout->picture_lock );
501 switch( p_pic->i_status )
503 case RESERVED_PICTURE:
504 p_pic->i_status = RESERVED_DISP_PICTURE;
506 case RESERVED_DATED_PICTURE:
507 p_pic->i_status = READY_PICTURE;
511 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
517 intf_DbgMsg("picture %p\n", p_pic);
519 vlc_mutex_unlock( &p_vout->picture_lock );
522 /*****************************************************************************
523 * vout_DatePicture: date a picture
524 *****************************************************************************
525 * Remove the reservation flag of a picture, which will cause it to be ready for
526 * display. The picture won't be displayed until vout_DisplayPicture has been
528 *****************************************************************************/
529 void vout_DatePicture( vout_thread_t *p_vout, picture_t *p_pic, mtime_t date )
532 char psz_date[MSTRTIME_MAX_SIZE]; /* date */
535 vlc_mutex_lock( &p_vout->picture_lock );
537 switch( p_pic->i_status )
539 case RESERVED_PICTURE:
540 p_pic->i_status = RESERVED_DATED_PICTURE;
542 case RESERVED_DISP_PICTURE:
543 p_pic->i_status = READY_PICTURE;
547 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
553 intf_DbgMsg("picture %p, display date: %s\n", p_pic, mstrtime( psz_date, p_pic->date) );
555 vlc_mutex_unlock( &p_vout->picture_lock );
558 /*****************************************************************************
559 * vout_CreatePicture: allocate a picture in the video output heap.
560 *****************************************************************************
561 * This function create a reserved image in the video output heap.
562 * A null pointer is returned if the function fails. This method provides an
563 * already allocated zone of memory in the picture data fields. It needs locking
564 * since several pictures can be created by several producers threads.
565 *****************************************************************************/
566 picture_t *vout_CreatePicture( vout_thread_t *p_vout, int i_type,
567 int i_width, int i_height )
569 int i_picture; /* picture index */
570 int i_chroma_width = 0; /* chroma width */
571 picture_t * p_free_picture = NULL; /* first free picture */
572 picture_t * p_destroyed_picture = NULL; /* first destroyed picture */
575 vlc_mutex_lock( &p_vout->picture_lock );
578 * Look for an empty place
580 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
582 if( p_vout->p_picture[i_picture].i_status == DESTROYED_PICTURE )
584 /* Picture is marked for destruction, but is still allocated - note
585 * that if width and type are the same for two pictures, chroma_width
586 * should also be the same */
587 if( (p_vout->p_picture[i_picture].i_type == i_type) &&
588 (p_vout->p_picture[i_picture].i_height == i_height) &&
589 (p_vout->p_picture[i_picture].i_width == i_width) )
591 /* Memory size do match : memory will not be reallocated, and function
592 * can end immediately - this is the best possible case, since no
593 * memory allocation needs to be done */
594 p_vout->p_picture[i_picture].i_status = RESERVED_PICTURE;
595 p_vout->i_pictures++;
597 intf_DbgMsg("picture %p (in destroyed picture slot)\n",
598 &p_vout->p_picture[i_picture] );
600 vlc_mutex_unlock( &p_vout->picture_lock );
601 return( &p_vout->p_picture[i_picture] );
603 else if( p_destroyed_picture == NULL )
605 /* Memory size do not match, but picture index will be kept in
606 * case no other place are left */
607 p_destroyed_picture = &p_vout->p_picture[i_picture];
610 else if( (p_free_picture == NULL) &&
611 (p_vout->p_picture[i_picture].i_status == FREE_PICTURE ))
613 /* Picture is empty and ready for allocation */
614 p_free_picture = &p_vout->p_picture[i_picture];
618 /* If no free picture is available, use a destroyed picture */
619 if( (p_free_picture == NULL) && (p_destroyed_picture != NULL ) )
621 /* No free picture or matching destroyed picture has been found, but
622 * a destroyed picture is still avalaible */
623 free( p_destroyed_picture->p_data );
624 p_free_picture = p_destroyed_picture;
630 if( p_free_picture != NULL )
632 /* Allocate memory */
635 case YUV_420_PICTURE: /* YUV 420: 1,1/4,1/4 samples per pixel */
636 i_chroma_width = i_width / 2;
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*4/2;
640 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*5/2;
642 case YUV_422_PICTURE: /* YUV 422: 1,1/2,1/2 samples per pixel */
643 i_chroma_width = i_width / 2;
644 p_free_picture->p_data = malloc( i_height * i_chroma_width * 4 * sizeof( yuv_data_t ) );
645 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
646 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
647 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*3;
649 case YUV_444_PICTURE: /* YUV 444: 1,1,1 samples per pixel */
650 i_chroma_width = i_width;
651 p_free_picture->p_data = malloc( i_height * i_chroma_width * 3 * sizeof( yuv_data_t ) );
652 p_free_picture->p_y = (yuv_data_t *)p_free_picture->p_data;
653 p_free_picture->p_u = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width;
654 p_free_picture->p_v = (yuv_data_t *)p_free_picture->p_data +i_height*i_chroma_width*2;
658 intf_DbgMsg("error: unknown picture type %d\n", i_type );
659 p_free_picture->p_data = NULL;
664 if( p_free_picture->p_data != NULL )
666 /* Copy picture informations, set some default values */
667 p_free_picture->i_type = i_type;
668 p_free_picture->i_status = RESERVED_PICTURE;
669 p_free_picture->i_matrix_coefficients = 1;
670 p_free_picture->i_width = i_width;
671 p_free_picture->i_height = i_height;
672 p_free_picture->i_chroma_width = i_chroma_width;
673 p_free_picture->i_display_horizontal_offset = 0;
674 p_free_picture->i_display_vertical_offset = 0;
675 p_free_picture->i_display_width = i_width;
676 p_free_picture->i_display_height = i_height;
677 p_free_picture->i_aspect_ratio = AR_SQUARE_PICTURE;
678 p_free_picture->i_refcount = 0;
679 p_vout->i_pictures++;
683 /* Memory allocation failed : set picture as empty */
684 p_free_picture->i_type = EMPTY_PICTURE;
685 p_free_picture->i_status = FREE_PICTURE;
686 p_free_picture = NULL;
687 intf_ErrMsg("warning: %s\n", strerror( ENOMEM ) );
691 intf_DbgMsg("picture %p (in free picture slot)\n", p_free_picture );
693 vlc_mutex_unlock( &p_vout->picture_lock );
694 return( p_free_picture );
697 /* No free or destroyed picture could be found */
698 intf_DbgMsg( "warning: heap is full\n" );
699 vlc_mutex_unlock( &p_vout->picture_lock );
703 /*****************************************************************************
704 * vout_DestroyPicture: remove a permanent or reserved picture from the heap
705 *****************************************************************************
706 * This function frees a previously reserved picture or a permanent
707 * picture. It is meant to be used when the construction of a picture aborted.
708 * Note that the picture will be destroyed even if it is linked !
709 *****************************************************************************/
710 void vout_DestroyPicture( vout_thread_t *p_vout, picture_t *p_pic )
712 vlc_mutex_lock( &p_vout->picture_lock );
715 /* Check if picture status is valid */
716 if( (p_pic->i_status != RESERVED_PICTURE) &&
717 (p_pic->i_status != RESERVED_DATED_PICTURE) &&
718 (p_pic->i_status != RESERVED_DISP_PICTURE) )
720 intf_DbgMsg("error: picture %p has invalid status %d\n", p_pic, p_pic->i_status );
724 p_pic->i_status = DESTROYED_PICTURE;
725 p_vout->i_pictures--;
728 intf_DbgMsg("picture %p\n", p_pic);
730 vlc_mutex_unlock( &p_vout->picture_lock );
733 /*****************************************************************************
734 * vout_LinkPicture: increment reference counter of a picture
735 *****************************************************************************
736 * This function increment the reference counter of a picture in the video
737 * heap. It needs a lock since several producer threads can access the picture.
738 *****************************************************************************/
739 void vout_LinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
741 vlc_mutex_lock( &p_vout->picture_lock );
745 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
748 vlc_mutex_unlock( &p_vout->picture_lock );
751 /*****************************************************************************
752 * vout_UnlinkPicture: decrement reference counter of a picture
753 *****************************************************************************
754 * This function decrement the reference counter of a picture in the video heap.
755 *****************************************************************************/
756 void vout_UnlinkPicture( vout_thread_t *p_vout, picture_t *p_pic )
758 vlc_mutex_lock( &p_vout->picture_lock );
762 if( p_pic->i_refcount < 0 )
764 intf_DbgMsg("error: refcount < 0\n");
765 p_pic->i_refcount = 0;
769 if( (p_pic->i_refcount == 0) && (p_pic->i_status == DISPLAYED_PICTURE) )
771 p_pic->i_status = DESTROYED_PICTURE;
772 p_vout->i_pictures--;
776 intf_DbgMsg("picture %p refcount=%d\n", p_pic, p_pic->i_refcount );
779 vlc_mutex_unlock( &p_vout->picture_lock );
782 /*****************************************************************************
783 * vout_SetBuffers: set buffers adresses
784 *****************************************************************************
785 * This function is called by system drivers to set buffers video memory
787 *****************************************************************************/
788 void vout_SetBuffers( vout_thread_t *p_vout, void *p_buf1, void *p_buf2 )
790 /* No picture previously */
791 p_vout->p_buffer[0].i_pic_x = 0;
792 p_vout->p_buffer[0].i_pic_y = 0;
793 p_vout->p_buffer[0].i_pic_width = 0;
794 p_vout->p_buffer[0].i_pic_height = 0;
795 p_vout->p_buffer[1].i_pic_x = 0;
796 p_vout->p_buffer[1].i_pic_y = 0;
797 p_vout->p_buffer[1].i_pic_width = 0;
798 p_vout->p_buffer[1].i_pic_height = 0;
800 /* The first area covers all the screen */
801 p_vout->p_buffer[0].i_areas = 1;
802 p_vout->p_buffer[0].pi_area_begin[0] = 0;
803 p_vout->p_buffer[0].pi_area_end[0] = p_vout->i_height - 1;
804 p_vout->p_buffer[1].i_areas = 1;
805 p_vout->p_buffer[1].pi_area_begin[0] = 0;
806 p_vout->p_buffer[1].pi_area_end[0] = p_vout->i_height - 1;
809 p_vout->p_buffer[0].p_data = p_buf1;
810 p_vout->p_buffer[1].p_data = p_buf2;
813 /*****************************************************************************
814 * vout_Pixel2RGB: return red, green and blue from pixel value
815 *****************************************************************************
816 * Return color values, in 0-255 range, of the decomposition of a pixel. This
817 * is a slow routine and should only be used for initialization phase.
818 *****************************************************************************/
819 void vout_Pixel2RGB( vout_thread_t *p_vout, u32 i_pixel, int *pi_red, int *pi_green, int *pi_blue )
821 *pi_red = i_pixel & p_vout->i_red_mask;
822 *pi_green = i_pixel & p_vout->i_green_mask;
823 *pi_blue = i_pixel & p_vout->i_blue_mask;
826 /* following functions are local */
828 /*****************************************************************************
829 * BinaryLog: computes the base 2 log of a binary value
830 *****************************************************************************
831 * This functions is used by MaskToShift, to get a bit index from a binary
833 *****************************************************************************/
834 static int BinaryLog(u32 i)
859 if (i != ((u32)1 << i_log))
861 intf_ErrMsg("internal error: binary log overflow\n");
867 /*****************************************************************************
868 * MaskToShift: transform a color mask into right and left shifts
869 *****************************************************************************
870 * This function is used for obtaining color shifts from masks.
871 *****************************************************************************/
872 static void MaskToShift( int *pi_left, int *pi_right, u32 i_mask )
874 u32 i_low, i_high; /* lower hand higher bits of the mask */
877 i_low = i_mask & (- i_mask); /* lower bit of the mask */
878 i_high = i_mask + i_low; /* higher bit of the mask */
880 /* Transform bits into an index */
881 i_low = BinaryLog (i_low);
882 i_high = BinaryLog (i_high);
884 /* Update pointers and return */
886 *pi_right = (8 - i_high + i_low);
889 /*****************************************************************************
890 * InitThread: initialize video output thread
891 *****************************************************************************
892 * This function is called from RunThread and performs the second step of the
893 * initialization. It returns 0 on success. Note that the thread's flag are not
894 * modified inside this function.
895 *****************************************************************************/
896 static int InitThread( vout_thread_t *p_vout )
900 *p_vout->pi_status = THREAD_START;
902 /* Initialize output method - this function issues its own error messages */
903 if( p_vout->p_sys_init( p_vout ) )
908 /* Initialize convertion tables and functions */
909 if( vout_InitYUV( p_vout ) )
911 intf_ErrMsg("error: can't allocate YUV translation tables\n");
915 /* Mark thread as running and return */
916 p_vout->b_active = 1;
917 *p_vout->pi_status = THREAD_READY;
918 intf_DbgMsg("thread ready\n");
922 /*****************************************************************************
923 * RunThread: video output thread
924 *****************************************************************************
925 * Video output thread. This function does only returns when the thread is
926 * terminated. It handles the pictures arriving in the video heap and the
927 * display device events.
928 *****************************************************************************/
929 static void RunThread( vout_thread_t *p_vout)
931 /* XXX?? welcome to gore land */
932 static int i_trash_count = 0;
933 static mtime_t last_display_date = 0;
935 int i_index; /* index in heap */
936 mtime_t current_date; /* current date */
937 mtime_t display_date; /* display date */
938 boolean_t b_display; /* display flag */
939 picture_t * p_pic; /* picture pointer */
940 subpicture_t * p_subpic; /* subpicture pointer */
945 p_vout->b_error = InitThread( p_vout );
946 if( p_vout->b_error )
948 DestroyThread( p_vout, THREAD_ERROR );
954 * Main loop - it is not executed if an error occured during
957 while( (!p_vout->b_die) && (!p_vout->b_error) )
959 /* Initialize loop variables */
963 current_date = mdate();
966 * Find the picture to display - this operation does not need lock,
967 * since only READY_PICTUREs are handled
969 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
971 if( (p_vout->p_picture[i_index].i_status == READY_PICTURE) &&
973 (p_vout->p_picture[i_index].date < display_date) ) )
975 p_pic = &p_vout->p_picture[i_index];
976 display_date = p_pic->date;
983 /* Computes FPS rate */
984 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
988 //fprintf( stderr, "gap : %Ld\n", display_date-last_display_date );
989 last_display_date = display_date;
991 if( display_date < current_date && i_trash_count > 4 )
993 /* Picture is late: it will be destroyed and the thread
994 * will sleep and go to next picture */
996 vlc_mutex_lock( &p_vout->picture_lock );
997 if( p_pic->i_refcount )
999 p_pic->i_status = DISPLAYED_PICTURE;
1003 p_pic->i_status = DESTROYED_PICTURE;
1004 p_vout->i_pictures--;
1006 intf_DbgMsg( "warning: late picture %p skipped refcount=%d\n", p_pic, p_pic->i_refcount );
1007 vlc_mutex_unlock( &p_vout->picture_lock );
1009 /* Update synchronization information as if display delay
1011 Synchronize( p_vout, display_date - current_date );
1019 if( display_date > current_date + VOUT_DISPLAY_DELAY )
1021 /* A picture is ready to be rendered, but its rendering date
1022 * is far from the current one so the thread will perform an
1023 * empty loop as if no picture were found. The picture state
1030 /* Picture will be displayed, update synchronization
1032 Synchronize( p_vout, display_date - current_date );
1036 * Find the subpictures to display - this operation does not need
1037 * lock, since only READY_SUBPICTURE are handled. If no picture
1038 * has been selected, display_date will depend on the subpicture.
1039 * We get an easily parsable chained list of subpictures which
1040 * ends with NULL since p_subpic was initialized to NULL.
1042 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1044 if( p_vout->p_subpicture[i_index].i_status == READY_SUBPICTURE )
1046 p_vout->p_subpicture[i_index].p_next = p_subpic;
1047 p_subpic = &p_vout->p_subpicture[i_index];
1052 * Perform rendering, sleep and display rendered picture
1054 if( p_pic ) /* picture and perhaps subpicture */
1056 b_display = p_vout->b_active;
1057 p_vout->last_display_date = display_date;
1061 /* Set picture dimensions and clear buffer */
1062 SetBufferPicture( p_vout, p_pic );
1064 /* Render picture and informations */
1065 RenderPicture( p_vout, p_pic );
1066 if( p_vout->b_info )
1068 RenderPictureInfo( p_vout, p_pic );
1069 RenderInfo( p_vout );
1073 /* Remove picture from heap */
1074 vlc_mutex_lock( &p_vout->picture_lock );
1075 if( p_pic->i_refcount )
1077 p_pic->i_status = DISPLAYED_PICTURE;
1081 p_pic->i_status = DESTROYED_PICTURE;
1082 p_vout->i_pictures--;
1084 vlc_mutex_unlock( &p_vout->picture_lock );
1086 /* Render interface and subpicture */
1087 if( b_display && p_vout->b_interface )
1089 RenderInterface( p_vout );
1095 RenderSubPicture( p_vout, p_subpic );
1098 /* Remove subpicture from heap */
1099 /*vlc_mutex_lock( &p_vout->subpicture_lock );
1100 p_subpic->i_status = DESTROYED_SUBPICTURE;
1101 vlc_mutex_unlock( &p_vout->subpicture_lock );*/
1106 else if( p_subpic ) /* subpicture alone */
1108 b_display = p_vout->b_active;
1109 p_vout->last_display_date = display_date;
1114 SetBufferPicture( p_vout, NULL );
1116 /* Render informations, interface and subpicture */
1117 if( p_vout->b_info )
1119 RenderInfo( p_vout );
1121 if( p_vout->b_interface )
1123 RenderInterface( p_vout );
1125 RenderSubPicture( p_vout, p_subpic );
1128 /* Remove subpicture from heap */
1129 /*vlc_mutex_lock( &p_vout->subpicture_lock );
1130 p_subpic->i_status = DESTROYED_SUBPICTURE;
1131 vlc_mutex_unlock( &p_vout->subpicture_lock );*/
1134 else if( p_vout->b_active ) /* idle or interface screen alone */
1136 if( p_vout->b_interface && 0 /* && XXX?? intf_change */ )
1138 /* Interface has changed, so a new rendering is required - force
1139 * it by setting last idle date to 0 */
1140 p_vout->last_idle_date = 0;
1143 /* Render idle screen and update idle date, then render interface if
1145 b_display = RenderIdle( p_vout );
1148 p_vout->last_idle_date = current_date;
1149 if( p_vout->b_interface )
1151 RenderInterface( p_vout );
1162 * Sleep, wake up and display rendered picture
1166 /* Store render time */
1167 p_vout->render_time = mdate() - current_date;
1170 /* Give back change lock */
1171 vlc_mutex_unlock( &p_vout->change_lock );
1173 /* Sleep a while or until a given date */
1174 if( display_date != 0 )
1176 mwait( display_date );
1180 msleep( VOUT_IDLE_SLEEP );
1183 /* On awakening, take back lock and send immediately picture to display,
1184 * then swap buffers */
1185 vlc_mutex_lock( &p_vout->change_lock );
1187 intf_DbgMsg( "picture %p, subpicture %p in buffer %d, display=%d\n", p_pic, p_subpic,
1188 p_vout->i_buffer_index, b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) );
1190 if( b_display && !(p_vout->i_changes & VOUT_NODISPLAY_CHANGE) )
1192 p_vout->p_sys_display( p_vout );
1194 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
1199 * Check events and manage thread
1201 if( p_vout->p_sys_manage( p_vout ) | Manage( p_vout ) )
1203 /* A fatal error occured, and the thread must terminate immediately,
1204 * without displaying anything - setting b_error to 1 cause the
1205 * immediate end of the main while() loop. */
1206 p_vout->b_error = 1;
1211 * Error loop - wait until the thread destruction is requested
1213 if( p_vout->b_error )
1215 ErrorThread( p_vout );
1219 EndThread( p_vout );
1220 DestroyThread( p_vout, THREAD_OVER );
1221 intf_DbgMsg( "thread end\n" );
1224 /*****************************************************************************
1225 * ErrorThread: RunThread() error loop
1226 *****************************************************************************
1227 * This function is called when an error occured during thread main's loop. The
1228 * thread can still receive feed, but must be ready to terminate as soon as
1230 *****************************************************************************/
1231 static void ErrorThread( vout_thread_t *p_vout )
1233 /* Wait until a `die' order */
1235 while( !p_vout->b_die )
1238 msleep( VOUT_IDLE_SLEEP );
1242 /*****************************************************************************
1243 * EndThread: thread destruction
1244 *****************************************************************************
1245 * This function is called when the thread ends after a sucessful
1246 * initialization. It frees all ressources allocated by InitThread.
1247 *****************************************************************************/
1248 static void EndThread( vout_thread_t *p_vout )
1250 int i_index; /* index in heap */
1254 *p_vout->pi_status = THREAD_END;
1256 /* Destroy all remaining pictures and subpictures */
1257 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
1259 if( p_vout->p_picture[i_index].i_status != FREE_PICTURE )
1261 free( p_vout->p_picture[i_index].p_data );
1264 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1266 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1268 free( p_vout->p_subpicture[i_index].p_data );
1272 /* Destroy translation tables */
1273 vout_EndYUV( p_vout );
1274 p_vout->p_sys_end( p_vout );
1277 /*****************************************************************************
1278 * DestroyThread: thread destruction
1279 *****************************************************************************
1280 * This function is called when the thread ends. It frees all ressources
1281 * allocated by CreateThread. Status is available at this stage.
1282 *****************************************************************************/
1283 static void DestroyThread( vout_thread_t *p_vout, int i_status )
1285 int *pi_status; /* status adress */
1287 /* Store status adress */
1289 pi_status = p_vout->pi_status;
1291 /* Destroy thread structures allocated by Create and InitThread */
1292 vout_UnloadFont( p_vout->p_default_font );
1293 vout_UnloadFont( p_vout->p_large_font );
1294 p_vout->p_sys_destroy( p_vout );
1297 TrashPlugin( p_vout->vout_plugin );
1299 /* Free structure */
1301 *pi_status = i_status;
1304 /*****************************************************************************
1305 * Print: print simple text on a picture
1306 *****************************************************************************
1307 * This function will print a simple text on the picture. It is designed to
1308 * print debugging or general informations.
1309 *****************************************************************************/
1310 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 )
1312 int i_text_height; /* total text height */
1313 int i_text_width; /* total text width */
1315 /* Update upper left coordinates according to alignment */
1316 vout_TextSize( p_vout->p_default_font, 0, psz_text, &i_text_width, &i_text_height );
1317 if( !Align( p_vout, &i_x, &i_y, i_text_width, i_text_height, i_h_align, i_v_align ) )
1319 /* Set area and print text */
1320 SetBufferArea( p_vout, i_x, i_y, i_text_width, i_text_height );
1321 vout_Print( p_vout->p_default_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1322 i_y * p_vout->i_bytes_per_line + i_x * p_vout->i_bytes_per_pixel,
1323 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1324 p_vout->i_white_pixel, 0, 0,
1329 /*****************************************************************************
1330 * SetBufferArea: activate an area in current buffer
1331 *****************************************************************************
1332 * This function is called when something is rendered on the current buffer.
1333 * It set the area as active and prepare it to be cleared on next rendering.
1334 * Pay attention to the fact that in this functions, i_h is in fact the end y
1335 * coordinate of the new area.
1336 *****************************************************************************/
1337 static void SetBufferArea( vout_thread_t *p_vout, int i_x, int i_y, int i_w, int i_h )
1339 vout_buffer_t * p_buffer; /* current buffer */
1340 int i_area_begin, i_area_end; /* area vertical extension */
1341 int i_area, i_area_copy; /* area index */
1342 int i_area_shift; /* shift distance for areas */
1344 /* Choose buffer and modify h to end of area position */
1345 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1349 * Remove part of the area which is inside the picture - this is done
1350 * by calling again SetBufferArea with the correct areas dimensions.
1352 if( (i_x >= p_buffer->i_pic_x) && (i_x + i_w <= p_buffer->i_pic_x + p_buffer->i_pic_width) )
1354 i_area_begin = p_buffer->i_pic_y;
1355 i_area_end = i_area_begin + p_buffer->i_pic_height - 1;
1357 if( ((i_y >= i_area_begin) && (i_y <= i_area_end)) ||
1358 ((i_h >= i_area_begin) && (i_h <= i_area_end)) ||
1359 ((i_y < i_area_begin) && (i_h > i_area_end)) )
1361 /* Keep the stripe above the picture, if any */
1362 if( i_y < i_area_begin )
1364 SetBufferArea( p_vout, i_x, i_y, i_w, i_area_begin - i_y );
1366 /* Keep the stripe below the picture, if any */
1367 if( i_h > i_area_end )
1369 SetBufferArea( p_vout, i_x, i_area_end, i_w, i_h - i_area_end );
1375 /* Skip some extensions until interesting areas */
1377 (i_area < p_buffer->i_areas) &&
1378 (p_buffer->pi_area_end[i_area] + 1 <= i_y);
1384 if( i_area == p_buffer->i_areas )
1386 /* New area is below all existing ones: just add it at the end of the
1387 * array, if possible - else, append it to the last one */
1388 if( i_area < VOUT_MAX_AREAS )
1390 p_buffer->pi_area_begin[i_area] = i_y;
1391 p_buffer->pi_area_end[i_area] = i_h;
1392 p_buffer->i_areas++;
1397 intf_DbgMsg("areas overflow\n");
1399 p_buffer->pi_area_end[VOUT_MAX_AREAS - 1] = i_h;
1404 i_area_begin = p_buffer->pi_area_begin[i_area];
1405 i_area_end = p_buffer->pi_area_end[i_area];
1407 if( i_y < i_area_begin )
1409 if( i_h >= i_area_begin - 1 )
1411 /* Extend area above */
1412 p_buffer->pi_area_begin[i_area] = i_y;
1416 /* Create a new area above : merge last area if overflow, then
1417 * move all old areas down */
1418 if( p_buffer->i_areas == VOUT_MAX_AREAS )
1421 intf_DbgMsg("areas overflow\n");
1423 p_buffer->pi_area_end[VOUT_MAX_AREAS - 2] = p_buffer->pi_area_end[VOUT_MAX_AREAS - 1];
1427 p_buffer->i_areas++;
1429 for( i_area_copy = p_buffer->i_areas - 1; i_area_copy > i_area; i_area_copy-- )
1431 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy - 1];
1432 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy - 1];
1434 p_buffer->pi_area_begin[i_area] = i_y;
1435 p_buffer->pi_area_end[i_area] = i_h;
1439 if( i_h > i_area_end )
1441 /* Find further areas which can be merged with the new one */
1442 for( i_area_copy = i_area + 1;
1443 (i_area_copy < p_buffer->i_areas) &&
1444 (p_buffer->pi_area_begin[i_area] <= i_h);
1451 if( i_area_copy != i_area )
1453 /* Merge with last possible areas */
1454 p_buffer->pi_area_end[i_area] = MAX( i_h, p_buffer->pi_area_end[i_area_copy] );
1456 /* Shift lower areas upward */
1457 i_area_shift = i_area_copy - i_area;
1458 p_buffer->i_areas -= i_area_shift;
1459 for( i_area_copy = i_area + 1; i_area_copy < p_buffer->i_areas; i_area_copy++ )
1461 p_buffer->pi_area_begin[i_area_copy] = p_buffer->pi_area_begin[i_area_copy + i_area_shift];
1462 p_buffer->pi_area_end[i_area_copy] = p_buffer->pi_area_end[i_area_copy + i_area_shift];
1467 /* Extend area below */
1468 p_buffer->pi_area_end[i_area] = i_h;
1474 /*****************************************************************************
1475 * SetBufferPicture: clear buffer and set picture area
1476 *****************************************************************************
1477 * This function is called before any rendering. It clears the current
1478 * rendering buffer and set the new picture area. If the picture pointer is
1479 * NULL, then no picture area is defined. Floating operations are avoided since
1480 * some MMX calculations may follow.
1481 *****************************************************************************/
1482 static void SetBufferPicture( vout_thread_t *p_vout, picture_t *p_pic )
1484 vout_buffer_t * p_buffer; /* current buffer */
1485 int i_pic_x, i_pic_y; /* picture position */
1486 int i_pic_width, i_pic_height; /* picture dimensions */
1487 int i_old_pic_y, i_old_pic_height; /* old picture area */
1488 int i_vout_width, i_vout_height; /* display dimensions */
1489 int i_area; /* area index */
1490 int i_data_index; /* area data index */
1491 int i_data_size; /* area data size, in 256 bytes blocs */
1492 u64 * p_data; /* area data, for clearing */
1493 byte_t * p_data8; /* area data, for clearing (slow) */
1495 /* Choose buffer and set display dimensions */
1496 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1497 i_vout_width = p_vout->i_width;
1498 i_vout_height = p_vout->i_height;
1501 * Computes new picture size
1505 /* Try horizontal scaling first - width must be a mutiple of 16 */
1506 i_pic_width = (( p_vout->b_scale || (p_pic->i_width > i_vout_width)) ?
1507 i_vout_width : p_pic->i_width) & ~0xf;
1508 switch( p_pic->i_aspect_ratio )
1510 case AR_3_4_PICTURE:
1511 i_pic_height = i_pic_width * 3 / 4;
1513 case AR_16_9_PICTURE:
1514 i_pic_height = i_pic_width * 9 / 16;
1516 case AR_221_1_PICTURE:
1517 i_pic_height = i_pic_width * 100 / 221;
1519 case AR_SQUARE_PICTURE:
1521 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1525 /* If picture dimensions using horizontal scaling are too large, use
1526 * vertical scaling. Since width must be a multiple of 16, height is
1527 * adjusted again after. */
1528 if( i_pic_height > i_vout_height )
1530 i_pic_height = ( p_vout->b_scale || (p_pic->i_height > i_vout_height)) ?
1531 i_vout_height : p_pic->i_height;
1532 switch( p_pic->i_aspect_ratio )
1534 case AR_3_4_PICTURE:
1535 i_pic_width = (i_pic_height * 4 / 3) & ~0xf;
1536 i_pic_height = i_pic_width * 3 / 4;
1538 case AR_16_9_PICTURE:
1539 i_pic_width = (i_pic_height * 16 / 9) & ~0xf;
1540 i_pic_height = i_pic_width * 9 / 16;
1542 case AR_221_1_PICTURE:
1543 i_pic_width = (i_pic_height * 221 / 100) & ~0xf;
1544 i_pic_height = i_pic_width * 100 / 221;
1546 case AR_SQUARE_PICTURE:
1548 i_pic_width = (p_pic->i_width * i_pic_height / p_pic->i_height) & ~0xf;
1549 i_pic_height = p_pic->i_height * i_pic_width / p_pic->i_width;
1554 /* Set picture position */
1555 i_pic_x = (p_vout->i_width - i_pic_width) / 2;
1556 i_pic_y = (p_vout->i_height - i_pic_height) / 2;
1560 /* No picture: size is 0 */
1568 * Set new picture size - if is is smaller than the previous one, clear
1569 * around it. Since picture are centered, only their size is tested.
1571 if( (p_buffer->i_pic_width > i_pic_width) || (p_buffer->i_pic_height > i_pic_height) )
1573 i_old_pic_y = p_buffer->i_pic_y;
1574 i_old_pic_height = p_buffer->i_pic_height;
1575 p_buffer->i_pic_x = i_pic_x;
1576 p_buffer->i_pic_y = i_pic_y;
1577 p_buffer->i_pic_width = i_pic_width;
1578 p_buffer->i_pic_height = i_pic_height;
1579 SetBufferArea( p_vout, 0, i_old_pic_y, p_vout->i_width, i_old_pic_height );
1583 p_buffer->i_pic_x = i_pic_x;
1584 p_buffer->i_pic_y = i_pic_y;
1585 p_buffer->i_pic_width = i_pic_width;
1586 p_buffer->i_pic_height = i_pic_height;
1592 for( i_area = 0; i_area < p_buffer->i_areas; i_area++ )
1595 intf_DbgMsg("clearing picture %p area in buffer %d: %d-%d\n", p_pic,
1596 p_vout->i_buffer_index, p_buffer->pi_area_begin[i_area], p_buffer->pi_area_end[i_area] );
1598 i_data_size = (p_buffer->pi_area_end[i_area] - p_buffer->pi_area_begin[i_area] + 1) * p_vout->i_bytes_per_line;
1599 p_data = (u64*) (p_buffer->p_data + p_vout->i_bytes_per_line * p_buffer->pi_area_begin[i_area]);
1600 for( i_data_index = i_data_size / 256; i_data_index-- ; )
1602 /* Clear 256 bytes block */
1603 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1604 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1605 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1606 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1607 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1608 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1609 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1610 *p_data++ = 0; *p_data++ = 0; *p_data++ = 0; *p_data++ = 0;
1612 for( i_data_index = (i_data_size % 256) / 16; i_data_index--; )
1614 /* Clear remaining 16 bytes blocks */
1615 *p_data++ = 0; *p_data++ = 0;
1617 p_data8 = (byte_t *)p_data;
1618 for( i_data_index = i_data_size % 16; i_data_index--; )
1620 /* Clear remaining bytes */
1628 p_buffer->i_areas = 0;
1631 /*****************************************************************************
1632 * RenderPicture: render a picture
1633 *****************************************************************************
1634 * This function convert a picture from a video heap to a pixel-encoded image
1635 * and copy it to the current rendering buffer. No lock is required, since the
1636 * rendered picture has been determined as existant, and will only be destroyed
1637 * by the vout thread later.
1638 *****************************************************************************/
1639 static void RenderPicture( vout_thread_t *p_vout, picture_t *p_pic )
1642 char psz_date[MSTRTIME_MAX_SIZE]; /* picture date */
1643 mtime_t render_time; /* picture rendering time */
1645 vout_buffer_t * p_buffer; /* rendering buffer */
1646 byte_t * p_pic_data; /* convertion destination */
1648 /* Get and set rendering informations */
1649 p_buffer = &p_vout->p_buffer[ p_vout->i_buffer_index ];
1650 p_pic_data = p_buffer->p_data +
1651 p_buffer->i_pic_x * p_vout->i_bytes_per_pixel +
1652 p_buffer->i_pic_y * p_vout->i_bytes_per_line;
1654 render_time = mdate();
1658 * Choose appropriate rendering function and render picture
1660 switch( p_pic->i_type )
1662 case YUV_420_PICTURE:
1663 p_vout->yuv.p_Convert420( p_vout, p_pic_data,
1664 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1665 p_pic->i_width, p_pic->i_height,
1666 p_buffer->i_pic_width, p_buffer->i_pic_height,
1667 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1668 p_pic->i_matrix_coefficients );
1670 case YUV_422_PICTURE:
1671 p_vout->yuv.p_Convert422( p_vout, p_pic_data,
1672 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1673 p_pic->i_width, p_pic->i_height,
1674 p_buffer->i_pic_width, p_buffer->i_pic_height,
1675 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1676 p_pic->i_matrix_coefficients );
1678 case YUV_444_PICTURE:
1679 p_vout->yuv.p_Convert444( p_vout, p_pic_data,
1680 p_pic->p_y, p_pic->p_u, p_pic->p_v,
1681 p_pic->i_width, p_pic->i_height,
1682 p_buffer->i_pic_width, p_buffer->i_pic_height,
1683 p_vout->i_bytes_per_line / p_vout->i_bytes_per_pixel,
1684 p_pic->i_matrix_coefficients );
1688 intf_DbgMsg("error: unknown picture type %d\n", p_pic->i_type );
1694 /* Print picture date and rendering time */
1695 intf_DbgMsg("picture %p rendered in buffer %d (%ld us), display date: %s\n", p_pic,
1696 p_vout->i_buffer_index, (long) (mdate() - render_time),
1697 mstrtime( psz_date, p_pic->date ));
1701 /*****************************************************************************
1702 * RenderPictureInfo: print additionnal informations on a picture
1703 *****************************************************************************
1704 * This function will print informations such as fps and other picture
1705 * dependant informations.
1706 *****************************************************************************/
1707 static void RenderPictureInfo( vout_thread_t *p_vout, picture_t *p_pic )
1709 #if defined(STATS) || defined(DEBUG)
1710 char psz_buffer[256]; /* string buffer */
1715 * Print FPS rate in upper right corner
1717 if( p_vout->c_fps_samples > VOUT_FPS_SAMPLES )
1719 sprintf( psz_buffer, "%.2f fps", (double) VOUT_FPS_SAMPLES * 1000000 /
1720 ( p_vout->p_fps_sample[ (p_vout->c_fps_samples - 1) % VOUT_FPS_SAMPLES ] -
1721 p_vout->p_fps_sample[ p_vout->c_fps_samples % VOUT_FPS_SAMPLES ] ) );
1722 Print( p_vout, 0, 0, RIGHT_RALIGN, TOP_RALIGN, psz_buffer );
1726 * Print frames count and loop time in upper left corner
1728 sprintf( psz_buffer, "%ld frames rendering: %ld us",
1729 (long) p_vout->c_fps_samples, (long) p_vout->render_time );
1730 Print( p_vout, 0, 0, LEFT_RALIGN, TOP_RALIGN, psz_buffer );
1735 * Print picture information in lower right corner
1737 sprintf( psz_buffer, "%s picture %dx%d (%dx%d%+d%+d %s) -> %dx%d+%d+%d",
1738 (p_pic->i_type == YUV_420_PICTURE) ? "4:2:0" :
1739 ((p_pic->i_type == YUV_422_PICTURE) ? "4:2:2" :
1740 ((p_pic->i_type == YUV_444_PICTURE) ? "4:4:4" : "ukn-type")),
1741 p_pic->i_width, p_pic->i_height,
1742 p_pic->i_display_width, p_pic->i_display_height,
1743 p_pic->i_display_horizontal_offset, p_pic->i_display_vertical_offset,
1744 (p_pic->i_aspect_ratio == AR_SQUARE_PICTURE) ? "sq" :
1745 ((p_pic->i_aspect_ratio == AR_3_4_PICTURE) ? "4:3" :
1746 ((p_pic->i_aspect_ratio == AR_16_9_PICTURE) ? "16:9" :
1747 ((p_pic->i_aspect_ratio == AR_221_1_PICTURE) ? "2.21:1" : "ukn-ar" ))),
1748 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_width,
1749 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height,
1750 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_x,
1751 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y );
1752 Print( p_vout, 0, 0, RIGHT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1756 /*****************************************************************************
1757 * RenderIdle: render idle picture
1758 *****************************************************************************
1759 * This function will print something on the screen. It will return 0 if
1760 * nothing has been rendered, or 1 if something has been changed on the screen.
1761 * Note that if you absolutely want something to be printed, you will have
1762 * to force it by setting the last idle date to 0.
1763 * Unlike other rendering functions, this one calls the SetBufferPicture
1764 * function when needed.
1765 *****************************************************************************/
1766 static int RenderIdle( vout_thread_t *p_vout )
1768 int i_x = 0, i_y = 0; /* text position */
1769 int i_width, i_height; /* text size */
1770 mtime_t current_date; /* current date */
1771 const char *psz_text = "waiting for stream ..."; /* text to display */
1774 current_date = mdate();
1775 if( (current_date - p_vout->last_display_date) > VOUT_IDLE_DELAY &&
1776 (current_date - p_vout->last_idle_date) > VOUT_IDLE_DELAY )
1778 SetBufferPicture( p_vout, NULL );
1779 vout_TextSize( p_vout->p_large_font, WIDE_TEXT | OUTLINED_TEXT, psz_text,
1780 &i_width, &i_height );
1781 if( !Align( p_vout, &i_x, &i_y, i_width, i_height, CENTER_RALIGN, CENTER_RALIGN ) )
1783 vout_Print( p_vout->p_large_font,
1784 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1785 i_x * p_vout->i_bytes_per_pixel + i_y * p_vout->i_bytes_per_line,
1786 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1787 p_vout->i_white_pixel, p_vout->i_gray_pixel, 0,
1788 WIDE_TEXT | OUTLINED_TEXT, psz_text );
1789 SetBufferArea( p_vout, i_x, i_y, i_width, i_height );
1796 /*****************************************************************************
1797 * RenderInfo: render additionnal informations
1798 *****************************************************************************
1799 * This function render informations which do not depend of the current picture
1801 *****************************************************************************/
1802 static void RenderInfo( vout_thread_t *p_vout )
1805 char psz_buffer[256]; /* string buffer */
1806 int i_ready_pic = 0; /* ready pictures */
1807 int i_reserved_pic = 0; /* reserved pictures */
1808 int i_picture; /* picture index */
1813 * Print thread state in lower left corner
1815 for( i_picture = 0; i_picture < VOUT_MAX_PICTURES; i_picture++ )
1817 switch( p_vout->p_picture[i_picture].i_status )
1819 case RESERVED_PICTURE:
1820 case RESERVED_DATED_PICTURE:
1821 case RESERVED_DISP_PICTURE:
1829 sprintf( psz_buffer, "pic: %d (%d/%d)/%d",
1830 p_vout->i_pictures, i_reserved_pic, i_ready_pic, VOUT_MAX_PICTURES );
1831 Print( p_vout, 0, 0, LEFT_RALIGN, BOTTOM_RALIGN, psz_buffer );
1835 /*****************************************************************************
1836 * RenderSubPicture: render a subpicture
1837 *****************************************************************************
1838 * This function render a sub picture unit.
1839 *****************************************************************************/
1840 static void RenderSubPicture( vout_thread_t *p_vout, subpicture_t *p_subpic )
1842 p_vout_font_t p_font; /* text font */
1843 int i_width, i_height; /* subpicture dimensions */
1845 while( p_subpic != NULL )
1847 switch( p_subpic->i_type )
1849 case DVD_SUBPICTURE: /* DVD subpicture unit */
1850 /* test if the picture really has to be displayed */
1851 if( mdate() < p_subpic->begin_date )
1855 if( mdate() > p_subpic->end_date )
1857 /* too late, destroying the subpic */
1858 vout_DestroySubPicture( p_vout, p_subpic );
1859 printf( "destroying subpicture\n" );
1862 vout_RenderSPU( p_subpic->p_data, p_subpic->type.spu.i_offset,
1864 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data,
1865 p_vout->i_bytes_per_pixel,
1866 p_vout->i_bytes_per_line );
1868 case TEXT_SUBPICTURE: /* single line text */
1869 /* Select default font if not specified */
1870 p_font = p_subpic->type.text.p_font;
1871 if( p_font == NULL )
1873 p_font = p_vout->p_default_font;
1876 /* Compute text size (width and height fields are ignored)
1878 vout_TextSize( p_font, p_subpic->type.text.i_style,
1879 p_subpic->p_data, &i_width, &i_height );
1880 if( !Align( p_vout, &p_subpic->i_x, &p_subpic->i_y,
1881 i_width, i_height, p_subpic->i_horizontal_align,
1882 p_subpic->i_vertical_align ) )
1885 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1886 p_subpic->i_x * p_vout->i_bytes_per_pixel +
1887 p_subpic->i_y * p_vout->i_bytes_per_line,
1888 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1889 p_subpic->type.text.i_char_color,
1890 p_subpic->type.text.i_border_color,
1891 p_subpic->type.text.i_bg_color,
1892 p_subpic->type.text.i_style, p_subpic->p_data );
1893 SetBufferArea( p_vout, p_subpic->i_x, p_subpic->i_y,
1894 i_width, i_height );
1900 intf_DbgMsg( "error: unknown subpicture %p type %d\n",
1901 p_subpic, p_subpic->i_type );
1905 p_subpic = p_subpic->p_next;
1909 /*****************************************************************************
1910 * RenderInterface: render the interface
1911 *****************************************************************************
1912 * This function render the interface, if any.
1913 *****************************************************************************/
1914 static void RenderInterface( vout_thread_t *p_vout )
1916 int i_height, i_text_height; /* total and text height */
1917 int i_width_1, i_width_2; /* text width */
1918 int i_byte; /* byte index */
1919 const char *psz_text_1 = "[1-9] Channel [i]nfo [c]olor [g/G]amma";
1920 const char *psz_text_2 = "[+/-] Volume [m]ute [s]caling [Q]uit";
1923 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_1, &i_width_1, &i_height );
1924 vout_TextSize( p_vout->p_large_font, OUTLINED_TEXT, psz_text_2, &i_width_2, &i_text_height );
1925 i_height += i_text_height;
1927 /* Render background */
1928 for( i_byte = (p_vout->i_height - i_height) * p_vout->i_bytes_per_line;
1929 i_byte < p_vout->i_height * p_vout->i_bytes_per_line;
1933 p_vout->p_buffer[ p_vout->i_buffer_index ].p_data[ i_byte ] = p_vout->i_blue_pixel;
1936 /* Render text, if not larger than screen */
1937 if( i_width_1 < p_vout->i_width )
1939 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1940 (p_vout->i_height - i_height) * p_vout->i_bytes_per_line,
1941 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1942 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1943 OUTLINED_TEXT, psz_text_1 );
1945 if( i_width_2 < p_vout->i_width )
1947 vout_Print( p_vout->p_large_font, p_vout->p_buffer[ p_vout->i_buffer_index ].p_data +
1948 (p_vout->i_height - i_height + i_text_height) * p_vout->i_bytes_per_line,
1949 p_vout->i_bytes_per_pixel, p_vout->i_bytes_per_line,
1950 p_vout->i_white_pixel, p_vout->i_black_pixel, 0,
1951 OUTLINED_TEXT, psz_text_2 );
1954 /* Activate modified area */
1955 SetBufferArea( p_vout, 0, p_vout->i_height - i_height, p_vout->i_width, i_height );
1958 /*****************************************************************************
1959 * Synchronize: update synchro level depending of heap state
1960 *****************************************************************************
1961 * This function is called during the main vout loop.
1962 *****************************************************************************/
1963 static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
1965 int i_synchro_inc = 0;
1966 /* XXX?? gore following */
1967 static int i_panic_count = 0;
1968 static int i_last_synchro_inc = 0;
1969 static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
1970 static int i_truc = 10;
1974 //fprintf( stderr, "PANIC %d\n", i_panic_count );
1980 if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
1983 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
1988 if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
1991 i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
1995 if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
1996 i_synchro_inc*i_last_synchro_inc < 0 )
2001 if( i_delay < 6000 )
2006 else if( i_delay < 70000 )
2008 i_truc = 24+(24*i_delay)/70000;
2011 i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
2013 else if( i_delay > 100000 )
2015 r_synchro_level += 1;
2016 if( i_delay > 130000 )
2017 r_synchro_level += 1;
2020 r_synchro_level += (float)i_synchro_inc / i_truc;
2021 p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
2023 if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
2025 r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
2028 //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
2029 // p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
2030 i_last_synchro_inc = i_synchro_inc;
2033 /*****************************************************************************
2034 * Manage: manage thread
2035 *****************************************************************************
2036 * This function will handle changes in thread configuration.
2037 *****************************************************************************/
2038 static int Manage( vout_thread_t *p_vout )
2041 if( p_vout->i_changes )
2043 intf_DbgMsg("changes: 0x%x (no display: 0x%x)\n", p_vout->i_changes,
2044 p_vout->i_changes & VOUT_NODISPLAY_CHANGE );
2048 /* On gamma or grayscale change, rebuild tables */
2049 if( p_vout->i_changes & (VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2052 if( vout_ResetYUV( p_vout ) )
2054 intf_ErrMsg("error: can't rebuild convertion tables\n");
2059 /* Clear changes flags which does not need management or have been
2061 p_vout->i_changes &= ~(VOUT_GAMMA_CHANGE | VOUT_GRAYSCALE_CHANGE |
2062 VOUT_YUV_CHANGE | VOUT_INFO_CHANGE |
2063 VOUT_INTF_CHANGE | VOUT_SCALE_CHANGE );
2065 /* Detect unauthorized changes */
2066 if( p_vout->i_changes )
2068 /* Some changes were not acknowledged by p_vout->p_sys_manage or this
2069 * function, it means they should not be authorized */
2070 intf_ErrMsg( "error: unauthorized changes in the video output thread\n" );
2077 /*****************************************************************************
2078 * Align: align a subpicture in the screen
2079 *****************************************************************************
2080 * This function is used for rendering text or subpictures. It returns non 0
2081 * it the final aera is not fully included in display area. Return coordinates
2083 *****************************************************************************/
2084 static int Align( vout_thread_t *p_vout, int *pi_x, int *pi_y,
2085 int i_width, int i_height, int i_h_align, int i_v_align )
2087 /* Align horizontally */
2091 *pi_x -= i_width / 2;
2094 *pi_x += (p_vout->i_width - i_width) / 2;
2100 *pi_x += p_vout->i_width - i_width;
2104 /* Align vertically */
2108 *pi_y -= i_height / 2;
2111 *pi_y += (p_vout->i_height - i_height) / 2;
2117 *pi_y += p_vout->i_height - i_height;
2119 case SUBTITLE_RALIGN:
2120 *pi_y += (p_vout->i_height + p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_y +
2121 p_vout->p_buffer[ p_vout->i_buffer_index ].i_pic_height - i_height) / 2;
2125 /* Return non 0 if clipping failed */
2126 return( (*pi_x < 0) || (*pi_y < 0) ||
2127 (*pi_x + i_width > p_vout->i_width) || (*pi_y + i_height > p_vout->i_height) );
2130 /*****************************************************************************
2131 * SetPalette: sets an 8 bpp palette
2132 *****************************************************************************
2133 * This function is just a prototype that does nothing. Architectures that
2134 * support palette allocation should override it.
2135 *****************************************************************************/
2136 static void SetPalette ( p_vout_thread_t p_vout, u16 *red,
2137 u16 *green, u16 *blue, u16 *transp )
2139 intf_ErrMsg( "SetPalette: method does not support palette changing\n" );