]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/demux.c
* modules/codec/ffmpeg/ffmpeg.c: more fourccs added.
[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.3 2004/01/15 19:46:32 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     URLContext     url;
55     URLProtocol    prot;
56
57     int             i_tk;
58     es_out_id_t     **tk;
59
60     int64_t     i_pcr;
61     int64_t     i_pcr_inc;
62     int         i_pcr_tk;
63 };
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int Demux  ( demux_t *p_demux );
69 static int Control( demux_t *p_demux, int i_query, va_list args );
70
71 static int IORead( void *opaque, uint8_t *buf, int buf_size );
72 static int IOSeek( void *opaque, offset_t offset, int whence );
73
74 /*****************************************************************************
75  * Open
76  *****************************************************************************/
77 int E_(OpenDemux)( vlc_object_t *p_this )
78 {
79     demux_t       *p_demux = (demux_t*)p_this;
80     demux_sys_t   *p_sys;
81     AVProbeData   pd;
82     AVInputFormat *fmt;
83     int i, b_forced;
84
85     b_forced = ( p_demux->psz_demux && *p_demux->psz_demux &&
86                  !strcmp( p_demux->psz_demux, "ffmpeg" ) ) ? 1 : 0;
87
88     /* Init Probe data */
89     pd.filename = p_demux->psz_path;
90     if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 ) ) <= 0 )
91     {
92         msg_Warn( p_demux, "cannot peek" );
93         return VLC_EGENERIC;
94     }
95
96     /* Should we call it only once ? */
97     av_register_all();
98
99     /* Guess format */
100     if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
101     {
102         msg_Dbg( p_demux, "couldn't guess format" );
103         return VLC_EGENERIC;
104     }
105
106     /* Don't try to handle MPEG unless forced */
107     if( !b_forced &&
108         ( !strcmp( fmt->name, "mpeg" ) ||
109           !strcmp( fmt->name, "vcd" ) ||
110           !strcmp( fmt->name, "vob" ) ||
111           !strcmp( fmt->name, "mpegts" ) ) )
112     {
113         return VLC_EGENERIC;
114     }
115
116     /* Fill p_demux fields */
117     p_demux->pf_demux = Demux;
118     p_demux->pf_control = Control;
119     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
120     p_sys->fmt = fmt;
121     p_sys->i_tk = 0;
122     p_sys->tk = NULL;
123     p_sys->i_pcr_tk = -1;
124
125     /* Create I/O wrapper */
126     p_sys->io_buffer_size = 32768;  /* FIXME */
127     p_sys->io_buffer = malloc( p_sys->io_buffer_size );
128     p_sys->url.priv_data = p_demux;
129     p_sys->url.prot = &p_sys->prot;
130     p_sys->url.prot->name = "VLC I/O wrapper";
131     p_sys->url.prot->url_open = 0;
132     p_sys->url.prot->url_read = IORead;
133     p_sys->url.prot->url_write = 0;
134     p_sys->url.prot->url_seek = IOSeek;
135     p_sys->url.prot->url_close = 0;
136     p_sys->url.prot->next = 0;
137     init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
138                    0, &p_sys->url, IORead, NULL, IOSeek );
139
140     p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
141
142     /* Open it */
143     if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path,
144                               p_sys->fmt, NULL ) )
145     {
146         msg_Err( p_demux, "av_open_input_stream failed" );
147         return VLC_EGENERIC;
148     }
149
150     if( av_find_stream_info( p_sys->ic ) )
151     {
152         msg_Err( p_demux, "av_find_stream_info failed" );
153         return VLC_EGENERIC;
154     }
155
156     for( i = 0; i < p_sys->ic->nb_streams; i++ )
157     {
158         AVCodecContext *cc = &p_sys->ic->streams[i]->codec;
159         es_out_id_t  *es;
160         es_format_t  fmt;
161         vlc_fourcc_t fcc;
162
163         if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
164             fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
165
166         switch( cc->codec_type )
167         {
168         case CODEC_TYPE_AUDIO:
169             es_format_Init( &fmt, AUDIO_ES, fcc );
170             fmt.audio.i_channels = cc->channels;
171             fmt.audio.i_rate = cc->sample_rate;
172             fmt.audio.i_bitspersample = cc->bits_per_sample;
173             fmt.audio.i_blockalign = cc->block_align;
174             break;
175         case CODEC_TYPE_VIDEO:
176             es_format_Init( &fmt, VIDEO_ES, fcc );
177             fmt.video.i_width = cc->width;
178             fmt.video.i_height = cc->height;
179             break;
180         default:
181             break;
182         }
183
184         fmt.i_extra = cc->extradata_size;
185         fmt.p_extra = cc->extradata;
186         es = es_out_Add( p_demux->out, &fmt );
187
188         msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
189                  cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
190                  (char*)&fcc );
191         TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
192     }
193
194     msg_Dbg( p_demux, "AVFormat supported stream" );
195     msg_Dbg( p_demux, "    - format = %s (%s)",
196              p_sys->fmt->name, p_sys->fmt->long_name );
197     msg_Dbg( p_demux, "    - start time = "I64Fd,
198              ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
199              p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
200     msg_Dbg( p_demux, "    - duration = "I64Fd,
201              ( p_sys->ic->duration != AV_NOPTS_VALUE ) ?
202              p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
203
204     return VLC_SUCCESS;
205 }
206
207 /*****************************************************************************
208  * Close
209  *****************************************************************************/
210 void E_(CloseDemux)( vlc_object_t *p_this )
211 {
212     demux_t     *p_demux = (demux_t*)p_this;
213     demux_sys_t *p_sys = p_demux->p_sys;
214
215     av_close_input_file( p_sys->ic );
216
217     free( p_sys->io_buffer );
218     free( p_sys );
219 }
220
221 /*****************************************************************************
222  * Demux:
223  *****************************************************************************/
224 static int Demux( demux_t *p_demux )
225 {
226     demux_sys_t *p_sys = p_demux->p_sys;
227     AVPacket    pkt;
228     block_t     *p_frame;
229     int64_t     i_start_time;
230
231     /* Read a frame */
232     if( av_read_frame( p_sys->ic, &pkt ) )
233     {
234         return 0;
235     }
236     if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
237     {
238         av_free_packet( &pkt );
239         return 1;
240     }
241     if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
242     {
243         return 0;
244     }
245
246     memcpy( p_frame->p_buffer, pkt.data, pkt.size );
247
248     i_start_time = ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
249         p_sys->ic->start_time : 0;
250
251     p_frame->i_dts = ( pkt.dts == AV_NOPTS_VALUE ) ?
252         0 : (pkt.dts - i_start_time) * 1000000 / AV_TIME_BASE;
253     p_frame->i_pts = ( pkt.pts == AV_NOPTS_VALUE ) ?
254         0 : (pkt.pts - i_start_time) * 1000000 / AV_TIME_BASE;
255
256     msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
257              pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
258
259     if( pkt.dts > 0  &&
260         ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
261     {    
262         p_sys->i_pcr_tk = pkt.stream_index;
263         p_sys->i_pcr = pkt.dts - i_start_time;
264
265         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
266     }
267
268     es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
269     av_free_packet( &pkt );
270     return 1;
271 }
272
273 /*****************************************************************************
274  * Control:
275  *****************************************************************************/
276 static int Control( demux_t *p_demux, int i_query, va_list args )
277 {
278     demux_sys_t *p_sys = p_demux->p_sys;
279     double f, *pf;
280     int64_t i64, *pi64;
281
282     switch( i_query )
283     {
284         case DEMUX_GET_POSITION:
285             pf = (double*) va_arg( args, double* );
286             i64 = stream_Size( p_demux->s );
287             if( i64 > 0 )
288             {
289                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
290             }
291             else
292             {
293                 *pf = 0.0;
294             }
295             return VLC_SUCCESS;
296
297         case DEMUX_SET_POSITION:
298             f = (double) va_arg( args, double );
299             i64 = stream_Tell( p_demux->s );
300             if( i64 && p_sys->i_pcr )
301             {
302                 int64_t i_size = stream_Size( p_demux->s );
303
304                 i64 = p_sys->i_pcr * i_size / i64 * f;
305                 if( p_sys->ic->start_time != AV_NOPTS_VALUE )
306                     i64 += p_sys->ic->start_time;
307
308                 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
309
310                 if( av_seek_frame( p_sys->ic, -1, i64 ) )
311                 {
312                     return VLC_EGENERIC;
313                 }
314                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
315                 p_sys->i_pcr = -1; /* Invalidate time display */
316             }
317             return VLC_SUCCESS;
318
319         case DEMUX_GET_TIME:
320             pi64 = (int64_t*)va_arg( args, int64_t * );
321             *pi64 = p_sys->i_pcr;
322             return VLC_SUCCESS;
323
324         case DEMUX_SET_TIME:
325             i64 = (int64_t)va_arg( args, int64_t );
326             if( p_sys->ic->start_time != AV_NOPTS_VALUE )
327                 i64 += p_sys->ic->start_time;
328
329             msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
330
331             if( av_seek_frame( p_sys->ic, -1, i64 ) < 0 )
332             {
333                 return VLC_EGENERIC;
334             }
335             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
336             p_sys->i_pcr = -1; /* Invalidate time display */
337             return VLC_SUCCESS;
338
339         default:
340             return VLC_EGENERIC;
341     }
342 }
343
344 /*****************************************************************************
345  * I/O wrappers for libavformat
346  *****************************************************************************/
347 static int IORead( void *opaque, uint8_t *buf, int buf_size )
348 {
349     URLContext *p_url = opaque;
350     demux_t *p_demux = p_url->priv_data;
351     return stream_Read( p_demux->s, buf, buf_size );
352 }
353
354 static int IOSeek( void *opaque, offset_t offset, int whence )
355 {
356     URLContext *p_url = opaque;
357     demux_t *p_demux = p_url->priv_data;
358     int64_t i_absolute;
359
360     msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
361
362     switch( whence )
363     {
364         case SEEK_SET:
365             i_absolute = offset;
366             break;
367         case SEEK_CUR:
368             i_absolute = stream_Tell( p_demux->s ) + offset;
369             break;
370         case SEEK_END:
371             i_absolute = stream_Size( p_demux->s ) - offset;
372             break;
373         default:
374             return -1;
375
376     }
377
378     if( stream_Seek( p_demux->s, i_absolute ) )
379     {
380         return -1;
381     }
382
383     return 0;
384 }
385
386 #else /* LIBAVFORMAT_BUILD >= 4611 */
387
388 int E_(OpenDemux)( vlc_object_t *p_this )
389 {
390     return VLC_EGENERIC;
391 }
392
393 void E_(CloseDemux)( vlc_object_t *p_this )
394 {
395 }
396
397 #endif /* LIBAVFORMAT_BUILD >= 4611 */