]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/demux.c
* modules/codec/ffmpeg/demux.c: don't use libavformat's redirector demux.
[vlc] / modules / codec / ffmpeg / demux.c
1 /*****************************************************************************
2  * demux.c: demuxer using ffmpeg (libavformat).
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
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           /* libavformat's redirector won't work */
113           !strcmp( fmt->name, "redir" ) ||
114           !strcmp( fmt->name, "sdp" ) ) )
115     {
116         return VLC_EGENERIC;
117     }
118
119     msg_Dbg( p_demux, "detected format: %s", fmt->name );
120
121     /* Fill p_demux fields */
122     p_demux->pf_demux = Demux;
123     p_demux->pf_control = Control;
124     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
125     p_sys->fmt = fmt;
126     p_sys->i_tk = 0;
127     p_sys->tk = NULL;
128     p_sys->i_pcr_tk = -1;
129
130     /* Create I/O wrapper */
131     p_sys->io_buffer_size = 32768;  /* FIXME */
132     p_sys->io_buffer = malloc( p_sys->io_buffer_size );
133     p_sys->url.priv_data = p_demux;
134     p_sys->url.prot = &p_sys->prot;
135     p_sys->url.prot->name = "VLC I/O wrapper";
136     p_sys->url.prot->url_open = 0;
137     p_sys->url.prot->url_read = IORead;
138     p_sys->url.prot->url_write = 0;
139     p_sys->url.prot->url_seek = IOSeek;
140     p_sys->url.prot->url_close = 0;
141     p_sys->url.prot->next = 0;
142     init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
143                    0, &p_sys->url, IORead, NULL, IOSeek );
144
145     p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
146
147     /* Open it */
148     if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path,
149                               p_sys->fmt, NULL ) )
150     {
151         msg_Err( p_demux, "av_open_input_stream failed" );
152         return VLC_EGENERIC;
153     }
154
155     if( av_find_stream_info( p_sys->ic ) )
156     {
157         msg_Err( p_demux, "av_find_stream_info failed" );
158         return VLC_EGENERIC;
159     }
160
161     for( i = 0; i < p_sys->ic->nb_streams; i++ )
162     {
163         AVCodecContext *cc = &p_sys->ic->streams[i]->codec;
164         es_out_id_t  *es;
165         es_format_t  fmt;
166         vlc_fourcc_t fcc;
167
168         if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
169             fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
170
171         switch( cc->codec_type )
172         {
173         case CODEC_TYPE_AUDIO:
174             es_format_Init( &fmt, AUDIO_ES, fcc );
175             fmt.audio.i_channels = cc->channels;
176             fmt.audio.i_rate = cc->sample_rate;
177             fmt.audio.i_bitspersample = cc->bits_per_sample;
178             fmt.audio.i_blockalign = cc->block_align;
179             break;
180         case CODEC_TYPE_VIDEO:
181             es_format_Init( &fmt, VIDEO_ES, fcc );
182             fmt.video.i_width = cc->width;
183             fmt.video.i_height = cc->height;
184             if( cc->palctrl )
185             {
186                 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
187                 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
188             }
189             break;
190         default:
191             break;
192         }
193
194         fmt.i_extra = cc->extradata_size;
195         fmt.p_extra = cc->extradata;
196         es = es_out_Add( p_demux->out, &fmt );
197
198         msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
199                  cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
200                  (char*)&fcc );
201         TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
202     }
203
204     msg_Dbg( p_demux, "AVFormat supported stream" );
205     msg_Dbg( p_demux, "    - format = %s (%s)",
206              p_sys->fmt->name, p_sys->fmt->long_name );
207     msg_Dbg( p_demux, "    - start time = "I64Fd,
208              ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
209              p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
210     msg_Dbg( p_demux, "    - duration = "I64Fd,
211              ( p_sys->ic->duration != AV_NOPTS_VALUE ) ?
212              p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
213
214     return VLC_SUCCESS;
215 }
216
217 /*****************************************************************************
218  * Close
219  *****************************************************************************/
220 void E_(CloseDemux)( vlc_object_t *p_this )
221 {
222     demux_t     *p_demux = (demux_t*)p_this;
223     demux_sys_t *p_sys = p_demux->p_sys;
224
225     av_close_input_file( p_sys->ic );
226
227     free( p_sys->io_buffer );
228     free( p_sys );
229 }
230
231 /*****************************************************************************
232  * Demux:
233  *****************************************************************************/
234 static int Demux( demux_t *p_demux )
235 {
236     demux_sys_t *p_sys = p_demux->p_sys;
237     AVPacket    pkt;
238     block_t     *p_frame;
239     int64_t     i_start_time;
240
241     /* Read a frame */
242     if( av_read_frame( p_sys->ic, &pkt ) )
243     {
244         return 0;
245     }
246     if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
247     {
248         av_free_packet( &pkt );
249         return 1;
250     }
251     if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
252     {
253         return 0;
254     }
255
256     memcpy( p_frame->p_buffer, pkt.data, pkt.size );
257
258     i_start_time = ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
259         p_sys->ic->start_time : 0;
260
261     p_frame->i_dts = ( pkt.dts == AV_NOPTS_VALUE ) ?
262         0 : (pkt.dts - i_start_time) * 1000000 / AV_TIME_BASE;
263     p_frame->i_pts = ( pkt.pts == AV_NOPTS_VALUE ) ?
264         0 : (pkt.pts - i_start_time) * 1000000 / AV_TIME_BASE;
265
266     msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
267              pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
268
269     if( pkt.dts > 0  &&
270         ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
271     {    
272         p_sys->i_pcr_tk = pkt.stream_index;
273         p_sys->i_pcr = pkt.dts - i_start_time;
274
275         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
276     }
277
278     es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
279     av_free_packet( &pkt );
280     return 1;
281 }
282
283 /*****************************************************************************
284  * Control:
285  *****************************************************************************/
286 static int Control( demux_t *p_demux, int i_query, va_list args )
287 {
288     demux_sys_t *p_sys = p_demux->p_sys;
289     double f, *pf;
290     int64_t i64, *pi64;
291
292     switch( i_query )
293     {
294         case DEMUX_GET_POSITION:
295             pf = (double*) va_arg( args, double* );
296             i64 = stream_Size( p_demux->s );
297             if( i64 > 0 )
298             {
299                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
300             }
301             else
302             {
303                 *pf = 0.0;
304             }
305             return VLC_SUCCESS;
306
307         case DEMUX_SET_POSITION:
308             f = (double) va_arg( args, double );
309             i64 = stream_Tell( p_demux->s );
310             if( i64 && p_sys->i_pcr )
311             {
312                 int64_t i_size = stream_Size( p_demux->s );
313
314                 i64 = p_sys->i_pcr * i_size / i64 * f;
315                 if( p_sys->ic->start_time != AV_NOPTS_VALUE )
316                     i64 += p_sys->ic->start_time;
317
318                 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
319
320                 if( av_seek_frame( p_sys->ic, -1, i64 ) )
321                 {
322                     return VLC_EGENERIC;
323                 }
324                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
325                 p_sys->i_pcr = -1; /* Invalidate time display */
326             }
327             return VLC_SUCCESS;
328
329         case DEMUX_GET_TIME:
330             pi64 = (int64_t*)va_arg( args, int64_t * );
331             *pi64 = p_sys->i_pcr;
332             return VLC_SUCCESS;
333
334         case DEMUX_SET_TIME:
335             i64 = (int64_t)va_arg( args, int64_t );
336             if( p_sys->ic->start_time != AV_NOPTS_VALUE )
337                 i64 += p_sys->ic->start_time;
338
339             msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
340
341             if( av_seek_frame( p_sys->ic, -1, i64 ) < 0 )
342             {
343                 return VLC_EGENERIC;
344             }
345             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
346             p_sys->i_pcr = -1; /* Invalidate time display */
347             return VLC_SUCCESS;
348
349         default:
350             return VLC_EGENERIC;
351     }
352 }
353
354 /*****************************************************************************
355  * I/O wrappers for libavformat
356  *****************************************************************************/
357 static int IORead( void *opaque, uint8_t *buf, int buf_size )
358 {
359     URLContext *p_url = opaque;
360     demux_t *p_demux = p_url->priv_data;
361     return stream_Read( p_demux->s, buf, buf_size );
362 }
363
364 static int IOSeek( void *opaque, offset_t offset, int whence )
365 {
366     URLContext *p_url = opaque;
367     demux_t *p_demux = p_url->priv_data;
368     int64_t i_absolute;
369
370     msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
371
372     switch( whence )
373     {
374         case SEEK_SET:
375             i_absolute = offset;
376             break;
377         case SEEK_CUR:
378             i_absolute = stream_Tell( p_demux->s ) + offset;
379             break;
380         case SEEK_END:
381             i_absolute = stream_Size( p_demux->s ) - offset;
382             break;
383         default:
384             return -1;
385
386     }
387
388     if( stream_Seek( p_demux->s, i_absolute ) )
389     {
390         return -1;
391     }
392
393     return 0;
394 }
395
396 #else /* LIBAVFORMAT_BUILD >= 4611 */
397
398 int E_(OpenDemux)( vlc_object_t *p_this )
399 {
400     return VLC_EGENERIC;
401 }
402
403 void E_(CloseDemux)( vlc_object_t *p_this )
404 {
405 }
406
407 #endif /* LIBAVFORMAT_BUILD >= 4611 */