]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/demux.c
* modules/codec/ffmpeg/demux.c: added raw video support.
[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., 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 >= 4629) && 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     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                 switch( cc->pix_fmt )
202                 {
203                 case PIX_FMT_YUV444P:
204                     fcc = VLC_FOURCC( 'I', '4', '4', '4' );
205                     break;
206                 case PIX_FMT_YUV422P:
207                     fcc = VLC_FOURCC( 'I', '4', '2', '2' );
208                     break;
209                 case PIX_FMT_YUV420P:
210                     fcc = VLC_FOURCC( 'I', '4', '2', '0' );
211                     break;
212                 case PIX_FMT_YUV411P:
213                     fcc = VLC_FOURCC( 'I', '4', '1', '1' );
214                     break;
215                 case PIX_FMT_YUV410P:
216                     fcc = VLC_FOURCC( 'I', '4', '1', '0' );
217                     break;
218
219                 case PIX_FMT_YUV422:
220                     fcc = VLC_FOURCC('Y','U','Y','2');
221                     break;
222
223                 case PIX_FMT_RGB555:
224                     fcc = VLC_FOURCC('R','V','1','5');
225                     break;
226                 case PIX_FMT_RGB565:
227                     fcc = VLC_FOURCC('R','V','1','6');
228                     break;
229                 case PIX_FMT_RGB24:
230                     fcc = VLC_FOURCC('R','V','2','4');
231                     break;
232                 case PIX_FMT_RGBA32:
233                     fcc = VLC_FOURCC('R','V','3','2');
234                     break;
235                 case PIX_FMT_GRAY8:
236                     fcc = VLC_FOURCC('G','R','E','Y');
237                     break;
238                 default:
239                     break;
240                 }
241             }
242         }
243
244         switch( cc->codec_type )
245         {
246         case CODEC_TYPE_AUDIO:
247             es_format_Init( &fmt, AUDIO_ES, fcc );
248             fmt.audio.i_channels = cc->channels;
249             fmt.audio.i_rate = cc->sample_rate;
250             fmt.audio.i_bitspersample = cc->bits_per_sample;
251             fmt.audio.i_blockalign = cc->block_align;
252             break;
253         case CODEC_TYPE_VIDEO:
254             es_format_Init( &fmt, VIDEO_ES, fcc );
255             fmt.video.i_width = cc->width;
256             fmt.video.i_height = cc->height;
257             if( cc->palctrl )
258             {
259                 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
260                 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
261             }
262             break;
263         default:
264             break;
265         }
266
267         fmt.i_extra = cc->extradata_size;
268         fmt.p_extra = cc->extradata;
269         es = es_out_Add( p_demux->out, &fmt );
270
271         msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
272                  cc->codec_type == CODEC_TYPE_AUDIO ? "audio" : "video",
273                  (char*)&fcc );
274         TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
275     }
276
277     msg_Dbg( p_demux, "AVFormat supported stream" );
278     msg_Dbg( p_demux, "    - format = %s (%s)",
279              p_sys->fmt->name, p_sys->fmt->long_name );
280     msg_Dbg( p_demux, "    - start time = "I64Fd,
281              ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
282              p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
283     msg_Dbg( p_demux, "    - duration = "I64Fd,
284              ( p_sys->ic->duration != AV_NOPTS_VALUE ) ?
285              p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
286
287     return VLC_SUCCESS;
288 }
289
290 /*****************************************************************************
291  * Close
292  *****************************************************************************/
293 void E_(CloseDemux)( vlc_object_t *p_this )
294 {
295     demux_t     *p_demux = (demux_t*)p_this;
296     demux_sys_t *p_sys = p_demux->p_sys;
297
298     if( p_sys->ic ) av_close_input_file( p_sys->ic );
299     if( p_sys->io_buffer ) free( p_sys->io_buffer );
300     free( p_sys );
301 }
302
303 /*****************************************************************************
304  * Demux:
305  *****************************************************************************/
306 static int Demux( demux_t *p_demux )
307 {
308     demux_sys_t *p_sys = p_demux->p_sys;
309     AVPacket    pkt;
310     block_t     *p_frame;
311     int64_t     i_start_time;
312
313     /* Read a frame */
314     if( av_read_frame( p_sys->ic, &pkt ) )
315     {
316         return 0;
317     }
318     if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
319     {
320         av_free_packet( &pkt );
321         return 1;
322     }
323     if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
324     {
325         return 0;
326     }
327
328     memcpy( p_frame->p_buffer, pkt.data, pkt.size );
329
330     i_start_time = ( p_sys->ic->start_time != AV_NOPTS_VALUE ) ?
331         p_sys->ic->start_time : 0;
332
333     p_frame->i_dts = ( pkt.dts == AV_NOPTS_VALUE ) ?
334         0 : (pkt.dts - i_start_time) * 1000000 *
335         p_sys->ic->streams[pkt.stream_index]->time_base.num /
336         p_sys->ic->streams[pkt.stream_index]->time_base.den;
337     p_frame->i_pts = ( pkt.pts == AV_NOPTS_VALUE ) ?
338         0 : (pkt.pts - i_start_time) * 1000000 *
339         p_sys->ic->streams[pkt.stream_index]->time_base.num /
340         p_sys->ic->streams[pkt.stream_index]->time_base.den;
341
342 #ifdef AVFORMAT_DEBUG
343     msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
344              pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
345 #endif
346
347     if( pkt.dts > 0  &&
348         ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
349     {    
350         p_sys->i_pcr_tk = pkt.stream_index;
351         p_sys->i_pcr = p_frame->i_dts;
352
353         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
354     }
355
356     es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
357     av_free_packet( &pkt );
358     return 1;
359 }
360
361 /*****************************************************************************
362  * Control:
363  *****************************************************************************/
364 static int Control( demux_t *p_demux, int i_query, va_list args )
365 {
366     demux_sys_t *p_sys = p_demux->p_sys;
367     double f, *pf;
368     int64_t i64, *pi64;
369
370     switch( i_query )
371     {
372         case DEMUX_GET_POSITION:
373             pf = (double*) va_arg( args, double* ); *pf = 0.0;
374             i64 = stream_Size( p_demux->s );
375             if( i64 > 0 )
376             {
377                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
378             }
379
380             if( p_sys->ic->duration != AV_NOPTS_VALUE && p_sys->i_pcr > 0 )
381             {
382                 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
383             }
384
385             return VLC_SUCCESS;
386
387         case DEMUX_SET_POSITION:
388             f = (double) va_arg( args, double );
389             i64 = stream_Tell( p_demux->s );
390             if( i64 && p_sys->i_pcr > 0 )
391             {
392                 int64_t i_size = stream_Size( p_demux->s );
393
394                 i64 = p_sys->i_pcr * i_size / i64 * f;
395                 if( p_sys->ic->start_time != AV_NOPTS_VALUE )
396                     i64 += p_sys->ic->start_time;
397
398                 if( p_sys->ic->duration != AV_NOPTS_VALUE )
399                     i64 = p_sys->ic->duration * f;
400
401                 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
402
403                 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
404                 {
405                     return VLC_EGENERIC;
406                 }
407                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
408                 p_sys->i_pcr = -1; /* Invalidate time display */
409             }
410             return VLC_SUCCESS;
411
412         case DEMUX_GET_LENGTH:
413             pi64 = (int64_t*)va_arg( args, int64_t * );
414             if( p_sys->ic->duration != AV_NOPTS_VALUE )
415             {
416                 *pi64 = p_sys->ic->duration;
417             }
418             else *pi64 = 0;
419             return VLC_SUCCESS;
420
421         case DEMUX_GET_TIME:
422             pi64 = (int64_t*)va_arg( args, int64_t * );
423             *pi64 = p_sys->i_pcr;
424             return VLC_SUCCESS;
425
426         case DEMUX_SET_TIME:
427             i64 = (int64_t)va_arg( args, int64_t );
428             if( p_sys->ic->start_time != AV_NOPTS_VALUE )
429                 i64 += p_sys->ic->start_time;
430
431             msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
432
433             if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
434             {
435                 return VLC_EGENERIC;
436             }
437             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
438             p_sys->i_pcr = -1; /* Invalidate time display */
439             return VLC_SUCCESS;
440
441         case DEMUX_GET_META:
442         {
443             vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
444             vlc_meta_t *meta;
445
446             if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
447                 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
448                 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
449             {
450                 return VLC_EGENERIC;
451             }
452
453             *pp_meta = meta = vlc_meta_New();
454
455             if( p_sys->ic->title[0] )
456                 vlc_meta_Add( meta, VLC_META_TITLE, p_sys->ic->title );
457             if( p_sys->ic->author[0] )
458                 vlc_meta_Add( meta, VLC_META_AUTHOR, p_sys->ic->author );
459             if( p_sys->ic->copyright[0] )
460                 vlc_meta_Add( meta, VLC_META_COPYRIGHT, p_sys->ic->copyright );
461             if( p_sys->ic->comment[0] )
462                 vlc_meta_Add( meta, VLC_META_DESCRIPTION, p_sys->ic->comment );
463             if( p_sys->ic->genre[0] )
464                 vlc_meta_Add( meta, VLC_META_GENRE, p_sys->ic->genre );
465             return VLC_SUCCESS;
466         }
467
468         default:
469             return VLC_EGENERIC;
470     }
471 }
472
473 /*****************************************************************************
474  * I/O wrappers for libavformat
475  *****************************************************************************/
476 static int IORead( void *opaque, uint8_t *buf, int buf_size )
477 {
478     URLContext *p_url = opaque;
479     demux_t *p_demux = p_url->priv_data;
480     int i_ret = stream_Read( p_demux->s, buf, buf_size );
481     return i_ret ? i_ret : -1;
482 }
483
484 static int IOSeek( void *opaque, offset_t offset, int whence )
485 {
486     URLContext *p_url = opaque;
487     demux_t *p_demux = p_url->priv_data;
488     int64_t i_absolute;
489
490 #ifdef AVFORMAT_DEBUG
491     msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
492 #endif
493
494     switch( whence )
495     {
496         case SEEK_SET:
497             i_absolute = offset;
498             break;
499         case SEEK_CUR:
500             i_absolute = stream_Tell( p_demux->s ) + offset;
501             break;
502         case SEEK_END:
503             i_absolute = stream_Size( p_demux->s ) - offset;
504             break;
505         default:
506             return -1;
507
508     }
509
510     if( stream_Seek( p_demux->s, i_absolute ) )
511     {
512         return -1;
513     }
514
515     return 0;
516 }
517
518 #else /* LIBAVFORMAT_BUILD >= 4611 */
519
520 int E_(OpenDemux)( vlc_object_t *p_this )
521 {
522     return VLC_EGENERIC;
523 }
524
525 void E_(CloseDemux)( vlc_object_t *p_this )
526 {
527 }
528
529 #endif /* LIBAVFORMAT_BUILD >= 4629 */