Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

util.c

00001 /*****************************************************************************
00002  * util.c: Utility functions and exceptions management
00003  *****************************************************************************
00004  * Copyright (C) 2005 the VideoLAN team
00005  * $Id: vlc.c 10786 2005-04-23 23:19:17Z zorglub $
00006  *
00007  * Authors: Olivier Aubert <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 #include <vlc/control.h>
00025 
00026 #include <vlc/intf.h>
00027 #include <vlc/vout.h>
00028 #include <vlc/aout.h>
00029 #include <vlc_demux.h>
00030 
00031 #include <vlc_osd.h>
00032 
00033 #define HAS_SNAPSHOT 1
00034 
00035 #ifdef HAS_SNAPSHOT
00036 #include <snapshot.h>
00037 #endif
00038 
00039 #include <stdlib.h>                                      /* malloc(), free() */
00040 #include <string.h>
00041 
00042 #include <errno.h>                                                 /* ENOMEM */
00043 #include <stdio.h>
00044 #include <ctype.h>
00045 
00046 #ifdef HAVE_UNISTD_H
00047 #    include <unistd.h>
00048 #endif
00049 #ifdef HAVE_SYS_TIME_H
00050 #    include <sys/time.h>
00051 #endif
00052 #ifdef HAVE_SYS_TYPES_H
00053 #    include <sys/types.h>
00054 #endif
00055 
00056 #define RAISE( c, m )  exception->code = c; \
00057                        exception->message = strdup(m);
00058 
00059 vlc_int64_t mediacontrol_unit_convert( input_thread_t *p_input,
00060                                        mediacontrol_PositionKey from,
00061                                        mediacontrol_PositionKey to,
00062                                        vlc_int64_t value )
00063 {
00064     if( to == from )
00065         return value;
00066 
00067     /* For all conversions, we need data from p_input */
00068     if( !p_input )
00069         return 0;
00070 
00071     switch( from )
00072     {
00073     case mediacontrol_MediaTime:
00074         if( to == mediacontrol_ByteCount )
00075         {
00076             /* FIXME */
00077             /* vlc < 0.8 API */
00078             /* return value * 50 * p_input->stream.i_mux_rate / 1000; */
00079             return 0;
00080         }
00081         if( to == mediacontrol_SampleCount )
00082         {
00083             double f_fps;
00084 
00085             if( demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
00086                 return 0;
00087             else
00088                 return( value * f_fps / 1000.0 );
00089         }
00090         /* Cannot happen */
00091         /* See http://catb.org/~esr/jargon/html/entry/can't-happen.html */
00092         break;
00093 
00094     case mediacontrol_SampleCount:
00095     {
00096         double f_fps;
00097 
00098         if( demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
00099             return 0;
00100 
00101         if( to == mediacontrol_ByteCount )
00102         {
00103             /* FIXME */
00104             /* vlc < 0.8 API */
00105 /*             return ( vlc_int64_t )( value * 50 * p_input->stream.i_mux_rate / f_fps ); */
00106             return 0;
00107         }
00108 
00109         if( to == mediacontrol_MediaTime )
00110             return( vlc_int64_t )( value * 1000.0 / ( double )f_fps );
00111 
00112         /* Cannot happen */
00113         break;
00114     }
00115     case mediacontrol_ByteCount:
00116         /* FIXME */
00117         return 0;
00118 /* vlc < 0.8 API: */
00119 
00120 //         if( p_input->stream.i_mux_rate == 0 )
00121 //             return 0;
00122 // 
00123 //         /* Convert an offset into milliseconds. Taken from input_ext-intf.c.
00124 //            The 50 hardcoded constant comes from the definition of i_mux_rate :
00125 //            i_mux_rate : the rate we read the stream (in units of 50 bytes/s) ;
00126 //            0 if undef */
00127 //         if( to == mediacontrol_MediaTime )
00128 //             return ( vlc_int64_t )( 1000 * value / 50 / p_input->stream.i_mux_rate );
00129 // 
00130 //         if( to == mediacontrol_SampleCount )
00131 //         {
00132 //             double f_fps;
00133 //             if( demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
00134 //                 return 0;
00135 //             else
00136 //                 return ( vlc_int64_t )( value * f_fps / 50 / p_input->stream.i_mux_rate );
00137 //         }
00138         /* Cannot happen */
00139         break;
00140     }
00141     /* Cannot happen */
00142     return 0;
00143 }
00144 
00145 /* Converts a mediacontrol_Position into a time in microseconds in
00146    movie clock time */
00147 vlc_int64_t
00148 mediacontrol_position2microsecond( input_thread_t* p_input, const mediacontrol_Position * pos )
00149 {
00150     switch( pos->origin )
00151     {
00152     case mediacontrol_AbsolutePosition:
00153         return ( 1000 * mediacontrol_unit_convert( p_input,
00154                                                    pos->key, /* from */
00155                                                    mediacontrol_MediaTime,  /* to */
00156                                                    pos->value ) );
00157         break;
00158     case mediacontrol_RelativePosition:
00159     {
00160         vlc_int64_t l_pos;
00161         vlc_value_t val;
00162 
00163         val.i_time = 0;
00164         if( p_input )
00165         {
00166             var_Get( p_input, "time", &val );
00167         }
00168 
00169         l_pos = 1000 * mediacontrol_unit_convert( p_input,
00170                                                   pos->key,
00171                                                   mediacontrol_MediaTime,
00172                                                   pos->value );
00173         return val.i_time + l_pos;
00174         break;
00175     }
00176     case mediacontrol_ModuloPosition:
00177     {
00178         vlc_int64_t l_pos;
00179         vlc_value_t val;
00180 
00181         val.i_time = 0;
00182         if( p_input )
00183         {
00184             var_Get( p_input, "length", &val );
00185         }
00186 
00187         if( val.i_time > 0)
00188         {
00189             l_pos = ( 1000 * mediacontrol_unit_convert( p_input,
00190                                                         pos->key,
00191                                                         mediacontrol_MediaTime,
00192                                                         pos->value ) );
00193         }
00194         else
00195             l_pos = 0;
00196 
00197         return l_pos % val.i_time;
00198         break;
00199     }
00200     }
00201     return 0;
00202 }
00203 
00204 mediacontrol_RGBPicture*
00205 mediacontrol_RGBPicture__alloc( int datasize )
00206 {
00207     mediacontrol_RGBPicture* pic;
00208 
00209     pic = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
00210     if( ! pic )
00211         return NULL;
00212 
00213     pic->size = datasize;
00214     pic->data = ( char* )malloc( datasize );
00215     return pic;
00216 }
00217 
00218 void
00219 mediacontrol_RGBPicture__free( mediacontrol_RGBPicture* pic )
00220 {
00221     if( pic )
00222         free( pic->data );
00223     free( pic );
00224 }
00225 
00226 mediacontrol_PlaylistSeq*
00227 mediacontrol_PlaylistSeq__alloc( int size )
00228 {
00229     mediacontrol_PlaylistSeq* ps;
00230 
00231     ps =( mediacontrol_PlaylistSeq* )malloc( sizeof( mediacontrol_PlaylistSeq ) );
00232     if( ! ps )
00233         return NULL;
00234 
00235     ps->size = size;
00236     ps->data = ( char** )malloc( size * sizeof( char* ) );
00237     return ps;
00238 }
00239 
00240 void
00241 mediacontrol_PlaylistSeq__free( mediacontrol_PlaylistSeq* ps )
00242 {
00243     if( ps )
00244     {
00245         int i;
00246         for( i = 0 ; i < ps->size ; i++ )
00247             free( ps->data[i] );
00248     }
00249     free( ps->data );
00250     free( ps );
00251 }
00252 
00253 mediacontrol_Exception*
00254 mediacontrol_exception_init( mediacontrol_Exception *exception )
00255 {
00256     if( exception == NULL )
00257     {
00258         exception = ( mediacontrol_Exception* )malloc( sizeof( mediacontrol_Exception ) );
00259     }
00260 
00261     exception->code = 0;
00262     exception->message = NULL;
00263     return exception;
00264 }
00265 
00266 void
00267 mediacontrol_exception_free( mediacontrol_Exception *exception )
00268 {
00269     if( ! exception )
00270         return;
00271 
00272     free( exception->message );
00273     free( exception );
00274 }
00275 
00276 mediacontrol_RGBPicture*
00277 _mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma, vlc_int64_t l_date,
00278                                 char* p_data, int i_datasize )
00279 {
00280     mediacontrol_RGBPicture *retval;
00281 
00282     retval = mediacontrol_RGBPicture__alloc( i_datasize );
00283     if( retval )
00284     {
00285         retval->width  = i_width;
00286         retval->height = i_height;
00287         retval->type   = i_chroma;
00288         retval->date   = l_date;
00289         retval->size   = i_datasize;
00290         memcpy( retval->data, p_data, i_datasize );
00291     }
00292     return retval;
00293 }

Generated on Tue Dec 20 10:14:31 2005 for vlc-0.8.4a by  doxygen 1.4.2