1 /*****************************************************************************
2 * demux.c: demuxer using ffmpeg (libavformat).
3 *****************************************************************************
4 * Copyright (C) 2004-2007 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc_demux.h>
32 #include <vlc_stream.h>
34 #include <vlc_input.h>
37 #ifdef HAVE_FFMPEG_AVFORMAT_H
38 # include <ffmpeg/avformat.h>
39 #elif defined(HAVE_LIBAVFORMAT_TREE)
40 # include <avformat.h>
45 //#define AVFORMAT_DEBUG 1
47 /* Version checking */
48 #if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_TREE)
50 /*****************************************************************************
51 * demux_sys_t: demux descriptor
52 *****************************************************************************/
72 /*****************************************************************************
74 *****************************************************************************/
75 static int Demux ( demux_t *p_demux );
76 static int Control( demux_t *p_demux, int i_query, va_list args );
78 static int IORead( void *opaque, uint8_t *buf, int buf_size );
79 static offset_t IOSeek( void *opaque, offset_t offset, int whence );
81 /*****************************************************************************
83 *****************************************************************************/
84 int E_(OpenDemux)( vlc_object_t *p_this )
86 demux_t *p_demux = (demux_t*)p_this;
91 vlc_bool_t b_avfmt_nofile;
94 pd.filename = p_demux->psz_path;
95 if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 ) ) <= 0 )
97 msg_Warn( p_demux, "cannot peek" );
101 av_register_all(); /* Can be called several times */
104 if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
106 msg_Dbg( p_demux, "couldn't guess format" );
110 /* Don't try to handle MPEG unless forced */
111 if( !p_demux->b_force &&
112 ( !strcmp( fmt->name, "mpeg" ) ||
113 !strcmp( fmt->name, "vcd" ) ||
114 !strcmp( fmt->name, "vob" ) ||
115 !strcmp( fmt->name, "mpegts" ) ||
116 /* libavformat's redirector won't work */
117 !strcmp( fmt->name, "redir" ) ||
118 !strcmp( fmt->name, "sdp" ) ) )
123 /* Don't trigger false alarms on bin files */
124 if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
128 if( !p_demux->psz_path ) return VLC_EGENERIC;
130 i_len = strlen( p_demux->psz_path );
131 if( i_len < 4 ) return VLC_EGENERIC;
133 if( strcasecmp( &p_demux->psz_path[i_len - 4], ".str" ) &&
134 strcasecmp( &p_demux->psz_path[i_len - 4], ".xai" ) &&
135 strcasecmp( &p_demux->psz_path[i_len - 3], ".xa" ) )
141 msg_Dbg( p_demux, "detected format: %s", fmt->name );
143 /* Fill p_demux fields */
144 p_demux->pf_demux = Demux;
145 p_demux->pf_control = Control;
146 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
151 p_sys->i_pcr_tk = -1;
154 /* Create I/O wrapper */
155 p_sys->io_buffer_size = 32768; /* FIXME */
156 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
157 p_sys->url.priv_data = p_demux;
158 p_sys->url.prot = &p_sys->prot;
159 p_sys->url.prot->name = "VLC I/O wrapper";
160 p_sys->url.prot->url_open = 0;
161 p_sys->url.prot->url_read =
162 (int (*) (URLContext *, unsigned char *, int))IORead;
163 p_sys->url.prot->url_write = 0;
164 p_sys->url.prot->url_seek =
165 (offset_t (*) (URLContext *, offset_t, int))IOSeek;
166 p_sys->url.prot->url_close = 0;
167 p_sys->url.prot->next = 0;
168 init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
169 0, &p_sys->url, IORead, NULL, IOSeek );
171 b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE;
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 if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
180 E_(CloseDemux)( p_this );
184 if( av_find_stream_info( p_sys->ic ) < 0 )
186 msg_Err( p_demux, "av_find_stream_info failed" );
187 if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
188 E_(CloseDemux)( p_this );
191 if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
193 for( i = 0; i < p_sys->ic->nb_streams; i++ )
195 AVCodecContext *cc = p_sys->ic->streams[i]->codec;
200 if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
202 fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
204 /* Special case for raw video data */
205 if( cc->codec_id == CODEC_ID_RAWVIDEO )
207 msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt );
208 fcc = E_(GetVlcChroma)( cc->pix_fmt );
212 switch( cc->codec_type )
214 case CODEC_TYPE_AUDIO:
215 es_format_Init( &fmt, AUDIO_ES, fcc );
216 fmt.audio.i_channels = cc->channels;
217 fmt.audio.i_rate = cc->sample_rate;
218 fmt.audio.i_bitspersample = cc->bits_per_sample;
219 fmt.audio.i_blockalign = cc->block_align;
221 case CODEC_TYPE_VIDEO:
222 es_format_Init( &fmt, VIDEO_ES, fcc );
223 fmt.video.i_width = cc->width;
224 fmt.video.i_height = cc->height;
227 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
228 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
231 case CODEC_TYPE_SUBTITLE:
232 es_format_Init( &fmt, SPU_ES, fcc );
235 msg_Warn( p_demux, "unsupported track type in ffmpeg demux" );
239 fmt.psz_language = strdup( p_sys->ic->streams[i]->language );
240 fmt.i_extra = cc->extradata_size;
241 fmt.p_extra = cc->extradata;
242 es = es_out_Add( p_demux->out, &fmt );
244 msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
245 cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
247 TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
250 msg_Dbg( p_demux, "AVFormat supported stream" );
251 msg_Dbg( p_demux, " - format = %s (%s)",
252 p_sys->fmt->name, p_sys->fmt->long_name );
253 msg_Dbg( p_demux, " - start time = "I64Fd,
254 ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
255 p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
256 msg_Dbg( p_demux, " - duration = "I64Fd,
257 ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
258 p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
260 input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
263 module_t *p_meta = module_Need( p_demux, "meta reader", NULL, 0 );
266 vlc_meta_Merge( input_GetItem(p_input)->p_meta, (vlc_meta_t*)(p_demux->p_private ) );
267 module_Unneed( p_demux, p_meta );
269 vlc_object_release( p_input );
273 vlc_object_release( p_input );
279 /*****************************************************************************
281 *****************************************************************************/
282 void E_(CloseDemux)( vlc_object_t *p_this )
284 demux_t *p_demux = (demux_t*)p_this;
285 demux_sys_t *p_sys = p_demux->p_sys;
286 vlc_bool_t b_avfmt_nofile;
288 FREENULL( p_sys->tk );
290 b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE;
291 p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
292 if( p_sys->ic ) av_close_input_file( p_sys->ic );
293 if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
295 if( p_sys->io_buffer ) free( p_sys->io_buffer );
299 /*****************************************************************************
301 *****************************************************************************/
302 static int Demux( demux_t *p_demux )
304 demux_sys_t *p_sys = p_demux->p_sys;
307 int64_t i_start_time;
310 if( av_read_frame( p_sys->ic, &pkt ) )
314 if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
316 av_free_packet( &pkt );
319 if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
324 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
326 i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
327 p_sys->ic->start_time : 0;
329 p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ?
330 0 : (pkt.dts - i_start_time) * 1000000 *
331 p_sys->ic->streams[pkt.stream_index]->time_base.num /
332 p_sys->ic->streams[pkt.stream_index]->time_base.den;
333 p_frame->i_pts = ( pkt.pts == (int64_t)AV_NOPTS_VALUE ) ?
334 0 : (pkt.pts - i_start_time) * 1000000 *
335 p_sys->ic->streams[pkt.stream_index]->time_base.num /
336 p_sys->ic->streams[pkt.stream_index]->time_base.den;
338 #ifdef AVFORMAT_DEBUG
339 msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
340 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
344 ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
346 p_sys->i_pcr_tk = pkt.stream_index;
347 p_sys->i_pcr = p_frame->i_dts;
349 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
352 es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
353 av_free_packet( &pkt );
357 /*****************************************************************************
359 *****************************************************************************/
360 static int Control( demux_t *p_demux, int i_query, va_list args )
362 demux_sys_t *p_sys = p_demux->p_sys;
368 case DEMUX_GET_POSITION:
369 pf = (double*) va_arg( args, double* ); *pf = 0.0;
370 i64 = stream_Size( p_demux->s );
373 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
376 if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
378 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
383 case DEMUX_SET_POSITION:
384 f = (double) va_arg( args, double );
385 i64 = stream_Tell( p_demux->s );
386 if( p_sys->i_pcr > 0 )
388 i64 = p_sys->ic->duration * f;
389 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
390 i64 += p_sys->ic->start_time;
392 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
394 /* If we have a duration, we prefer to seek by time
395 but if we don't, or if the seek fails, try BYTE seeking */
396 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
397 (av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) )
399 int64_t i_size = stream_Size( p_demux->s );
400 i64 = (int64_t)i_size * f;
402 msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: "I64Fd, i64 );
403 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
406 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
407 p_sys->i_pcr = -1; /* Invalidate time display */
411 case DEMUX_GET_LENGTH:
412 pi64 = (int64_t*)va_arg( args, int64_t * );
413 if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
415 *pi64 = p_sys->ic->duration;
421 pi64 = (int64_t*)va_arg( args, int64_t * );
422 *pi64 = p_sys->i_pcr;
426 i64 = (int64_t)va_arg( args, int64_t );
427 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
428 i64 += p_sys->ic->start_time;
430 msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
432 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
436 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
437 p_sys->i_pcr = -1; /* Invalidate time display */
442 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
444 if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
445 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
446 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
451 if( p_sys->ic->title[0] )
452 vlc_meta_SetTitle( p_meta, p_sys->ic->title );
453 if( p_sys->ic->author[0] )
454 vlc_meta_SetArtist( p_meta, p_sys->ic->author );
455 if( p_sys->ic->copyright[0] )
456 vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright );
457 if( p_sys->ic->comment[0] )
458 vlc_meta_SetDescription( p_meta, p_sys->ic->comment );
459 if( p_sys->ic->genre[0] )
460 vlc_meta_SetGenre( p_meta, p_sys->ic->genre );
469 /*****************************************************************************
470 * I/O wrappers for libavformat
471 *****************************************************************************/
472 static int IORead( void *opaque, uint8_t *buf, int buf_size )
474 URLContext *p_url = opaque;
475 demux_t *p_demux = p_url->priv_data;
476 int i_ret = stream_Read( p_demux->s, buf, buf_size );
477 return i_ret ? i_ret : -1;
480 static offset_t IOSeek( void *opaque, offset_t offset, int whence )
482 URLContext *p_url = opaque;
483 demux_t *p_demux = p_url->priv_data;
484 int64_t i_absolute = (int64_t)offset;
485 int64_t i_size = stream_Size( p_demux->s );
487 #ifdef AVFORMAT_DEBUG
488 msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
496 i_absolute = stream_Tell( p_demux->s ) + offset;
499 i_absolute = i_size + offset;
507 if( i_size && i_absolute > i_size )
510 if( stream_Seek( p_demux->s, i_absolute ) )
515 return stream_Tell( p_demux->s );
518 #else /* HAVE_FFMPEG_AVFORMAT_H */
520 int E_(OpenDemux)( vlc_object_t *p_this )
525 void E_(CloseDemux)( vlc_object_t *p_this )
529 #endif /* HAVE_FFMPEG_AVFORMAT_H */