1 /*****************************************************************************
2 * demux.c: demuxer using ffmpeg (libavformat).
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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>
34 #ifdef HAVE_FFMPEG_AVCODEC_H
35 # include <ffmpeg/avformat.h>
37 # include <avformat.h>
42 /* Version checking */
43 #if (LIBAVFORMAT_BUILD >= 4611) && defined(HAVE_LIBAVFORMAT)
45 /*****************************************************************************
46 * demux_sys_t: demux descriptor
47 *****************************************************************************/
67 /*****************************************************************************
69 *****************************************************************************/
70 static int Demux ( demux_t *p_demux );
71 static int Control( demux_t *p_demux, int i_query, va_list args );
73 static int IORead( void *opaque, uint8_t *buf, int buf_size );
74 static int IOSeek( void *opaque, offset_t offset, int whence );
76 /*****************************************************************************
78 *****************************************************************************/
79 int E_(OpenDemux)( vlc_object_t *p_this )
81 demux_t *p_demux = (demux_t*)p_this;
87 b_forced = ( p_demux->psz_demux && *p_demux->psz_demux &&
88 !strcmp( p_demux->psz_demux, "ffmpeg" ) ) ? 1 : 0;
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 /* Should we call it only once ? */
102 if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
104 msg_Dbg( p_demux, "couldn't guess format" );
108 /* Don't try to handle MPEG unless forced */
110 ( !strcmp( fmt->name, "mpeg" ) ||
111 !strcmp( fmt->name, "vcd" ) ||
112 !strcmp( fmt->name, "vob" ) ||
113 !strcmp( fmt->name, "mpegts" ) ||
114 /* libavformat's redirector won't work */
115 !strcmp( fmt->name, "redir" ) ||
116 !strcmp( fmt->name, "sdp" ) ) )
121 msg_Dbg( p_demux, "detected format: %s", fmt->name );
123 /* Fill p_demux fields */
124 p_demux->pf_demux = Demux;
125 p_demux->pf_control = Control;
126 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
130 p_sys->i_pcr_tk = -1;
132 /* Create I/O wrapper */
133 p_sys->io_buffer_size = 32768; /* FIXME */
134 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
135 p_sys->url.priv_data = p_demux;
136 p_sys->url.prot = &p_sys->prot;
137 p_sys->url.prot->name = "VLC I/O wrapper";
138 p_sys->url.prot->url_open = 0;
139 p_sys->url.prot->url_read =
140 (int (*) (URLContext *, unsigned char *, int))IORead;
141 p_sys->url.prot->url_write = 0;
142 p_sys->url.prot->url_seek =
143 (offset_t (*) (URLContext *, offset_t, int))IOSeek;
144 p_sys->url.prot->url_close = 0;
145 p_sys->url.prot->next = 0;
146 init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
147 0, &p_sys->url, IORead, NULL, IOSeek );
149 p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
152 if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path,
155 msg_Err( p_demux, "av_open_input_stream failed" );
159 if( av_find_stream_info( p_sys->ic ) )
161 msg_Err( p_demux, "av_find_stream_info failed" );
165 for( i = 0; i < p_sys->ic->nb_streams; i++ )
167 AVCodecContext *cc = &p_sys->ic->streams[i]->codec;
172 if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
173 fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
175 switch( cc->codec_type )
177 case CODEC_TYPE_AUDIO:
178 es_format_Init( &fmt, AUDIO_ES, fcc );
179 fmt.audio.i_channels = cc->channels;
180 fmt.audio.i_rate = cc->sample_rate;
181 fmt.audio.i_bitspersample = cc->bits_per_sample;
182 fmt.audio.i_blockalign = cc->block_align;
184 case CODEC_TYPE_VIDEO:
185 es_format_Init( &fmt, VIDEO_ES, fcc );
186 fmt.video.i_width = cc->width;
187 fmt.video.i_height = cc->height;
190 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
191 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
198 fmt.i_extra = cc->extradata_size;
199 fmt.p_extra = cc->extradata;
200 es = es_out_Add( p_demux->out, &fmt );
202 msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
203 cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
205 TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
208 msg_Dbg( p_demux, "AVFormat supported stream" );
209 msg_Dbg( p_demux, " - format = %s (%s)",
210 p_sys->fmt->name, p_sys->fmt->long_name );
211 msg_Dbg( p_demux, " - start time = "I64Fd,
212 ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
213 p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
214 msg_Dbg( p_demux, " - duration = "I64Fd,
215 ( p_sys->ic->duration != AV_NOPTS_VALUE ) ?
216 p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
221 /*****************************************************************************
223 *****************************************************************************/
224 void E_(CloseDemux)( vlc_object_t *p_this )
226 demux_t *p_demux = (demux_t*)p_this;
227 demux_sys_t *p_sys = p_demux->p_sys;
229 av_close_input_file( p_sys->ic );
231 free( p_sys->io_buffer );
235 /*****************************************************************************
237 *****************************************************************************/
238 static int Demux( demux_t *p_demux )
240 demux_sys_t *p_sys = p_demux->p_sys;
243 int64_t i_start_time;
246 if( av_read_frame( p_sys->ic, &pkt ) )
250 if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
252 av_free_packet( &pkt );
255 if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
260 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
262 i_start_time = ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
263 p_sys->ic->start_time : 0;
265 p_frame->i_dts = ( pkt.dts == AV_NOPTS_VALUE ) ?
266 0 : (pkt.dts - i_start_time) * 1000000 / AV_TIME_BASE;
267 p_frame->i_pts = ( pkt.pts == AV_NOPTS_VALUE ) ?
268 0 : (pkt.pts - i_start_time) * 1000000 / AV_TIME_BASE;
270 msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
271 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
274 ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
276 p_sys->i_pcr_tk = pkt.stream_index;
277 p_sys->i_pcr = pkt.dts - i_start_time;
279 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
282 es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
283 av_free_packet( &pkt );
287 /*****************************************************************************
289 *****************************************************************************/
290 static int Control( demux_t *p_demux, int i_query, va_list args )
292 demux_sys_t *p_sys = p_demux->p_sys;
298 case DEMUX_GET_POSITION:
299 pf = (double*) va_arg( args, double* );
300 i64 = stream_Size( p_demux->s );
303 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
311 case DEMUX_SET_POSITION:
312 f = (double) va_arg( args, double );
313 i64 = stream_Tell( p_demux->s );
314 if( i64 && p_sys->i_pcr )
316 int64_t i_size = stream_Size( p_demux->s );
318 i64 = p_sys->i_pcr * i_size / i64 * f;
319 if( p_sys->ic->start_time != AV_NOPTS_VALUE )
320 i64 += p_sys->ic->start_time;
322 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
324 if( av_seek_frame( p_sys->ic, -1, i64 ) )
328 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
329 p_sys->i_pcr = -1; /* Invalidate time display */
334 pi64 = (int64_t*)va_arg( args, int64_t * );
335 *pi64 = p_sys->i_pcr;
339 i64 = (int64_t)va_arg( args, int64_t );
340 if( p_sys->ic->start_time != AV_NOPTS_VALUE )
341 i64 += p_sys->ic->start_time;
343 msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
345 if( av_seek_frame( p_sys->ic, -1, i64 ) < 0 )
349 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
350 p_sys->i_pcr = -1; /* Invalidate time display */
358 /*****************************************************************************
359 * I/O wrappers for libavformat
360 *****************************************************************************/
361 static int IORead( void *opaque, uint8_t *buf, int buf_size )
363 URLContext *p_url = opaque;
364 demux_t *p_demux = p_url->priv_data;
365 return stream_Read( p_demux->s, buf, buf_size );
368 static int IOSeek( void *opaque, offset_t offset, int whence )
370 URLContext *p_url = opaque;
371 demux_t *p_demux = p_url->priv_data;
374 msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
382 i_absolute = stream_Tell( p_demux->s ) + offset;
385 i_absolute = stream_Size( p_demux->s ) - offset;
392 if( stream_Seek( p_demux->s, i_absolute ) )
400 #else /* LIBAVFORMAT_BUILD >= 4611 */
402 int E_(OpenDemux)( vlc_object_t *p_this )
407 void E_(CloseDemux)( vlc_object_t *p_this )
411 #endif /* LIBAVFORMAT_BUILD >= 4611 */