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