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-2001 VideoLAN
8 * $Id: video_output.c,v 1.151 2002/01/02 14:37:42 sam Exp $
10 * Authors: Vincent Seguin <seguin@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <stdio.h> /* sprintf() */
33 #include <string.h> /* strerror() */
35 #include <videolan/vlc.h>
37 #ifdef HAVE_SYS_TIMES_H
38 # include <sys/times.h>
42 #include "video_output.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 static int InitThread ( vout_thread_t *p_vout );
48 static void RunThread ( vout_thread_t *p_vout );
49 static void ErrorThread ( vout_thread_t *p_vout );
50 static void EndThread ( vout_thread_t *p_vout );
51 static void DestroyThread ( vout_thread_t *p_vout, int i_status );
52 static int ReduceHeight ( int );
54 /*****************************************************************************
55 * vout_InitBank: initialize the video output bank.
56 *****************************************************************************/
57 void vout_InitBank ( void )
59 p_vout_bank->i_count = 0;
61 vlc_mutex_init( &p_vout_bank->lock );
64 /*****************************************************************************
65 * vout_EndBank: empty the video output bank.
66 *****************************************************************************
67 * This function ends all unused video outputs and empties the bank in
69 *****************************************************************************/
70 void vout_EndBank ( void )
72 /* Ask all remaining video outputs to die */
73 while( p_vout_bank->i_count )
76 p_vout_bank->pp_vout[ --p_vout_bank->i_count ], NULL );
79 vlc_mutex_destroy( &p_vout_bank->lock );
82 /*****************************************************************************
83 * vout_CreateThread: creates a new video output thread
84 *****************************************************************************
85 * This function creates a new video output thread, and returns a pointer
86 * to its description. On error, it returns NULL.
87 * If pi_status is NULL, then the function will block until the thread is ready.
88 * If not, it will be updated using one of the THREAD_* constants.
89 *****************************************************************************/
90 vout_thread_t * vout_CreateThread ( int *pi_status,
91 int i_width, int i_height,
92 u64 i_chroma, int i_aspect )
94 vout_thread_t * p_vout; /* thread descriptor */
95 int i_status; /* thread status */
96 int i_index; /* loop variable */
99 /* Allocate descriptor */
100 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
103 intf_ErrMsg( "vout error: vout thread creation returned %s",
108 /* Choose the best module */
109 psz_plugin = main_GetPszVariable( VOUT_FILTER_VAR, "" );
110 if( psz_plugin[0] == '\0' )
112 psz_plugin = main_GetPszVariable( VOUT_METHOD_VAR, "" );
115 p_vout->p_module = module_Need( MODULE_CAPABILITY_VOUT, psz_plugin, NULL );
117 if( p_vout->p_module == NULL )
119 intf_ErrMsg( "vout error: no suitable vout module" );
124 #define f p_vout->p_module->p_functions->vout.functions.vout
125 p_vout->pf_create = f.pf_create;
126 p_vout->pf_init = f.pf_init;
127 p_vout->pf_end = f.pf_end;
128 p_vout->pf_destroy = f.pf_destroy;
129 p_vout->pf_manage = f.pf_manage;
130 p_vout->pf_display = f.pf_display;
131 p_vout->pf_setpalette = f.pf_setpalette;
134 /* Initialize thread properties - thread id and locks will be initialized
138 p_vout->b_active = 0;
139 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
140 *p_vout->pi_status = THREAD_CREATE;
142 /* Initialize pictures and subpictures - translation tables and functions
143 * will be initialized later in InitThread */
144 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
146 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
147 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
150 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
152 p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
153 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
156 /* Initialize the rendering heap */
157 p_vout->i_heap_size = 0;
159 I_RENDERPICTURES = 0;
160 p_vout->render.i_width = i_width;
161 p_vout->render.i_height = i_height;
162 p_vout->render.i_chroma = i_chroma;
163 p_vout->render.i_aspect = i_aspect;
165 /* Initialize misc stuff */
166 p_vout->i_changes = 0;
168 p_vout->b_grayscale = 0;
170 p_vout->b_interface = 0;
172 p_vout->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
173 VOUT_FULLSCREEN_DEFAULT );
174 p_vout->render_time = 10;
176 /* Create thread and set locks */
177 vlc_mutex_init( &p_vout->picture_lock );
178 vlc_mutex_init( &p_vout->subpicture_lock );
179 vlc_mutex_init( &p_vout->change_lock );
181 if( vlc_thread_create( &p_vout->thread_id, "video output",
182 (void *) RunThread, (void *) p_vout) )
184 intf_ErrMsg("vout error: %s", strerror(ENOMEM));
185 p_vout->pf_destroy( p_vout );
186 module_Unneed( p_vout->p_module );
191 /* If status is NULL, wait until the thread is created */
192 if( pi_status == NULL )
196 msleep( THREAD_SLEEP );
197 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
198 && (i_status != THREAD_FATAL) );
199 if( i_status != THREAD_READY )
208 /*****************************************************************************
209 * vout_DestroyThread: destroys a previously created thread
210 *****************************************************************************
211 * Destroy a terminated thread.
212 * The function will request a destruction of the specified thread. If pi_error
213 * is NULL, it will return once the thread is destroyed. Else, it will be
214 * update using one of the THREAD_* constants.
215 *****************************************************************************/
216 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
218 int i_status; /* thread status */
221 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
222 *p_vout->pi_status = THREAD_DESTROY;
224 /* Request thread destruction */
227 /* If status is NULL, wait until thread has been destroyed */
228 if( pi_status == NULL )
232 msleep( THREAD_SLEEP );
233 } while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
234 && (i_status != THREAD_FATAL) );
238 /*****************************************************************************
239 * InitThread: initialize video output thread
240 *****************************************************************************
241 * This function is called from RunThread and performs the second step of the
242 * initialization. It returns 0 on success. Note that the thread's flag are not
243 * modified inside this function.
244 *****************************************************************************/
245 static int InitThread( vout_thread_t *p_vout )
251 *p_vout->pi_status = THREAD_START;
253 vlc_mutex_lock( &p_vout->change_lock );
259 /* Initialize output method, it issues its own error messages */
260 if( p_vout->pf_init( p_vout ) )
262 vlc_mutex_unlock( &p_vout->change_lock );
266 if( !I_OUTPUTPICTURES )
268 intf_ErrMsg( "vout error: plugin was unable to allocate at least "
269 "one direct buffer" );
270 p_vout->pf_end( p_vout );
271 vlc_mutex_unlock( &p_vout->change_lock );
275 intf_WarnMsg( 1, "vout info: got %i direct buffer(s)", I_OUTPUTPICTURES );
277 i_pgcd = ReduceHeight( p_vout->render.i_aspect );
278 intf_WarnMsg( 1, "vout info: picture in %ix%i, chroma 0x%.16llx, "
279 "aspect ratio %i:%i",
280 p_vout->render.i_width, p_vout->render.i_height,
281 p_vout->render.i_chroma, p_vout->render.i_aspect / i_pgcd,
282 VOUT_ASPECT_FACTOR / i_pgcd );
284 i_pgcd = ReduceHeight( p_vout->output.i_aspect );
285 intf_WarnMsg( 1, "vout info: picture out %ix%i, chroma 0x%.16llx, "
286 "aspect ratio %i:%i",
287 p_vout->output.i_width, p_vout->output.i_height,
288 p_vout->output.i_chroma, p_vout->output.i_aspect / i_pgcd,
289 VOUT_ASPECT_FACTOR / i_pgcd );
291 /* Check whether we managed to create direct buffers similar to
292 * the render buffers, ie same size, chroma and aspect ratio */
293 if( ( p_vout->output.i_width == p_vout->render.i_width )
294 && ( p_vout->output.i_height == p_vout->render.i_height )
295 && ( p_vout->output.i_chroma == p_vout->render.i_chroma )
296 && ( p_vout->output.i_aspect == p_vout->render.i_aspect ) )
298 /* Cool ! We have direct buffers, we can ask the decoder to
299 * directly decode into them ! Map the first render buffers to
300 * the first direct buffers, but keep the first direct buffer
301 * for memcpy operations */
302 p_vout->b_direct = 1;
304 intf_WarnMsg( 2, "vout info: mapping "
305 "render pictures 0-%i to system pictures 1-%i",
306 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
308 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
310 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
317 /* Rats... Something is wrong here, we could not find an output
318 * plugin able to directly render what we decode. See if we can
319 * find a chroma plugin to do the conversion */
320 p_vout->b_direct = 0;
322 /* Choose the best module */
323 data.chroma.p_render = &p_vout->render;
324 data.chroma.p_output = &p_vout->output;
325 p_vout->chroma.p_module
326 = module_Need( MODULE_CAPABILITY_CHROMA, NULL, &data );
328 if( p_vout->chroma.p_module == NULL )
330 intf_ErrMsg( "vout error: no suitable chroma module" );
331 p_vout->pf_end( p_vout );
332 vlc_mutex_unlock( &p_vout->change_lock );
336 #define f p_vout->chroma.p_module->p_functions->chroma.functions.chroma
337 p_vout->chroma.pf_init = f.pf_init;
338 p_vout->chroma.pf_end = f.pf_end;
341 if( p_vout->chroma.pf_init( p_vout ) )
343 intf_ErrMsg( "vout error: could not initialize chroma module" );
344 module_Unneed( p_vout->chroma.p_module );
345 p_vout->pf_end( p_vout );
346 vlc_mutex_unlock( &p_vout->change_lock );
350 intf_WarnMsg( 2, "vout info: mapping "
351 "render pictures %i-%i to system pictures %i-%i",
352 I_OUTPUTPICTURES - 1, VOUT_MAX_PICTURES - 2,
353 I_OUTPUTPICTURES, VOUT_MAX_PICTURES - 1 );
355 /* Append render buffers after the direct buffers */
356 for( i = I_OUTPUTPICTURES; i < VOUT_MAX_PICTURES; i++ )
358 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
363 /* Mark thread as running and return */
364 p_vout->b_active = 1;
365 *p_vout->pi_status = THREAD_READY;
367 intf_DbgMsg("thread ready");
371 /*****************************************************************************
372 * RunThread: video output thread
373 *****************************************************************************
374 * Video output thread. This function does only returns when the thread is
375 * terminated. It handles the pictures arriving in the video heap and the
376 * display device events.
377 *****************************************************************************/
378 static void RunThread( vout_thread_t *p_vout)
380 int i_index; /* index in heap */
381 mtime_t current_date; /* current date */
382 mtime_t display_date; /* display date */
384 picture_t * p_picture; /* picture pointer */
385 picture_t * p_directbuffer; /* direct buffer to display */
387 subpicture_t * p_subpic; /* subpicture pointer */
389 /* Create and initialize system-dependant method - this function issues its
390 * own error messages */
391 if( p_vout->pf_create( p_vout ) )
393 DestroyThread( p_vout, THREAD_ERROR );
400 p_vout->b_error = InitThread( p_vout );
401 if( p_vout->b_error )
403 /* Destroy thread structures allocated by Create and InitThread */
404 p_vout->pf_destroy( p_vout );
406 DestroyThread( p_vout, THREAD_ERROR );
411 * Main loop - it is not executed if an error occured during
414 while( (!p_vout->b_die) && (!p_vout->b_error) )
416 /* Initialize loop variables */
418 current_date = mdate();
422 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
424 intf_Msg( "vout stats: picture heap: %d/%d",
425 I_RENDERPICTURES, p_vout->i_heap_size );
430 * Find the picture to display - this operation does not need lock,
431 * since only READY_PICTUREs are handled
436 i_index < I_RENDERPICTURES;
439 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
440 && ( (p_picture == NULL) ||
441 (PP_RENDERPICTURE[i_index]->date < display_date) ) )
443 p_picture = PP_RENDERPICTURE[i_index];
444 display_date = p_picture->date;
448 if( p_picture != NULL )
450 /* Compute FPS rate */
451 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
454 if( display_date < current_date + p_vout->render_time )
456 /* Picture is late: it will be destroyed and the thread
457 * will directly choose the next picture */
458 vlc_mutex_lock( &p_vout->picture_lock );
459 if( p_picture->i_refcount )
461 /* Pretend we displayed the picture, but don't destroy
462 * it since the decoder might still need it. */
463 p_picture->i_status = DISPLAYED_PICTURE;
467 /* Destroy the picture without displaying it */
468 p_picture->i_status = DESTROYED_PICTURE;
469 p_vout->i_heap_size--;
471 intf_WarnMsg( 1, "vout warning: late picture skipped (%p)",
473 vlc_mutex_unlock( &p_vout->picture_lock );
477 else if( display_date > current_date + VOUT_DISPLAY_DELAY )
479 /* A picture is ready to be rendered, but its rendering date
480 * is far from the current one so the thread will perform an
481 * empty loop as if no picture were found. The picture state
489 * Check for subpictures to display
491 p_subpic = vout_SortSubPictures( p_vout, display_date );
496 p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
501 if( display_date != 0 )
503 /* Store render time using Bresenham algorithm */
504 p_vout->render_time += mdate() - current_date;
505 p_vout->render_time >>= 1;
508 /* Give back change lock */
509 vlc_mutex_unlock( &p_vout->change_lock );
511 /* Sleep a while or until a given date */
512 if( display_date != 0 )
514 mwait( display_date - VOUT_MWAIT_TOLERANCE );
518 msleep( VOUT_IDLE_SLEEP );
521 /* On awakening, take back lock and send immediately picture
523 vlc_mutex_lock( &p_vout->change_lock );
526 intf_DbgMsg( "picture %p, subpicture %p", p_picture, p_subpic );
530 * Display the previously rendered picture
532 if( p_picture != NULL )
534 /* Display the direct buffer returned by vout_RenderPicture */
535 p_vout->pf_display( p_vout, p_directbuffer );
537 /* Remove picture from heap */
538 vlc_mutex_lock( &p_vout->picture_lock );
539 if( p_picture->i_refcount )
541 p_picture->i_status = DISPLAYED_PICTURE;
545 p_picture->i_status = DESTROYED_PICTURE;
546 p_vout->i_heap_size--;
548 vlc_mutex_unlock( &p_vout->picture_lock );
552 * Check events and manage thread
554 if( p_vout->pf_manage( p_vout ) )
556 /* A fatal error occured, and the thread must terminate immediately,
557 * without displaying anything - setting b_error to 1 causes the
558 * immediate end of the main while() loop. */
564 * Error loop - wait until the thread destruction is requested
566 if( p_vout->b_error )
568 ErrorThread( p_vout );
574 /* Destroy method-dependant resources */
575 p_vout->pf_destroy( p_vout );
577 /* Destroy thread structures allocated by CreateThread */
578 DestroyThread( p_vout, THREAD_OVER );
579 intf_DbgMsg( "thread end" );
582 /*****************************************************************************
583 * ErrorThread: RunThread() error loop
584 *****************************************************************************
585 * This function is called when an error occured during thread main's loop. The
586 * thread can still receive feed, but must be ready to terminate as soon as
588 *****************************************************************************/
589 static void ErrorThread( vout_thread_t *p_vout )
591 /* Wait until a `die' order */
592 while( !p_vout->b_die )
595 msleep( VOUT_IDLE_SLEEP );
599 /*****************************************************************************
600 * EndThread: thread destruction
601 *****************************************************************************
602 * This function is called when the thread ends after a sucessful
603 * initialization. It frees all ressources allocated by InitThread.
604 *****************************************************************************/
605 static void EndThread( vout_thread_t *p_vout )
607 int i_index; /* index in heap */
610 *p_vout->pi_status = THREAD_END;
614 struct tms cpu_usage;
617 intf_Msg( "vout stats: cpu usage (user: %d, system: %d)",
618 cpu_usage.tms_utime, cpu_usage.tms_stime );
622 if( !p_vout->b_direct )
624 p_vout->chroma.pf_end( p_vout );
625 module_Unneed( p_vout->chroma.p_module );
628 /* Destroy all remaining pictures */
629 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
631 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
633 free( p_vout->p_picture[i_index].planes[0].p_data );
637 /* Destroy all remaining subpictures */
638 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
640 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
642 free( p_vout->p_subpicture[i_index].p_data );
646 /* Destroy translation tables */
647 p_vout->pf_end( p_vout );
649 /* Release the change lock */
650 vlc_mutex_unlock( &p_vout->change_lock );
653 /*****************************************************************************
654 * DestroyThread: thread destruction
655 *****************************************************************************
656 * This function is called when the thread ends. It frees all ressources
657 * allocated by CreateThread. Status is available at this stage.
658 *****************************************************************************/
659 static void DestroyThread( vout_thread_t *p_vout, int i_status )
661 int *pi_status; /* status adress */
663 /* Store status adress */
664 pi_status = p_vout->pi_status;
666 /* Destroy the locks */
667 vlc_mutex_destroy( &p_vout->picture_lock );
668 vlc_mutex_destroy( &p_vout->subpicture_lock );
669 vlc_mutex_destroy( &p_vout->change_lock );
671 /* Release the module */
672 module_Unneed( p_vout->p_module );
676 *pi_status = i_status;
679 static int ReduceHeight( int i_ratio )
681 int i_dummy = VOUT_ASPECT_FACTOR;
684 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
685 while( !(i_ratio & 1) && !(i_dummy & 1) )
692 while( !(i_ratio % 3) && !(i_ratio % 3) )
699 while( !(i_ratio % 5) && !(i_ratio % 5) )