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/avcommon.h"
42 /* Support for deprecated APIs */
43 #if LIBAVFORMAT_VERSION_INT < ((52<<16)+(105<<8)+0)
44 # define avio_flush put_flush_packet
47 //#define AVFORMAT_DEBUG 1
49 static const char *const ppsz_mux_options[] = {
50 "mux", "options", NULL
53 /*****************************************************************************
54 * mux_sys_t: mux descriptor
55 *****************************************************************************/
58 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(105<<8)+0)
71 int64_t i_initial_dts;
74 /*****************************************************************************
76 *****************************************************************************/
77 static int Control ( sout_mux_t *, int, va_list );
78 static int AddStream( sout_mux_t *, sout_input_t * );
79 static int DelStream( sout_mux_t *, sout_input_t * );
80 static int Mux ( sout_mux_t * );
82 static int IOWrite( void *opaque, uint8_t *buf, int buf_size );
83 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
85 /*****************************************************************************
87 *****************************************************************************/
88 int OpenMux( vlc_object_t *p_this )
90 AVOutputFormat *file_oformat;
91 sout_mux_t *p_mux = (sout_mux_t*)p_this;
92 sout_mux_sys_t *p_sys;
97 config_ChainParse( p_mux, "sout-avformat-", ppsz_mux_options, p_mux->p_cfg );
99 /* Find the requested muxer */
100 psz_mux = var_GetNonEmptyString( p_mux, "sout-avformat-mux" );
103 #if( LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT( 52, 45, 0 ) )
104 file_oformat = av_guess_format( psz_mux, NULL, NULL );
106 file_oformat = guess_format( psz_mux, NULL, NULL );
113 #if( LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT( 52, 45, 0 ) )
114 av_guess_format( NULL, p_mux->p_access->psz_path, NULL);
116 guess_format( NULL, p_mux->p_access->psz_path, NULL);
121 msg_Err( p_mux, "unable for find a suitable output format" );
125 /* Fill p_mux fields */
126 p_mux->pf_control = Control;
127 p_mux->pf_addstream = AddStream;
128 p_mux->pf_delstream = DelStream;
130 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
132 p_sys->oc = avformat_alloc_context();
133 p_sys->oc->oformat = file_oformat;
135 /* Create I/O wrapper */
136 p_sys->io_buffer_size = 32768; /* FIXME */
137 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
139 #if (LIBAVFORMAT_VERSION_INT >= ((52<<16)+(105<<8)+0))
140 p_sys->io = avio_alloc_context(
142 init_put_byte( &p_sys->io,
144 p_sys->io_buffer, p_sys->io_buffer_size,
145 1, p_mux, NULL, IOWrite, IOSeek );
148 #if (LIBAVFORMAT_VERSION_INT < ((52<<16)+(105<<8)+0))
149 AVFormatParameters params, *ap = ¶ms;
150 memset( ap, 0, sizeof(*ap) );
151 if( av_set_parameters( p_sys->oc, ap ) < 0 )
153 msg_Err( p_mux, "invalid encoding parameters" );
154 av_free( p_sys->oc );
155 free( p_sys->io_buffer );
161 #if (LIBAVFORMAT_VERSION_INT >= ((52<<16)+(105<<8)+0))
162 p_sys->oc->pb = p_sys->io;
164 p_sys->oc->pb = &p_sys->io;
166 p_sys->oc->nb_streams = 0;
168 p_sys->b_write_header = true;
169 p_sys->b_error = false;
170 p_sys->i_initial_dts = 0;
175 /*****************************************************************************
177 *****************************************************************************/
178 void CloseMux( vlc_object_t *p_this )
180 sout_mux_t *p_mux = (sout_mux_t*)p_this;
181 sout_mux_sys_t *p_sys = p_mux->p_sys;
183 if( !p_sys->b_write_header && !p_sys->b_error && av_write_trailer( p_sys->oc ) < 0 )
185 msg_Err( p_mux, "could not write trailer" );
188 #if( LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT( 52, 96, 0 ) )
189 avformat_free_context(p_sys->oc);
191 for( unsigned i = 0 ; i < p_sys->oc->nb_streams; i++ )
193 av_free( p_sys->oc->streams[i]->codec->extradata );
194 av_free( p_sys->oc->streams[i]->codec );
195 #if( LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT( 52, 81, 0 ) )
196 av_free( p_sys->oc->streams[i]->info );
198 av_free( p_sys->oc->streams[i] );
200 av_free( p_sys->oc->streams );
201 av_free( p_sys->oc );
204 free( p_sys->io_buffer );
208 /*****************************************************************************
210 *****************************************************************************/
211 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
213 sout_mux_sys_t *p_sys = p_mux->p_sys;
214 AVCodecContext *codec;
218 msg_Dbg( p_mux, "adding input" );
220 if( !GetFfmpegCodec( p_input->p_fmt->i_codec, 0, &i_codec_id, 0 ) )
222 msg_Dbg( p_mux, "couldn't find codec for fourcc '%4.4s'",
223 (char *)&p_input->p_fmt->i_codec );
227 p_input->p_sys = malloc( sizeof( int ) );
228 *((int *)p_input->p_sys) = p_sys->oc->nb_streams;
230 if( p_input->p_fmt->i_cat != VIDEO_ES && p_input->p_fmt->i_cat != AUDIO_ES)
232 msg_Warn( p_mux, "Unhandled ES category" );
236 #if (LIBAVFORMAT_VERSION_INT >= ((53<<16)+(10<<8)+0))
237 stream = avformat_new_stream( p_sys->oc, NULL);
239 stream = av_new_stream( p_sys->oc, p_sys->oc->nb_streams);
243 free( p_input->p_sys );
246 codec = stream->codec;
248 codec->opaque = p_mux;
250 switch( p_input->p_fmt->i_cat )
253 codec->codec_type = AVMEDIA_TYPE_AUDIO;
254 codec->channels = p_input->p_fmt->audio.i_channels;
255 codec->sample_rate = p_input->p_fmt->audio.i_rate;
256 codec->time_base = (AVRational){1, codec->sample_rate};
257 codec->frame_size = p_input->p_fmt->audio.i_frame_length;
261 if( !p_input->p_fmt->video.i_frame_rate ||
262 !p_input->p_fmt->video.i_frame_rate_base )
264 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
265 p_input->p_fmt->video.i_frame_rate = 25;
266 p_input->p_fmt->video.i_frame_rate_base = 1;
268 codec->codec_type = AVMEDIA_TYPE_VIDEO;
269 codec->width = p_input->p_fmt->video.i_width;
270 codec->height = p_input->p_fmt->video.i_height;
271 av_reduce( &codec->sample_aspect_ratio.num,
272 &codec->sample_aspect_ratio.den,
273 p_input->p_fmt->video.i_sar_num,
274 p_input->p_fmt->video.i_sar_den, 1 << 30 /* something big */ );
275 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
276 stream->sample_aspect_ratio.num = codec->sample_aspect_ratio.num;
277 stream->sample_aspect_ratio.den = codec->sample_aspect_ratio.den;
279 codec->time_base.den = p_input->p_fmt->video.i_frame_rate;
280 codec->time_base.num = p_input->p_fmt->video.i_frame_rate_base;
285 codec->bit_rate = p_input->p_fmt->i_bitrate;
286 #if LIBAVFORMAT_VERSION_INT >= ((51<<16)+(8<<8)+0)
287 codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
288 if( !codec->codec_tag && i_codec_id == CODEC_ID_MP2 )
290 i_codec_id = CODEC_ID_MP3;
291 codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
294 # warning "WARNING!!!!!!!"
295 # warning "Using libavformat muxing with versions older than 51.8.0 (r7593) might produce broken files."
297 if( i_codec_id == CODEC_ID_MP2 )
298 i_codec_id = CODEC_ID_MP3;
299 codec->codec_tag = p_input->p_fmt->i_original_fourcc ?: p_input->p_fmt->i_codec;
301 codec->codec_id = i_codec_id;
303 if( p_input->p_fmt->i_extra )
305 codec->extradata_size = p_input->p_fmt->i_extra;
306 codec->extradata = av_malloc( p_input->p_fmt->i_extra );
307 memcpy( codec->extradata, p_input->p_fmt->p_extra,
308 p_input->p_fmt->i_extra );
314 /*****************************************************************************
316 *****************************************************************************/
317 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
319 msg_Dbg( p_mux, "removing input" );
320 free( p_input->p_sys );
324 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
326 sout_mux_sys_t *p_sys = p_mux->p_sys;
327 block_t *p_data = block_FifoGet( p_input->p_fifo );
328 int i_stream = *((int *)p_input->p_sys);
329 AVStream *p_stream = p_sys->oc->streams[i_stream];
332 memset( &pkt, 0, sizeof(AVPacket) );
334 av_init_packet(&pkt);
335 pkt.data = p_data->p_buffer;
336 pkt.size = p_data->i_buffer;
337 pkt.stream_index = i_stream;
339 if( p_data->i_flags & BLOCK_FLAG_TYPE_I ) pkt.flags |= AV_PKT_FLAG_KEY;
341 /* avformat expects pts/dts which start from 0 */
342 p_data->i_dts -= p_mux->p_sys->i_initial_dts;
343 p_data->i_pts -= p_mux->p_sys->i_initial_dts;
345 if( p_data->i_pts > 0 )
346 pkt.pts = p_data->i_pts * p_stream->time_base.den /
347 INT64_C(1000000) / p_stream->time_base.num;
348 if( p_data->i_dts > 0 )
349 pkt.dts = p_data->i_dts * p_stream->time_base.den /
350 INT64_C(1000000) / p_stream->time_base.num;
352 /* this is another hack to prevent libavformat from triggering the "non monotone timestamps" check in avformat/utils.c */
353 p_stream->cur_dts = ( p_data->i_dts * p_stream->time_base.den /
354 INT64_C(1000000) / p_stream->time_base.num ) - 1;
356 if( av_write_frame( p_sys->oc, &pkt ) < 0 )
358 msg_Err( p_mux, "could not write frame (pts: %"PRId64", dts: %"PRId64") "
359 "(pkt pts: %"PRId64", dts: %"PRId64")",
360 p_data->i_pts, p_data->i_dts, pkt.pts, pkt.dts );
361 block_Release( p_data );
365 block_Release( p_data );
369 /*****************************************************************************
370 * Mux: multiplex available data in input fifos
371 *****************************************************************************/
372 static int Mux( sout_mux_t *p_mux )
374 sout_mux_sys_t *p_sys = p_mux->p_sys;
376 if( p_sys->b_error ) return VLC_EGENERIC;
378 if( p_sys->b_write_header )
381 msg_Dbg( p_mux, "writing header" );
383 #if (LIBAVFORMAT_VERSION_INT >= ((53<<16)+(2<<8)+0))
384 char *psz_opts = var_GetNonEmptyString( p_mux, "sout-avformat-options" );
385 AVDictionary *options = NULL;
386 if (psz_opts && *psz_opts)
387 options = vlc_av_get_options(psz_opts);
389 error = avformat_write_header( p_sys->oc, options ? &options : NULL);
390 AVDictionaryEntry *t = NULL;
391 while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX))) {
392 msg_Err( p_mux, "Unknown option \"%s\"", t->key );
394 av_dict_free(&options);
396 error = av_write_header( p_sys->oc );
400 errno = AVUNERROR(error);
401 msg_Err( p_mux, "could not write header: %m" );
402 p_sys->b_write_header = false;
403 p_sys->b_error = true;
407 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
408 avio_flush( p_sys->oc->pb );
410 avio_flush( &p_sys->oc->pb );
412 p_sys->b_write_header = false;
419 int i_stream = sout_MuxGetStream( p_mux, 1, &i_dts );
423 if( !p_mux->p_sys->i_initial_dts )
424 p_mux->p_sys->i_initial_dts = i_dts;
426 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
432 /*****************************************************************************
434 *****************************************************************************/
435 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
441 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
442 pb_bool = (bool*)va_arg( args, bool * );
446 case MUX_GET_ADD_STREAM_WAIT:
447 pb_bool = (bool*)va_arg( args, bool * );
453 char **ppsz = (char**)va_arg( args, char ** );
454 *ppsz = strdup( p_mux->p_sys->oc->oformat->mime_type );
463 /*****************************************************************************
464 * I/O wrappers for libavformat
465 *****************************************************************************/
466 static int IOWrite( void *opaque, uint8_t *buf, int buf_size )
468 sout_mux_t *p_mux = opaque;
471 #ifdef AVFORMAT_DEBUG
472 msg_Dbg( p_mux, "IOWrite %i bytes", buf_size );
475 block_t *p_buf = block_New( p_mux->p_sout, buf_size );
476 if( buf_size > 0 ) memcpy( p_buf->p_buffer, buf, buf_size );
478 if( p_mux->p_sys->b_write_header )
479 p_buf->i_flags |= BLOCK_FLAG_HEADER;
481 i_ret = sout_AccessOutWrite( p_mux->p_access, p_buf );
482 return i_ret ? i_ret : -1;
485 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
487 sout_mux_t *p_mux = opaque;
489 #ifdef AVFORMAT_DEBUG
490 msg_Dbg( p_mux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
496 return sout_AccessOutSeek( p_mux->p_access, offset );