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.201 2002/11/28 14:34:39 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 <stdlib.h> /* free() */
34 #ifdef HAVE_SYS_TIMES_H
35 # include <sys/times.h>
39 #include "video_output.h"
41 #if defined( SYS_DARWIN )
42 #include "darwin_specific.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static int InitThread ( vout_thread_t * );
49 static void RunThread ( vout_thread_t * );
50 static void ErrorThread ( vout_thread_t * );
51 static void EndThread ( vout_thread_t * );
52 static void DestroyThread ( vout_thread_t * );
54 static int ReduceHeight ( int );
55 static int BinaryLog ( uint32_t );
56 static void MaskToShift ( int *, int *, uint32_t );
57 static void InitWindowSize ( vout_thread_t *, int *, int * );
59 /*****************************************************************************
60 * vout_CreateThread: creates a new video output thread
61 *****************************************************************************
62 * This function creates a new video output thread, and returns a pointer
63 * to its description. On error, it returns NULL.
64 *****************************************************************************/
65 vout_thread_t * __vout_CreateThread ( vlc_object_t *p_parent,
67 unsigned int i_height,
68 vlc_fourcc_t i_chroma,
69 unsigned int i_aspect )
71 vout_thread_t * p_vout; /* thread descriptor */
72 int i_index; /* loop variable */
75 /* Allocate descriptor */
76 p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
79 msg_Err( p_parent, "out of memory" );
83 /* If the parent is not a VOUT object, that means we are at the start of
84 * the video output pipe */
85 if( p_parent->i_object_type != VLC_OBJECT_VOUT )
87 char *psz_aspect = config_GetPsz( p_parent, "aspect-ratio" );
89 /* Check whether the user tried to override aspect ratio */
92 unsigned int i_new_aspect = i_aspect;
93 char *psz_parser = strchr( psz_aspect, ':' );
98 i_new_aspect = atoi( psz_aspect ) * VOUT_ASPECT_FACTOR
103 i_new_aspect = i_width * VOUT_ASPECT_FACTOR
110 if( i_new_aspect && i_new_aspect != i_aspect )
112 int i_pgcd = ReduceHeight( i_new_aspect );
114 msg_Dbg( p_vout, "overriding source aspect ratio to %i:%i",
115 i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
117 i_aspect = i_new_aspect;
121 /* Look for the default filter configuration */
122 p_vout->psz_filter_chain = config_GetPsz( p_parent, "filter" );
126 /* continue the parent's filter chain */
129 psz_end = strchr( ((vout_thread_t *)p_parent)->psz_filter_chain, ':' );
130 if( psz_end && *(psz_end+1) )
131 p_vout->psz_filter_chain = strdup( psz_end+1 );
132 else p_vout->psz_filter_chain = NULL;
135 /* Choose the video output module */
136 if( !p_vout->psz_filter_chain )
138 psz_plugin = config_GetPsz( p_parent, "vout" );
142 /* the filter chain is a string list of filters separated by double
146 psz_end = strchr( p_vout->psz_filter_chain, ':' );
148 psz_plugin = strndup( p_vout->psz_filter_chain,
149 psz_end - p_vout->psz_filter_chain );
150 else psz_plugin = strdup( p_vout->psz_filter_chain );
153 /* Initialize pictures and subpictures - translation tables and functions
154 * will be initialized later in InitThread */
155 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++)
157 p_vout->p_picture[i_index].i_status = FREE_PICTURE;
158 p_vout->p_picture[i_index].i_type = EMPTY_PICTURE;
161 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
163 p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
164 p_vout->p_subpicture[i_index].i_type = EMPTY_SUBPICTURE;
167 /* No images in the heap */
168 p_vout->i_heap_size = 0;
170 /* Initialize the rendering heap */
171 I_RENDERPICTURES = 0;
172 p_vout->render.i_width = i_width;
173 p_vout->render.i_height = i_height;
174 p_vout->render.i_chroma = i_chroma;
175 p_vout->render.i_aspect = i_aspect;
177 p_vout->render.i_rmask = 0;
178 p_vout->render.i_gmask = 0;
179 p_vout->render.i_bmask = 0;
181 p_vout->render.i_last_used_pic = -1;
182 p_vout->render.b_allow_modify_pics = 1;
184 /* Zero the output heap */
185 I_OUTPUTPICTURES = 0;
186 p_vout->output.i_width = 0;
187 p_vout->output.i_height = 0;
188 p_vout->output.i_chroma = 0;
189 p_vout->output.i_aspect = 0;
191 p_vout->output.i_rmask = 0;
192 p_vout->output.i_gmask = 0;
193 p_vout->output.i_bmask = 0;
195 /* Initialize misc stuff */
196 p_vout->i_changes = 0;
198 p_vout->b_grayscale = 0;
200 p_vout->b_interface = 0;
202 p_vout->b_fullscreen = 0;
203 p_vout->render_time = 10;
204 p_vout->c_fps_samples= 0;
206 /* Mouse coordinates */
207 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
208 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
209 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
210 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
212 /* user requested fullscreen? */
213 if( config_GetInt( p_vout, "fullscreen" ) )
215 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
218 /* Initialize the dimensions of the video window */
219 InitWindowSize( p_vout, &p_vout->i_window_width,
220 &p_vout->i_window_height );
223 p_vout->p_module = module_Need( p_vout,
224 ( p_vout->psz_filter_chain ) ?
225 "video filter" : "video output",
228 if( psz_plugin ) free( psz_plugin );
229 if( p_vout->p_module == NULL )
231 msg_Err( p_vout, "no suitable vout module" );
232 vlc_object_destroy( p_vout );
236 /* Create thread and set locks */
237 vlc_mutex_init( p_vout, &p_vout->picture_lock );
238 vlc_mutex_init( p_vout, &p_vout->subpicture_lock );
239 vlc_mutex_init( p_vout, &p_vout->change_lock );
241 vlc_object_attach( p_vout, p_parent );
243 if( vlc_thread_create( p_vout, "video output", RunThread,
244 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
246 msg_Err( p_vout, "out of memory" );
247 module_Unneed( p_vout, p_vout->p_module );
248 vlc_object_destroy( p_vout );
255 /*****************************************************************************
256 * vout_DestroyThread: destroys a previously created thread
257 *****************************************************************************
258 * Destroy a terminated thread.
259 * The function will request a destruction of the specified thread. If pi_error
260 * is NULL, it will return once the thread is destroyed. Else, it will be
261 * update using one of the THREAD_* constants.
262 *****************************************************************************/
263 void vout_DestroyThread( vout_thread_t *p_vout )
265 /* Request thread destruction */
267 vlc_thread_join( p_vout );
270 vlc_object_destroy( p_vout );
273 /*****************************************************************************
274 * InitThread: initialize video output thread
275 *****************************************************************************
276 * This function is called from RunThread and performs the second step of the
277 * initialization. It returns 0 on success. Note that the thread's flag are not
278 * modified inside this function.
279 *****************************************************************************/
280 static int InitThread( vout_thread_t *p_vout )
284 vlc_mutex_lock( &p_vout->change_lock );
290 /* Initialize output method, it allocates direct buffers for us */
291 if( p_vout->pf_init( p_vout ) )
293 vlc_mutex_unlock( &p_vout->change_lock );
297 if( !I_OUTPUTPICTURES )
299 msg_Err( p_vout, "plugin was unable to allocate at least "
300 "one direct buffer" );
301 p_vout->pf_end( p_vout );
302 vlc_mutex_unlock( &p_vout->change_lock );
306 if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
308 msg_Err( p_vout, "plugin allocated too many direct buffers, "
309 "our internal buffers must have overflown." );
310 p_vout->pf_end( p_vout );
311 vlc_mutex_unlock( &p_vout->change_lock );
315 msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
317 if( !p_vout->psz_filter_chain )
319 char *psz_aspect = config_GetPsz( p_vout, "pixel-ratio" );
323 int i_new_aspect = p_vout->output.i_width * VOUT_ASPECT_FACTOR
325 / p_vout->output.i_height;
328 if( i_new_aspect && i_new_aspect != p_vout->output.i_aspect )
330 int i_pgcd = ReduceHeight( i_new_aspect );
331 msg_Dbg( p_vout, "output ratio forced to %i:%i\n",
332 i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
333 p_vout->output.i_aspect = i_new_aspect;
338 i_pgcd = ReduceHeight( p_vout->render.i_aspect );
340 "picture in %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
341 p_vout->render.i_width, p_vout->render.i_height,
342 p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
343 p_vout->render.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
345 i_pgcd = ReduceHeight( p_vout->output.i_aspect );
347 "picture out %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
348 p_vout->output.i_width, p_vout->output.i_height,
349 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
350 p_vout->output.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
352 /* Calculate shifts from system-updated masks */
353 MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
354 p_vout->output.i_rmask );
355 MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
356 p_vout->output.i_gmask );
357 MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
358 p_vout->output.i_bmask );
360 /* Check whether we managed to create direct buffers similar to
361 * the render buffers, ie same size and chroma */
362 if( ( p_vout->output.i_width == p_vout->render.i_width )
363 && ( p_vout->output.i_height == p_vout->render.i_height )
364 && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
366 /* Cool ! We have direct buffers, we can ask the decoder to
367 * directly decode into them ! Map the first render buffers to
368 * the first direct buffers, but keep the first direct buffer
369 * for memcpy operations */
370 p_vout->b_direct = 1;
372 for( i = 1; i < VOUT_MAX_PICTURES; i++ )
374 if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
375 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
376 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
378 /* We have enough direct buffers so there's no need to
379 * try to use system memory buffers. */
382 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
386 msg_Dbg( p_vout, "direct render, mapping "
387 "render pictures 0-%i to system pictures 1-%i",
388 VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
392 /* Rats... Something is wrong here, we could not find an output
393 * plugin able to directly render what we decode. See if we can
394 * find a chroma plugin to do the conversion */
395 p_vout->b_direct = 0;
397 /* Choose the best module */
398 p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL );
400 if( p_vout->chroma.p_module == NULL )
402 msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
403 &p_vout->render.i_chroma, &p_vout->output.i_chroma );
404 p_vout->pf_end( p_vout );
405 vlc_mutex_unlock( &p_vout->change_lock );
409 msg_Dbg( p_vout, "indirect render, mapping "
410 "render pictures 0-%i to system pictures %i-%i",
411 VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
412 I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
414 /* Append render buffers after the direct buffers */
415 for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
417 PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
420 /* Check if we have enough render pictures */
421 if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
426 /* Link pictures back to their heap */
427 for( i = 0 ; i < I_RENDERPICTURES ; i++ )
429 PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
432 for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
434 PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
437 /* XXX XXX mark thread ready */
441 /*****************************************************************************
442 * RunThread: video output thread
443 *****************************************************************************
444 * Video output thread. This function does only returns when the thread is
445 * terminated. It handles the pictures arriving in the video heap and the
446 * display device events.
447 *****************************************************************************/
448 static void RunThread( vout_thread_t *p_vout)
450 int i_index; /* index in heap */
451 int i_idle_loops = 0; /* loops without displaying a picture */
452 mtime_t current_date; /* current date */
453 mtime_t display_date; /* display date */
455 picture_t * p_picture; /* picture pointer */
456 picture_t * p_last_picture = NULL; /* last picture */
457 picture_t * p_directbuffer; /* direct buffer to display */
459 subpicture_t * p_subpic; /* subpicture pointer */
464 p_vout->b_error = InitThread( p_vout );
465 if( p_vout->b_error )
467 /* Destroy thread structures allocated by Create and InitThread */
468 DestroyThread( p_vout );
473 * Main loop - it is not executed if an error occured during
476 while( (!p_vout->b_die) && (!p_vout->b_error) )
478 /* Initialize loop variables */
481 current_date = mdate();
485 if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
487 msg_Dbg( p_vout, "picture heap: %d/%d",
488 I_RENDERPICTURES, p_vout->i_heap_size );
493 * Find the picture to display (the one with the earliest date).
494 * This operation does not need lock, since only READY_PICTUREs
496 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
498 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
499 && ( (p_picture == NULL) ||
500 (PP_RENDERPICTURE[i_index]->date < display_date) ) )
502 p_picture = PP_RENDERPICTURE[i_index];
503 display_date = p_picture->date;
509 /* If we met the last picture, parse again to see whether there is
510 * a more appropriate one. */
511 if( p_picture == p_last_picture )
513 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
515 if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
516 && (PP_RENDERPICTURE[i_index] != p_last_picture)
517 && ((p_picture == p_last_picture) ||
518 (PP_RENDERPICTURE[i_index]->date < display_date)) )
520 p_picture = PP_RENDERPICTURE[i_index];
521 display_date = p_picture->date;
526 /* If we found better than the last picture, destroy it */
527 if( p_last_picture && p_picture != p_last_picture )
529 vlc_mutex_lock( &p_vout->picture_lock );
530 if( p_last_picture->i_refcount )
532 p_last_picture->i_status = DISPLAYED_PICTURE;
536 p_last_picture->i_status = DESTROYED_PICTURE;
537 p_vout->i_heap_size--;
539 vlc_mutex_unlock( &p_vout->picture_lock );
540 p_last_picture = NULL;
543 /* Compute FPS rate */
544 p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
547 if( !p_picture->b_force &&
548 p_picture != p_last_picture &&
549 display_date < current_date + p_vout->render_time )
551 /* Picture is late: it will be destroyed and the thread
552 * will directly choose the next picture */
553 vlc_mutex_lock( &p_vout->picture_lock );
554 if( p_picture->i_refcount )
556 /* Pretend we displayed the picture, but don't destroy
557 * it since the decoder might still need it. */
558 p_picture->i_status = DISPLAYED_PICTURE;
562 /* Destroy the picture without displaying it */
563 p_picture->i_status = DESTROYED_PICTURE;
564 p_vout->i_heap_size--;
566 msg_Warn( p_vout, "late picture skipped ("I64Fd")",
567 current_date - display_date );
568 vlc_mutex_unlock( &p_vout->picture_lock );
573 /* Removed because it causes problems for some people --Meuuh */
574 if( display_date > current_date + VOUT_BOGUS_DELAY )
576 /* Picture is waaay too early: it will be destroyed */
577 vlc_mutex_lock( &p_vout->picture_lock );
578 if( p_picture->i_refcount )
580 /* Pretend we displayed the picture, but don't destroy
581 * it since the decoder might still need it. */
582 p_picture->i_status = DISPLAYED_PICTURE;
586 /* Destroy the picture without displaying it */
587 p_picture->i_status = DESTROYED_PICTURE;
588 p_vout->i_heap_size--;
590 intf_WarnMsg( 1, "vout warning: early picture skipped "
591 "("I64Fd")", display_date - current_date );
592 vlc_mutex_unlock( &p_vout->picture_lock );
597 if( display_date > current_date + VOUT_DISPLAY_DELAY )
599 /* A picture is ready to be rendered, but its rendering date
600 * is far from the current one so the thread will perform an
601 * empty loop as if no picture were found. The picture state
606 else if( p_picture == p_last_picture )
608 /* We are asked to repeat the previous picture, but we first
609 * wait for a couple of idle loops */
610 if( i_idle_loops < 4 )
617 /* We set the display date to something high, otherwise
618 * we'll have lots of problems with late pictures */
619 display_date = current_date + p_vout->render_time;
624 if( p_picture == NULL )
630 * Check for subpictures to display
632 p_subpic = vout_SortSubPictures( p_vout, display_date );
637 p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
640 * Call the plugin-specific rendering method if there is one
642 if( p_picture != NULL && p_vout->pf_render )
644 /* Render the direct buffer returned by vout_RenderPicture */
645 p_vout->pf_render( p_vout, p_directbuffer );
651 if( display_date != 0 )
653 /* Store render time using a sliding mean */
654 p_vout->render_time += mdate() - current_date;
655 p_vout->render_time >>= 1;
658 /* Give back change lock */
659 vlc_mutex_unlock( &p_vout->change_lock );
661 /* Sleep a while or until a given date */
662 if( display_date != 0 )
664 mwait( display_date - VOUT_MWAIT_TOLERANCE );
668 msleep( VOUT_IDLE_SLEEP );
671 /* On awakening, take back lock and send immediately picture
673 vlc_mutex_lock( &p_vout->change_lock );
676 * Display the previously rendered picture
678 if( p_picture != NULL )
680 /* Display the direct buffer returned by vout_RenderPicture */
681 if( p_vout->pf_display )
683 p_vout->pf_display( p_vout, p_directbuffer );
686 /* Reinitialize idle loop count */
689 /* Tell the vout this was the last picture and that it does not
690 * need to be forced anymore. */
691 p_last_picture = p_picture;
692 p_last_picture->b_force = 0;
696 * Check events and manage thread
698 if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
700 /* A fatal error occured, and the thread must terminate
701 * immediately, without displaying anything - setting b_error to 1
702 * causes the immediate end of the main while() loop. */
706 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
708 /* this must only happen when the vout plugin is incapable of
709 * rescaling the picture itself. In this case we need to destroy
710 * the current picture buffers and recreate new ones with the right
714 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
716 p_vout->pf_end( p_vout );
717 for( i = 0; i < I_OUTPUTPICTURES; i++ )
718 p_vout->p_picture[ i ].i_status = FREE_PICTURE;
720 I_OUTPUTPICTURES = 0;
721 if( p_vout->pf_init( p_vout ) )
723 msg_Err( p_vout, "cannot resize display" );
724 /* FIXME: pf_end will be called again in EndThread() */
728 /* Need to reinitialise the chroma plugin */
729 p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
730 p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
735 * Error loop - wait until the thread destruction is requested
737 if( p_vout->b_error )
739 ErrorThread( p_vout );
745 /* Destroy thread structures allocated by CreateThread */
746 DestroyThread( p_vout );
749 /*****************************************************************************
750 * ErrorThread: RunThread() error loop
751 *****************************************************************************
752 * This function is called when an error occured during thread main's loop. The
753 * thread can still receive feed, but must be ready to terminate as soon as
755 *****************************************************************************/
756 static void ErrorThread( vout_thread_t *p_vout )
758 /* Wait until a `die' order */
759 while( !p_vout->b_die )
762 msleep( VOUT_IDLE_SLEEP );
766 /*****************************************************************************
767 * EndThread: thread destruction
768 *****************************************************************************
769 * This function is called when the thread ends after a sucessful
770 * initialization. It frees all ressources allocated by InitThread.
771 *****************************************************************************/
772 static void EndThread( vout_thread_t *p_vout )
774 int i_index; /* index in heap */
778 struct tms cpu_usage;
781 msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
782 cpu_usage.tms_utime, cpu_usage.tms_stime );
786 if( !p_vout->b_direct )
788 module_Unneed( p_vout, p_vout->chroma.p_module );
791 /* Destroy all remaining pictures */
792 for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++ )
794 if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
796 free( p_vout->p_picture[i_index].p_data_orig );
800 /* Destroy all remaining subpictures */
801 for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
803 if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
805 free( p_vout->p_subpicture[i_index].p_sys );
809 /* Destroy translation tables */
810 p_vout->pf_end( p_vout );
812 /* Release the change lock */
813 vlc_mutex_unlock( &p_vout->change_lock );
816 /*****************************************************************************
817 * DestroyThread: thread destruction
818 *****************************************************************************
819 * This function is called when the thread ends. It frees all ressources
820 * allocated by CreateThread. Status is available at this stage.
821 *****************************************************************************/
822 static void DestroyThread( vout_thread_t *p_vout )
824 /* Destroy the locks */
825 vlc_mutex_destroy( &p_vout->picture_lock );
826 vlc_mutex_destroy( &p_vout->subpicture_lock );
827 vlc_mutex_destroy( &p_vout->change_lock );
829 /* Release the module */
830 module_Unneed( p_vout, p_vout->p_module );
833 /* following functions are local */
835 static int ReduceHeight( int i_ratio )
837 int i_dummy = VOUT_ASPECT_FACTOR;
845 /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
846 while( !(i_ratio & 1) && !(i_dummy & 1) )
853 while( !(i_ratio % 3) && !(i_dummy % 3) )
860 while( !(i_ratio % 5) && !(i_dummy % 5) )
870 /*****************************************************************************
871 * BinaryLog: computes the base 2 log of a binary value
872 *****************************************************************************
873 * This functions is used by MaskToShift, to get a bit index from a binary
875 *****************************************************************************/
876 static int BinaryLog( uint32_t i )
880 if( i == 0 ) return -31337;
882 if( i & 0xffff0000 ) i_log += 16;
883 if( i & 0xff00ff00 ) i_log += 8;
884 if( i & 0xf0f0f0f0 ) i_log += 4;
885 if( i & 0xcccccccc ) i_log += 2;
886 if( i & 0xaaaaaaaa ) i_log += 1;
891 /*****************************************************************************
892 * MaskToShift: transform a color mask into right and left shifts
893 *****************************************************************************
894 * This function is used for obtaining color shifts from masks.
895 *****************************************************************************/
896 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
898 uint32_t i_low, i_high; /* lower hand higher bits of the mask */
902 *pi_left = *pi_right = 0;
907 i_low = i_mask & (- (int32_t)i_mask); /* lower bit of the mask */
908 i_high = i_mask + i_low; /* higher bit of the mask */
910 /* Transform bits into an index */
911 i_low = BinaryLog (i_low);
912 i_high = BinaryLog (i_high);
914 /* Update pointers and return */
916 *pi_right = (8 - i_high + i_low);
919 /*****************************************************************************
920 * InitWindowSize: find the initial dimensions the video window should have.
921 *****************************************************************************
922 * This function will check the "width", "height" and "zoom" config options and
923 * will calculate the size that the video window should have.
924 *****************************************************************************/
925 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
928 int i_width, i_height;
931 #define FP_FACTOR 1000 /* our fixed point factor */
933 i_width = config_GetInt( p_vout, "width" );
934 i_height = config_GetInt( p_vout, "height" );
935 ll_zoom = (uint64_t)( FP_FACTOR * config_GetFloat( p_vout, "zoom" ) );
937 if( (i_width >= 0) && (i_height >= 0))
939 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
940 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
943 else if( i_width >= 0 )
945 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
946 *pi_height = (int)( i_width * ll_zoom * VOUT_ASPECT_FACTOR /
947 p_vout->render.i_aspect / FP_FACTOR );
950 else if( i_height >= 0 )
952 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
953 *pi_width = (int)( i_height * ll_zoom * p_vout->render.i_aspect /
954 VOUT_ASPECT_FACTOR / FP_FACTOR );
958 if( p_vout->render.i_height * p_vout->render.i_aspect
959 >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
961 *pi_width = (int)( p_vout->render.i_height * ll_zoom
962 * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR / FP_FACTOR );
963 *pi_height = (int)( p_vout->render.i_height * ll_zoom / FP_FACTOR );
967 *pi_width = (int)( p_vout->render.i_width * ll_zoom / FP_FACTOR );
968 *pi_height = (int)( p_vout->render.i_width * ll_zoom
969 * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect / FP_FACTOR );