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