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