]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/demux.c
* Merged trunk changes r9234:9235 to 0.8.1 branch.
[vlc] / modules / codec / ffmpeg / demux.c
1 /*****************************************************************************
2  * demux.c: demuxer using ffmpeg (libavformat).
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32 #include "vlc_meta.h"
33
34 /* ffmpeg header */
35 #ifdef HAVE_FFMPEG_AVCODEC_H
36 #   include <ffmpeg/avformat.h>
37 #else
38 #   include <avformat.h>
39 #endif
40
41 #include "ffmpeg.h"
42
43 //#define AVFORMAT_DEBUG 1
44
45 /* Version checking */
46 #if (LIBAVFORMAT_BUILD >= 4611) && defined(HAVE_LIBAVFORMAT)
47
48 #if LIBAVFORMAT_BUILD < 4619
49 #   define av_seek_frame(a,b,c,d) av_seek_frame(a,b,c)
50 #endif
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 int 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
94     /* Init Probe data */
95     pd.filename = p_demux->psz_path;
96     if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 ) ) <= 0 )
97     {
98         msg_Warn( p_demux, "cannot peek" );
99         return VLC_EGENERIC;
100     }
101
102     av_register_all(); /* Can be called several times */
103
104     /* Guess format */
105     if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
106     {
107         msg_Dbg( p_demux, "couldn't guess format" );
108         return VLC_EGENERIC;
109     }
110
111     /* Don't try to handle MPEG unless forced */
112     if( !p_demux->b_force &&
113         ( !strcmp( fmt->name, "mpeg" ) ||
114           !strcmp( fmt->name, "vcd" ) ||
115           !strcmp( fmt->name, "vob" ) ||
116           !strcmp( fmt->name, "mpegts" ) ||
117           /* libavformat's redirector won't work */
118           !strcmp( fmt->name, "redir" ) ||
119           !strcmp( fmt->name, "sdp" ) ) )
120     {
121         return VLC_EGENERIC;
122     }
123
124     /* Don't trigger false alarms on bin files */
125     if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
126     {
127         int i_len;
128
129         if( !p_demux->psz_path ) return VLC_EGENERIC;
130
131         i_len = strlen( p_demux->psz_path );
132         if( i_len < 4 ) return VLC_EGENERIC;
133
134         if( strcasecmp( &p_demux->psz_path[i_len - 4], ".str" ) &&
135             strcasecmp( &p_demux->psz_path[i_len - 4], ".xai" ) &&
136             strcasecmp( &p_demux->psz_path[i_len - 4], ".xa" ) )
137         {
138             return VLC_EGENERIC;
139         }
140     }
141
142     msg_Dbg( p_demux, "detected format: %s", fmt->name );
143
144     /* Fill p_demux fields */
145     p_demux->pf_demux = Demux;
146     p_demux->pf_control = Control;
147     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
148     p_sys->ic = 0;
149     p_sys->fmt = fmt;
150     p_sys->i_tk = 0;
151     p_sys->tk = NULL;
152     p_sys->i_pcr_tk = -1;
153     p_sys->i_pcr = -1;
154
155     /* Create I/O wrapper */
156     p_sys->io_buffer_size = 32768;  /* FIXME */
157     p_sys->io_buffer = malloc( p_sys->io_buffer_size );
158     p_sys->url.priv_data = p_demux;
159     p_sys->url.prot = &p_sys->prot;
160     p_sys->url.prot->name = "VLC I/O wrapper";
161     p_sys->url.prot->url_open = 0;
162     p_sys->url.prot->url_read =
163                     (int (*) (URLContext *, unsigned char *, int))IORead;
164     p_sys->url.prot->url_write = 0;
165     p_sys->url.prot->url_seek =
166                     (offset_t (*) (URLContext *, offset_t, int))IOSeek;
167     p_sys->url.prot->url_close = 0;
168     p_sys->url.prot->next = 0;
169     init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
170                    0, &p_sys->url, IORead, NULL, IOSeek );
171
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         E_(CloseDemux)( p_this );
180         return VLC_EGENERIC;
181     }
182
183     if( av_find_stream_info( p_sys->ic ) )
184     {
185         msg_Err( p_demux, "av_find_stream_info failed" );
186         E_(CloseDemux)( p_this );
187         return VLC_EGENERIC;
188     }
189
190     for( i = 0; i < p_sys->ic->nb_streams; i++ )
191     {
192         AVCodecContext *cc = &p_sys->ic->streams[i]->codec;
193         es_out_id_t  *es;
194         es_format_t  fmt;
195         vlc_fourcc_t fcc;
196
197         if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
198             fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
199
200         switch( cc->codec_type )
201         {
202         case CODEC_TYPE_AUDIO:
203             es_format_Init( &fmt, AUDIO_ES, fcc );
204             fmt.audio.i_channels = cc->channels;
205             fmt.audio.i_rate = cc->sample_rate;
206             fmt.audio.i_bitspersample = cc->bits_per_sample;
207             fmt.audio.i_blockalign = cc->block_align;
208             break;
209         case CODEC_TYPE_VIDEO:
210             es_format_Init( &fmt, VIDEO_ES, fcc );
211             fmt.video.i_width = cc->width;
212             fmt.video.i_height = cc->height;
213             if( cc->palctrl )
214             {
215                 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
216                 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
217             }
218             break;
219         default:
220             break;
221         }
222
223         fmt.i_extra = cc->extradata_size;
224         fmt.p_extra = cc->extradata;
225         es = es_out_Add( p_demux->out, &fmt );
226
227         msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
228                  cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
229                  (char*)&fcc );
230         TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
231     }
232
233     msg_Dbg( p_demux, "AVFormat supported stream" );
234     msg_Dbg( p_demux, "    - format = %s (%s)",
235              p_sys->fmt->name, p_sys->fmt->long_name );
236     msg_Dbg( p_demux, "    - start time = "I64Fd,
237              ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
238              p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
239     msg_Dbg( p_demux, "    - duration = "I64Fd,
240              ( p_sys->ic->duration != AV_NOPTS_VALUE ) ?
241              p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
242
243     return VLC_SUCCESS;
244 }
245
246 /*****************************************************************************
247  * Close
248  *****************************************************************************/
249 void E_(CloseDemux)( vlc_object_t *p_this )
250 {
251     demux_t     *p_demux = (demux_t*)p_this;
252     demux_sys_t *p_sys = p_demux->p_sys;
253
254     if( p_sys->ic ) av_close_input_file( p_sys->ic );
255     if( p_sys->io_buffer ) free( p_sys->io_buffer );
256     free( p_sys );
257 }
258
259 /*****************************************************************************
260  * Demux:
261  *****************************************************************************/
262 static int Demux( demux_t *p_demux )
263 {
264     demux_sys_t *p_sys = p_demux->p_sys;
265     AVPacket    pkt;
266     block_t     *p_frame;
267     int64_t     i_start_time;
268
269     /* Read a frame */
270     if( av_read_frame( p_sys->ic, &pkt ) )
271     {
272         return 0;
273     }
274     if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
275     {
276         av_free_packet( &pkt );
277         return 1;
278     }
279     if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
280     {
281         return 0;
282     }
283
284     memcpy( p_frame->p_buffer, pkt.data, pkt.size );
285
286     i_start_time = ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
287         p_sys->ic->start_time : 0;
288
289     p_frame->i_dts = ( pkt.dts == AV_NOPTS_VALUE ) ?
290         0 : (pkt.dts - i_start_time) * 1000000 / AV_TIME_BASE;
291     p_frame->i_pts = ( pkt.pts == AV_NOPTS_VALUE ) ?
292         0 : (pkt.pts - i_start_time) * 1000000 / AV_TIME_BASE;
293
294 #ifdef AVFORMAT_DEBUG
295     msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
296              pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
297 #endif
298
299     if( pkt.dts > 0  &&
300         ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
301     {    
302         p_sys->i_pcr_tk = pkt.stream_index;
303         p_sys->i_pcr = pkt.dts - i_start_time;
304
305         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
306     }
307
308     es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
309     av_free_packet( &pkt );
310     return 1;
311 }
312
313 /*****************************************************************************
314  * Control:
315  *****************************************************************************/
316 static int Control( demux_t *p_demux, int i_query, va_list args )
317 {
318     demux_sys_t *p_sys = p_demux->p_sys;
319     double f, *pf;
320     int64_t i64, *pi64;
321
322     switch( i_query )
323     {
324         case DEMUX_GET_POSITION:
325             pf = (double*) va_arg( args, double* ); *pf = 0.0;
326             i64 = stream_Size( p_demux->s );
327             if( i64 > 0 )
328             {
329                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
330             }
331
332             if( p_sys->ic->duration != AV_NOPTS_VALUE && p_sys->i_pcr > 0 )
333             {
334                 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
335             }
336
337             return VLC_SUCCESS;
338
339         case DEMUX_SET_POSITION:
340             f = (double) va_arg( args, double );
341             i64 = stream_Tell( p_demux->s );
342             if( i64 && p_sys->i_pcr > 0 )
343             {
344                 int64_t i_size = stream_Size( p_demux->s );
345
346                 i64 = p_sys->i_pcr * i_size / i64 * f;
347                 if( p_sys->ic->start_time != AV_NOPTS_VALUE )
348                     i64 += p_sys->ic->start_time;
349
350                 if( p_sys->ic->duration != AV_NOPTS_VALUE )
351                     i64 = p_sys->ic->duration * f;
352
353                 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
354
355                 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
356                 {
357                     return VLC_EGENERIC;
358                 }
359                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
360                 p_sys->i_pcr = -1; /* Invalidate time display */
361             }
362             return VLC_SUCCESS;
363
364         case DEMUX_GET_LENGTH:
365             pi64 = (int64_t*)va_arg( args, int64_t * );
366             if( p_sys->ic->duration != AV_NOPTS_VALUE )
367             {
368                 *pi64 = p_sys->ic->duration;
369             }
370             else *pi64 = 0;
371             return VLC_SUCCESS;
372
373         case DEMUX_GET_TIME:
374             pi64 = (int64_t*)va_arg( args, int64_t * );
375             *pi64 = p_sys->i_pcr;
376             return VLC_SUCCESS;
377
378         case DEMUX_SET_TIME:
379             i64 = (int64_t)va_arg( args, int64_t );
380             if( p_sys->ic->start_time != AV_NOPTS_VALUE )
381                 i64 += p_sys->ic->start_time;
382
383             msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
384
385             if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
386             {
387                 return VLC_EGENERIC;
388             }
389             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
390             p_sys->i_pcr = -1; /* Invalidate time display */
391             return VLC_SUCCESS;
392
393         case DEMUX_GET_META:
394         {
395             vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
396             vlc_meta_t *meta;
397
398             if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
399                 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
400                 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
401             {
402                 return VLC_EGENERIC;
403             }
404
405             *pp_meta = meta = vlc_meta_New();
406
407             if( p_sys->ic->title[0] )
408                 vlc_meta_Add( meta, VLC_META_TITLE, p_sys->ic->title );
409             if( p_sys->ic->author[0] )
410                 vlc_meta_Add( meta, VLC_META_AUTHOR, p_sys->ic->author );
411             if( p_sys->ic->copyright[0] )
412                 vlc_meta_Add( meta, VLC_META_COPYRIGHT, p_sys->ic->copyright );
413             if( p_sys->ic->comment[0] )
414                 vlc_meta_Add( meta, VLC_META_DESCRIPTION, p_sys->ic->comment );
415             if( p_sys->ic->genre[0] )
416                 vlc_meta_Add( meta, VLC_META_GENRE, p_sys->ic->genre );
417             return VLC_SUCCESS;
418         }
419
420         default:
421             return VLC_EGENERIC;
422     }
423 }
424
425 /*****************************************************************************
426  * I/O wrappers for libavformat
427  *****************************************************************************/
428 static int IORead( void *opaque, uint8_t *buf, int buf_size )
429 {
430     URLContext *p_url = opaque;
431     demux_t *p_demux = p_url->priv_data;
432     int i_ret = stream_Read( p_demux->s, buf, buf_size );
433     return i_ret ? i_ret : -1;
434 }
435
436 static int IOSeek( void *opaque, offset_t offset, int whence )
437 {
438     URLContext *p_url = opaque;
439     demux_t *p_demux = p_url->priv_data;
440     int64_t i_absolute;
441
442 #ifdef AVFORMAT_DEBUG
443     msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
444 #endif
445
446     switch( whence )
447     {
448         case SEEK_SET:
449             i_absolute = offset;
450             break;
451         case SEEK_CUR:
452             i_absolute = stream_Tell( p_demux->s ) + offset;
453             break;
454         case SEEK_END:
455             i_absolute = stream_Size( p_demux->s ) - offset;
456             break;
457         default:
458             return -1;
459
460     }
461
462     if( stream_Seek( p_demux->s, i_absolute ) )
463     {
464         return -1;
465     }
466
467     return 0;
468 }
469
470 #else /* LIBAVFORMAT_BUILD >= 4611 */
471
472 int E_(OpenDemux)( vlc_object_t *p_this )
473 {
474     return VLC_EGENERIC;
475 }
476
477 void E_(CloseDemux)( vlc_object_t *p_this )
478 {
479 }
480
481 #endif /* LIBAVFORMAT_BUILD >= 4611 */