1 /*****************************************************************************
2 * mux.c: muxer using libavformat
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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"
43 //#define AVFORMAT_DEBUG 1
45 static const char *const ppsz_mux_options[] = {
46 "mux", "options", NULL
49 /*****************************************************************************
50 * mux_sys_t: mux descriptor
51 *****************************************************************************/
63 int64_t i_initial_dts;
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Control ( sout_mux_t *, int, va_list );
70 static int AddStream( sout_mux_t *, sout_input_t * );
71 static int DelStream( sout_mux_t *, sout_input_t * );
72 static int Mux ( sout_mux_t * );
74 static int IOWrite( void *opaque, uint8_t *buf, int buf_size );
75 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
77 /*****************************************************************************
79 *****************************************************************************/
80 int OpenMux( vlc_object_t *p_this )
82 AVOutputFormat *file_oformat;
83 sout_mux_t *p_mux = (sout_mux_t*)p_this;
84 sout_mux_sys_t *p_sys;
89 config_ChainParse( p_mux, "sout-avformat-", ppsz_mux_options, p_mux->p_cfg );
91 /* Find the requested muxer */
92 psz_mux = var_GetNonEmptyString( p_mux, "sout-avformat-mux" );
95 file_oformat = av_guess_format( psz_mux, NULL, NULL );
101 av_guess_format( NULL, p_mux->p_access->psz_path, NULL);
105 msg_Err( p_mux, "unable for find a suitable output format" );
109 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
113 p_sys->oc = avformat_alloc_context();
114 p_sys->oc->oformat = file_oformat;
115 /* If we use dummy access, let avformat write output */
116 if( !strcmp( p_mux->p_access->psz_access, "dummy") )
117 strcpy( p_sys->oc->filename, p_mux->p_access->psz_path );
119 /* Create I/O wrapper */
120 p_sys->io_buffer_size = 32768; /* FIXME */
121 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
123 p_sys->io = avio_alloc_context(
124 p_sys->io_buffer, p_sys->io_buffer_size,
125 1, p_mux, NULL, IOWrite, IOSeek );
127 p_sys->oc->pb = p_sys->io;
128 p_sys->oc->nb_streams = 0;
130 p_sys->b_write_header = true;
131 p_sys->b_error = false;
132 p_sys->i_initial_dts = 0;
134 /* Fill p_mux fields */
135 p_mux->pf_control = Control;
136 p_mux->pf_addstream = AddStream;
137 p_mux->pf_delstream = DelStream;
143 /*****************************************************************************
145 *****************************************************************************/
146 void CloseMux( vlc_object_t *p_this )
148 sout_mux_t *p_mux = (sout_mux_t*)p_this;
149 sout_mux_sys_t *p_sys = p_mux->p_sys;
151 if( !p_sys->b_write_header && !p_sys->b_error && av_write_trailer( p_sys->oc ) < 0 )
153 msg_Err( p_mux, "could not write trailer" );
156 avformat_free_context(p_sys->oc);
158 free( p_sys->io_buffer );
162 /*****************************************************************************
164 *****************************************************************************/
165 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
167 sout_mux_sys_t *p_sys = p_mux->p_sys;
168 AVCodecContext *codec;
172 msg_Dbg( p_mux, "adding input" );
174 if( !GetFfmpegCodec( p_input->p_fmt->i_codec, 0, &i_codec_id, 0 ) )
176 msg_Dbg( p_mux, "couldn't find codec for fourcc '%4.4s'",
177 (char *)&p_input->p_fmt->i_codec );
181 p_input->p_sys = malloc( sizeof( int ) );
182 *((int *)p_input->p_sys) = p_sys->oc->nb_streams;
184 if( p_input->p_fmt->i_cat != VIDEO_ES && p_input->p_fmt->i_cat != AUDIO_ES)
186 msg_Warn( p_mux, "Unhandled ES category" );
190 stream = avformat_new_stream( p_sys->oc, NULL);
193 free( p_input->p_sys );
196 codec = stream->codec;
198 codec->opaque = p_mux;
200 switch( p_input->p_fmt->i_cat )
203 codec->codec_type = AVMEDIA_TYPE_AUDIO;
204 codec->channels = p_input->p_fmt->audio.i_channels;
205 codec->sample_rate = p_input->p_fmt->audio.i_rate;
206 codec->time_base = (AVRational){1, codec->sample_rate};
207 codec->frame_size = p_input->p_fmt->audio.i_frame_length;
211 if( !p_input->p_fmt->video.i_frame_rate ||
212 !p_input->p_fmt->video.i_frame_rate_base )
214 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
215 p_input->p_fmt->video.i_frame_rate = 25;
216 p_input->p_fmt->video.i_frame_rate_base = 1;
218 codec->codec_type = AVMEDIA_TYPE_VIDEO;
219 codec->width = p_input->p_fmt->video.i_width;
220 codec->height = p_input->p_fmt->video.i_height;
221 av_reduce( &codec->sample_aspect_ratio.num,
222 &codec->sample_aspect_ratio.den,
223 p_input->p_fmt->video.i_sar_num,
224 p_input->p_fmt->video.i_sar_den, 1 << 30 /* something big */ );
225 stream->sample_aspect_ratio.den = codec->sample_aspect_ratio.den;
226 stream->sample_aspect_ratio.num = codec->sample_aspect_ratio.num;
227 codec->time_base.den = p_input->p_fmt->video.i_frame_rate;
228 codec->time_base.num = p_input->p_fmt->video.i_frame_rate_base;
233 codec->bit_rate = p_input->p_fmt->i_bitrate;
234 codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
235 if( !codec->codec_tag && i_codec_id == AV_CODEC_ID_MP2 )
237 i_codec_id = AV_CODEC_ID_MP3;
238 codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
240 codec->codec_id = i_codec_id;
242 if( p_input->p_fmt->i_extra )
244 codec->extradata_size = p_input->p_fmt->i_extra;
245 codec->extradata = av_malloc( p_input->p_fmt->i_extra );
246 memcpy( codec->extradata, p_input->p_fmt->p_extra,
247 p_input->p_fmt->i_extra );
253 /*****************************************************************************
255 *****************************************************************************/
256 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
258 msg_Dbg( p_mux, "removing input" );
259 free( p_input->p_sys );
263 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
265 sout_mux_sys_t *p_sys = p_mux->p_sys;
266 block_t *p_data = block_FifoGet( p_input->p_fifo );
267 int i_stream = *((int *)p_input->p_sys);
268 AVStream *p_stream = p_sys->oc->streams[i_stream];
271 memset( &pkt, 0, sizeof(AVPacket) );
273 av_init_packet(&pkt);
274 pkt.data = p_data->p_buffer;
275 pkt.size = p_data->i_buffer;
276 pkt.stream_index = i_stream;
278 if( p_data->i_flags & BLOCK_FLAG_TYPE_I ) pkt.flags |= AV_PKT_FLAG_KEY;
280 /* avformat expects pts/dts which start from 0 */
281 p_data->i_dts -= p_mux->p_sys->i_initial_dts;
282 p_data->i_pts -= p_mux->p_sys->i_initial_dts;
284 if( p_data->i_pts > 0 )
285 pkt.pts = p_data->i_pts * p_stream->time_base.den /
286 INT64_C(1000000) / p_stream->time_base.num;
287 if( p_data->i_dts > 0 )
288 pkt.dts = p_data->i_dts * p_stream->time_base.den /
289 INT64_C(1000000) / p_stream->time_base.num;
291 /* this is another hack to prevent libavformat from triggering the "non monotone timestamps" check in avformat/utils.c */
292 p_stream->cur_dts = ( p_data->i_dts * p_stream->time_base.den /
293 INT64_C(1000000) / p_stream->time_base.num ) - 1;
295 if( av_write_frame( p_sys->oc, &pkt ) < 0 )
297 msg_Err( p_mux, "could not write frame (pts: %"PRId64", dts: %"PRId64") "
298 "(pkt pts: %"PRId64", dts: %"PRId64")",
299 p_data->i_pts, p_data->i_dts, pkt.pts, pkt.dts );
300 block_Release( p_data );
304 block_Release( p_data );
308 /*****************************************************************************
309 * Mux: multiplex available data in input fifos
310 *****************************************************************************/
311 static int Mux( sout_mux_t *p_mux )
313 sout_mux_sys_t *p_sys = p_mux->p_sys;
315 if( p_sys->b_error ) return VLC_EGENERIC;
317 if( p_sys->b_write_header )
320 msg_Dbg( p_mux, "writing header" );
322 char *psz_opts = var_GetNonEmptyString( p_mux, "sout-avformat-options" );
323 AVDictionary *options = NULL;
324 if (psz_opts && *psz_opts)
325 options = vlc_av_get_options(psz_opts);
327 error = avformat_write_header( p_sys->oc, options ? &options : NULL);
328 AVDictionaryEntry *t = NULL;
329 while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX))) {
330 msg_Err( p_mux, "Unknown option \"%s\"", t->key );
332 av_dict_free(&options);
335 errno = AVUNERROR(error);
336 msg_Err( p_mux, "could not write header: %m" );
337 p_sys->b_write_header = false;
338 p_sys->b_error = true;
342 avio_flush( p_sys->oc->pb );
343 p_sys->b_write_header = false;
350 int i_stream = sout_MuxGetStream( p_mux, 1, &i_dts );
354 if( !p_mux->p_sys->i_initial_dts )
355 p_mux->p_sys->i_initial_dts = i_dts;
357 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
363 /*****************************************************************************
365 *****************************************************************************/
366 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
372 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
373 pb_bool = (bool*)va_arg( args, bool * );
377 case MUX_GET_ADD_STREAM_WAIT:
378 pb_bool = (bool*)va_arg( args, bool * );
384 char **ppsz = (char**)va_arg( args, char ** );
385 *ppsz = strdup( p_mux->p_sys->oc->oformat->mime_type );
394 /*****************************************************************************
395 * I/O wrappers for libavformat
396 *****************************************************************************/
397 static int IOWrite( void *opaque, uint8_t *buf, int buf_size )
399 sout_mux_t *p_mux = opaque;
402 #ifdef AVFORMAT_DEBUG
403 msg_Dbg( p_mux, "IOWrite %i bytes", buf_size );
406 block_t *p_buf = block_Alloc( buf_size );
407 if( buf_size > 0 ) memcpy( p_buf->p_buffer, buf, buf_size );
409 if( p_mux->p_sys->b_write_header )
410 p_buf->i_flags |= BLOCK_FLAG_HEADER;
412 i_ret = sout_AccessOutWrite( p_mux->p_access, p_buf );
413 return i_ret ? i_ret : -1;
416 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
418 sout_mux_t *p_mux = opaque;
420 #ifdef AVFORMAT_DEBUG
421 msg_Dbg( p_mux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
427 return sout_AccessOutSeek( p_mux->p_access, offset );