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.149 2001/12/30 07:09:56 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 int 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 */
98 /* Allocate descriptor */
99 p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
102 intf_ErrMsg( "vout error: vout thread creation returned %s",
107 /* Choose the best module */
109 module_Need( MODULE_CAPABILITY_VOUT,
110 main_GetPszVariable( VOUT_FILTER_VAR,
111 main_GetPszVariable( VOUT_METHOD_VAR, NULL ) ),
114 if( p_vout->p_module == NULL )
116 intf_ErrMsg( "vout error: no suitable vout module" );
121 #define f p_vout->p_module->p_functions->vout.functions.vout
122 p_vout->pf_create = f.pf_create;
123 p_vout->pf_init = f.pf_init;
124 p_vout->pf_end = f.pf_end;
125 p_vout->pf_destroy = f.pf_destroy;
126 p_vout->pf_manage = f.pf_manage;
127 p_vout->pf_display = f.pf_display;
128 p_vout->pf_setpalette = f.pf_setpalette;
131 /* Initialize thread properties - thread id and locks will be initialized
135 p_vout->b_active = 0;
136 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
137 *p_vout->pi_status = THREAD_CREATE;
139 /* Initialize pictures and subpictures - translation tables and functions
140 * will be initialized later in InitThread */
141 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
143 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
144 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
147 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
149 p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
150 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
153 /* Initialize the rendering heap */
154 p_vout->i_heap_size = 0;
156 I_RENDERPICTURES = 0;
157 p_vout->render.i_width = i_width;
158 p_vout->render.i_height = i_height;
159 p_vout->render.i_chroma = i_chroma;
160 p_vout->render.i_aspect = i_aspect;
162 /* Initialize misc stuff */
163 p_vout->i_changes = 0;
165 p_vout->b_grayscale = 0;
167 p_vout->b_interface = 0;
169 p_vout->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
170 VOUT_FULLSCREEN_DEFAULT );
171 p_vout->render_time = 10;
173 /* Create thread and set locks */
174 vlc_mutex_init( &p_vout->picture_lock );
175 vlc_mutex_init( &p_vout->subpicture_lock );
176 vlc_mutex_init( &p_vout->change_lock );
178 if( vlc_thread_create( &p_vout->thread_id, "video output",
179 (void *) RunThread, (void *) p_vout) )
181 intf_ErrMsg("vout error: %s", strerror(ENOMEM));
182 p_vout->pf_destroy( p_vout );
183 module_Unneed( p_vout->p_module );
188 /* If status is NULL, wait until the thread is created */
189 if( pi_status == NULL )
193 msleep( THREAD_SLEEP );
194 }while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
195 && (i_status != THREAD_FATAL) );
196 if( i_status != THREAD_READY )
205 /*****************************************************************************
206 * vout_DestroyThread: destroys a previously created thread
207 *****************************************************************************
208 * Destroy a terminated thread.
209 * The function will request a destruction of the specified thread. If pi_error
210 * is NULL, it will return once the thread is destroyed. Else, it will be
211 * update using one of the THREAD_* constants.
212 *****************************************************************************/
213 void vout_DestroyThread( vout_thread_t *p_vout, int *pi_status )
215 int i_status; /* thread status */
218 p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
219 *p_vout->pi_status = THREAD_DESTROY;
221 /* Request thread destruction */
224 /* If status is NULL, wait until thread has been destroyed */
225 if( pi_status == NULL )
229 msleep( THREAD_SLEEP );
230 } while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
231 && (i_status != THREAD_FATAL) );
235 /*****************************************************************************
236 * InitThread: initialize video output thread
237 *****************************************************************************
238 * This function is called from RunThread and performs the second step of the
239 * initialization. It returns 0 on success. Note that the thread's flag are not
240 * modified inside this function.
241 *****************************************************************************/
242 static int InitThread( vout_thread_t *p_vout )
248 *p_vout->pi_status = THREAD_START;
250 vlc_mutex_lock( &p_vout->change_lock );
256 /* Initialize output method, it issues its own error messages */
257 if( p_vout->pf_init( p_vout ) )
259 vlc_mutex_unlock( &p_vout->change_lock );
263 if( !I_OUTPUTPICTURES )
265 intf_ErrMsg( "vout error: plugin was unable to allocate at least "
266 "one direct buffer" );
267 p_vout->pf_end( p_vout );
268 vlc_mutex_unlock( &p_vout->change_lock );
272 intf_WarnMsg( 1, "vout info: got %i direct buffer(s)", I_OUTPUTPICTURES );
274 i_pgcd = ReduceHeight( p_vout->render.i_aspect );
275 intf_WarnMsg( 1, "vout info: picture in %ix%i, chroma %i, "
276 "aspect ratio %i:%i",
277 p_vout->render.i_width, p_vout->render.i_height,
278 p_vout->render.i_chroma, p_vout->render.i_aspect / i_pgcd,
279 VOUT_ASPECT_FACTOR / i_pgcd );
281 i_pgcd = ReduceHeight( p_vout->output.i_aspect );
282 intf_WarnMsg( 1, "vout info: picture out %ix%i, chroma %i, "
283 "aspect ratio %i:%i",
284 p_vout->output.i_width, p_vout->output.i_height,
285 p_vout->output.i_chroma, p_vout->output.i_aspect / i_pgcd,
286 VOUT_ASPECT_FACTOR / i_pgcd );
288 /* Check whether we managed to create direct buffers similar to
289 * the render buffers, ie same size, chroma and aspect ratio */
290 if( ( p_vout->output.i_width == p_vout->render.i_width )
291 && ( p_vout->output.i_height == p_vout->render.i_height )
292 && ( p_vout->output.i_chroma == p_vout->render.i_chroma )
293 && ( p_vout->output.i_aspect == p_vout->render.i_aspect ) )
295 /* Cool ! We have direct buffers, we can ask the decoder to
296 * directly decode into them ! Map the first render buffers to
297 * the first direct buffers, but keep the first direct buffer
298 * for memcpy operations */
299 p_vout->b_direct = 1;
301 intf_WarnMsg( 2, "vout info: mapping "
302 "render pictures 0-%i to system pictures 1-%i",
303 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
305 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
307 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
314 /* Rats... Something is wrong here, we could not find an output
315 * plugin able to directly render what we decode. See if we can
316 * find a chroma plugin to do the conversion */
317 p_vout->b_direct = 0;
319 /* Choose the best module */
320 data.chroma.p_render = &p_vout->render;
321 data.chroma.p_output = &p_vout->output;
322 p_vout->chroma.p_module
323 = module_Need( MODULE_CAPABILITY_CHROMA, NULL, &data );
325 if( p_vout->chroma.p_module == NULL )
327 intf_ErrMsg( "vout error: no suitable chroma module" );
328 p_vout->pf_end( p_vout );
329 vlc_mutex_unlock( &p_vout->change_lock );
333 #define f p_vout->chroma.p_module->p_functions->chroma.functions.chroma
334 p_vout->chroma.pf_init = f.pf_init;
335 p_vout->chroma.pf_end = f.pf_end;
338 if( p_vout->chroma.pf_init( p_vout ) )
340 intf_ErrMsg( "vout error: could not initialize chroma module" );
341 module_Unneed( p_vout->chroma.p_module );
342 p_vout->pf_end( p_vout );
343 vlc_mutex_unlock( &p_vout->change_lock );
347 intf_WarnMsg( 2, "vout info: mapping "
348 "render pictures %i-%i to system pictures %i-%i",
349 I_OUTPUTPICTURES - 1, VOUT_MAX_PICTURES - 2,
350 I_OUTPUTPICTURES, VOUT_MAX_PICTURES - 1 );
352 /* Append render buffers after the direct buffers */
353 for( i = I_OUTPUTPICTURES; i < VOUT_MAX_PICTURES; i++ )
355 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
360 /* Mark thread as running and return */
361 p_vout->b_active = 1;
362 *p_vout->pi_status = THREAD_READY;
364 intf_DbgMsg("thread ready");
368 /*****************************************************************************
369 * RunThread: video output thread
370 *****************************************************************************
371 * Video output thread. This function does only returns when the thread is
372 * terminated. It handles the pictures arriving in the video heap and the
373 * display device events.
374 *****************************************************************************/
375 static void RunThread( vout_thread_t *p_vout)
377 int i_index; /* index in heap */
378 mtime_t current_date; /* current date */
379 mtime_t display_date; /* display date */
381 picture_t * p_picture; /* picture pointer */
382 picture_t * p_directbuffer; /* direct buffer to display */
384 subpicture_t * p_subpic; /* subpicture pointer */
386 /* Create and initialize system-dependant method - this function issues its
387 * own error messages */
388 if( p_vout->pf_create( p_vout ) )
390 DestroyThread( p_vout, THREAD_ERROR );
397 p_vout->b_error = InitThread( p_vout );
398 if( p_vout->b_error )
400 /* Destroy thread structures allocated by Create and InitThread */
401 p_vout->pf_destroy( p_vout );
403 DestroyThread( p_vout, THREAD_ERROR );
408 * Main loop - it is not executed if an error occured during
411 while( (!p_vout->b_die) && (!p_vout->b_error) )
413 /* Initialize loop variables */
415 current_date = mdate();
419 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
421 intf_Msg( "vout stats: picture heap: %d/%d",
422 I_RENDERPICTURES, p_vout->i_heap_size );
427 * Find the picture to display - this operation does not need lock,
428 * since only READY_PICTUREs are handled
433 i_index < I_RENDERPICTURES;
436 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
437 && ( (p_picture == NULL) ||
438 (PP_RENDERPICTURE[i_index]->date < display_date) ) )
440 p_picture = PP_RENDERPICTURE[i_index];
441 display_date = p_picture->date;
445 if( p_picture != NULL )
447 /* Compute FPS rate */
448 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
451 if( display_date < current_date + p_vout->render_time )
453 /* Picture is late: it will be destroyed and the thread
454 * will directly choose the next picture */
455 vlc_mutex_lock( &p_vout->picture_lock );
456 if( p_picture->i_refcount )
458 /* Pretend we displayed the picture, but don't destroy
459 * it since the decoder might still need it. */
460 p_picture->i_status = DISPLAYED_PICTURE;
464 /* Destroy the picture without displaying it */
465 p_picture->i_status = DESTROYED_PICTURE;
466 p_vout->i_heap_size--;
468 intf_WarnMsg( 1, "vout warning: late picture skipped (%p)",
470 vlc_mutex_unlock( &p_vout->picture_lock );
474 else if( display_date > current_date + VOUT_DISPLAY_DELAY )
476 /* A picture is ready to be rendered, but its rendering date
477 * is far from the current one so the thread will perform an
478 * empty loop as if no picture were found. The picture state
486 * Check for subpictures to display
488 p_subpic = vout_SortSubPictures( p_vout, display_date );
493 p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
498 if( display_date != 0 )
500 /* Store render time using Bresenham algorithm */
501 p_vout->render_time += mdate() - current_date;
502 p_vout->render_time >>= 1;
505 /* Give back change lock */
506 vlc_mutex_unlock( &p_vout->change_lock );
508 /* Sleep a while or until a given date */
509 if( display_date != 0 )
511 mwait( display_date - VOUT_MWAIT_TOLERANCE );
515 msleep( VOUT_IDLE_SLEEP );
518 /* On awakening, take back lock and send immediately picture
520 vlc_mutex_lock( &p_vout->change_lock );
523 intf_DbgMsg( "picture %p, subpicture %p", p_picture, p_subpic );
527 * Display the previously rendered picture
529 if( p_picture != NULL )
531 /* Display the direct buffer returned by vout_RenderPicture */
532 p_vout->pf_display( p_vout, p_directbuffer );
534 /* Remove picture from heap */
535 vlc_mutex_lock( &p_vout->picture_lock );
536 if( p_picture->i_refcount )
538 p_picture->i_status = DISPLAYED_PICTURE;
542 p_picture->i_status = DESTROYED_PICTURE;
543 p_vout->i_heap_size--;
545 vlc_mutex_unlock( &p_vout->picture_lock );
549 * Check events and manage thread
551 if( p_vout->pf_manage( p_vout ) )
553 /* A fatal error occured, and the thread must terminate immediately,
554 * without displaying anything - setting b_error to 1 causes the
555 * immediate end of the main while() loop. */
561 * Error loop - wait until the thread destruction is requested
563 if( p_vout->b_error )
565 ErrorThread( p_vout );
571 /* Destroy method-dependant resources */
572 p_vout->pf_destroy( p_vout );
574 /* Destroy thread structures allocated by CreateThread */
575 DestroyThread( p_vout, THREAD_OVER );
576 intf_DbgMsg( "thread end" );
579 /*****************************************************************************
580 * ErrorThread: RunThread() error loop
581 *****************************************************************************
582 * This function is called when an error occured during thread main's loop. The
583 * thread can still receive feed, but must be ready to terminate as soon as
585 *****************************************************************************/
586 static void ErrorThread( vout_thread_t *p_vout )
588 /* Wait until a `die' order */
589 while( !p_vout->b_die )
592 msleep( VOUT_IDLE_SLEEP );
596 /*****************************************************************************
597 * EndThread: thread destruction
598 *****************************************************************************
599 * This function is called when the thread ends after a sucessful
600 * initialization. It frees all ressources allocated by InitThread.
601 *****************************************************************************/
602 static void EndThread( vout_thread_t *p_vout )
604 int i_index; /* index in heap */
607 *p_vout->pi_status = THREAD_END;
611 struct tms cpu_usage;
614 intf_Msg( "vout stats: cpu usage (user: %d, system: %d)",
615 cpu_usage.tms_utime, cpu_usage.tms_stime );
619 if( !p_vout->b_direct )
621 p_vout->chroma.pf_end( p_vout );
622 module_Unneed( p_vout->chroma.p_module );
625 /* Destroy all remaining pictures */
626 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
628 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
630 free( p_vout->p_picture[i_index].planes[0].p_data );
634 /* Destroy all remaining subpictures */
635 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
637 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
639 free( p_vout->p_subpicture[i_index].p_data );
643 /* Destroy translation tables */
644 p_vout->pf_end( p_vout );
646 /* Release the change lock */
647 vlc_mutex_unlock( &p_vout->change_lock );
650 /*****************************************************************************
651 * DestroyThread: thread destruction
652 *****************************************************************************
653 * This function is called when the thread ends. It frees all ressources
654 * allocated by CreateThread. Status is available at this stage.
655 *****************************************************************************/
656 static void DestroyThread( vout_thread_t *p_vout, int i_status )
658 int *pi_status; /* status adress */
660 /* Store status adress */
661 pi_status = p_vout->pi_status;
663 /* Destroy the locks */
664 vlc_mutex_destroy( &p_vout->picture_lock );
665 vlc_mutex_destroy( &p_vout->subpicture_lock );
666 vlc_mutex_destroy( &p_vout->change_lock );
668 /* Release the module */
669 module_Unneed( p_vout->p_module );
673 *pi_status = i_status;
676 static int ReduceHeight( int i_ratio )
678 int i_dummy = VOUT_ASPECT_FACTOR;
681 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
682 while( !(i_ratio & 1) && !(i_dummy & 1) )
689 while( !(i_ratio % 3) && !(i_ratio % 3) )
696 while( !(i_ratio % 5) && !(i_ratio % 5) )