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>
36 #ifdef HAVE_FFMPEG_AVFORMAT_H
37 # include <ffmpeg/avformat.h>
38 #elif defined(HAVE_LIBAVFORMAT_TREE)
39 # include <avformat.h>
44 //#define AVFORMAT_DEBUG 1
46 /* Version checking */
47 #if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_TREE)
49 /*****************************************************************************
50 * demux_sys_t: demux descriptor
51 *****************************************************************************/
71 /*****************************************************************************
73 *****************************************************************************/
74 static int Demux ( demux_t *p_demux );
75 static int Control( demux_t *p_demux, int i_query, va_list args );
77 static int IORead( void *opaque, uint8_t *buf, int buf_size );
78 static offset_t IOSeek( void *opaque, offset_t offset, int whence );
80 /*****************************************************************************
82 *****************************************************************************/
83 int E_(OpenDemux)( vlc_object_t *p_this )
85 demux_t *p_demux = (demux_t*)p_this;
90 vlc_bool_t b_avfmt_nofile;
93 pd.filename = p_demux->psz_path;
94 if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 ) ) <= 0 )
96 msg_Warn( p_demux, "cannot peek" );
100 av_register_all(); /* Can be called several times */
103 if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
105 msg_Dbg( p_demux, "couldn't guess format" );
109 /* Don't try to handle MPEG unless forced */
110 if( !p_demux->b_force &&
111 ( !strcmp( fmt->name, "mpeg" ) ||
112 !strcmp( fmt->name, "vcd" ) ||
113 !strcmp( fmt->name, "vob" ) ||
114 !strcmp( fmt->name, "mpegts" ) ||
115 /* libavformat's redirector won't work */
116 !strcmp( fmt->name, "redir" ) ||
117 !strcmp( fmt->name, "sdp" ) ) )
122 /* Don't trigger false alarms on bin files */
123 if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
127 if( !p_demux->psz_path ) return VLC_EGENERIC;
129 i_len = strlen( p_demux->psz_path );
130 if( i_len < 4 ) return VLC_EGENERIC;
132 if( strcasecmp( &p_demux->psz_path[i_len - 4], ".str" ) &&
133 strcasecmp( &p_demux->psz_path[i_len - 4], ".xai" ) &&
134 strcasecmp( &p_demux->psz_path[i_len - 3], ".xa" ) )
140 msg_Dbg( p_demux, "detected format: %s", fmt->name );
142 /* Fill p_demux fields */
143 p_demux->pf_demux = Demux;
144 p_demux->pf_control = Control;
145 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
150 p_sys->i_pcr_tk = -1;
153 /* Create I/O wrapper */
154 p_sys->io_buffer_size = 32768; /* FIXME */
155 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
156 p_sys->url.priv_data = p_demux;
157 p_sys->url.prot = &p_sys->prot;
158 p_sys->url.prot->name = "VLC I/O wrapper";
159 p_sys->url.prot->url_open = 0;
160 p_sys->url.prot->url_read =
161 (int (*) (URLContext *, unsigned char *, int))IORead;
162 p_sys->url.prot->url_write = 0;
163 p_sys->url.prot->url_seek =
164 (offset_t (*) (URLContext *, offset_t, int))IOSeek;
165 p_sys->url.prot->url_close = 0;
166 p_sys->url.prot->next = 0;
167 init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
168 0, &p_sys->url, IORead, NULL, IOSeek );
170 b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE;
171 p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
174 if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path,
177 msg_Err( p_demux, "av_open_input_stream failed" );
178 if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
179 E_(CloseDemux)( p_this );
183 if( av_find_stream_info( p_sys->ic ) < 0 )
185 msg_Err( p_demux, "av_find_stream_info failed" );
186 if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
187 E_(CloseDemux)( p_this );
190 if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
192 for( i = 0; i < p_sys->ic->nb_streams; i++ )
194 AVCodecContext *cc = p_sys->ic->streams[i]->codec;
199 if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
201 fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
203 /* Special case for raw video data */
204 if( cc->codec_id == CODEC_ID_RAWVIDEO )
206 msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt );
207 fcc = E_(GetVlcChroma)( cc->pix_fmt );
211 switch( cc->codec_type )
213 case CODEC_TYPE_AUDIO:
214 es_format_Init( &fmt, AUDIO_ES, fcc );
215 fmt.audio.i_channels = cc->channels;
216 fmt.audio.i_rate = cc->sample_rate;
217 fmt.audio.i_bitspersample = cc->bits_per_sample;
218 fmt.audio.i_blockalign = cc->block_align;
220 case CODEC_TYPE_VIDEO:
221 es_format_Init( &fmt, VIDEO_ES, fcc );
222 fmt.video.i_width = cc->width;
223 fmt.video.i_height = cc->height;
226 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
227 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
230 case CODEC_TYPE_SUBTITLE:
231 es_format_Init( &fmt, SPU_ES, fcc );
234 msg_Warn( p_demux, "unsupported track type in ffmpeg demux" );
238 fmt.psz_language = strdup( p_sys->ic->streams[i]->language );
239 fmt.i_extra = cc->extradata_size;
240 fmt.p_extra = cc->extradata;
241 es = es_out_Add( p_demux->out, &fmt );
243 msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
244 cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
246 TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
249 msg_Dbg( p_demux, "AVFormat supported stream" );
250 msg_Dbg( p_demux, " - format = %s (%s)",
251 p_sys->fmt->name, p_sys->fmt->long_name );
252 msg_Dbg( p_demux, " - start time = "I64Fd,
253 ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
254 p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
255 msg_Dbg( p_demux, " - duration = "I64Fd,
256 ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
257 p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
262 /*****************************************************************************
264 *****************************************************************************/
265 void E_(CloseDemux)( vlc_object_t *p_this )
267 demux_t *p_demux = (demux_t*)p_this;
268 demux_sys_t *p_sys = p_demux->p_sys;
269 vlc_bool_t b_avfmt_nofile;
271 FREENULL( p_sys->tk );
273 b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE;
274 p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
275 if( p_sys->ic ) av_close_input_file( p_sys->ic );
276 if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
278 if( p_sys->io_buffer ) free( p_sys->io_buffer );
282 /*****************************************************************************
284 *****************************************************************************/
285 static int Demux( demux_t *p_demux )
287 demux_sys_t *p_sys = p_demux->p_sys;
290 int64_t i_start_time;
293 if( av_read_frame( p_sys->ic, &pkt ) )
297 if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
299 av_free_packet( &pkt );
302 if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
307 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
309 i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
310 p_sys->ic->start_time : 0;
312 p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ?
313 0 : (pkt.dts - i_start_time) * 1000000 *
314 p_sys->ic->streams[pkt.stream_index]->time_base.num /
315 p_sys->ic->streams[pkt.stream_index]->time_base.den;
316 p_frame->i_pts = ( pkt.pts == (int64_t)AV_NOPTS_VALUE ) ?
317 0 : (pkt.pts - i_start_time) * 1000000 *
318 p_sys->ic->streams[pkt.stream_index]->time_base.num /
319 p_sys->ic->streams[pkt.stream_index]->time_base.den;
321 #ifdef AVFORMAT_DEBUG
322 msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
323 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
327 ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
329 p_sys->i_pcr_tk = pkt.stream_index;
330 p_sys->i_pcr = p_frame->i_dts;
332 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
335 es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
336 av_free_packet( &pkt );
340 /*****************************************************************************
342 *****************************************************************************/
343 static int Control( demux_t *p_demux, int i_query, va_list args )
345 demux_sys_t *p_sys = p_demux->p_sys;
351 case DEMUX_GET_POSITION:
352 pf = (double*) va_arg( args, double* ); *pf = 0.0;
353 i64 = stream_Size( p_demux->s );
356 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
359 if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
361 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
366 case DEMUX_SET_POSITION:
367 f = (double) va_arg( args, double );
368 i64 = stream_Tell( p_demux->s );
369 if( p_sys->i_pcr > 0 )
371 i64 = p_sys->ic->duration * f;
372 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
373 i64 += p_sys->ic->start_time;
375 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
377 /* If we have a duration, we prefer to seek by time
378 but if we don't, or if the seek fails, try BYTE seeking */
379 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
380 (av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) )
382 int64_t i_size = stream_Size( p_demux->s );
383 i64 = (int64_t)i_size * f;
385 msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: "I64Fd, i64 );
386 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
389 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
390 p_sys->i_pcr = -1; /* Invalidate time display */
394 case DEMUX_GET_LENGTH:
395 pi64 = (int64_t*)va_arg( args, int64_t * );
396 if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
398 *pi64 = p_sys->ic->duration;
404 pi64 = (int64_t*)va_arg( args, int64_t * );
405 *pi64 = p_sys->i_pcr;
409 i64 = (int64_t)va_arg( args, int64_t );
410 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
411 i64 += p_sys->ic->start_time;
413 msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
415 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
419 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
420 p_sys->i_pcr = -1; /* Invalidate time display */
425 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
427 if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
428 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
429 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
434 if( p_sys->ic->title[0] )
435 vlc_meta_SetTitle( p_meta, p_sys->ic->title );
436 if( p_sys->ic->author[0] )
437 vlc_meta_SetArtist( p_meta, p_sys->ic->author );
438 if( p_sys->ic->copyright[0] )
439 vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright );
440 if( p_sys->ic->comment[0] )
441 vlc_meta_SetDescription( p_meta, p_sys->ic->comment );
442 if( p_sys->ic->genre[0] )
443 vlc_meta_SetGenre( p_meta, p_sys->ic->genre );
452 /*****************************************************************************
453 * I/O wrappers for libavformat
454 *****************************************************************************/
455 static int IORead( void *opaque, uint8_t *buf, int buf_size )
457 URLContext *p_url = opaque;
458 demux_t *p_demux = p_url->priv_data;
459 int i_ret = stream_Read( p_demux->s, buf, buf_size );
460 return i_ret ? i_ret : -1;
463 static offset_t IOSeek( void *opaque, offset_t offset, int whence )
465 URLContext *p_url = opaque;
466 demux_t *p_demux = p_url->priv_data;
467 int64_t i_absolute = (int64_t)offset;
468 int64_t i_size = stream_Size( p_demux->s );
470 #ifdef AVFORMAT_DEBUG
471 msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
479 i_absolute = stream_Tell( p_demux->s ) + offset;
482 i_absolute = i_size + offset;
490 if( i_size && i_absolute > i_size )
493 if( stream_Seek( p_demux->s, i_absolute ) )
498 return stream_Tell( p_demux->s );
501 #else /* HAVE_FFMPEG_AVFORMAT_H */
503 int E_(OpenDemux)( vlc_object_t *p_this )
508 void E_(CloseDemux)( vlc_object_t *p_this )
512 #endif /* HAVE_FFMPEG_AVFORMAT_H */