Annotation of multiplexer/global.c, revision 1.9

1.1       oskar       1: /*
                      2:  * ISO 13818 stream multiplexer
                      3:  * Copyright (C) 2001 Convergence Integrated Media GmbH Berlin
1.9     ! oskar       4:  * Copyright (C) 2004 Oskar Schirmer (schirmer@scara.com)
1.7       oskar       5:  *
                      6:  * This program is free software; you can redistribute it and/or modify
                      7:  * it under the terms of the GNU General Public License as published by
                      8:  * the Free Software Foundation; either version 2 of the License, or
                      9:  * (at your option) any later version.
                     10:  *
                     11:  * This program is distributed in the hope that it will be useful,
                     12:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
                     13:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     14:  * GNU General Public License for more details.
                     15:  *
                     16:  * You should have received a copy of the GNU General Public License
                     17:  * along with this program; if not, write to the Free Software
                     18:  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1.1       oskar      19:  */
                     20: 
                     21: /*
                     22:  * Module:  Global
                     23:  * Purpose: Service functions.
                     24:  */
                     25: 
                     26: #include "global.h"
                     27: #include "error.h"
                     28: 
                     29: boolean timed_io;
                     30: boolean accept_weird_scr;
1.8       oskar      31: boolean conservative_pid_assignment;
1.3       oskar      32: t_msec global_delta;
1.1       oskar      33: 
                     34: #ifdef DEBUG_TIMEPOLL
                     35: timepoll logtp [max_timepoll];
                     36: long logtpc;
                     37: timepoll *ltp;
                     38: #endif
                     39: 
1.4       oskar      40: /* Provide the present system time in relative milliseconds.
                     41:  * The zero point may be moved as unconditional waiting is proposed
                     42:  * in the dispatcher, but timed_io=FALSE.
                     43:  * Return: milliseconds
                     44:  */
1.3       oskar      45: t_msec msec_now (void)
1.1       oskar      46: {
1.3       oskar      47: #define MSEC_EXPONENT 21
1.1       oskar      48:   static long last;
                     49:   static int local_delta;
                     50:   struct timeval tv;
                     51:   register int now;
                     52:   gettimeofday (&tv,NULL);
                     53: #ifdef DEBUG_TIMEPOLL
                     54:   ltp->tv.tv_sec = tv.tv_sec;
                     55:   ltp->tv.tv_usec = tv.tv_usec;
                     56: #endif
1.3       oskar      57:   if ((tv.tv_sec & (~((1L << MSEC_EXPONENT) - 1))) != last) {
                     58:     last = tv.tv_sec & (~((1L << MSEC_EXPONENT) - 1));
                     59:     local_delta += 1000 * (1L << MSEC_EXPONENT);
1.1       oskar      60:   }
1.3       oskar      61:   now = (tv.tv_sec & ((1L << MSEC_EXPONENT) - 1)) * 1000
                     62:       + tv.tv_usec / 1000 + local_delta;
1.1       oskar      63:   warn (LDEB,"msec_now",EGLO,3,0,now);
                     64: #ifdef DEBUG_TIMEPOLL
                     65:   ltp->cnt_msecnow += 1;
                     66:   ltp->msec_now = now + global_delta;
                     67: #endif
                     68:   return (now + global_delta);
1.2       oskar      69: }
                     70: 
1.5       oskar      71: /* Convert a clock reference value (90kHz) to milliseconds,
1.4       oskar      72:  * using a conversion base to avoid wrap around errors.
                     73:  */
1.2       oskar      74: void cref2msec (conversion_base *b,
                     75:     clockref c,
1.3       oskar      76:     t_msec *m)
1.2       oskar      77: {
                     78: #define CREF2MSEC_LIMIT (90 * 1024 * 16) /* 16 sec */
                     79:   unsigned long d;
                     80:   d = c.base - b->base;
                     81:   if (d >= (2 * CREF2MSEC_LIMIT)) {
                     82:     if (d >= (3 * CREF2MSEC_LIMIT)) {
1.3       oskar      83:       warn (LDEB,"cref2msec",EGLO,4,1,d);
1.2       oskar      84:       b->base = c.base - CREF2MSEC_LIMIT;
                     85:       b->msec = b->base / 90;
                     86:     } else {
1.3       oskar      87:       warn (LDEB,"cref2msec",EGLO,4,2,d);
1.2       oskar      88:       b->base += CREF2MSEC_LIMIT;
                     89:       b->msec += CREF2MSEC_LIMIT / 90;
                     90:     }
                     91:     d = c.base - b->base;
                     92:   }
                     93:   *m = (d / 90) + b->msec;
                     94: }
                     95:  
1.4       oskar      96: /* Convert milliseconds to a clock reference value (90kHz),
                     97:  * using a conversion base to avoid wrap around errors.
                     98:  */
1.2       oskar      99: void msec2cref (conversion_base *b,
1.3       oskar     100:     t_msec m,
1.2       oskar     101:     clockref *c)
                    102: {
                    103: #define MSEC2CREF_LIMIT (1024 * 10) /* 10 sec */
                    104:   unsigned int d;
                    105:   d = m - b->msec;
                    106:   if (d >= (2 * MSEC2CREF_LIMIT)) {
                    107:     if (d >= (3 * MSEC2CREF_LIMIT)) {
1.3       oskar     108:       warn (LDEB,"msec2cref",EGLO,5,1,d);
1.2       oskar     109:       b->msec = m - MSEC2CREF_LIMIT;
                    110:       b->base = b->msec * 45;
                    111:     } else {
1.3       oskar     112:       warn (LDEB,"msec2cref",EGLO,5,2,d);
1.2       oskar     113:       b->msec += MSEC2CREF_LIMIT;
                    114:       b->base += MSEC2CREF_LIMIT * 45;
                    115:     }
                    116:     d = m - b->msec;
                    117:   }
                    118:   d = (d * 45) + b->base;
                    119:   c->base = d * 2;
                    120:   c->ba33 = (d >> 31) & 0x01;
                    121:   c->ext = 0;
                    122:   c->valid = TRUE;
1.1       oskar     123: }
                    124: 
                    125: void global_init (void)
                    126: {
                    127: #ifdef DEBUG_TIMEPOLL
                    128:   {
                    129:     struct timeval tv;
                    130:     memset (&logtp[0],0,sizeof(logtp));
                    131:     gettimeofday (&tv,NULL);
                    132:     logtp[0].usec = tv.tv_usec;
                    133:     logtpc = 0;
                    134:     ltp = &logtp[0];
                    135:   }
                    136: #endif
                    137:   verbose_level = LWAR;
                    138:   global_delta = 0;
                    139:   global_delta = - msec_now ();
                    140:   timed_io = FALSE;
                    141:   accept_weird_scr = FALSE;
1.8       oskar     142:   conservative_pid_assignment = FALSE;
1.1       oskar     143: }
                    144: 

LinuxTV legacy CVS <linuxtv.org/cvs>