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