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 >= 4611) && defined(HAVE_LIBAVFORMAT)
48 #if LIBAVFORMAT_BUILD < 4619
49 # define av_seek_frame(a,b,c,d) av_seek_frame(a,b,c)
52 /*****************************************************************************
53 * demux_sys_t: demux descriptor
54 *****************************************************************************/
74 /*****************************************************************************
76 *****************************************************************************/
77 static int Demux ( demux_t *p_demux );
78 static int Control( demux_t *p_demux, int i_query, va_list args );
80 static int IORead( void *opaque, uint8_t *buf, int buf_size );
81 static int IOSeek( void *opaque, offset_t offset, int whence );
83 /*****************************************************************************
85 *****************************************************************************/
86 int E_(OpenDemux)( vlc_object_t *p_this )
88 demux_t *p_demux = (demux_t*)p_this;
95 pd.filename = p_demux->psz_path;
96 if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 ) ) <= 0 )
98 msg_Warn( p_demux, "cannot peek" );
102 av_register_all(); /* Can be called several times */
105 if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
107 msg_Dbg( p_demux, "couldn't guess format" );
111 /* Don't try to handle MPEG unless forced */
112 if( !p_demux->b_force &&
113 ( !strcmp( fmt->name, "mpeg" ) ||
114 !strcmp( fmt->name, "vcd" ) ||
115 !strcmp( fmt->name, "vob" ) ||
116 !strcmp( fmt->name, "mpegts" ) ||
117 /* libavformat's redirector won't work */
118 !strcmp( fmt->name, "redir" ) ||
119 !strcmp( fmt->name, "sdp" ) ) )
124 /* Don't trigger false alarms on bin files */
125 if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
129 if( !p_demux->psz_path ) return VLC_EGENERIC;
131 i_len = strlen( p_demux->psz_path );
132 if( i_len < 4 ) return VLC_EGENERIC;
134 if( strcasecmp( &p_demux->psz_path[i_len - 4], ".str" ) &&
135 strcasecmp( &p_demux->psz_path[i_len - 4], ".xai" ) &&
136 strcasecmp( &p_demux->psz_path[i_len - 3], ".xa" ) )
142 msg_Dbg( p_demux, "detected format: %s", fmt->name );
144 /* Fill p_demux fields */
145 p_demux->pf_demux = Demux;
146 p_demux->pf_control = Control;
147 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
152 p_sys->i_pcr_tk = -1;
155 /* Create I/O wrapper */
156 p_sys->io_buffer_size = 32768; /* FIXME */
157 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
158 p_sys->url.priv_data = p_demux;
159 p_sys->url.prot = &p_sys->prot;
160 p_sys->url.prot->name = "VLC I/O wrapper";
161 p_sys->url.prot->url_open = 0;
162 p_sys->url.prot->url_read =
163 (int (*) (URLContext *, unsigned char *, int))IORead;
164 p_sys->url.prot->url_write = 0;
165 p_sys->url.prot->url_seek =
166 (offset_t (*) (URLContext *, offset_t, int))IOSeek;
167 p_sys->url.prot->url_close = 0;
168 p_sys->url.prot->next = 0;
169 init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
170 0, &p_sys->url, IORead, NULL, IOSeek );
172 p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
175 if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path,
178 msg_Err( p_demux, "av_open_input_stream failed" );
179 E_(CloseDemux)( p_this );
183 if( av_find_stream_info( p_sys->ic ) )
185 msg_Err( p_demux, "av_find_stream_info failed" );
186 E_(CloseDemux)( p_this );
190 for( i = 0; i < p_sys->ic->nb_streams; i++ )
192 AVCodecContext *cc = &p_sys->ic->streams[i]->codec;
197 if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
198 fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
200 switch( cc->codec_type )
202 case CODEC_TYPE_AUDIO:
203 es_format_Init( &fmt, AUDIO_ES, fcc );
204 fmt.audio.i_channels = cc->channels;
205 fmt.audio.i_rate = cc->sample_rate;
206 fmt.audio.i_bitspersample = cc->bits_per_sample;
207 fmt.audio.i_blockalign = cc->block_align;
209 case CODEC_TYPE_VIDEO:
210 es_format_Init( &fmt, VIDEO_ES, fcc );
211 fmt.video.i_width = cc->width;
212 fmt.video.i_height = cc->height;
215 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
216 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
220 E_(CloseDemux)( p_this );
224 fmt.i_extra = cc->extradata_size;
225 fmt.p_extra = cc->extradata;
226 es = es_out_Add( p_demux->out, &fmt );
228 msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
229 cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
231 TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
234 msg_Dbg( p_demux, "AVFormat supported stream" );
235 msg_Dbg( p_demux, " - format = %s (%s)",
236 p_sys->fmt->name, p_sys->fmt->long_name );
237 msg_Dbg( p_demux, " - start time = "I64Fd,
238 ( p_sys->ic->start_time != (signed int) AV_NOPTS_VALUE ) ?
239 p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
240 msg_Dbg( p_demux, " - duration = "I64Fd,
241 ( p_sys->ic->duration != (signed int) AV_NOPTS_VALUE ) ?
242 p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
247 /*****************************************************************************
249 *****************************************************************************/
250 void E_(CloseDemux)( vlc_object_t *p_this )
252 demux_t *p_demux = (demux_t*)p_this;
253 demux_sys_t *p_sys = p_demux->p_sys;
255 if( p_sys->ic ) av_close_input_file( p_sys->ic );
256 if( p_sys->io_buffer ) free( p_sys->io_buffer );
260 /*****************************************************************************
262 *****************************************************************************/
263 static int Demux( demux_t *p_demux )
265 demux_sys_t *p_sys = p_demux->p_sys;
268 int64_t i_start_time;
271 if( av_read_frame( p_sys->ic, &pkt ) )
275 if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
277 av_free_packet( &pkt );
280 if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
285 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
287 i_start_time = ( p_sys->ic->start_time != (signed int) AV_NOPTS_VALUE ) ?
288 p_sys->ic->start_time : 0;
290 p_frame->i_dts = ( pkt.dts == (signed int) AV_NOPTS_VALUE ) ?
291 0 : (pkt.dts - i_start_time) * 1000000 / AV_TIME_BASE;
292 p_frame->i_pts = ( pkt.pts == (signed int) AV_NOPTS_VALUE ) ?
293 0 : (pkt.pts - i_start_time) * 1000000 / AV_TIME_BASE;
295 #ifdef AVFORMAT_DEBUG
296 msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
297 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
301 ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
303 p_sys->i_pcr_tk = pkt.stream_index;
304 p_sys->i_pcr = pkt.dts - i_start_time;
306 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
309 es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
310 av_free_packet( &pkt );
314 /*****************************************************************************
316 *****************************************************************************/
317 static int Control( demux_t *p_demux, int i_query, va_list args )
319 demux_sys_t *p_sys = p_demux->p_sys;
325 case DEMUX_GET_POSITION:
326 pf = (double*) va_arg( args, double* ); *pf = 0.0;
327 i64 = stream_Size( p_demux->s );
330 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
333 if( p_sys->ic->duration != (signed int) AV_NOPTS_VALUE && p_sys->i_pcr > 0 )
335 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
340 case DEMUX_SET_POSITION:
341 f = (double) va_arg( args, double );
342 i64 = stream_Tell( p_demux->s );
343 if( i64 && p_sys->i_pcr > 0 )
345 int64_t i_size = stream_Size( p_demux->s );
347 i64 = p_sys->i_pcr * i_size / i64 * f;
348 if( p_sys->ic->start_time != (signed int) AV_NOPTS_VALUE )
349 i64 += p_sys->ic->start_time;
351 if( p_sys->ic->duration != (signed int) AV_NOPTS_VALUE )
352 i64 = p_sys->ic->duration * f;
354 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
356 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
360 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
361 p_sys->i_pcr = -1; /* Invalidate time display */
365 case DEMUX_GET_LENGTH:
366 pi64 = (int64_t*)va_arg( args, int64_t * );
367 if( p_sys->ic->duration != (signed int) AV_NOPTS_VALUE )
369 *pi64 = p_sys->ic->duration;
375 pi64 = (int64_t*)va_arg( args, int64_t * );
376 *pi64 = p_sys->i_pcr;
380 i64 = (int64_t)va_arg( args, int64_t );
381 if( p_sys->ic->start_time != (signed int) AV_NOPTS_VALUE )
382 i64 += p_sys->ic->start_time;
384 msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
386 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
390 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
391 p_sys->i_pcr = -1; /* Invalidate time display */
396 vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
399 if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
400 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
401 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
406 *pp_meta = meta = vlc_meta_New();
408 if( p_sys->ic->title[0] )
409 vlc_meta_Add( meta, VLC_META_TITLE, p_sys->ic->title );
410 if( p_sys->ic->author[0] )
411 vlc_meta_Add( meta, VLC_META_AUTHOR, p_sys->ic->author );
412 if( p_sys->ic->copyright[0] )
413 vlc_meta_Add( meta, VLC_META_COPYRIGHT, p_sys->ic->copyright );
414 if( p_sys->ic->comment[0] )
415 vlc_meta_Add( meta, VLC_META_DESCRIPTION, p_sys->ic->comment );
416 if( p_sys->ic->genre[0] )
417 vlc_meta_Add( meta, VLC_META_GENRE, p_sys->ic->genre );
426 /*****************************************************************************
427 * I/O wrappers for libavformat
428 *****************************************************************************/
429 static int IORead( void *opaque, uint8_t *buf, int buf_size )
431 URLContext *p_url = opaque;
432 demux_t *p_demux = p_url->priv_data;
433 int i_ret = stream_Read( p_demux->s, buf, buf_size );
434 return i_ret ? i_ret : -1;
437 static int IOSeek( void *opaque, offset_t offset, int whence )
439 URLContext *p_url = opaque;
440 demux_t *p_demux = p_url->priv_data;
443 #ifdef AVFORMAT_DEBUG
444 msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
453 i_absolute = stream_Tell( p_demux->s ) + offset;
456 i_absolute = stream_Size( p_demux->s ) - offset;
463 if( stream_Seek( p_demux->s, i_absolute ) )
471 #else /* LIBAVFORMAT_BUILD >= 4611 */
473 int E_(OpenDemux)( vlc_object_t *p_this )
478 void E_(CloseDemux)( vlc_object_t *p_this )
482 #endif /* LIBAVFORMAT_BUILD >= 4611 */