]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/mux.c
ffmpeg/mux.c: implement MUX_GET_MIME.
[vlc] / modules / codec / ffmpeg / mux.c
1 /*****************************************************************************
2  * mux.c: muxer using ffmpeg (libavformat).
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id: demux.c 8444 2004-08-17 08:21:07Z gbazin $
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc_block.h>
31 #include <vlc_sout.h>
32
33 /* ffmpeg header */
34 #ifdef HAVE_FFMPEG_AVFORMAT_H
35 #   include <ffmpeg/avformat.h>
36 #elif defined(HAVE_LIBAVFORMAT_TREE)
37 #   include <avformat.h>
38 #endif
39
40 #include "ffmpeg.h"
41
42 //#define AVFORMAT_DEBUG 1
43
44 /* Version checking */
45 #if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_TREE)
46
47 static const char *ppsz_mux_options[] = {
48     "mux", NULL
49 };
50
51 /*****************************************************************************
52  * mux_sys_t: mux descriptor
53  *****************************************************************************/
54 struct sout_mux_sys_t
55 {
56     ByteIOContext   io;
57     int             io_buffer_size;
58     uint8_t        *io_buffer;
59
60     AVFormatContext *oc;
61     URLContext     url;
62     URLProtocol    prot;
63
64     vlc_bool_t     b_write_header;
65     vlc_bool_t     b_error;
66
67     int64_t        i_initial_dts;
68 };
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static int Control  ( sout_mux_t *, int, va_list );
74 static int AddStream( sout_mux_t *, sout_input_t * );
75 static int DelStream( sout_mux_t *, sout_input_t * );
76 static int Mux      ( sout_mux_t * );
77
78 static int IOWrite( void *opaque, uint8_t *buf, int buf_size );
79 static offset_t IOSeek( void *opaque, offset_t offset, int whence );
80
81 /*****************************************************************************
82  * Open
83  *****************************************************************************/
84 int E_(OpenMux)( vlc_object_t *p_this )
85 {
86     AVOutputFormat *file_oformat;
87     sout_mux_t *p_mux = (sout_mux_t*)p_this;
88     sout_mux_sys_t *p_sys;
89     AVFormatParameters params, *ap = &params;
90     char *psz_mux;
91
92     /* Should we call it only once ? */
93     av_register_all();
94     av_log_set_callback( E_(LibavcodecCallback) );
95
96     config_ChainParse( p_mux, "ffmpeg-", ppsz_mux_options, p_mux->p_cfg );
97
98     /* Find the requested muxer */
99     psz_mux = var_GetNonEmptyString( p_mux, "ffmpeg-mux" );
100     if( psz_mux )
101     {
102         file_oformat = guess_format( psz_mux, NULL, NULL );
103     }
104     else
105     {
106         file_oformat =
107             guess_format(NULL, p_mux->p_access->psz_path, NULL);
108     }
109     if (!file_oformat)
110     {
111       msg_Err( p_mux, "unable for find a suitable output format" );
112       return VLC_EGENERIC;
113     }
114
115     /* Fill p_mux fields */
116     p_mux->pf_control   = Control;
117     p_mux->pf_addstream = AddStream;
118     p_mux->pf_delstream = DelStream;
119     p_mux->pf_mux       = Mux;
120     p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
121
122     p_sys->oc = av_alloc_format_context();
123     p_sys->oc->oformat = file_oformat;
124
125     /* Create I/O wrapper */
126     p_sys->io_buffer_size = 32768;  /* FIXME */
127     p_sys->io_buffer = malloc( p_sys->io_buffer_size );
128     p_sys->url.priv_data = p_mux;
129     p_sys->url.prot = &p_sys->prot;
130     p_sys->url.prot->name = "VLC I/O wrapper";
131     p_sys->url.prot->url_open = 0;
132     p_sys->url.prot->url_read = 0;
133     p_sys->url.prot->url_write =
134                     (int (*) (URLContext *, unsigned char *, int))IOWrite;
135     p_sys->url.prot->url_seek =
136                     (offset_t (*) (URLContext *, offset_t, int))IOSeek;
137     p_sys->url.prot->url_close = 0;
138     p_sys->url.prot->next = 0;
139     init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
140                    1, &p_sys->url, NULL, IOWrite, IOSeek );
141
142     memset( ap, 0, sizeof(*ap) );
143     if( av_set_parameters( p_sys->oc, ap ) < 0 )
144     {
145         msg_Err( p_mux, "invalid encoding parameters" );
146         av_free( p_sys->oc );
147         free( p_sys->io_buffer );
148         free( p_sys );
149         return VLC_EGENERIC;
150     }
151
152     p_sys->oc->pb = p_sys->io;
153     p_sys->oc->nb_streams = 0;
154
155     p_sys->b_write_header = VLC_TRUE;
156     p_sys->b_error = VLC_FALSE;
157     p_sys->i_initial_dts = 0;
158
159     return VLC_SUCCESS;
160 }
161
162 /*****************************************************************************
163  * Close
164  *****************************************************************************/
165 void E_(CloseMux)( vlc_object_t *p_this )
166 {
167     sout_mux_t *p_mux = (sout_mux_t*)p_this;
168     sout_mux_sys_t *p_sys = p_mux->p_sys;
169     int i;
170
171     if( av_write_trailer( p_sys->oc ) < 0 )
172     {
173         msg_Err( p_mux, "could not write trailer" );
174     }
175
176     for( i = 0 ; i < p_sys->oc->nb_streams; i++ )
177     {
178         if( p_sys->oc->streams[i]->codec->extradata )
179             av_free( p_sys->oc->streams[i]->codec->extradata );
180         av_free( p_sys->oc->streams[i]->codec );
181         av_free( p_sys->oc->streams[i] );
182     }
183     av_free( p_sys->oc );
184
185     free( p_sys->io_buffer );
186     free( p_sys );
187 }
188
189 /*****************************************************************************
190  * AddStream
191  *****************************************************************************/
192 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
193 {
194     sout_mux_sys_t *p_sys = p_mux->p_sys;
195     AVCodecContext *codec;
196     AVStream *stream;
197     int i_codec_id, i_aspect_num, i_aspect_den;
198
199     msg_Dbg( p_mux, "adding input" );
200
201     if( !E_(GetFfmpegCodec)( p_input->p_fmt->i_codec, 0, &i_codec_id, 0 ) )
202     {
203         msg_Dbg( p_mux, "couldn't find codec for fourcc '%4.4s'",
204                  (char *)&p_input->p_fmt->i_codec );
205         return VLC_EGENERIC;
206     }
207
208     p_input->p_sys = malloc( sizeof( int ) );
209     *((int *)p_input->p_sys) = p_sys->oc->nb_streams;
210
211     stream = av_new_stream( p_sys->oc, p_sys->oc->nb_streams);
212     if( !stream )
213     {
214         free( p_input->p_sys );
215         return VLC_EGENERIC;
216     }
217     codec = stream->codec;
218
219     /* This is used by LibavcodecCallback (ffmpeg.c) to print messages */
220     codec->opaque = (void*)p_mux;
221
222     switch( p_input->p_fmt->i_cat )
223     {
224     case AUDIO_ES:
225         codec->codec_type = CODEC_TYPE_AUDIO;
226         codec->channels = p_input->p_fmt->audio.i_channels;
227         codec->sample_rate = p_input->p_fmt->audio.i_rate;
228         codec->time_base = (AVRational){1, codec->sample_rate};
229         break;
230
231     case VIDEO_ES:
232         if( !p_input->p_fmt->video.i_frame_rate ||
233             !p_input->p_fmt->video.i_frame_rate_base )
234         {
235             msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
236             p_input->p_fmt->video.i_frame_rate = 25;
237             p_input->p_fmt->video.i_frame_rate_base = 1;
238         }
239         codec->codec_type = CODEC_TYPE_VIDEO;
240         codec->width = p_input->p_fmt->video.i_width;
241         codec->height = p_input->p_fmt->video.i_height;
242         av_reduce( &i_aspect_num, &i_aspect_den,
243                    p_input->p_fmt->video.i_aspect,
244                    VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
245         av_reduce( &codec->sample_aspect_ratio.num,
246                    &codec->sample_aspect_ratio.den,
247                    i_aspect_num * (int64_t)codec->height,
248                    i_aspect_den * (int64_t)codec->width, 1 << 30 );
249         codec->time_base.den = p_input->p_fmt->video.i_frame_rate;
250         codec->time_base.num = p_input->p_fmt->video.i_frame_rate_base;
251         break;
252     }
253
254     codec->bit_rate = p_input->p_fmt->i_bitrate;
255 #if LIBAVFORMAT_VERSION_INT >= ((51<<16)+(8<<8)+0)
256     codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
257     if( !codec->codec_tag && i_codec_id == CODEC_ID_MP2 )
258     {
259         i_codec_id = CODEC_ID_MP3;
260         codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
261     }
262 #else
263 #   warning "WARNING!!!!!!!"
264 #   warning "Using libavformat muxing with versions older than 51.8.0 (r7593) might produce broken files.
265     /* This is a hack */
266     if( i_codec_id == CODEC_ID_MP2 )
267         i_codec_id = CODEC_ID_MP3;
268     codec->codec_tag = p_input->p_fmt->i_codec;
269 #endif
270     codec->codec_id = i_codec_id;
271
272     if( p_input->p_fmt->i_extra )
273     {
274         codec->extradata_size = p_input->p_fmt->i_extra;
275         codec->extradata = av_malloc( p_input->p_fmt->i_extra );
276         memcpy( codec->extradata, p_input->p_fmt->p_extra,
277                 p_input->p_fmt->i_extra );
278     }
279
280     return VLC_SUCCESS;
281 }
282
283 /*****************************************************************************
284  * DelStream
285  *****************************************************************************/
286 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
287 {
288     msg_Dbg( p_mux, "removing input" );
289     free( p_input->p_sys );
290     return VLC_SUCCESS;
291 }
292
293 /*
294  * TODO  move this function to src/stream_output.c (used by nearly all muxers)
295  */
296 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
297 {
298     mtime_t i_dts;
299     int     i_stream, i;
300
301     for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
302     {
303         block_fifo_t  *p_fifo;
304
305         p_fifo = p_mux->pp_inputs[i]->p_fifo;
306
307         /* We don't really need to have anything in the SPU fifo */
308         if( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
309             p_fifo->i_depth == 0 ) continue;
310
311         if( p_fifo->i_depth )
312         {
313             block_t *p_buf;
314
315             p_buf = block_FifoShow( p_fifo );
316             if( i_stream < 0 || p_buf->i_dts < i_dts )
317             {
318                 i_dts = p_buf->i_dts;
319                 i_stream = i;
320             }
321         }
322         else return -1;
323
324     }
325     if( pi_stream ) *pi_stream = i_stream;
326     if( pi_dts ) *pi_dts = i_dts;
327     if( !p_mux->p_sys->i_initial_dts ) p_mux->p_sys->i_initial_dts = i_dts;
328     return i_stream;
329 }
330
331 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
332 {
333     sout_mux_sys_t *p_sys = p_mux->p_sys;
334     block_t *p_data = block_FifoGet( p_input->p_fifo );
335     int i_stream = *((int *)p_input->p_sys);
336     AVStream *p_stream = p_sys->oc->streams[i_stream];
337     AVPacket pkt;
338
339     memset( &pkt, 0, sizeof(AVPacket) );
340
341     av_init_packet(&pkt);
342     pkt.data = p_data->p_buffer;
343     pkt.size = p_data->i_buffer;
344     pkt.stream_index = i_stream;
345
346     if( p_data->i_flags & BLOCK_FLAG_TYPE_I ) pkt.flags |= PKT_FLAG_KEY;
347
348     /* avformat expects pts/dts which start from 0 */
349     p_data->i_dts -= p_mux->p_sys->i_initial_dts;
350     p_data->i_pts -= p_mux->p_sys->i_initial_dts;
351
352     if( p_data->i_pts > 0 )
353         pkt.pts = p_data->i_pts * p_stream->time_base.den /
354             I64C(1000000) / p_stream->time_base.num;
355     if( p_data->i_dts > 0 )
356         pkt.dts = p_data->i_dts * p_stream->time_base.den /
357             I64C(1000000) / p_stream->time_base.num;
358
359     /* this is another hack to prevent libavformat from triggering the "non monotone timestamps" check in avformat/utils.c */
360     p_stream->cur_dts = AV_NOPTS_VALUE;
361
362     if( av_write_frame( p_sys->oc, &pkt ) < 0 )
363     {
364         msg_Err( p_mux, "could not write frame (pts: "I64Fd", dts: "I64Fd") "
365                  "(pkt pts: "I64Fd", dts: "I64Fd")",
366                  p_data->i_pts, p_data->i_dts, pkt.pts, pkt.dts );
367         block_Release( p_data );
368         return VLC_EGENERIC;
369     }
370
371     block_Release( p_data );
372     return VLC_SUCCESS;
373 }
374
375 /*****************************************************************************
376  * Mux: multiplex available data in input fifos
377  *****************************************************************************/
378 static int Mux( sout_mux_t *p_mux )
379 {
380     sout_mux_sys_t *p_sys = p_mux->p_sys;
381     int i_stream;
382
383     if( p_sys->b_error ) return VLC_EGENERIC;
384
385     if( p_sys->b_write_header )
386     {
387         msg_Dbg( p_mux, "writing header" );
388
389
390         if( av_write_header( p_sys->oc ) < 0 )
391         {
392             msg_Err( p_mux, "could not write header" );
393         p_sys->b_write_header = VLC_FALSE;
394             p_sys->b_error = VLC_TRUE;
395             return VLC_EGENERIC;
396         }
397
398         put_flush_packet( &p_sys->oc->pb );
399         p_sys->b_write_header = VLC_FALSE;
400     }
401
402     for( ;; )
403     {
404         if( MuxGetStream( p_mux, &i_stream, 0 ) < 0 ) return VLC_SUCCESS;
405         MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
406     }
407
408     return VLC_SUCCESS;
409 }
410
411 /*****************************************************************************
412  * Control:
413  *****************************************************************************/
414 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
415 {
416     vlc_bool_t *pb_bool;
417
418     switch( i_query )
419     {
420     case MUX_CAN_ADD_STREAM_WHILE_MUXING:
421         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
422         *pb_bool = VLC_FALSE;
423         return VLC_SUCCESS;
424
425     case MUX_GET_ADD_STREAM_WAIT:
426         pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
427         *pb_bool = VLC_TRUE;
428         return VLC_SUCCESS;
429
430     case MUX_GET_MIME:
431     {
432         char **ppsz = (char**)va_arg( args, char ** );
433         *ppsz = strdup( p_mux->p_sys->oc->oformat->mime_type );
434         return VLC_SUCCESS;
435     }
436
437     default:
438         return VLC_EGENERIC;
439     }
440 }
441
442 /*****************************************************************************
443  * I/O wrappers for libavformat
444  *****************************************************************************/
445 static int IOWrite( void *opaque, uint8_t *buf, int buf_size )
446 {
447     URLContext *p_url = opaque;
448     sout_mux_t *p_mux = p_url->priv_data;
449     int i_ret;
450
451 #ifdef AVFORMAT_DEBUG
452     msg_Dbg( p_mux, "IOWrite %i bytes", buf_size );
453 #endif
454
455     block_t *p_buf = block_New( p_mux->p_sout, buf_size );
456     if( buf_size > 0 ) memcpy( p_buf->p_buffer, buf, buf_size );
457
458     if( p_mux->p_sys->b_write_header )
459         p_buf->i_flags |= BLOCK_FLAG_HEADER;
460
461     i_ret = sout_AccessOutWrite( p_mux->p_access, p_buf );
462     return i_ret ? i_ret : -1;
463 }
464
465 static offset_t IOSeek( void *opaque, offset_t offset, int whence )
466 {
467     URLContext *p_url = opaque;
468     sout_mux_t *p_mux = p_url->priv_data;
469     int64_t i_absolute;
470
471 #ifdef AVFORMAT_DEBUG
472     msg_Dbg( p_mux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
473 #endif
474
475     switch( whence )
476     {
477     case SEEK_SET:
478         i_absolute = offset;
479         break;
480     case SEEK_CUR:
481     case SEEK_END:
482     default:
483         return -1;
484     }
485
486     if( sout_AccessOutSeek( p_mux->p_access, i_absolute ) )
487     {
488         return -1;
489     }
490
491     return 0;
492 }
493
494 #else /* HAVE_FFMPEG_AVFORMAT_H */
495
496 int E_(OpenMux)( vlc_object_t *p_this )
497 {
498     return VLC_EGENERIC;
499 }
500
501 void E_(CloseMux)( vlc_object_t *p_this )
502 {
503 }
504
505 #endif /* HAVE_FFMPEG_AVFORMAT_H */