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