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