]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/demux.c
Don't pretend to have ffmpeg based muxers and demuxers if you don't.
[vlc] / modules / codec / ffmpeg / demux.c
1 /*****************************************************************************
2  * demux.c: demuxer using ffmpeg (libavformat).
3  *****************************************************************************
4  * Copyright (C) 2004-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30 #include <vlc_demux.h>
31 #include <vlc_stream.h>
32 #include <vlc_meta.h>
33
34 /* ffmpeg header */
35 #ifdef HAVE_FFMPEG_AVFORMAT_H
36 #   include <ffmpeg/avformat.h>
37 #elif defined(HAVE_LIBAVFORMAT_TREE)
38 #   include <avformat.h>
39 #endif
40
41 #include "ffmpeg.h"
42
43 //#define AVFORMAT_DEBUG 1
44
45 /* Version checking */
46 #if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_TREE)
47
48 /*****************************************************************************
49  * demux_sys_t: demux descriptor
50  *****************************************************************************/
51 struct demux_sys_t
52 {
53     ByteIOContext   io;
54     int             io_buffer_size;
55     uint8_t        *io_buffer;
56
57     AVInputFormat  *fmt;
58     AVFormatContext *ic;
59     URLContext     url;
60     URLProtocol    prot;
61
62     int             i_tk;
63     es_out_id_t     **tk;
64
65     int64_t     i_pcr;
66     int64_t     i_pcr_inc;
67     int         i_pcr_tk;
68 };
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static int Demux  ( demux_t *p_demux );
74 static int Control( demux_t *p_demux, int i_query, va_list args );
75
76 static int IORead( void *opaque, uint8_t *buf, int buf_size );
77 static offset_t IOSeek( void *opaque, offset_t offset, int whence );
78
79 /*****************************************************************************
80  * Open
81  *****************************************************************************/
82 int E_(OpenDemux)( vlc_object_t *p_this )
83 {
84     demux_t       *p_demux = (demux_t*)p_this;
85     demux_sys_t   *p_sys;
86     AVProbeData   pd;
87     AVInputFormat *fmt;
88     int i;
89     vlc_bool_t b_avfmt_nofile;
90
91     /* Init Probe data */
92     pd.filename = p_demux->psz_path;
93     if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 ) ) <= 0 )
94     {
95         msg_Warn( p_demux, "cannot peek" );
96         return VLC_EGENERIC;
97     }
98
99     av_register_all(); /* Can be called several times */
100
101     /* Guess format */
102     if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
103     {
104         msg_Dbg( p_demux, "couldn't guess format" );
105         return VLC_EGENERIC;
106     }
107
108     /* Don't try to handle MPEG unless forced */
109     if( !p_demux->b_force &&
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" ) ) )
117     {
118         return VLC_EGENERIC;
119     }
120
121     /* Don't trigger false alarms on bin files */
122     if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
123     {
124         int i_len;
125
126         if( !p_demux->psz_path ) return VLC_EGENERIC;
127
128         i_len = strlen( p_demux->psz_path );
129         if( i_len < 4 ) return VLC_EGENERIC;
130
131         if( strcasecmp( &p_demux->psz_path[i_len - 4], ".str" ) &&
132             strcasecmp( &p_demux->psz_path[i_len - 4], ".xai" ) &&
133             strcasecmp( &p_demux->psz_path[i_len - 3], ".xa" ) )
134         {
135             return VLC_EGENERIC;
136         }
137     }
138
139     msg_Dbg( p_demux, "detected format: %s", fmt->name );
140
141     /* Fill p_demux fields */
142     p_demux->pf_demux = Demux;
143     p_demux->pf_control = Control;
144     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
145     p_sys->ic = 0;
146     p_sys->fmt = fmt;
147     p_sys->i_tk = 0;
148     p_sys->tk = NULL;
149     p_sys->i_pcr_tk = -1;
150     p_sys->i_pcr = -1;
151
152     /* Create I/O wrapper */
153     p_sys->io_buffer_size = 32768;  /* FIXME */
154     p_sys->io_buffer = malloc( p_sys->io_buffer_size );
155     p_sys->url.priv_data = p_demux;
156     p_sys->url.prot = &p_sys->prot;
157     p_sys->url.prot->name = "VLC I/O wrapper";
158     p_sys->url.prot->url_open = 0;
159     p_sys->url.prot->url_read =
160                     (int (*) (URLContext *, unsigned char *, int))IORead;
161     p_sys->url.prot->url_write = 0;
162     p_sys->url.prot->url_seek =
163                     (offset_t (*) (URLContext *, offset_t, int))IOSeek;
164     p_sys->url.prot->url_close = 0;
165     p_sys->url.prot->next = 0;
166     init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
167                    0, &p_sys->url, IORead, NULL, IOSeek );
168
169     b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE;
170     p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
171
172     /* Open it */
173     if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path,
174                               p_sys->fmt, NULL ) )
175     {
176         msg_Err( p_demux, "av_open_input_stream failed" );
177         if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
178         E_(CloseDemux)( p_this );
179         return VLC_EGENERIC;
180     }
181
182     if( av_find_stream_info( p_sys->ic ) < 0 )
183     {
184         msg_Err( p_demux, "av_find_stream_info failed" );
185         if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
186         E_(CloseDemux)( p_this );
187         return VLC_EGENERIC;
188     }
189     if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
190
191     for( i = 0; i < p_sys->ic->nb_streams; i++ )
192     {
193         AVCodecContext *cc = p_sys->ic->streams[i]->codec;
194         es_out_id_t  *es;
195         es_format_t  fmt;
196         vlc_fourcc_t fcc;
197
198         if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
199         {
200             fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
201
202             /* Special case for raw video data */
203             if( cc->codec_id == CODEC_ID_RAWVIDEO )
204             {
205                 msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt );
206                 fcc = E_(GetVlcChroma)( cc->pix_fmt );
207             }
208         }
209
210         switch( cc->codec_type )
211         {
212         case CODEC_TYPE_AUDIO:
213             es_format_Init( &fmt, AUDIO_ES, fcc );
214             fmt.audio.i_channels = cc->channels;
215             fmt.audio.i_rate = cc->sample_rate;
216             fmt.audio.i_bitspersample = cc->bits_per_sample;
217             fmt.audio.i_blockalign = cc->block_align;
218             break;
219         case CODEC_TYPE_VIDEO:
220             es_format_Init( &fmt, VIDEO_ES, fcc );
221             fmt.video.i_width = cc->width;
222             fmt.video.i_height = cc->height;
223             if( cc->palctrl )
224             {
225                 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
226                 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
227             }
228             break;
229         case CODEC_TYPE_SUBTITLE:
230             es_format_Init( &fmt, SPU_ES, fcc );
231             break;
232         default:
233             msg_Warn( p_demux, "unsupported track type in ffmpeg demux" );
234             break;
235         }
236
237         fmt.psz_language = strdup( p_sys->ic->streams[i]->language );
238         fmt.i_extra = cc->extradata_size;
239         fmt.p_extra = cc->extradata;
240         es = es_out_Add( p_demux->out, &fmt );
241
242         msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
243                  cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
244                  (char*)&fcc );
245         TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
246     }
247
248     msg_Dbg( p_demux, "AVFormat supported stream" );
249     msg_Dbg( p_demux, "    - format = %s (%s)",
250              p_sys->fmt->name, p_sys->fmt->long_name );
251     msg_Dbg( p_demux, "    - start time = "I64Fd,
252              ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
253              p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
254     msg_Dbg( p_demux, "    - duration = "I64Fd,
255              ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
256              p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
257
258     return VLC_SUCCESS;
259 }
260
261 /*****************************************************************************
262  * Close
263  *****************************************************************************/
264 void E_(CloseDemux)( vlc_object_t *p_this )
265 {
266     demux_t     *p_demux = (demux_t*)p_this;
267     demux_sys_t *p_sys = p_demux->p_sys;
268     vlc_bool_t b_avfmt_nofile;
269
270     FREENULL( p_sys->tk );
271
272     b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE;
273     p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
274     if( p_sys->ic ) av_close_input_file( p_sys->ic );
275     if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
276
277     if( p_sys->io_buffer ) free( p_sys->io_buffer );
278     free( p_sys );
279 }
280
281 /*****************************************************************************
282  * Demux:
283  *****************************************************************************/
284 static int Demux( demux_t *p_demux )
285 {
286     demux_sys_t *p_sys = p_demux->p_sys;
287     AVPacket    pkt;
288     block_t     *p_frame;
289     int64_t     i_start_time;
290
291     /* Read a frame */
292     if( av_read_frame( p_sys->ic, &pkt ) )
293     {
294         return 0;
295     }
296     if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
297     {
298         av_free_packet( &pkt );
299         return 1;
300     }
301     if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
302     {
303         return 0;
304     }
305
306     memcpy( p_frame->p_buffer, pkt.data, pkt.size );
307
308     i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
309         p_sys->ic->start_time : 0;
310
311     p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ?
312         0 : (pkt.dts - i_start_time) * 1000000 *
313         p_sys->ic->streams[pkt.stream_index]->time_base.num /
314         p_sys->ic->streams[pkt.stream_index]->time_base.den;
315     p_frame->i_pts = ( pkt.pts == (int64_t)AV_NOPTS_VALUE ) ?
316         0 : (pkt.pts - i_start_time) * 1000000 *
317         p_sys->ic->streams[pkt.stream_index]->time_base.num /
318         p_sys->ic->streams[pkt.stream_index]->time_base.den;
319
320 #ifdef AVFORMAT_DEBUG
321     msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
322              pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
323 #endif
324
325     if( pkt.dts > 0  &&
326         ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
327     {
328         p_sys->i_pcr_tk = pkt.stream_index;
329         p_sys->i_pcr = p_frame->i_dts;
330
331         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
332     }
333
334     es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
335     av_free_packet( &pkt );
336     return 1;
337 }
338
339 /*****************************************************************************
340  * Control:
341  *****************************************************************************/
342 static int Control( demux_t *p_demux, int i_query, va_list args )
343 {
344     demux_sys_t *p_sys = p_demux->p_sys;
345     double f, *pf;
346     int64_t i64, *pi64;
347
348     switch( i_query )
349     {
350         case DEMUX_GET_POSITION:
351             pf = (double*) va_arg( args, double* ); *pf = 0.0;
352             i64 = stream_Size( p_demux->s );
353             if( i64 > 0 )
354             {
355                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
356             }
357
358             if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
359             {
360                 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
361             }
362
363             return VLC_SUCCESS;
364
365         case DEMUX_SET_POSITION:
366             f = (double) va_arg( args, double );
367             i64 = stream_Tell( p_demux->s );
368             if( p_sys->i_pcr > 0 )
369             {
370                 i64 = p_sys->ic->duration * f;
371                 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
372                     i64 += p_sys->ic->start_time;
373
374                 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
375
376                 /* If we have a duration, we prefer to seek by time
377                    but if we don't, or if the seek fails, try BYTE seeking */
378                 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
379                     (av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) )
380                 {
381                     int64_t i_size = stream_Size( p_demux->s );
382                     i64 = (int64_t)i_size * f;
383
384                     msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: "I64Fd, i64 );
385                     if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
386                         return VLC_EGENERIC;
387                 }
388                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
389                 p_sys->i_pcr = -1; /* Invalidate time display */
390             }
391             return VLC_SUCCESS;
392
393         case DEMUX_GET_LENGTH:
394             pi64 = (int64_t*)va_arg( args, int64_t * );
395             if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
396             {
397                 *pi64 = p_sys->ic->duration;
398             }
399             else *pi64 = 0;
400             return VLC_SUCCESS;
401
402         case DEMUX_GET_TIME:
403             pi64 = (int64_t*)va_arg( args, int64_t * );
404             *pi64 = p_sys->i_pcr;
405             return VLC_SUCCESS;
406
407         case DEMUX_SET_TIME:
408             i64 = (int64_t)va_arg( args, int64_t );
409             if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
410                 i64 += p_sys->ic->start_time;
411
412             msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
413
414             if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
415             {
416                 return VLC_EGENERIC;
417             }
418             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
419             p_sys->i_pcr = -1; /* Invalidate time display */
420             return VLC_SUCCESS;
421
422         case DEMUX_GET_META:
423         {
424             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
425
426             if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
427                 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
428                 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
429             {
430                 return VLC_EGENERIC;
431             }
432
433             if( p_sys->ic->title[0] )
434                 vlc_meta_SetTitle( p_meta, p_sys->ic->title );
435             if( p_sys->ic->author[0] )
436                 vlc_meta_SetArtist( p_meta, p_sys->ic->author );
437             if( p_sys->ic->copyright[0] )
438                 vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright );
439             if( p_sys->ic->comment[0] )
440                 vlc_meta_SetDescription( p_meta, p_sys->ic->comment );
441             if( p_sys->ic->genre[0] )
442                 vlc_meta_SetGenre( p_meta, p_sys->ic->genre );
443             return VLC_SUCCESS;
444         }
445
446         default:
447             return VLC_EGENERIC;
448     }
449 }
450
451 /*****************************************************************************
452  * I/O wrappers for libavformat
453  *****************************************************************************/
454 static int IORead( void *opaque, uint8_t *buf, int buf_size )
455 {
456     URLContext *p_url = opaque;
457     demux_t *p_demux = p_url->priv_data;
458     int i_ret = stream_Read( p_demux->s, buf, buf_size );
459     return i_ret ? i_ret : -1;
460 }
461
462 static offset_t IOSeek( void *opaque, offset_t offset, int whence )
463 {
464     URLContext *p_url = opaque;
465     demux_t *p_demux = p_url->priv_data;
466     int64_t i_absolute = (int64_t)offset;
467     int64_t i_size = stream_Size( p_demux->s );
468
469 #ifdef AVFORMAT_DEBUG
470     msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
471 #endif
472
473     switch( whence )
474     {
475         case SEEK_SET:
476             break;
477         case SEEK_CUR:
478             i_absolute = stream_Tell( p_demux->s ) + offset;
479             break;
480         case SEEK_END:
481             i_absolute = i_size + offset;
482             break;
483         default:
484             return -1;
485
486     }
487     if( i_absolute < 0 )
488         i_absolute = 0;
489     if( i_size && i_absolute > i_size )
490         i_absolute = i_size;
491
492     if( stream_Seek( p_demux->s, i_absolute ) )
493     {
494         return -1;
495     }
496
497     return stream_Tell( p_demux->s );
498 }
499
500 #endif /* HAVE_FFMPEG_AVFORMAT_H */