1 /*****************************************************************************
2 * mux.c: muxer using ffmpeg (libavformat).
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_block.h>
37 #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
38 # include <libavformat/avformat.h>
39 #elif defined(HAVE_FFMPEG_AVFORMAT_H)
40 # include <ffmpeg/avformat.h>
44 #include "../../codec/avcodec/avcodec.h"
45 #include "../../codec/avcodec/avutil.h"
47 //#define AVFORMAT_DEBUG 1
49 static const char *const ppsz_mux_options[] = {
53 /*****************************************************************************
54 * mux_sys_t: mux descriptor
55 *****************************************************************************/
69 int64_t i_initial_dts;
72 /*****************************************************************************
74 *****************************************************************************/
75 static int Control ( sout_mux_t *, int, va_list );
76 static int AddStream( sout_mux_t *, sout_input_t * );
77 static int DelStream( sout_mux_t *, sout_input_t * );
78 static int Mux ( sout_mux_t * );
80 static int IOWrite( void *opaque, uint8_t *buf, int buf_size );
81 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
83 /*****************************************************************************
85 *****************************************************************************/
86 int OpenMux( vlc_object_t *p_this )
88 AVOutputFormat *file_oformat;
89 sout_mux_t *p_mux = (sout_mux_t*)p_this;
90 sout_mux_sys_t *p_sys;
91 AVFormatParameters params, *ap = ¶ms;
94 /* Should we call it only once ? */
96 av_log_set_callback( LibavutilCallback );
98 config_ChainParse( p_mux, "ffmpeg-", ppsz_mux_options, p_mux->p_cfg );
100 /* Find the requested muxer */
101 psz_mux = var_GetNonEmptyString( p_mux, "ffmpeg-mux" );
104 file_oformat = guess_format( psz_mux, NULL, NULL );
109 guess_format(NULL, p_mux->p_access->psz_path, NULL);
113 msg_Err( p_mux, "unable for find a suitable output format" );
117 /* Fill p_mux fields */
118 p_mux->pf_control = Control;
119 p_mux->pf_addstream = AddStream;
120 p_mux->pf_delstream = DelStream;
122 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
124 p_sys->oc = av_alloc_format_context();
125 p_sys->oc->oformat = file_oformat;
127 /* Create I/O wrapper */
128 p_sys->io_buffer_size = 32768; /* FIXME */
129 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
130 p_sys->url.priv_data = p_mux;
131 p_sys->url.prot = &p_sys->prot;
132 p_sys->url.prot->name = "VLC I/O wrapper";
133 p_sys->url.prot->url_open = 0;
134 p_sys->url.prot->url_read = 0;
135 p_sys->url.prot->url_write =
136 (int (*) (URLContext *, unsigned char *, int))IOWrite;
137 p_sys->url.prot->url_seek =
138 (int64_t (*) (URLContext *, int64_t, int))IOSeek;
139 p_sys->url.prot->url_close = 0;
140 p_sys->url.prot->next = 0;
141 init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
142 1, &p_sys->url, NULL, IOWrite, IOSeek );
144 memset( ap, 0, sizeof(*ap) );
145 if( av_set_parameters( p_sys->oc, ap ) < 0 )
147 msg_Err( p_mux, "invalid encoding parameters" );
148 av_free( p_sys->oc );
149 free( p_sys->io_buffer );
154 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
155 p_sys->oc->pb = &p_sys->io;
157 p_sys->oc->pb = p_sys->io;
159 p_sys->oc->nb_streams = 0;
161 p_sys->b_write_header = true;
162 p_sys->b_error = false;
163 p_sys->i_initial_dts = 0;
168 /*****************************************************************************
170 *****************************************************************************/
171 void CloseMux( vlc_object_t *p_this )
173 sout_mux_t *p_mux = (sout_mux_t*)p_this;
174 sout_mux_sys_t *p_sys = p_mux->p_sys;
177 if( !p_sys->b_write_header && av_write_trailer( p_sys->oc ) < 0 )
179 msg_Err( p_mux, "could not write trailer" );
182 for( i = 0 ; i < p_sys->oc->nb_streams; i++ )
184 if( p_sys->oc->streams[i]->codec->extradata )
185 av_free( p_sys->oc->streams[i]->codec->extradata );
186 av_free( p_sys->oc->streams[i]->codec );
187 av_free( p_sys->oc->streams[i] );
189 av_free( p_sys->oc );
191 free( p_sys->io_buffer );
195 /*****************************************************************************
197 *****************************************************************************/
198 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
200 sout_mux_sys_t *p_sys = p_mux->p_sys;
201 AVCodecContext *codec;
205 msg_Dbg( p_mux, "adding input" );
207 if( !GetFfmpegCodec( p_input->p_fmt->i_codec, 0, &i_codec_id, 0 ) )
209 msg_Dbg( p_mux, "couldn't find codec for fourcc '%4.4s'",
210 (char *)&p_input->p_fmt->i_codec );
214 p_input->p_sys = malloc( sizeof( int ) );
215 *((int *)p_input->p_sys) = p_sys->oc->nb_streams;
217 stream = av_new_stream( p_sys->oc, p_sys->oc->nb_streams);
220 free( p_input->p_sys );
223 codec = stream->codec;
225 /* This is used by LibavutilCallback (avutil.h) to print messages */
226 codec->opaque = (void*)p_mux;
228 switch( p_input->p_fmt->i_cat )
231 codec->codec_type = CODEC_TYPE_AUDIO;
232 codec->channels = p_input->p_fmt->audio.i_channels;
233 codec->sample_rate = p_input->p_fmt->audio.i_rate;
234 codec->time_base = (AVRational){1, codec->sample_rate};
238 if( !p_input->p_fmt->video.i_frame_rate ||
239 !p_input->p_fmt->video.i_frame_rate_base )
241 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
242 p_input->p_fmt->video.i_frame_rate = 25;
243 p_input->p_fmt->video.i_frame_rate_base = 1;
245 codec->codec_type = CODEC_TYPE_VIDEO;
246 codec->width = p_input->p_fmt->video.i_width;
247 codec->height = p_input->p_fmt->video.i_height;
248 av_reduce( &codec->sample_aspect_ratio.num,
249 &codec->sample_aspect_ratio.den,
250 p_input->p_fmt->video.i_sar_num,
251 p_input->p_fmt->video.i_sar_den, 1 << 30 /* something big */ );
252 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
253 stream->sample_aspect_ratio.num = codec->sample_aspect_ratio.num;
254 stream->sample_aspect_ratio.den = codec->sample_aspect_ratio.den;
256 codec->time_base.den = p_input->p_fmt->video.i_frame_rate;
257 codec->time_base.num = p_input->p_fmt->video.i_frame_rate_base;
261 msg_Warn( p_mux, "Unhandled ES category" );
264 codec->bit_rate = p_input->p_fmt->i_bitrate;
265 #if LIBAVFORMAT_VERSION_INT >= ((51<<16)+(8<<8)+0)
266 codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
267 if( !codec->codec_tag && i_codec_id == CODEC_ID_MP2 )
269 i_codec_id = CODEC_ID_MP3;
270 codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
273 # warning "WARNING!!!!!!!"
274 # warning "Using libavformat muxing with versions older than 51.8.0 (r7593) might produce broken files."
276 if( i_codec_id == CODEC_ID_MP2 )
277 i_codec_id = CODEC_ID_MP3;
278 codec->codec_tag = p_input->p_fmt->i_original_fourcc ?: p_input->p_fmt->i_codec;
280 codec->codec_id = i_codec_id;
282 if( p_input->p_fmt->i_extra )
284 codec->extradata_size = p_input->p_fmt->i_extra;
285 codec->extradata = av_malloc( p_input->p_fmt->i_extra );
286 memcpy( codec->extradata, p_input->p_fmt->p_extra,
287 p_input->p_fmt->i_extra );
293 /*****************************************************************************
295 *****************************************************************************/
296 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
298 msg_Dbg( p_mux, "removing input" );
299 free( p_input->p_sys );
303 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
305 sout_mux_sys_t *p_sys = p_mux->p_sys;
306 block_t *p_data = block_FifoGet( p_input->p_fifo );
307 int i_stream = *((int *)p_input->p_sys);
308 AVStream *p_stream = p_sys->oc->streams[i_stream];
311 memset( &pkt, 0, sizeof(AVPacket) );
313 av_init_packet(&pkt);
314 pkt.data = p_data->p_buffer;
315 pkt.size = p_data->i_buffer;
316 pkt.stream_index = i_stream;
318 if( p_data->i_flags & BLOCK_FLAG_TYPE_I ) pkt.flags |= PKT_FLAG_KEY;
320 /* avformat expects pts/dts which start from 0 */
321 p_data->i_dts -= p_mux->p_sys->i_initial_dts;
322 p_data->i_pts -= p_mux->p_sys->i_initial_dts;
324 if( p_data->i_pts > 0 )
325 pkt.pts = p_data->i_pts * p_stream->time_base.den /
326 INT64_C(1000000) / p_stream->time_base.num;
327 if( p_data->i_dts > 0 )
328 pkt.dts = p_data->i_dts * p_stream->time_base.den /
329 INT64_C(1000000) / p_stream->time_base.num;
331 /* this is another hack to prevent libavformat from triggering the "non monotone timestamps" check in avformat/utils.c */
332 p_stream->cur_dts = ( p_data->i_dts * p_stream->time_base.den /
333 INT64_C(1000000) / p_stream->time_base.num ) - 1;
335 if( av_write_frame( p_sys->oc, &pkt ) < 0 )
337 msg_Err( p_mux, "could not write frame (pts: %"PRId64", dts: %"PRId64") "
338 "(pkt pts: %"PRId64", dts: %"PRId64")",
339 p_data->i_pts, p_data->i_dts, pkt.pts, pkt.dts );
340 block_Release( p_data );
344 block_Release( p_data );
348 /*****************************************************************************
349 * Mux: multiplex available data in input fifos
350 *****************************************************************************/
351 static int Mux( sout_mux_t *p_mux )
353 sout_mux_sys_t *p_sys = p_mux->p_sys;
355 if( p_sys->b_error ) return VLC_EGENERIC;
357 if( p_sys->b_write_header )
359 msg_Dbg( p_mux, "writing header" );
361 if( av_write_header( p_sys->oc ) < 0 )
363 msg_Err( p_mux, "could not write header" );
364 p_sys->b_write_header = false;
365 p_sys->b_error = true;
369 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
370 put_flush_packet( p_sys->oc->pb );
372 put_flush_packet( &p_sys->oc->pb );
374 p_sys->b_write_header = false;
381 int i_stream = sout_MuxGetStream( p_mux, 1, &i_dts );
385 if( !p_mux->p_sys->i_initial_dts )
386 p_mux->p_sys->i_initial_dts = i_dts;
388 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
394 /*****************************************************************************
396 *****************************************************************************/
397 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
403 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
404 pb_bool = (bool*)va_arg( args, bool * );
408 case MUX_GET_ADD_STREAM_WAIT:
409 pb_bool = (bool*)va_arg( args, bool * );
415 char **ppsz = (char**)va_arg( args, char ** );
416 *ppsz = strdup( p_mux->p_sys->oc->oformat->mime_type );
425 /*****************************************************************************
426 * I/O wrappers for libavformat
427 *****************************************************************************/
428 static int IOWrite( void *opaque, uint8_t *buf, int buf_size )
430 URLContext *p_url = opaque;
431 sout_mux_t *p_mux = p_url->priv_data;
434 #ifdef AVFORMAT_DEBUG
435 msg_Dbg( p_mux, "IOWrite %i bytes", buf_size );
438 block_t *p_buf = block_New( p_mux->p_sout, buf_size );
439 if( buf_size > 0 ) memcpy( p_buf->p_buffer, buf, buf_size );
441 if( p_mux->p_sys->b_write_header )
442 p_buf->i_flags |= BLOCK_FLAG_HEADER;
444 i_ret = sout_AccessOutWrite( p_mux->p_access, p_buf );
445 return i_ret ? i_ret : -1;
448 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
450 URLContext *p_url = opaque;
451 sout_mux_t *p_mux = p_url->priv_data;
453 #ifdef AVFORMAT_DEBUG
454 msg_Dbg( p_mux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
460 return sout_AccessOutSeek( p_mux->p_access, offset );