]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/demux.c
* configure.ac: detect if libavformat is present.
[vlc] / modules / codec / ffmpeg / demux.c
1 /*****************************************************************************
2  * demux.c: demuxer using ffmpeg (libavformat).
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id: demux.c,v 1.1 2004/01/08 00:12:50 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 /* ffmpeg header */
34 #ifdef HAVE_FFMPEG_AVCODEC_H
35 #   include <ffmpeg/avformat.h>
36 #else
37 #   include <avformat.h>
38 #endif
39
40 /* Version checking */
41 #if (LIBAVFORMAT_BUILD >= 4611) && defined(HAVE_LIBAVFORMAT)
42
43 /*****************************************************************************
44  * demux_sys_t: demux descriptor
45  *****************************************************************************/
46 struct demux_sys_t
47 {
48     ByteIOContext   io;
49     int             io_buffer_size;
50     uint8_t        *io_buffer;
51
52     AVInputFormat  *fmt;
53     AVFormatContext *ic;
54
55     int             i_tk;
56     es_out_id_t     **tk;
57
58     int64_t     i_pcr;
59     int64_t     i_pcr_inc;
60     int         i_pcr_tk;
61 };
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static int Demux  ( demux_t *p_demux );
67 static int Control( demux_t *p_demux, int i_query, va_list args );
68
69 static int IORead( void *opaque, uint8_t *buf, int buf_size );
70 static int IOSeek( void *opaque, offset_t offset, int whence );
71
72 /*****************************************************************************
73  * Open
74  *****************************************************************************/
75 int E_(OpenDemux)( vlc_object_t *p_this )
76 {
77     demux_t       *p_demux = (demux_t*)p_this;
78     demux_sys_t   *p_sys;
79     AVProbeData   pd;
80     AVInputFormat *fmt;
81     int i, b_forced;
82
83     b_forced = ( p_demux->psz_demux && *p_demux->psz_demux &&
84                  !strcmp( p_demux->psz_demux, "ffmpeg" ) ) ? 1 : 0;
85
86     /* Init Probe data */
87     pd.filename = p_demux->psz_path;
88     if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 ) ) <= 0 )
89     {
90         msg_Warn( p_demux, "cannot peek" );
91         return VLC_EGENERIC;
92     }
93
94     /* Should we call it only once ? */
95     av_register_all();
96
97     /* Guess format */
98     if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
99     {
100         msg_Dbg( p_demux, "couldn't guess format" );
101         return VLC_EGENERIC;
102     }
103
104     /* Don't try to handle MPEG unless forced */
105     if( !b_forced &&
106         ( !strcmp( fmt->name, "mpeg" ) ||
107           !strcmp( fmt->name, "vcd" ) ||
108           !strcmp( fmt->name, "vob" ) ||
109           !strcmp( fmt->name, "mpegts" ) ) )
110     {
111         return VLC_EGENERIC;
112     }
113
114     /* Fill p_demux fields */
115     p_demux->pf_demux = Demux;
116     p_demux->pf_control = Control;
117     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
118     p_sys->fmt = fmt;
119     p_sys->i_tk = 0;
120     p_sys->tk = NULL;
121     p_sys->i_pcr_tk = -1;
122
123     /* Create I/O wrapper */
124     p_sys->io_buffer_size = 32768;  /* FIXME */
125     p_sys->io_buffer = malloc( p_sys->io_buffer_size );
126     init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
127                    0, p_demux, IORead, NULL, IOSeek );
128
129     p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
130
131     /* Open it */
132     if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path,
133                               p_sys->fmt, NULL ) )
134     {
135         msg_Err( p_demux, "av_open_input_stream failed" );
136         return VLC_EGENERIC;
137     }
138
139     if( av_find_stream_info( p_sys->ic ) )
140     {
141         msg_Err( p_demux, "av_find_stream_info failed" );
142         return VLC_EGENERIC;
143     }
144
145     for( i = 0; i < p_sys->ic->nb_streams; i++ )
146     {
147         AVCodecContext *cc = &p_sys->ic->streams[i]->codec;
148         es_out_id_t  *es;
149         es_format_t  fmt;
150         vlc_fourcc_t fcc;
151
152         if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
153             fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
154
155         switch( cc->codec_type )
156         {
157         case CODEC_TYPE_AUDIO:
158             es_format_Init( &fmt, AUDIO_ES, fcc );
159             break;
160         case CODEC_TYPE_VIDEO:
161             es_format_Init( &fmt, VIDEO_ES, fcc );
162             break;
163         default:
164             break;
165         }
166
167         fmt.video.i_width = cc->width;
168         fmt.video.i_height = cc->height;
169         fmt.i_extra = cc->extradata_size;
170         fmt.p_extra = cc->extradata;
171         es = es_out_Add( p_demux->out, &fmt );
172
173         msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
174                  cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
175                  (char*)&fcc );
176         TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
177     }
178
179     msg_Dbg( p_demux, "AVFormat supported stream" );
180     msg_Dbg( p_demux, "    - format = %s (%s)",
181              p_sys->fmt->name, p_sys->fmt->long_name );
182     msg_Dbg( p_demux, "    - start time=%lld",
183              p_sys->ic->start_time / AV_TIME_BASE );
184     msg_Dbg( p_demux, "    - duration = %lld",
185              p_sys->ic->duration / AV_TIME_BASE );
186
187     return VLC_SUCCESS;
188 }
189
190 /*****************************************************************************
191  * Close
192  *****************************************************************************/
193 void E_(CloseDemux)( vlc_object_t *p_this )
194 {
195     demux_t     *p_demux = (demux_t*)p_this;
196     demux_sys_t *p_sys = p_demux->p_sys;
197
198     av_close_input_file( p_sys->ic );
199
200     free( p_sys->io_buffer );
201     free( p_sys );
202 }
203
204 /*****************************************************************************
205  * Demux:
206  *****************************************************************************/
207 static int Demux( demux_t *p_demux )
208 {
209     demux_sys_t *p_sys = p_demux->p_sys;
210     AVPacket    pkt;
211     block_t     *p_frame;
212
213     /* Read a frame */
214     if( av_read_frame( p_sys->ic, &pkt ) )
215     {
216         return 0;
217     }
218     if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
219     {
220         av_free_packet( &pkt );
221         return 1;
222     }
223     if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
224     {
225         return 0;
226     }
227
228     memcpy( p_frame->p_buffer, pkt.data, pkt.size );
229     p_frame->i_dts = pkt.dts * 1000000 / AV_TIME_BASE;
230     p_frame->i_pts = pkt.pts * 1000000 / AV_TIME_BASE;
231     msg_Dbg( p_demux, "tk[%d] dts=%lld pts=%lld",
232              pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
233
234     if( pkt.dts > 0 &&
235         ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
236     {
237         p_sys->i_pcr_tk = pkt.stream_index;
238         p_sys->i_pcr = pkt.dts;
239
240         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
241     }
242
243     es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
244     av_free_packet( &pkt );
245     return 1;
246 }
247
248 /*****************************************************************************
249  * Control:
250  *****************************************************************************/
251 static int Control( demux_t *p_demux, int i_query, va_list args )
252 {
253     demux_sys_t *p_sys = p_demux->p_sys;
254     double f, *pf;
255     int64_t i64, *pi64;
256
257     switch( i_query )
258     {
259         case DEMUX_GET_POSITION:
260             pf = (double*) va_arg( args, double* );
261             i64 = stream_Size( p_demux->s );
262             if( i64 > 0 )
263             {
264                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
265             }
266             else
267             {
268                 *pf = 0.0;
269             }
270             return VLC_SUCCESS;
271
272         case DEMUX_SET_POSITION:
273             f = (double) va_arg( args, double );
274             i64 = stream_Size( p_demux->s );
275
276             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
277             av_seek_frame( p_sys->ic, -1, -1 );
278             if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
279             {
280                 return VLC_EGENERIC;
281             }
282             p_sys->i_pcr = -1; /* Invalidate time display */
283             return VLC_SUCCESS;
284
285         case DEMUX_GET_TIME:
286             pi64 = (int64_t*)va_arg( args, int64_t * );
287             *pi64 = p_sys->i_pcr;
288             return VLC_SUCCESS;
289
290         case DEMUX_SET_TIME:
291             i64 = (int64_t)va_arg( args, int64_t );
292             if( av_seek_frame( p_sys->ic, -1, i64 ) < 0 )
293             {
294                 return VLC_EGENERIC;
295             }
296             p_sys->i_pcr = -1; /* Invalidate time display */
297             return VLC_SUCCESS;
298
299         default:
300             return VLC_EGENERIC;
301     }
302 }
303
304 /*****************************************************************************
305  * I/O wrappers for libavformat
306  *****************************************************************************/
307 static int IORead( void *opaque, uint8_t *buf, int buf_size )
308 {
309     demux_t *p_demux = opaque;
310     return stream_Read( p_demux->s, buf, buf_size );
311 }
312
313 static int IOSeek( void *opaque, offset_t offset, int whence )
314 {
315     demux_t *p_demux = opaque;
316     int64_t i_absolute;
317
318     switch( whence )
319     {
320         case SEEK_SET:
321             i_absolute = offset;
322             break;
323         case SEEK_CUR:
324             i_absolute = stream_Tell( p_demux->s ) + offset;
325             break;
326         case SEEK_END:
327             i_absolute = stream_Size( p_demux->s ) - offset;
328             break;
329         default:
330             return -1;
331
332     }
333
334     if( stream_Seek( p_demux->s, i_absolute ) )
335     {
336         return -1;
337     }
338
339     return 0;
340 }
341
342 #else /* LIBAVFORMAT_BUILD >= 4611 */
343
344 int E_(OpenDemux)( vlc_object_t *p_this )
345 {
346     return VLC_EGENERIC;
347 }
348
349 void E_(CloseDemux)( vlc_object_t *p_this )
350 {
351 }
352
353 #endif /* LIBAVFORMAT_BUILD >= 4611 */