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