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 *****************************************************************************/
34 #include <vlc_demux.h>
35 #include <vlc_stream.h>
39 #if defined(HAVE_LIBAVFORMAT_AVFORMAT_H)
40 # include <libavformat/avformat.h>
41 #elif defined(HAVE_FFMPEG_AVFORMAT_H)
42 # include <ffmpeg/avformat.h>
43 #elif defined(HAVE_LIBAVFORMAT_TREE)
44 # include <avformat.h>
49 //#define AVFORMAT_DEBUG 1
51 /* Version checking */
52 #if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_TREE)
54 /*****************************************************************************
55 * demux_sys_t: demux descriptor
56 *****************************************************************************/
76 /*****************************************************************************
78 *****************************************************************************/
79 static int Demux ( demux_t *p_demux );
80 static int Control( demux_t *p_demux, int i_query, va_list args );
82 static int IORead( void *opaque, uint8_t *buf, int buf_size );
83 static offset_t IOSeek( void *opaque, offset_t offset, int whence );
85 /*****************************************************************************
87 *****************************************************************************/
88 int E_(OpenDemux)( vlc_object_t *p_this )
90 demux_t *p_demux = (demux_t*)p_this;
98 pd.filename = p_demux->psz_path;
99 if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 ) ) <= 0 )
101 msg_Warn( p_demux, "cannot peek" );
105 av_register_all(); /* Can be called several times */
108 if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
110 msg_Dbg( p_demux, "couldn't guess format" );
114 /* Don't try to handle MPEG unless forced */
115 if( !p_demux->b_force &&
116 ( !strcmp( fmt->name, "mpeg" ) ||
117 !strcmp( fmt->name, "vcd" ) ||
118 !strcmp( fmt->name, "vob" ) ||
119 !strcmp( fmt->name, "mpegts" ) ||
120 /* libavformat's redirector won't work */
121 !strcmp( fmt->name, "redir" ) ||
122 !strcmp( fmt->name, "sdp" ) ) )
127 /* Don't trigger false alarms on bin files */
128 if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
132 if( !p_demux->psz_path ) return VLC_EGENERIC;
134 i_len = strlen( p_demux->psz_path );
135 if( i_len < 4 ) return VLC_EGENERIC;
137 if( strcasecmp( &p_demux->psz_path[i_len - 4], ".str" ) &&
138 strcasecmp( &p_demux->psz_path[i_len - 4], ".xai" ) &&
139 strcasecmp( &p_demux->psz_path[i_len - 3], ".xa" ) )
145 msg_Dbg( p_demux, "detected format: %s", fmt->name );
147 /* Fill p_demux fields */
148 p_demux->pf_demux = Demux;
149 p_demux->pf_control = Control;
150 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
155 p_sys->i_pcr_tk = -1;
158 /* Create I/O wrapper */
159 p_sys->io_buffer_size = 32768; /* FIXME */
160 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
161 p_sys->url.priv_data = p_demux;
162 p_sys->url.prot = &p_sys->prot;
163 p_sys->url.prot->name = "VLC I/O wrapper";
164 p_sys->url.prot->url_open = 0;
165 p_sys->url.prot->url_read =
166 (int (*) (URLContext *, unsigned char *, int))IORead;
167 p_sys->url.prot->url_write = 0;
168 p_sys->url.prot->url_seek =
169 (offset_t (*) (URLContext *, offset_t, int))IOSeek;
170 p_sys->url.prot->url_close = 0;
171 p_sys->url.prot->next = 0;
172 init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
173 0, &p_sys->url, IORead, NULL, IOSeek );
175 b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE;
176 p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
179 if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path,
182 msg_Err( p_demux, "av_open_input_stream failed" );
183 if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
184 E_(CloseDemux)( p_this );
188 if( av_find_stream_info( p_sys->ic ) < 0 )
190 msg_Err( p_demux, "av_find_stream_info failed" );
191 if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
192 E_(CloseDemux)( p_this );
195 if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
197 for( i = 0; i < p_sys->ic->nb_streams; i++ )
199 AVCodecContext *cc = p_sys->ic->streams[i]->codec;
204 if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
206 fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
208 /* Special case for raw video data */
209 if( cc->codec_id == CODEC_ID_RAWVIDEO )
211 msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt );
212 fcc = E_(GetVlcChroma)( cc->pix_fmt );
216 switch( cc->codec_type )
218 case CODEC_TYPE_AUDIO:
219 es_format_Init( &fmt, AUDIO_ES, fcc );
220 fmt.audio.i_channels = cc->channels;
221 fmt.audio.i_rate = cc->sample_rate;
222 fmt.audio.i_bitspersample = cc->bits_per_sample;
223 fmt.audio.i_blockalign = cc->block_align;
225 case CODEC_TYPE_VIDEO:
226 es_format_Init( &fmt, VIDEO_ES, fcc );
227 fmt.video.i_width = cc->width;
228 fmt.video.i_height = cc->height;
231 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
232 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
235 case CODEC_TYPE_SUBTITLE:
236 es_format_Init( &fmt, SPU_ES, fcc );
239 msg_Warn( p_demux, "unsupported track type in ffmpeg demux" );
243 fmt.psz_language = strdup( p_sys->ic->streams[i]->language );
244 fmt.i_extra = cc->extradata_size;
245 fmt.p_extra = cc->extradata;
246 es = es_out_Add( p_demux->out, &fmt );
248 msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
249 cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
251 TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
254 msg_Dbg( p_demux, "AVFormat supported stream" );
255 msg_Dbg( p_demux, " - format = %s (%s)",
256 p_sys->fmt->name, p_sys->fmt->long_name );
257 msg_Dbg( p_demux, " - start time = "I64Fd,
258 ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
259 p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
260 msg_Dbg( p_demux, " - duration = "I64Fd,
261 ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
262 p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
267 /*****************************************************************************
269 *****************************************************************************/
270 void E_(CloseDemux)( vlc_object_t *p_this )
272 demux_t *p_demux = (demux_t*)p_this;
273 demux_sys_t *p_sys = p_demux->p_sys;
276 FREENULL( p_sys->tk );
278 b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE;
279 p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
280 if( p_sys->ic ) av_close_input_file( p_sys->ic );
281 if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
283 free( p_sys->io_buffer );
287 /*****************************************************************************
289 *****************************************************************************/
290 static int Demux( demux_t *p_demux )
292 demux_sys_t *p_sys = p_demux->p_sys;
295 int64_t i_start_time;
298 if( av_read_frame( p_sys->ic, &pkt ) )
302 if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
304 av_free_packet( &pkt );
307 if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
312 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
314 i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
315 ( p_sys->ic->start_time / AV_TIME_BASE ) : 0;
317 p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ?
318 0 : (pkt.dts - i_start_time) * 1000000 *
319 p_sys->ic->streams[pkt.stream_index]->time_base.num /
320 p_sys->ic->streams[pkt.stream_index]->time_base.den;
321 p_frame->i_pts = ( pkt.pts == (int64_t)AV_NOPTS_VALUE ) ?
322 0 : (pkt.pts - i_start_time) * 1000000 *
323 p_sys->ic->streams[pkt.stream_index]->time_base.num /
324 p_sys->ic->streams[pkt.stream_index]->time_base.den;
326 #ifdef AVFORMAT_DEBUG
327 msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
328 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
332 ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
334 p_sys->i_pcr_tk = pkt.stream_index;
335 p_sys->i_pcr = p_frame->i_dts;
337 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
340 es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
341 av_free_packet( &pkt );
345 /*****************************************************************************
347 *****************************************************************************/
348 static int Control( demux_t *p_demux, int i_query, va_list args )
350 demux_sys_t *p_sys = p_demux->p_sys;
356 case DEMUX_GET_POSITION:
357 pf = (double*) va_arg( args, double* ); *pf = 0.0;
358 i64 = stream_Size( p_demux->s );
361 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
364 if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
366 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
371 case DEMUX_SET_POSITION:
372 f = (double) va_arg( args, double );
373 i64 = stream_Tell( p_demux->s );
374 if( p_sys->i_pcr > 0 )
376 i64 = p_sys->ic->duration * f;
377 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
378 i64 += p_sys->ic->start_time;
380 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
382 /* If we have a duration, we prefer to seek by time
383 but if we don't, or if the seek fails, try BYTE seeking */
384 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
385 (av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) )
387 int64_t i_size = stream_Size( p_demux->s );
388 i64 = (int64_t)i_size * f;
390 msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: "I64Fd, i64 );
391 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
394 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
395 p_sys->i_pcr = -1; /* Invalidate time display */
399 case DEMUX_GET_LENGTH:
400 pi64 = (int64_t*)va_arg( args, int64_t * );
401 if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
403 *pi64 = p_sys->ic->duration;
409 pi64 = (int64_t*)va_arg( args, int64_t * );
410 *pi64 = p_sys->i_pcr;
414 i64 = (int64_t)va_arg( args, int64_t );
415 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
416 i64 += p_sys->ic->start_time;
418 msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
420 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
424 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
425 p_sys->i_pcr = -1; /* Invalidate time display */
430 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
432 if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
433 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
434 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
439 if( p_sys->ic->title[0] )
440 vlc_meta_SetTitle( p_meta, p_sys->ic->title );
441 if( p_sys->ic->author[0] )
442 vlc_meta_SetArtist( p_meta, p_sys->ic->author );
443 if( p_sys->ic->copyright[0] )
444 vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright );
445 if( p_sys->ic->comment[0] )
446 vlc_meta_SetDescription( p_meta, p_sys->ic->comment );
447 if( p_sys->ic->genre[0] )
448 vlc_meta_SetGenre( p_meta, p_sys->ic->genre );
457 /*****************************************************************************
458 * I/O wrappers for libavformat
459 *****************************************************************************/
460 static int IORead( void *opaque, uint8_t *buf, int buf_size )
462 URLContext *p_url = opaque;
463 demux_t *p_demux = p_url->priv_data;
464 int i_ret = stream_Read( p_demux->s, buf, buf_size );
465 return i_ret ? i_ret : -1;
468 static offset_t IOSeek( void *opaque, offset_t offset, int whence )
470 URLContext *p_url = opaque;
471 demux_t *p_demux = p_url->priv_data;
472 int64_t i_absolute = (int64_t)offset;
473 int64_t i_size = stream_Size( p_demux->s );
475 #ifdef AVFORMAT_DEBUG
476 msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
484 i_absolute = stream_Tell( p_demux->s ) + offset;
487 i_absolute = i_size + offset;
497 if( i_absolute > i_size )
499 if( stream_Tell( p_demux->s ) >= i_size )
501 msg_Err( p_demux, "Seeking too far : EOF?" );
506 if( stream_Seek( p_demux->s, i_absolute ) )
511 return stream_Tell( p_demux->s );
514 #endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */