1 /*****************************************************************************
2 * demux.c: demuxer using ffmpeg (libavformat).
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc/input.h>
35 #ifdef HAVE_FFMPEG_AVCODEC_H
36 # include <ffmpeg/avformat.h>
38 # include <avformat.h>
43 //#define AVFORMAT_DEBUG 1
45 /* Version checking */
46 #if (LIBAVFORMAT_BUILD >= 4629) && defined(HAVE_LIBAVFORMAT)
48 /*****************************************************************************
49 * demux_sys_t: demux descriptor
50 *****************************************************************************/
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Demux ( demux_t *p_demux );
74 static int Control( demux_t *p_demux, int i_query, va_list args );
76 static int IORead( void *opaque, uint8_t *buf, int buf_size );
77 static int IOSeek( void *opaque, offset_t offset, int whence );
79 /*****************************************************************************
81 *****************************************************************************/
82 int E_(OpenDemux)( vlc_object_t *p_this )
84 demux_t *p_demux = (demux_t*)p_this;
91 pd.filename = p_demux->psz_path;
92 if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 ) ) <= 0 )
94 msg_Warn( p_demux, "cannot peek" );
98 av_register_all(); /* Can be called several times */
101 if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
103 msg_Dbg( p_demux, "couldn't guess format" );
107 /* Don't try to handle MPEG unless forced */
108 if( !p_demux->b_force &&
109 ( !strcmp( fmt->name, "mpeg" ) ||
110 !strcmp( fmt->name, "vcd" ) ||
111 !strcmp( fmt->name, "vob" ) ||
112 !strcmp( fmt->name, "mpegts" ) ||
113 /* libavformat's redirector won't work */
114 !strcmp( fmt->name, "redir" ) ||
115 !strcmp( fmt->name, "sdp" ) ) )
120 /* Don't trigger false alarms on bin files */
121 if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
125 if( !p_demux->psz_path ) return VLC_EGENERIC;
127 i_len = strlen( p_demux->psz_path );
128 if( i_len < 4 ) return VLC_EGENERIC;
130 if( strcasecmp( &p_demux->psz_path[i_len - 4], ".str" ) &&
131 strcasecmp( &p_demux->psz_path[i_len - 4], ".xai" ) &&
132 strcasecmp( &p_demux->psz_path[i_len - 3], ".xa" ) )
138 msg_Dbg( p_demux, "detected format: %s", fmt->name );
140 /* Fill p_demux fields */
141 p_demux->pf_demux = Demux;
142 p_demux->pf_control = Control;
143 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
148 p_sys->i_pcr_tk = -1;
151 /* Create I/O wrapper */
152 p_sys->io_buffer_size = 32768; /* FIXME */
153 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
154 p_sys->url.priv_data = p_demux;
155 p_sys->url.prot = &p_sys->prot;
156 p_sys->url.prot->name = "VLC I/O wrapper";
157 p_sys->url.prot->url_open = 0;
158 p_sys->url.prot->url_read =
159 (int (*) (URLContext *, unsigned char *, int))IORead;
160 p_sys->url.prot->url_write = 0;
161 p_sys->url.prot->url_seek =
162 (offset_t (*) (URLContext *, offset_t, int))IOSeek;
163 p_sys->url.prot->url_close = 0;
164 p_sys->url.prot->next = 0;
165 init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
166 0, &p_sys->url, IORead, NULL, IOSeek );
168 p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
171 if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path,
174 msg_Err( p_demux, "av_open_input_stream failed" );
175 E_(CloseDemux)( p_this );
179 if( av_find_stream_info( p_sys->ic ) < 0 )
181 msg_Err( p_demux, "av_find_stream_info failed" );
182 E_(CloseDemux)( p_this );
186 for( i = 0; i < p_sys->ic->nb_streams; i++ )
188 AVCodecContext *cc = p_sys->ic->streams[i]->codec;
193 if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
194 fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
196 switch( cc->codec_type )
198 case CODEC_TYPE_AUDIO:
199 es_format_Init( &fmt, AUDIO_ES, fcc );
200 fmt.audio.i_channels = cc->channels;
201 fmt.audio.i_rate = cc->sample_rate;
202 fmt.audio.i_bitspersample = cc->bits_per_sample;
203 fmt.audio.i_blockalign = cc->block_align;
205 case CODEC_TYPE_VIDEO:
206 es_format_Init( &fmt, VIDEO_ES, fcc );
207 fmt.video.i_width = cc->width;
208 fmt.video.i_height = cc->height;
211 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
212 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
219 fmt.i_extra = cc->extradata_size;
220 fmt.p_extra = cc->extradata;
221 es = es_out_Add( p_demux->out, &fmt );
223 msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
224 cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
226 TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
229 msg_Dbg( p_demux, "AVFormat supported stream" );
230 msg_Dbg( p_demux, " - format = %s (%s)",
231 p_sys->fmt->name, p_sys->fmt->long_name );
232 msg_Dbg( p_demux, " - start time = "I64Fd,
233 ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
234 p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
235 msg_Dbg( p_demux, " - duration = "I64Fd,
236 ( p_sys->ic->duration != AV_NOPTS_VALUE ) ?
237 p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
242 /*****************************************************************************
244 *****************************************************************************/
245 void E_(CloseDemux)( vlc_object_t *p_this )
247 demux_t *p_demux = (demux_t*)p_this;
248 demux_sys_t *p_sys = p_demux->p_sys;
250 if( p_sys->ic ) av_close_input_file( p_sys->ic );
251 if( p_sys->io_buffer ) free( p_sys->io_buffer );
255 /*****************************************************************************
257 *****************************************************************************/
258 static int Demux( demux_t *p_demux )
260 demux_sys_t *p_sys = p_demux->p_sys;
263 int64_t i_start_time;
266 if( av_read_frame( p_sys->ic, &pkt ) )
270 if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
272 av_free_packet( &pkt );
275 if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
280 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
282 i_start_time = ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
283 p_sys->ic->start_time : 0;
285 p_frame->i_dts = ( pkt.dts == AV_NOPTS_VALUE ) ?
286 0 : (pkt.dts - i_start_time) * 1000000 *
287 p_sys->ic->streams[pkt.stream_index]->time_base.num /
288 p_sys->ic->streams[pkt.stream_index]->time_base.den;
289 p_frame->i_pts = ( pkt.pts == AV_NOPTS_VALUE ) ?
290 0 : (pkt.pts - i_start_time) * 1000000 *
291 p_sys->ic->streams[pkt.stream_index]->time_base.num /
292 p_sys->ic->streams[pkt.stream_index]->time_base.den;
294 #ifdef AVFORMAT_DEBUG
295 msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
296 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
300 ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
302 p_sys->i_pcr_tk = pkt.stream_index;
303 p_sys->i_pcr = p_frame->i_dts;
305 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
308 es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
309 av_free_packet( &pkt );
313 /*****************************************************************************
315 *****************************************************************************/
316 static int Control( demux_t *p_demux, int i_query, va_list args )
318 demux_sys_t *p_sys = p_demux->p_sys;
324 case DEMUX_GET_POSITION:
325 pf = (double*) va_arg( args, double* ); *pf = 0.0;
326 i64 = stream_Size( p_demux->s );
329 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
332 if( p_sys->ic->duration != AV_NOPTS_VALUE && p_sys->i_pcr > 0 )
334 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
339 case DEMUX_SET_POSITION:
340 f = (double) va_arg( args, double );
341 i64 = stream_Tell( p_demux->s );
342 if( i64 && p_sys->i_pcr > 0 )
344 int64_t i_size = stream_Size( p_demux->s );
346 i64 = p_sys->i_pcr * i_size / i64 * f;
347 if( p_sys->ic->start_time != AV_NOPTS_VALUE )
348 i64 += p_sys->ic->start_time;
350 if( p_sys->ic->duration != AV_NOPTS_VALUE )
351 i64 = p_sys->ic->duration * f;
353 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
355 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
359 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
360 p_sys->i_pcr = -1; /* Invalidate time display */
364 case DEMUX_GET_LENGTH:
365 pi64 = (int64_t*)va_arg( args, int64_t * );
366 if( p_sys->ic->duration != AV_NOPTS_VALUE )
368 *pi64 = p_sys->ic->duration;
374 pi64 = (int64_t*)va_arg( args, int64_t * );
375 *pi64 = p_sys->i_pcr;
379 i64 = (int64_t)va_arg( args, int64_t );
380 if( p_sys->ic->start_time != AV_NOPTS_VALUE )
381 i64 += p_sys->ic->start_time;
383 msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
385 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
389 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
390 p_sys->i_pcr = -1; /* Invalidate time display */
395 vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
398 if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
399 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
400 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
405 *pp_meta = meta = vlc_meta_New();
407 if( p_sys->ic->title[0] )
408 vlc_meta_Add( meta, VLC_META_TITLE, p_sys->ic->title );
409 if( p_sys->ic->author[0] )
410 vlc_meta_Add( meta, VLC_META_AUTHOR, p_sys->ic->author );
411 if( p_sys->ic->copyright[0] )
412 vlc_meta_Add( meta, VLC_META_COPYRIGHT, p_sys->ic->copyright );
413 if( p_sys->ic->comment[0] )
414 vlc_meta_Add( meta, VLC_META_DESCRIPTION, p_sys->ic->comment );
415 if( p_sys->ic->genre[0] )
416 vlc_meta_Add( meta, VLC_META_GENRE, p_sys->ic->genre );
425 /*****************************************************************************
426 * I/O wrappers for libavformat
427 *****************************************************************************/
428 static int IORead( void *opaque, uint8_t *buf, int buf_size )
430 URLContext *p_url = opaque;
431 demux_t *p_demux = p_url->priv_data;
432 int i_ret = stream_Read( p_demux->s, buf, buf_size );
433 return i_ret ? i_ret : -1;
436 static int IOSeek( void *opaque, offset_t offset, int whence )
438 URLContext *p_url = opaque;
439 demux_t *p_demux = p_url->priv_data;
442 #ifdef AVFORMAT_DEBUG
443 msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
452 i_absolute = stream_Tell( p_demux->s ) + offset;
455 i_absolute = stream_Size( p_demux->s ) - offset;
462 if( stream_Seek( p_demux->s, i_absolute ) )
470 #else /* LIBAVFORMAT_BUILD >= 4611 */
472 int E_(OpenDemux)( vlc_object_t *p_this )
477 void E_(CloseDemux)( vlc_object_t *p_this )
481 #endif /* LIBAVFORMAT_BUILD >= 4629 */