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.159 2002/02/18 19:02:41 stef 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 u32 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 /* Initialize thread properties - thread id and locks will be initialized
119 p_vout->b_active = 0;
120 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
121 *p_vout->pi_status = THREAD_CREATE;
123 /* Initialize pictures and subpictures - translation tables and functions
124 * will be initialized later in InitThread */
125 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
127 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
128 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
131 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
133 p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
134 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
137 /* Initialize the rendering heap */
138 p_vout->i_heap_size = 0;
140 I_RENDERPICTURES = 0;
141 p_vout->render.i_width = i_width;
142 p_vout->render.i_height = i_height;
143 p_vout->render.i_chroma = i_chroma;
144 p_vout->render.i_aspect = i_aspect;
146 /* Initialize misc stuff */
147 p_vout->i_changes = 0;
149 p_vout->b_grayscale = 0;
151 p_vout->b_interface = 0;
153 p_vout->b_fullscreen = 0;
154 p_vout->render_time = 10;
156 /* user requested fullscreen? */
157 if( main_GetIntVariable( VOUT_FULLSCREEN_VAR, VOUT_FULLSCREEN_DEFAULT ) )
158 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
161 = module_Need( MODULE_CAPABILITY_VOUT, psz_plugin, (void *)p_vout );
163 if( p_vout->p_module == NULL )
165 intf_ErrMsg( "vout error: no suitable vout module" );
170 #define f p_vout->p_module->p_functions->vout.functions.vout
171 p_vout->pf_create = f.pf_create;
172 p_vout->pf_init = f.pf_init;
173 p_vout->pf_end = f.pf_end;
174 p_vout->pf_destroy = f.pf_destroy;
175 p_vout->pf_manage = f.pf_manage;
176 p_vout->pf_render = f.pf_render;
177 p_vout->pf_display = f.pf_display;
180 /* Create thread and set locks */
181 vlc_mutex_init( &p_vout->picture_lock );
182 vlc_mutex_init( &p_vout->subpicture_lock );
183 vlc_mutex_init( &p_vout->change_lock );
185 if( vlc_thread_create( &p_vout->thread_id, "video output",
186 (void *) RunThread, (void *) p_vout) )
188 intf_ErrMsg("vout error: %s", strerror(ENOMEM));
189 p_vout->pf_destroy( p_vout );
190 module_Unneed( p_vout->p_module );
195 /* If status is NULL, wait until the thread is created */
196 if( pi_status == NULL )
200 msleep( THREAD_SLEEP );
201 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
202 && (i_status != THREAD_FATAL) );
203 if( i_status != THREAD_READY )
212 /*****************************************************************************
213 * vout_DestroyThread: destroys a previously created thread
214 *****************************************************************************
215 * Destroy a terminated thread.
216 * The function will request a destruction of the specified thread. If pi_error
217 * is NULL, it will return once the thread is destroyed. Else, it will be
218 * update using one of the THREAD_* constants.
219 *****************************************************************************/
220 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
222 int i_status; /* thread status */
225 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
226 *p_vout->pi_status = THREAD_DESTROY;
228 /* Request thread destruction */
231 /* If status is NULL, wait until thread has been destroyed */
232 if( pi_status == NULL )
236 msleep( THREAD_SLEEP );
237 } while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
238 && (i_status != THREAD_FATAL) );
242 /*****************************************************************************
243 * InitThread: initialize video output thread
244 *****************************************************************************
245 * This function is called from RunThread and performs the second step of the
246 * initialization. It returns 0 on success. Note that the thread's flag are not
247 * modified inside this function.
248 *****************************************************************************/
249 static int InitThread( vout_thread_t *p_vout )
254 *p_vout->pi_status = THREAD_START;
256 vlc_mutex_lock( &p_vout->change_lock );
262 /* Initialize output method, it issues its own error messages */
263 if( p_vout->pf_init( p_vout ) )
265 vlc_mutex_unlock( &p_vout->change_lock );
269 if( !I_OUTPUTPICTURES )
271 intf_ErrMsg( "vout error: plugin was unable to allocate at least "
272 "one direct buffer" );
273 p_vout->pf_end( p_vout );
274 vlc_mutex_unlock( &p_vout->change_lock );
278 intf_WarnMsg( 1, "vout info: got %i direct buffer(s)", I_OUTPUTPICTURES );
280 i_pgcd = ReduceHeight( p_vout->render.i_aspect );
281 intf_WarnMsg( 1, "vout info: picture in %ix%i, chroma 0x%.8x (%4.4s), "
282 "aspect ratio %i:%i",
283 p_vout->render.i_width, p_vout->render.i_height,
284 p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
285 p_vout->render.i_aspect / i_pgcd,
286 VOUT_ASPECT_FACTOR / i_pgcd );
288 i_pgcd = ReduceHeight( p_vout->output.i_aspect );
289 intf_WarnMsg( 1, "vout info: picture out %ix%i, chroma 0x%.8x (%4.4s), "
290 "aspect ratio %i:%i",
291 p_vout->output.i_width, p_vout->output.i_height,
292 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
293 p_vout->output.i_aspect / i_pgcd,
294 VOUT_ASPECT_FACTOR / i_pgcd );
296 /* Check whether we managed to create direct buffers similar to
297 * the render buffers, ie same size, chroma and aspect ratio */
298 if( ( p_vout->output.i_width == p_vout->render.i_width )
299 && ( p_vout->output.i_height == p_vout->render.i_height )
300 && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) )
301 && ( p_vout->output.i_aspect == p_vout->render.i_aspect ) )
303 /* Cool ! We have direct buffers, we can ask the decoder to
304 * directly decode into them ! Map the first render buffers to
305 * the first direct buffers, but keep the first direct buffer
306 * for memcpy operations */
307 p_vout->b_direct = 1;
309 intf_WarnMsg( 2, "vout info: direct render, mapping "
310 "render pictures 0-%i to system pictures 1-%i",
311 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
313 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
315 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
322 /* Rats... Something is wrong here, we could not find an output
323 * plugin able to directly render what we decode. See if we can
324 * find a chroma plugin to do the conversion */
325 p_vout->b_direct = 0;
327 /* Choose the best module */
328 p_vout->chroma.p_module
329 = module_Need( MODULE_CAPABILITY_CHROMA, NULL, (void *)p_vout );
331 if( p_vout->chroma.p_module == NULL )
333 intf_ErrMsg( "vout error: no chroma module for %4.4s to %4.4s",
334 &p_vout->render.i_chroma, &p_vout->output.i_chroma );
335 p_vout->pf_end( p_vout );
336 vlc_mutex_unlock( &p_vout->change_lock );
340 #define f p_vout->chroma.p_module->p_functions->chroma.functions.chroma
341 p_vout->chroma.pf_init = f.pf_init;
342 p_vout->chroma.pf_end = f.pf_end;
345 if( p_vout->chroma.pf_init( p_vout ) )
347 intf_ErrMsg( "vout error: could not initialize chroma module" );
348 module_Unneed( p_vout->chroma.p_module );
349 p_vout->pf_end( p_vout );
350 vlc_mutex_unlock( &p_vout->change_lock );
354 if( I_OUTPUTPICTURES < VOUT_MAX_PICTURES )
356 intf_WarnMsg( 2, "vout info: indirect render, mapping "
357 "render pictures %i-%i to system pictures %i-%i",
358 I_OUTPUTPICTURES - 1, VOUT_MAX_PICTURES - 2,
359 I_OUTPUTPICTURES, VOUT_MAX_PICTURES - 1 );
363 intf_WarnMsg( 2, "vout info: indirect render, no system "
364 "pictures needed, we have %i directbuffers",
368 /* Append render buffers after the direct buffers */
369 for( i = I_OUTPUTPICTURES; i < VOUT_MAX_PICTURES; i++ )
371 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
376 /* Mark thread as running and return */
377 p_vout->b_active = 1;
378 *p_vout->pi_status = THREAD_READY;
380 intf_DbgMsg("thread ready");
384 /*****************************************************************************
385 * RunThread: video output thread
386 *****************************************************************************
387 * Video output thread. This function does only returns when the thread is
388 * terminated. It handles the pictures arriving in the video heap and the
389 * display device events.
390 *****************************************************************************/
391 static void RunThread( vout_thread_t *p_vout)
393 int i_index; /* index in heap */
394 mtime_t current_date; /* current date */
395 mtime_t display_date; /* display date */
397 picture_t * p_picture; /* picture pointer */
398 picture_t * p_directbuffer; /* direct buffer to display */
400 subpicture_t * p_subpic; /* subpicture pointer */
405 p_vout->b_error = InitThread( p_vout );
406 if( p_vout->b_error )
408 /* Destroy thread structures allocated by Create and InitThread */
409 p_vout->pf_destroy( p_vout );
411 DestroyThread( p_vout, THREAD_ERROR );
416 * Main loop - it is not executed if an error occured during
419 while( (!p_vout->b_die) && (!p_vout->b_error) )
421 /* Initialize loop variables */
423 current_date = mdate();
427 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
429 intf_Msg( "vout stats: picture heap: %d/%d",
430 I_RENDERPICTURES, p_vout->i_heap_size );
435 * Find the picture to display - this operation does not need lock,
436 * since only READY_PICTUREs are handled
441 i_index < I_RENDERPICTURES;
444 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
445 && ( (p_picture == NULL) ||
446 (PP_RENDERPICTURE[i_index]->date < display_date) ) )
448 p_picture = PP_RENDERPICTURE[i_index];
449 display_date = p_picture->date;
453 if( p_picture != NULL )
455 /* Compute FPS rate */
456 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
459 if( display_date < current_date + p_vout->render_time )
461 /* Picture is late: it will be destroyed and the thread
462 * will directly choose the next picture */
463 vlc_mutex_lock( &p_vout->picture_lock );
464 if( p_picture->i_refcount )
466 /* Pretend we displayed the picture, but don't destroy
467 * it since the decoder might still need it. */
468 p_picture->i_status = DISPLAYED_PICTURE;
472 /* Destroy the picture without displaying it */
473 p_picture->i_status = DESTROYED_PICTURE;
474 p_vout->i_heap_size--;
476 intf_WarnMsg( 1, "vout warning: late picture skipped (%p)",
478 vlc_mutex_unlock( &p_vout->picture_lock );
482 else if( display_date > current_date + VOUT_DISPLAY_DELAY )
484 /* A picture is ready to be rendered, but its rendering date
485 * is far from the current one so the thread will perform an
486 * empty loop as if no picture were found. The picture state
494 * Check for subpictures to display
496 p_subpic = vout_SortSubPictures( p_vout, display_date );
501 p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
504 * Call the plugin-specific rendering method
506 if( p_picture != NULL )
508 /* Render the direct buffer returned by vout_RenderPicture */
509 p_vout->pf_render( p_vout, p_directbuffer );
515 if( display_date != 0 )
517 /* Store render time using Bresenham algorithm */
518 p_vout->render_time += mdate() - current_date;
519 p_vout->render_time >>= 1;
522 /* Give back change lock */
523 vlc_mutex_unlock( &p_vout->change_lock );
525 /* Sleep a while or until a given date */
526 if( display_date != 0 )
528 mwait( display_date - VOUT_MWAIT_TOLERANCE );
532 msleep( VOUT_IDLE_SLEEP );
535 /* On awakening, take back lock and send immediately picture
537 vlc_mutex_lock( &p_vout->change_lock );
540 intf_DbgMsg( "picture %p, subpicture %p", p_picture, p_subpic );
544 * Display the previously rendered picture
546 if( p_picture != NULL )
548 /* Display the direct buffer returned by vout_RenderPicture */
549 p_vout->pf_display( p_vout, p_directbuffer );
551 /* Remove picture from heap */
552 vlc_mutex_lock( &p_vout->picture_lock );
553 if( p_picture->i_refcount )
555 p_picture->i_status = DISPLAYED_PICTURE;
559 p_picture->i_status = DESTROYED_PICTURE;
560 p_vout->i_heap_size--;
562 vlc_mutex_unlock( &p_vout->picture_lock );
566 * Check events and manage thread
568 if( p_vout->pf_manage( p_vout ) )
570 /* A fatal error occured, and the thread must terminate immediately,
571 * without displaying anything - setting b_error to 1 causes the
572 * immediate end of the main while() loop. */
578 * Error loop - wait until the thread destruction is requested
580 if( p_vout->b_error )
582 ErrorThread( p_vout );
588 /* Destroy method-dependant resources */
589 p_vout->pf_destroy( p_vout );
591 /* Destroy thread structures allocated by CreateThread */
592 DestroyThread( p_vout, THREAD_OVER );
593 intf_DbgMsg( "thread end" );
596 /*****************************************************************************
597 * ErrorThread: RunThread() error loop
598 *****************************************************************************
599 * This function is called when an error occured during thread main's loop. The
600 * thread can still receive feed, but must be ready to terminate as soon as
602 *****************************************************************************/
603 static void ErrorThread( vout_thread_t *p_vout )
605 /* Wait until a `die' order */
606 while( !p_vout->b_die )
609 msleep( VOUT_IDLE_SLEEP );
613 /*****************************************************************************
614 * EndThread: thread destruction
615 *****************************************************************************
616 * This function is called when the thread ends after a sucessful
617 * initialization. It frees all ressources allocated by InitThread.
618 *****************************************************************************/
619 static void EndThread( vout_thread_t *p_vout )
621 int i_index; /* index in heap */
624 *p_vout->pi_status = THREAD_END;
628 struct tms cpu_usage;
631 intf_Msg( "vout stats: cpu usage (user: %d, system: %d)",
632 cpu_usage.tms_utime, cpu_usage.tms_stime );
636 if( !p_vout->b_direct )
638 p_vout->chroma.pf_end( p_vout );
639 module_Unneed( p_vout->chroma.p_module );
642 /* Destroy all remaining pictures */
643 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
645 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
647 free( p_vout->p_picture[i_index].p_data );
651 /* Destroy all remaining subpictures */
652 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
654 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
656 free( p_vout->p_subpicture[i_index].p_data );
660 /* Destroy translation tables */
661 p_vout->pf_end( p_vout );
663 /* Release the change lock */
664 vlc_mutex_unlock( &p_vout->change_lock );
667 /*****************************************************************************
668 * DestroyThread: thread destruction
669 *****************************************************************************
670 * This function is called when the thread ends. It frees all ressources
671 * allocated by CreateThread. Status is available at this stage.
672 *****************************************************************************/
673 static void DestroyThread( vout_thread_t *p_vout, int i_status )
675 int *pi_status; /* status adress */
677 /* Store status adress */
678 pi_status = p_vout->pi_status;
680 /* Destroy the locks */
681 vlc_mutex_destroy( &p_vout->picture_lock );
682 vlc_mutex_destroy( &p_vout->subpicture_lock );
683 vlc_mutex_destroy( &p_vout->change_lock );
685 /* Release the module */
686 module_Unneed( p_vout->p_module );
690 *pi_status = i_status;
693 static int ReduceHeight( int i_ratio )
695 int i_dummy = VOUT_ASPECT_FACTOR;
703 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
704 while( !(i_ratio & 1) && !(i_dummy & 1) )
711 while( !(i_ratio % 3) && !(i_dummy % 3) )
718 while( !(i_ratio % 5) && !(i_dummy % 5) )