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