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