1 /*****************************************************************************
2 * util.c: Utility functions and exceptions management
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
5 * $Id: vlc.c 10786 2005-04-23 23:19:17Z zorglub $
7 * Authors: Olivier Aubert <olivier.aubert@liris.univ-lyon1.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 #include <vlc/control.h>
29 #include <vlc_demux.h>
33 #define HAS_SNAPSHOT 1
39 #include <stdlib.h> /* malloc(), free() */
42 #include <errno.h> /* ENOMEM */
50 #ifdef HAVE_SYS_TIME_H
51 # include <sys/time.h>
53 #include <sys/types.h>
55 #define RAISE( c, m ) exception->code = c; \
56 exception->message = strdup(m);
58 long long mediacontrol_unit_convert( input_thread_t *p_input,
59 mediacontrol_PositionKey from,
60 mediacontrol_PositionKey to,
66 /* For all conversions, we need data from p_input */
72 case mediacontrol_MediaTime:
73 if( to == mediacontrol_ByteCount )
77 /* return value * 50 * p_input->stream.i_mux_rate / 1000; */
80 if( to == mediacontrol_SampleCount )
84 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
87 return( value * f_fps / 1000.0 );
90 /* See http://catb.org/~esr/jargon/html/entry/can't-happen.html */
93 case mediacontrol_SampleCount:
97 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
100 if( to == mediacontrol_ByteCount )
104 /* return ( long long )( value * 50 * p_input->stream.i_mux_rate / f_fps ); */
108 if( to == mediacontrol_MediaTime )
109 return( long long )( value * 1000.0 / ( double )f_fps );
114 case mediacontrol_ByteCount:
119 // if( p_input->stream.i_mux_rate == 0 )
122 // /* Convert an offset into milliseconds. Taken from input_ext-intf.c.
123 // The 50 hardcoded constant comes from the definition of i_mux_rate :
124 // i_mux_rate : the rate we read the stream (in units of 50 bytes/s) ;
126 // if( to == mediacontrol_MediaTime )
127 // return ( long long )( 1000 * value / 50 / p_input->stream.i_mux_rate );
129 // if( to == mediacontrol_SampleCount )
132 // if( demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
135 // return ( long long )( value * f_fps / 50 / p_input->stream.i_mux_rate );
144 /* Converts a mediacontrol_Position into a time in microseconds in
147 mediacontrol_position2microsecond( input_thread_t* p_input, const mediacontrol_Position * pos )
149 switch( pos->origin )
151 case mediacontrol_AbsolutePosition:
152 return ( 1000 * mediacontrol_unit_convert( p_input,
154 mediacontrol_MediaTime, /* to */
157 case mediacontrol_RelativePosition:
165 var_Get( p_input, "time", &val );
168 l_pos = 1000 * mediacontrol_unit_convert( p_input,
170 mediacontrol_MediaTime,
172 return val.i_time + l_pos;
175 case mediacontrol_ModuloPosition:
183 var_Get( p_input, "length", &val );
188 l_pos = ( 1000 * mediacontrol_unit_convert( p_input,
190 mediacontrol_MediaTime,
196 return l_pos % val.i_time;
203 mediacontrol_RGBPicture*
204 mediacontrol_RGBPicture__alloc( int datasize )
206 mediacontrol_RGBPicture* pic;
208 pic = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
212 pic->size = datasize;
213 pic->data = ( char* )malloc( datasize );
218 mediacontrol_RGBPicture__free( mediacontrol_RGBPicture* pic )
225 mediacontrol_PlaylistSeq*
226 mediacontrol_PlaylistSeq__alloc( int size )
228 mediacontrol_PlaylistSeq* ps;
230 ps =( mediacontrol_PlaylistSeq* )malloc( sizeof( mediacontrol_PlaylistSeq ) );
235 ps->data = ( char** )malloc( size * sizeof( char* ) );
240 mediacontrol_PlaylistSeq__free( mediacontrol_PlaylistSeq* ps )
245 for( i = 0 ; i < ps->size ; i++ )
252 mediacontrol_Exception*
253 mediacontrol_exception_init( mediacontrol_Exception *exception )
255 if( exception == NULL )
257 exception = ( mediacontrol_Exception* )malloc( sizeof( mediacontrol_Exception ) );
261 exception->message = NULL;
266 mediacontrol_exception_free( mediacontrol_Exception *exception )
271 free( exception->message );
275 mediacontrol_RGBPicture*
276 _mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma, long long l_date,
277 char* p_data, int i_datasize )
279 mediacontrol_RGBPicture *retval;
281 retval = mediacontrol_RGBPicture__alloc( i_datasize );
284 retval->width = i_width;
285 retval->height = i_height;
286 retval->type = i_chroma;
287 retval->date = l_date;
288 retval->size = i_datasize;
289 memcpy( retval->data, p_data, i_datasize );