1 /*****************************************************************************
2 * mux.c: muxer using 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>
36 #include <libavformat/avformat.h>
39 #include "../../codec/avcodec/avcodec.h"
40 #include "../../codec/avcodec/avutil.h"
42 //#define AVFORMAT_DEBUG 1
44 static const char *const ppsz_mux_options[] = {
48 /*****************************************************************************
49 * mux_sys_t: mux descriptor
50 *****************************************************************************/
64 int64_t i_initial_dts;
67 /*****************************************************************************
69 *****************************************************************************/
70 static int Control ( sout_mux_t *, int, va_list );
71 static int AddStream( sout_mux_t *, sout_input_t * );
72 static int DelStream( sout_mux_t *, sout_input_t * );
73 static int Mux ( sout_mux_t * );
75 static int IOWrite( void *opaque, uint8_t *buf, int buf_size );
76 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
78 /*****************************************************************************
80 *****************************************************************************/
81 int OpenMux( vlc_object_t *p_this )
83 AVOutputFormat *file_oformat;
84 sout_mux_t *p_mux = (sout_mux_t*)p_this;
85 sout_mux_sys_t *p_sys;
86 AVFormatParameters params, *ap = ¶ms;
89 /* Should we call it only once ? */
91 av_log_set_callback( LibavutilCallback );
93 config_ChainParse( p_mux, "ffmpeg-", ppsz_mux_options, p_mux->p_cfg );
95 /* Find the requested muxer */
96 psz_mux = var_GetNonEmptyString( p_mux, "ffmpeg-mux" );
99 #if( LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT( 52, 45, 0 ) )
100 file_oformat = av_guess_format( psz_mux, NULL, NULL );
102 file_oformat = guess_format( psz_mux, NULL, NULL );
109 #if( LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT( 52, 45, 0 ) )
110 av_guess_format( NULL, p_mux->p_access->psz_path, NULL);
112 guess_format( NULL, p_mux->p_access->psz_path, NULL);
117 msg_Err( p_mux, "unable for find a suitable output format" );
121 /* Fill p_mux fields */
122 p_mux->pf_control = Control;
123 p_mux->pf_addstream = AddStream;
124 p_mux->pf_delstream = DelStream;
126 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
128 p_sys->oc = avformat_alloc_context();
129 p_sys->oc->oformat = file_oformat;
131 /* Create I/O wrapper */
132 p_sys->io_buffer_size = 32768; /* FIXME */
133 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
134 p_sys->url.priv_data = p_mux;
135 p_sys->url.prot = &p_sys->prot;
136 p_sys->url.prot->name = "VLC I/O wrapper";
137 p_sys->url.prot->url_open = 0;
138 p_sys->url.prot->url_read = 0;
139 p_sys->url.prot->url_write =
140 (int (*) (URLContext *, unsigned char *, int))IOWrite;
141 p_sys->url.prot->url_seek =
142 (int64_t (*) (URLContext *, int64_t, int))IOSeek;
143 p_sys->url.prot->url_close = 0;
144 p_sys->url.prot->next = 0;
145 init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
146 1, &p_sys->url, NULL, IOWrite, IOSeek );
148 memset( ap, 0, sizeof(*ap) );
149 if( av_set_parameters( p_sys->oc, ap ) < 0 )
151 msg_Err( p_mux, "invalid encoding parameters" );
152 av_free( p_sys->oc );
153 free( p_sys->io_buffer );
158 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;
176 if( !p_sys->b_write_header && av_write_trailer( p_sys->oc ) < 0 )
178 msg_Err( p_mux, "could not write trailer" );
181 #if( LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT( 52, 96, 0 ) )
182 avformat_free_context(p_sys->oc);
184 for( unsigned i = 0 ; i < p_sys->oc->nb_streams; i++ )
186 av_free( p_sys->oc->streams[i]->codec->extradata );
187 av_free( p_sys->oc->streams[i]->codec );
188 #if( LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT( 52, 81, 0 ) )
189 av_free( p_sys->oc->streams[i]->info );
191 av_free( p_sys->oc->streams[i] );
193 av_free( p_sys->oc->streams );
194 av_free( p_sys->oc );
197 free( p_sys->io_buffer );
201 /*****************************************************************************
203 *****************************************************************************/
204 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
206 sout_mux_sys_t *p_sys = p_mux->p_sys;
207 AVCodecContext *codec;
211 msg_Dbg( p_mux, "adding input" );
213 if( !GetFfmpegCodec( p_input->p_fmt->i_codec, 0, &i_codec_id, 0 ) )
215 msg_Dbg( p_mux, "couldn't find codec for fourcc '%4.4s'",
216 (char *)&p_input->p_fmt->i_codec );
220 p_input->p_sys = malloc( sizeof( int ) );
221 *((int *)p_input->p_sys) = p_sys->oc->nb_streams;
223 if( p_input->p_fmt->i_cat != VIDEO_ES && p_input->p_fmt->i_cat != AUDIO_ES)
225 msg_Warn( p_mux, "Unhandled ES category" );
229 stream = av_new_stream( p_sys->oc, p_sys->oc->nb_streams);
232 free( p_input->p_sys );
235 codec = stream->codec;
237 /* This is used by LibavutilCallback (avutil.h) to print messages */
238 codec->opaque = (void*)p_mux;
240 switch( p_input->p_fmt->i_cat )
243 codec->codec_type = AVMEDIA_TYPE_AUDIO;
244 codec->channels = p_input->p_fmt->audio.i_channels;
245 codec->sample_rate = p_input->p_fmt->audio.i_rate;
246 codec->time_base = (AVRational){1, codec->sample_rate};
247 codec->frame_size = p_input->p_fmt->audio.i_frame_length;
251 if( !p_input->p_fmt->video.i_frame_rate ||
252 !p_input->p_fmt->video.i_frame_rate_base )
254 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
255 p_input->p_fmt->video.i_frame_rate = 25;
256 p_input->p_fmt->video.i_frame_rate_base = 1;
258 codec->codec_type = AVMEDIA_TYPE_VIDEO;
259 codec->width = p_input->p_fmt->video.i_width;
260 codec->height = p_input->p_fmt->video.i_height;
261 av_reduce( &codec->sample_aspect_ratio.num,
262 &codec->sample_aspect_ratio.den,
263 p_input->p_fmt->video.i_sar_num,
264 p_input->p_fmt->video.i_sar_den, 1 << 30 /* something big */ );
265 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
266 stream->sample_aspect_ratio.num = codec->sample_aspect_ratio.num;
267 stream->sample_aspect_ratio.den = codec->sample_aspect_ratio.den;
269 codec->time_base.den = p_input->p_fmt->video.i_frame_rate;
270 codec->time_base.num = p_input->p_fmt->video.i_frame_rate_base;
275 codec->bit_rate = p_input->p_fmt->i_bitrate;
276 #if LIBAVFORMAT_VERSION_INT >= ((51<<16)+(8<<8)+0)
277 codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
278 if( !codec->codec_tag && i_codec_id == CODEC_ID_MP2 )
280 i_codec_id = CODEC_ID_MP3;
281 codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
284 # warning "WARNING!!!!!!!"
285 # warning "Using libavformat muxing with versions older than 51.8.0 (r7593) might produce broken files."
287 if( i_codec_id == CODEC_ID_MP2 )
288 i_codec_id = CODEC_ID_MP3;
289 codec->codec_tag = p_input->p_fmt->i_original_fourcc ?: p_input->p_fmt->i_codec;
291 codec->codec_id = i_codec_id;
293 if( p_input->p_fmt->i_extra )
295 codec->extradata_size = p_input->p_fmt->i_extra;
296 codec->extradata = av_malloc( p_input->p_fmt->i_extra );
297 memcpy( codec->extradata, p_input->p_fmt->p_extra,
298 p_input->p_fmt->i_extra );
304 /*****************************************************************************
306 *****************************************************************************/
307 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
309 msg_Dbg( p_mux, "removing input" );
310 free( p_input->p_sys );
314 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
316 sout_mux_sys_t *p_sys = p_mux->p_sys;
317 block_t *p_data = block_FifoGet( p_input->p_fifo );
318 int i_stream = *((int *)p_input->p_sys);
319 AVStream *p_stream = p_sys->oc->streams[i_stream];
322 memset( &pkt, 0, sizeof(AVPacket) );
324 av_init_packet(&pkt);
325 pkt.data = p_data->p_buffer;
326 pkt.size = p_data->i_buffer;
327 pkt.stream_index = i_stream;
329 if( p_data->i_flags & BLOCK_FLAG_TYPE_I ) pkt.flags |= AV_PKT_FLAG_KEY;
331 /* avformat expects pts/dts which start from 0 */
332 p_data->i_dts -= p_mux->p_sys->i_initial_dts;
333 p_data->i_pts -= p_mux->p_sys->i_initial_dts;
335 if( p_data->i_pts > 0 )
336 pkt.pts = p_data->i_pts * p_stream->time_base.den /
337 INT64_C(1000000) / p_stream->time_base.num;
338 if( p_data->i_dts > 0 )
339 pkt.dts = p_data->i_dts * p_stream->time_base.den /
340 INT64_C(1000000) / p_stream->time_base.num;
342 /* this is another hack to prevent libavformat from triggering the "non monotone timestamps" check in avformat/utils.c */
343 p_stream->cur_dts = ( p_data->i_dts * p_stream->time_base.den /
344 INT64_C(1000000) / p_stream->time_base.num ) - 1;
346 if( av_write_frame( p_sys->oc, &pkt ) < 0 )
348 msg_Err( p_mux, "could not write frame (pts: %"PRId64", dts: %"PRId64") "
349 "(pkt pts: %"PRId64", dts: %"PRId64")",
350 p_data->i_pts, p_data->i_dts, pkt.pts, pkt.dts );
351 block_Release( p_data );
355 block_Release( p_data );
359 /*****************************************************************************
360 * Mux: multiplex available data in input fifos
361 *****************************************************************************/
362 static int Mux( sout_mux_t *p_mux )
364 sout_mux_sys_t *p_sys = p_mux->p_sys;
366 if( p_sys->b_error ) return VLC_EGENERIC;
368 if( p_sys->b_write_header )
370 msg_Dbg( p_mux, "writing header" );
372 if( av_write_header( p_sys->oc ) < 0 )
374 msg_Err( p_mux, "could not write header" );
375 p_sys->b_write_header = false;
376 p_sys->b_error = true;
380 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
381 put_flush_packet( p_sys->oc->pb );
383 put_flush_packet( &p_sys->oc->pb );
385 p_sys->b_write_header = false;
392 int i_stream = sout_MuxGetStream( p_mux, 1, &i_dts );
396 if( !p_mux->p_sys->i_initial_dts )
397 p_mux->p_sys->i_initial_dts = i_dts;
399 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
405 /*****************************************************************************
407 *****************************************************************************/
408 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
414 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
415 pb_bool = (bool*)va_arg( args, bool * );
419 case MUX_GET_ADD_STREAM_WAIT:
420 pb_bool = (bool*)va_arg( args, bool * );
426 char **ppsz = (char**)va_arg( args, char ** );
427 *ppsz = strdup( p_mux->p_sys->oc->oformat->mime_type );
436 /*****************************************************************************
437 * I/O wrappers for libavformat
438 *****************************************************************************/
439 static int IOWrite( void *opaque, uint8_t *buf, int buf_size )
441 URLContext *p_url = opaque;
442 sout_mux_t *p_mux = p_url->priv_data;
445 #ifdef AVFORMAT_DEBUG
446 msg_Dbg( p_mux, "IOWrite %i bytes", buf_size );
449 block_t *p_buf = block_New( p_mux->p_sout, buf_size );
450 if( buf_size > 0 ) memcpy( p_buf->p_buffer, buf, buf_size );
452 if( p_mux->p_sys->b_write_header )
453 p_buf->i_flags |= BLOCK_FLAG_HEADER;
455 i_ret = sout_AccessOutWrite( p_mux->p_access, p_buf );
456 return i_ret ? i_ret : -1;
459 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
461 URLContext *p_url = opaque;
462 sout_mux_t *p_mux = p_url->priv_data;
464 #ifdef AVFORMAT_DEBUG
465 msg_Dbg( p_mux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
471 return sout_AccessOutSeek( p_mux->p_access, offset );