]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/demux.c
* modules/codec/ffmpeg/demux.c: compilation fix for ffmpeg cvs.
[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) av_seek_frame(a,b,c,AVSEEK_FLAG_BYTE)
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     /* Should we call it only once ? */
103     av_register_all();
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     msg_Dbg( p_demux, "detected format: %s", fmt->name );
126
127     /* Fill p_demux fields */
128     p_demux->pf_demux = Demux;
129     p_demux->pf_control = Control;
130     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
131     p_sys->fmt = fmt;
132     p_sys->i_tk = 0;
133     p_sys->tk = NULL;
134     p_sys->i_pcr_tk = -1;
135
136     /* Create I/O wrapper */
137     p_sys->io_buffer_size = 32768;  /* FIXME */
138     p_sys->io_buffer = malloc( p_sys->io_buffer_size );
139     p_sys->url.priv_data = p_demux;
140     p_sys->url.prot = &p_sys->prot;
141     p_sys->url.prot->name = "VLC I/O wrapper";
142     p_sys->url.prot->url_open = 0;
143     p_sys->url.prot->url_read =
144                     (int (*) (URLContext *, unsigned char *, int))IORead;
145     p_sys->url.prot->url_write = 0;
146     p_sys->url.prot->url_seek =
147                     (offset_t (*) (URLContext *, offset_t, int))IOSeek;
148     p_sys->url.prot->url_close = 0;
149     p_sys->url.prot->next = 0;
150     init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
151                    0, &p_sys->url, IORead, NULL, IOSeek );
152
153     p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
154
155     /* Open it */
156     if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path,
157                               p_sys->fmt, NULL ) )
158     {
159         msg_Err( p_demux, "av_open_input_stream failed" );
160         return VLC_EGENERIC;
161     }
162
163     if( av_find_stream_info( p_sys->ic ) )
164     {
165         msg_Err( p_demux, "av_find_stream_info failed" );
166         return VLC_EGENERIC;
167     }
168
169     for( i = 0; i < p_sys->ic->nb_streams; i++ )
170     {
171         AVCodecContext *cc = &p_sys->ic->streams[i]->codec;
172         es_out_id_t  *es;
173         es_format_t  fmt;
174         vlc_fourcc_t fcc;
175
176         if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
177             fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
178
179         switch( cc->codec_type )
180         {
181         case CODEC_TYPE_AUDIO:
182             es_format_Init( &fmt, AUDIO_ES, fcc );
183             fmt.audio.i_channels = cc->channels;
184             fmt.audio.i_rate = cc->sample_rate;
185             fmt.audio.i_bitspersample = cc->bits_per_sample;
186             fmt.audio.i_blockalign = cc->block_align;
187             break;
188         case CODEC_TYPE_VIDEO:
189             es_format_Init( &fmt, VIDEO_ES, fcc );
190             fmt.video.i_width = cc->width;
191             fmt.video.i_height = cc->height;
192             if( cc->palctrl )
193             {
194                 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
195                 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
196             }
197             break;
198         default:
199             break;
200         }
201
202         fmt.i_extra = cc->extradata_size;
203         fmt.p_extra = cc->extradata;
204         es = es_out_Add( p_demux->out, &fmt );
205
206         msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
207                  cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
208                  (char*)&fcc );
209         TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
210     }
211
212     msg_Dbg( p_demux, "AVFormat supported stream" );
213     msg_Dbg( p_demux, "    - format = %s (%s)",
214              p_sys->fmt->name, p_sys->fmt->long_name );
215     msg_Dbg( p_demux, "    - start time = "I64Fd,
216              ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
217              p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
218     msg_Dbg( p_demux, "    - duration = "I64Fd,
219              ( p_sys->ic->duration != AV_NOPTS_VALUE ) ?
220              p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
221
222     return VLC_SUCCESS;
223 }
224
225 /*****************************************************************************
226  * Close
227  *****************************************************************************/
228 void E_(CloseDemux)( vlc_object_t *p_this )
229 {
230     demux_t     *p_demux = (demux_t*)p_this;
231     demux_sys_t *p_sys = p_demux->p_sys;
232
233     av_close_input_file( p_sys->ic );
234
235     free( p_sys->io_buffer );
236     free( p_sys );
237 }
238
239 /*****************************************************************************
240  * Demux:
241  *****************************************************************************/
242 static int Demux( demux_t *p_demux )
243 {
244     demux_sys_t *p_sys = p_demux->p_sys;
245     AVPacket    pkt;
246     block_t     *p_frame;
247     int64_t     i_start_time;
248
249     /* Read a frame */
250     if( av_read_frame( p_sys->ic, &pkt ) )
251     {
252         return 0;
253     }
254     if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
255     {
256         av_free_packet( &pkt );
257         return 1;
258     }
259     if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
260     {
261         return 0;
262     }
263
264     memcpy( p_frame->p_buffer, pkt.data, pkt.size );
265
266     i_start_time = ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
267         p_sys->ic->start_time : 0;
268
269     p_frame->i_dts = ( pkt.dts == AV_NOPTS_VALUE ) ?
270         0 : (pkt.dts - i_start_time) * 1000000 / AV_TIME_BASE;
271     p_frame->i_pts = ( pkt.pts == AV_NOPTS_VALUE ) ?
272         0 : (pkt.pts - i_start_time) * 1000000 / AV_TIME_BASE;
273
274 #ifdef AVFORMAT_DEBUG
275     msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
276              pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
277 #endif
278
279     if( pkt.dts > 0  &&
280         ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
281     {    
282         p_sys->i_pcr_tk = pkt.stream_index;
283         p_sys->i_pcr = pkt.dts - i_start_time;
284
285         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
286     }
287
288     es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
289     av_free_packet( &pkt );
290     return 1;
291 }
292
293 /*****************************************************************************
294  * Control:
295  *****************************************************************************/
296 static int Control( demux_t *p_demux, int i_query, va_list args )
297 {
298     demux_sys_t *p_sys = p_demux->p_sys;
299     double f, *pf;
300     int64_t i64, *pi64;
301
302     switch( i_query )
303     {
304         case DEMUX_GET_POSITION:
305             pf = (double*) va_arg( args, double* );
306             i64 = stream_Size( p_demux->s );
307             if( i64 > 0 )
308             {
309                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
310             }
311             else
312             {
313                 *pf = 0.0;
314             }
315             return VLC_SUCCESS;
316
317         case DEMUX_SET_POSITION:
318             f = (double) va_arg( args, double );
319             i64 = stream_Tell( p_demux->s );
320             if( i64 && p_sys->i_pcr )
321             {
322                 int64_t i_size = stream_Size( p_demux->s );
323
324                 i64 = p_sys->i_pcr * i_size / i64 * f;
325                 if( p_sys->ic->start_time != AV_NOPTS_VALUE )
326                     i64 += p_sys->ic->start_time;
327
328                 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
329
330                 if( av_seek_frame( p_sys->ic, -1, i64 ) )
331                 {
332                     return VLC_EGENERIC;
333                 }
334                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
335                 p_sys->i_pcr = -1; /* Invalidate time display */
336             }
337             return VLC_SUCCESS;
338
339         case DEMUX_GET_TIME:
340             pi64 = (int64_t*)va_arg( args, int64_t * );
341             *pi64 = p_sys->i_pcr;
342             return VLC_SUCCESS;
343
344         case DEMUX_SET_TIME:
345             i64 = (int64_t)va_arg( args, int64_t );
346             if( p_sys->ic->start_time != AV_NOPTS_VALUE )
347                 i64 += p_sys->ic->start_time;
348
349             msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
350
351             if( av_seek_frame( p_sys->ic, -1, i64 ) < 0 )
352             {
353                 return VLC_EGENERIC;
354             }
355             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
356             p_sys->i_pcr = -1; /* Invalidate time display */
357             return VLC_SUCCESS;
358
359         case DEMUX_GET_META:
360         {
361             vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
362             vlc_meta_t *meta;
363
364             if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
365                 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
366                 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
367             {
368                 return VLC_EGENERIC;
369             }
370
371             *pp_meta = meta = vlc_meta_New();
372
373             if( p_sys->ic->title[0] )
374                 vlc_meta_Add( meta, VLC_META_TITLE, p_sys->ic->title );
375             if( p_sys->ic->author[0] )
376                 vlc_meta_Add( meta, VLC_META_AUTHOR, p_sys->ic->author );
377             if( p_sys->ic->copyright[0] )
378                 vlc_meta_Add( meta, VLC_META_COPYRIGHT, p_sys->ic->copyright );
379             if( p_sys->ic->comment[0] )
380                 vlc_meta_Add( meta, VLC_META_DESCRIPTION, p_sys->ic->comment );
381             if( p_sys->ic->genre[0] )
382                 vlc_meta_Add( meta, VLC_META_GENRE, p_sys->ic->genre );
383             return VLC_SUCCESS;
384         }
385
386         default:
387             return VLC_EGENERIC;
388     }
389 }
390
391 /*****************************************************************************
392  * I/O wrappers for libavformat
393  *****************************************************************************/
394 static int IORead( void *opaque, uint8_t *buf, int buf_size )
395 {
396     URLContext *p_url = opaque;
397     demux_t *p_demux = p_url->priv_data;
398     int i_ret = stream_Read( p_demux->s, buf, buf_size );
399     return i_ret ? i_ret : -1;
400 }
401
402 static int IOSeek( void *opaque, offset_t offset, int whence )
403 {
404     URLContext *p_url = opaque;
405     demux_t *p_demux = p_url->priv_data;
406     int64_t i_absolute;
407
408 #ifdef AVFORMAT_DEBUG
409     msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
410 #endif
411
412     switch( whence )
413     {
414         case SEEK_SET:
415             i_absolute = offset;
416             break;
417         case SEEK_CUR:
418             i_absolute = stream_Tell( p_demux->s ) + offset;
419             break;
420         case SEEK_END:
421             i_absolute = stream_Size( p_demux->s ) - offset;
422             break;
423         default:
424             return -1;
425
426     }
427
428     if( stream_Seek( p_demux->s, i_absolute ) )
429     {
430         return -1;
431     }
432
433     return 0;
434 }
435
436 #else /* LIBAVFORMAT_BUILD >= 4611 */
437
438 int E_(OpenDemux)( vlc_object_t *p_this )
439 {
440     return VLC_EGENERIC;
441 }
442
443 void E_(CloseDemux)( vlc_object_t *p_this )
444 {
445 }
446
447 #endif /* LIBAVFORMAT_BUILD >= 4611 */