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