]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
* ./BUGS: added a list of known bugs. Please add your findings!
[vlc] / src / video_output / video_output.c
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.152 2002/01/04 14:01:35 sam Exp $
9  *
10  * Authors: Vincent Seguin <seguin@via.ecp.fr>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdlib.h>                                                /* free() */
32 #include <stdio.h>                                              /* sprintf() */
33 #include <string.h>                                            /* strerror() */
34
35 #include <videolan/vlc.h>
36
37 #ifdef HAVE_SYS_TIMES_H
38 #   include <sys/times.h>
39 #endif
40
41 #include "video.h"
42 #include "video_output.h"
43
44 /*****************************************************************************
45  * Local prototypes
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 );
53
54 /*****************************************************************************
55  * vout_InitBank: initialize the video output bank.
56  *****************************************************************************/
57 void vout_InitBank ( void )
58 {
59     p_vout_bank->i_count = 0;
60
61     vlc_mutex_init( &p_vout_bank->lock );
62 }
63
64 /*****************************************************************************
65  * vout_EndBank: empty the video output bank.
66  *****************************************************************************
67  * This function ends all unused video outputs and empties the bank in
68  * case of success.
69  *****************************************************************************/
70 void vout_EndBank ( void )
71 {
72     /* Ask all remaining video outputs to die */
73     while( p_vout_bank->i_count )
74     {
75         vout_DestroyThread(
76                 p_vout_bank->pp_vout[ --p_vout_bank->i_count ], NULL );
77     }
78
79     vlc_mutex_destroy( &p_vout_bank->lock );
80 }
81
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 )
93 {
94     vout_thread_t * p_vout;                             /* thread descriptor */
95     int             i_status;                               /* thread status */
96     int             i_index;                                /* loop variable */
97     char          * psz_plugin;
98     probedata_t     data;
99
100     /* Allocate descriptor */
101     p_vout = (vout_thread_t *) malloc( sizeof(vout_thread_t) );
102     if( p_vout == NULL )
103     {
104         intf_ErrMsg( "vout error: vout thread creation returned %s",
105                      strerror(ENOMEM) );
106         return( NULL );
107     }
108
109     /* Choose the best module */
110     psz_plugin = main_GetPszVariable( VOUT_FILTER_VAR, "" );
111     if( psz_plugin[0] == '\0' )
112     {
113         psz_plugin = main_GetPszVariable( VOUT_METHOD_VAR, "" );
114     }
115
116     data.vout.i_chroma = i_chroma;
117     p_vout->p_module
118         = module_Need( MODULE_CAPABILITY_VOUT, psz_plugin, &data );
119
120     if( p_vout->p_module == NULL )
121     {
122         intf_ErrMsg( "vout error: no suitable vout module" );
123         free( p_vout );
124         return( NULL );
125     }
126
127 #define f p_vout->p_module->p_functions->vout.functions.vout
128     p_vout->pf_create     = f.pf_create;
129     p_vout->pf_init       = f.pf_init;
130     p_vout->pf_end        = f.pf_end;
131     p_vout->pf_destroy    = f.pf_destroy;
132     p_vout->pf_manage     = f.pf_manage;
133     p_vout->pf_render     = f.pf_render;
134     p_vout->pf_display    = f.pf_display;
135     p_vout->pf_setpalette = f.pf_setpalette;
136 #undef f
137
138     /* Initialize thread properties - thread id and locks will be initialized
139      * later */
140     p_vout->b_die               = 0;
141     p_vout->b_error             = 0;
142     p_vout->b_active            = 0;
143     p_vout->pi_status           = (pi_status != NULL) ? pi_status : &i_status;
144     *p_vout->pi_status          = THREAD_CREATE;
145
146     /* Initialize pictures and subpictures - translation tables and functions
147      * will be initialized later in InitThread */
148     for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++)
149     {
150         p_vout->p_picture[i_index].i_status = FREE_PICTURE;
151         p_vout->p_picture[i_index].i_type   = EMPTY_PICTURE;
152     }
153
154     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
155     {
156         p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
157         p_vout->p_subpicture[i_index].i_type   = EMPTY_SUBPICTURE;
158     }
159
160     /* Initialize the rendering heap */
161     p_vout->i_heap_size = 0;
162
163     I_RENDERPICTURES = 0;
164     p_vout->render.i_width    = i_width;
165     p_vout->render.i_height   = i_height;
166     p_vout->render.i_chroma   = i_chroma;
167     p_vout->render.i_aspect   = i_aspect;
168
169     /* Initialize misc stuff */
170     p_vout->i_changes    = 0;
171     p_vout->f_gamma      = 0;
172     p_vout->b_grayscale  = 0;
173     p_vout->b_info       = 0;
174     p_vout->b_interface  = 0;
175     p_vout->b_scale      = 1;
176     p_vout->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
177                                                 VOUT_FULLSCREEN_DEFAULT );
178     p_vout->render_time  = 10;
179
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 );
184
185     if( vlc_thread_create( &p_vout->thread_id, "video output",
186                            (void *) RunThread, (void *) p_vout) )
187     {
188         intf_ErrMsg("vout error: %s", strerror(ENOMEM));
189         p_vout->pf_destroy( p_vout );
190         module_Unneed( p_vout->p_module );
191         free( p_vout );
192         return( NULL );
193     }
194
195     /* If status is NULL, wait until the thread is created */
196     if( pi_status == NULL )
197     {
198         do
199         {
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 )
204         {
205             return( NULL );
206         }
207     }
208
209     return( p_vout );
210 }
211
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 )
221 {
222     int     i_status;                                       /* thread status */
223
224     /* Set status */
225     p_vout->pi_status = (pi_status != NULL) ? pi_status : &i_status;
226     *p_vout->pi_status = THREAD_DESTROY;
227
228     /* Request thread destruction */
229     p_vout->b_die = 1;
230
231     /* If status is NULL, wait until thread has been destroyed */
232     if( pi_status == NULL )
233     {
234         do
235         {
236             msleep( THREAD_SLEEP );
237         } while( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
238                  && (i_status != THREAD_FATAL) );
239     }
240 }
241
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 )
250 {
251     int i, i_pgcd;
252     probedata_t data;
253
254     /* Update status */
255     *p_vout->pi_status = THREAD_START;
256
257     vlc_mutex_lock( &p_vout->change_lock );
258
259 #ifdef STATS
260     p_vout->c_loops = 0;
261 #endif
262
263     /* Initialize output method, it issues its own error messages */
264     if( p_vout->pf_init( p_vout ) )
265     {
266         vlc_mutex_unlock( &p_vout->change_lock );
267         return( 1 );
268     }
269
270     if( !I_OUTPUTPICTURES )
271     {
272         intf_ErrMsg( "vout error: plugin was unable to allocate at least "
273                      "one direct buffer" );
274         p_vout->pf_end( p_vout );
275         vlc_mutex_unlock( &p_vout->change_lock );
276         return( 1 );
277     }
278
279     intf_WarnMsg( 1, "vout info: got %i direct buffer(s)", I_OUTPUTPICTURES );
280
281     i_pgcd = ReduceHeight( p_vout->render.i_aspect );
282     intf_WarnMsg( 1, "vout info: picture in %ix%i, chroma 0x%.8x, "
283                      "aspect ratio %i:%i",
284                   p_vout->render.i_width, p_vout->render.i_height,
285                   p_vout->render.i_chroma, p_vout->render.i_aspect / i_pgcd,
286                   VOUT_ASPECT_FACTOR / i_pgcd );
287
288     i_pgcd = ReduceHeight( p_vout->output.i_aspect );
289     intf_WarnMsg( 1, "vout info: picture out %ix%i, chroma 0x%.8x, "
290                      "aspect ratio %i:%i",
291                   p_vout->output.i_width, p_vout->output.i_height,
292                   p_vout->output.i_chroma, p_vout->output.i_aspect / i_pgcd,
293                   VOUT_ASPECT_FACTOR / i_pgcd );
294
295     /* Check whether we managed to create direct buffers similar to
296      * the render buffers, ie same size, chroma and aspect ratio */
297     if( ( p_vout->output.i_width == p_vout->render.i_width )
298      && ( p_vout->output.i_height == p_vout->render.i_height )
299      && ( p_vout->output.i_chroma == p_vout->render.i_chroma )
300      && ( p_vout->output.i_aspect == p_vout->render.i_aspect ) )
301     {
302         /* Cool ! We have direct buffers, we can ask the decoder to
303          * directly decode into them ! Map the first render buffers to
304          * the first direct buffers, but keep the first direct buffer
305          * for memcpy operations */
306         p_vout->b_direct = 1;
307
308         intf_WarnMsg( 2, "vout info: mapping "
309                          "render pictures 0-%i to system pictures 1-%i",
310                          VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
311
312         for( i = 1; i < VOUT_MAX_PICTURES; i++ )
313         {
314             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
315             I_RENDERPICTURES++;
316         }
317
318     }
319     else
320     {
321         /* Rats... Something is wrong here, we could not find an output
322          * plugin able to directly render what we decode. See if we can
323          * find a chroma plugin to do the conversion */
324         p_vout->b_direct = 0;
325
326         /* Choose the best module */
327         data.chroma.p_render = &p_vout->render;
328         data.chroma.p_output = &p_vout->output;
329         p_vout->chroma.p_module
330             = module_Need( MODULE_CAPABILITY_CHROMA, NULL, &data );
331
332         if( p_vout->chroma.p_module == NULL )
333         {
334             intf_ErrMsg( "vout error: no suitable chroma module" );
335             p_vout->pf_end( p_vout );
336             vlc_mutex_unlock( &p_vout->change_lock );
337             return( 1 );
338         }
339
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;
343 #undef f
344
345         if( p_vout->chroma.pf_init( p_vout ) )
346         {
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 );
351             return( 1 );
352         }
353
354         intf_WarnMsg( 2, "vout info: mapping "
355                          "render pictures %i-%i to system pictures %i-%i",
356                          I_OUTPUTPICTURES - 1, VOUT_MAX_PICTURES - 2,
357                          I_OUTPUTPICTURES, VOUT_MAX_PICTURES - 1 );
358
359         /* Append render buffers after the direct buffers */
360         for( i = I_OUTPUTPICTURES; i < VOUT_MAX_PICTURES; i++ )
361         {
362             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
363             I_RENDERPICTURES++;
364         }
365     }
366
367     /* Mark thread as running and return */
368     p_vout->b_active = 1;
369     *p_vout->pi_status = THREAD_READY;
370
371     intf_DbgMsg("thread ready");
372     return( 0 );
373 }
374
375 /*****************************************************************************
376  * RunThread: video output thread
377  *****************************************************************************
378  * Video output thread. This function does only returns when the thread is
379  * terminated. It handles the pictures arriving in the video heap and the
380  * display device events.
381  *****************************************************************************/
382 static void RunThread( vout_thread_t *p_vout)
383 {
384     int             i_index;                                /* index in heap */
385     mtime_t         current_date;                            /* current date */
386     mtime_t         display_date;                            /* display date */
387
388     picture_t *     p_picture;                            /* picture pointer */
389     picture_t *     p_directbuffer;              /* direct buffer to display */
390
391     subpicture_t *  p_subpic;                          /* subpicture pointer */
392
393     /* Create and initialize system-dependant method - this function issues its
394      * own error messages */
395     if( p_vout->pf_create( p_vout ) )
396     {
397         DestroyThread( p_vout, THREAD_ERROR );
398         return;
399     }
400
401     /*
402      * Initialize thread
403      */
404     p_vout->b_error = InitThread( p_vout );
405     if( p_vout->b_error )
406     {
407         /* Destroy thread structures allocated by Create and InitThread */
408         p_vout->pf_destroy( p_vout );
409
410         DestroyThread( p_vout, THREAD_ERROR );
411         return;
412     }
413
414     /*
415      * Main loop - it is not executed if an error occured during
416      * initialization
417      */
418     while( (!p_vout->b_die) && (!p_vout->b_error) )
419     {
420         /* Initialize loop variables */
421         display_date = 0;
422         current_date = mdate();
423
424 #ifdef STATS
425         p_vout->c_loops++;
426         if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
427         {
428             intf_Msg( "vout stats: picture heap: %d/%d",
429                       I_RENDERPICTURES, p_vout->i_heap_size );
430         }
431 #endif
432
433         /*
434          * Find the picture to display - this operation does not need lock,
435          * since only READY_PICTUREs are handled
436          */
437         p_picture = NULL;
438
439         for( i_index = 0;
440              i_index < I_RENDERPICTURES;
441              i_index++ )
442         {
443             if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
444                 && ( (p_picture == NULL) ||
445                      (PP_RENDERPICTURE[i_index]->date < display_date) ) )
446             {
447                 p_picture = PP_RENDERPICTURE[i_index];
448                 display_date = p_picture->date;
449             }
450         }
451
452         if( p_picture != NULL )
453         {
454             /* Compute FPS rate */
455             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
456                 = display_date;
457
458             if( display_date < current_date + p_vout->render_time )
459             {
460                 /* Picture is late: it will be destroyed and the thread
461                  * will directly choose the next picture */
462                 vlc_mutex_lock( &p_vout->picture_lock );
463                 if( p_picture->i_refcount )
464                 {
465                     /* Pretend we displayed the picture, but don't destroy
466                      * it since the decoder might still need it. */
467                     p_picture->i_status = DISPLAYED_PICTURE;
468                 }
469                 else
470                 {
471                     /* Destroy the picture without displaying it */
472                     p_picture->i_status = DESTROYED_PICTURE;
473                     p_vout->i_heap_size--;
474                 }
475                 intf_WarnMsg( 1, "vout warning: late picture skipped (%p)",
476                               p_picture );
477                 vlc_mutex_unlock( &p_vout->picture_lock );
478
479                 continue;
480             }
481             else if( display_date > current_date + VOUT_DISPLAY_DELAY )
482             {
483                 /* A picture is ready to be rendered, but its rendering date
484                  * is far from the current one so the thread will perform an
485                  * empty loop as if no picture were found. The picture state
486                  * is unchanged */
487                 p_picture    = NULL;
488                 display_date = 0;
489             }
490         }
491
492         /*
493          * Check for subpictures to display
494          */
495         p_subpic = vout_SortSubPictures( p_vout, display_date );
496
497         /*
498          * Perform rendering
499          */
500         p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
501
502         /*
503          * Call the plugin-specific rendering method
504          */
505         if( p_picture != NULL )
506         {
507             /* Render the direct buffer returned by vout_RenderPicture */
508             p_vout->pf_render( p_vout, p_directbuffer );
509         }
510
511         /*
512          * Sleep, wake up
513          */
514         if( display_date != 0 )
515         {
516             /* Store render time using Bresenham algorithm */
517             p_vout->render_time += mdate() - current_date;
518             p_vout->render_time >>= 1;
519         }
520
521         /* Give back change lock */
522         vlc_mutex_unlock( &p_vout->change_lock );
523
524         /* Sleep a while or until a given date */
525         if( display_date != 0 )
526         {
527             mwait( display_date - VOUT_MWAIT_TOLERANCE );
528         }
529         else
530         {
531             msleep( VOUT_IDLE_SLEEP );
532         }
533
534         /* On awakening, take back lock and send immediately picture
535          * to display. */
536         vlc_mutex_lock( &p_vout->change_lock );
537
538 #ifdef TRACE_VOUT
539         intf_DbgMsg( "picture %p, subpicture %p", p_picture, p_subpic );
540 #endif
541
542         /*
543          * Display the previously rendered picture
544          */
545         if( p_picture != NULL )
546         {
547             /* Display the direct buffer returned by vout_RenderPicture */
548             p_vout->pf_display( p_vout, p_directbuffer );
549
550             /* Remove picture from heap */
551             vlc_mutex_lock( &p_vout->picture_lock );
552             if( p_picture->i_refcount )
553             {
554                 p_picture->i_status = DISPLAYED_PICTURE;
555             }
556             else
557             {
558                 p_picture->i_status = DESTROYED_PICTURE;
559                 p_vout->i_heap_size--;
560             }
561             vlc_mutex_unlock( &p_vout->picture_lock );
562         }
563
564         /*
565          * Check events and manage thread
566          */
567         if( p_vout->pf_manage( p_vout ) )
568         {
569             /* A fatal error occured, and the thread must terminate immediately,
570              * without displaying anything - setting b_error to 1 causes the
571              * immediate end of the main while() loop. */
572             p_vout->b_error = 1;
573         }
574     }
575
576     /*
577      * Error loop - wait until the thread destruction is requested
578      */
579     if( p_vout->b_error )
580     {
581         ErrorThread( p_vout );
582     }
583
584     /* End of thread */
585     EndThread( p_vout );
586
587     /* Destroy method-dependant resources */
588     p_vout->pf_destroy( p_vout );
589
590     /* Destroy thread structures allocated by CreateThread */
591     DestroyThread( p_vout, THREAD_OVER );
592     intf_DbgMsg( "thread end" );
593 }
594
595 /*****************************************************************************
596  * ErrorThread: RunThread() error loop
597  *****************************************************************************
598  * This function is called when an error occured during thread main's loop. The
599  * thread can still receive feed, but must be ready to terminate as soon as
600  * possible.
601  *****************************************************************************/
602 static void ErrorThread( vout_thread_t *p_vout )
603 {
604     /* Wait until a `die' order */
605     while( !p_vout->b_die )
606     {
607         /* Sleep a while */
608         msleep( VOUT_IDLE_SLEEP );
609     }
610 }
611
612 /*****************************************************************************
613  * EndThread: thread destruction
614  *****************************************************************************
615  * This function is called when the thread ends after a sucessful
616  * initialization. It frees all ressources allocated by InitThread.
617  *****************************************************************************/
618 static void EndThread( vout_thread_t *p_vout )
619 {
620     int     i_index;                                        /* index in heap */
621
622     /* Store status */
623     *p_vout->pi_status = THREAD_END;
624
625 #ifdef STATS
626     {
627         struct tms cpu_usage;
628         times( &cpu_usage );
629
630         intf_Msg( "vout stats: cpu usage (user: %d, system: %d)",
631                   cpu_usage.tms_utime, cpu_usage.tms_stime );
632     }
633 #endif
634
635     if( !p_vout->b_direct )
636     {
637         p_vout->chroma.pf_end( p_vout );
638         module_Unneed( p_vout->chroma.p_module );
639     }
640
641     /* Destroy all remaining pictures */
642     for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
643     {
644         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
645         {
646             free( p_vout->p_picture[i_index].p_data );
647         }
648     }
649
650     /* Destroy all remaining subpictures */
651     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
652     {
653         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
654         {
655             free( p_vout->p_subpicture[i_index].p_data );
656         }
657     }
658
659     /* Destroy translation tables */
660     p_vout->pf_end( p_vout );
661
662     /* Release the change lock */
663     vlc_mutex_unlock( &p_vout->change_lock );
664 }
665
666 /*****************************************************************************
667  * DestroyThread: thread destruction
668  *****************************************************************************
669  * This function is called when the thread ends. It frees all ressources
670  * allocated by CreateThread. Status is available at this stage.
671  *****************************************************************************/
672 static void DestroyThread( vout_thread_t *p_vout, int i_status )
673 {
674     int *pi_status;                                         /* status adress */
675
676     /* Store status adress */
677     pi_status = p_vout->pi_status;
678
679     /* Destroy the locks */
680     vlc_mutex_destroy( &p_vout->picture_lock );
681     vlc_mutex_destroy( &p_vout->subpicture_lock );
682     vlc_mutex_destroy( &p_vout->change_lock );
683
684     /* Release the module */
685     module_Unneed( p_vout->p_module );
686
687     /* Free structure */
688     free( p_vout );
689     *pi_status = i_status;
690 }
691
692 static int ReduceHeight( int i_ratio )
693 {
694     int i_dummy = VOUT_ASPECT_FACTOR;
695     int i_pgcd  = 1;
696  
697     /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
698     while( !(i_ratio & 1) && !(i_dummy & 1) )
699     {
700         i_ratio >>= 1;
701         i_dummy >>= 1;
702         i_pgcd  <<= 1;
703     }
704
705     while( !(i_ratio % 3) && !(i_ratio % 3) )
706     {
707         i_ratio /= 3;
708         i_dummy /= 3;
709         i_pgcd  *= 3;
710     }
711
712     while( !(i_ratio % 5) && !(i_ratio % 5) )
713     {
714         i_ratio /= 5;
715         i_dummy /= 5;
716         i_pgcd  *= 5;
717     }
718
719     return i_pgcd;
720 }
721