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"
44 //#define AVFORMAT_DEBUG 1
46 static const char *const ppsz_mux_options[] = {
47 "mux", "options", NULL
50 /*****************************************************************************
51 * mux_sys_t: mux descriptor
52 *****************************************************************************/
62 bool b_write_keyframe;
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Control ( sout_mux_t *, int, va_list );
70 static int AddStream( sout_mux_t *, sout_input_t * );
71 static void 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 bool dummy = !strcmp( p_mux->p_access->psz_access, "dummy");
86 if( dummy && strlen(p_mux->p_access->psz_path)
87 >= sizeof (((AVFormatContext *)NULL)->filename) )
90 vlc_init_avformat(p_this);
92 config_ChainParse( p_mux, "sout-avformat-", ppsz_mux_options, p_mux->p_cfg );
94 /* Find the requested muxer */
95 char *psz_mux = var_InheritString( p_mux, "sout-avformat-mux" );
98 file_oformat = av_guess_format( psz_mux, NULL, NULL );
104 av_guess_format( NULL, p_mux->p_access->psz_path, NULL);
108 msg_Err( p_mux, "unable for find a suitable output format" );
112 sout_mux_sys_t *p_sys = malloc( sizeof( sout_mux_sys_t ) );
113 if( unlikely(p_sys == NULL) )
116 p_mux->p_sys = p_sys;
117 p_sys->oc = avformat_alloc_context();
118 p_sys->oc->oformat = file_oformat;
119 /* If we use dummy access, let avformat write output */
121 strcpy( p_sys->oc->filename, p_mux->p_access->psz_path );
123 /* Create I/O wrapper */
124 p_sys->io_buffer_size = 10 * 1024 * 1024; /* FIXME */
125 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
128 if( sout_AccessOutControl( p_mux->p_access, ACCESS_OUT_CAN_SEEK, &b_can_seek ) )
130 p_sys->io = avio_alloc_context(
131 p_sys->io_buffer, p_sys->io_buffer_size,
132 1, p_mux, NULL, IOWrite, b_can_seek ? IOSeek : NULL );
134 p_sys->oc->pb = p_sys->io;
135 p_sys->oc->nb_streams = 0;
137 p_sys->b_write_header = true;
138 p_sys->b_write_keyframe = false;
139 p_sys->b_error = false;
141 /* Fill p_mux fields */
142 p_mux->pf_control = Control;
143 p_mux->pf_addstream = AddStream;
144 p_mux->pf_delstream = DelStream;
150 /*****************************************************************************
152 *****************************************************************************/
153 void CloseMux( vlc_object_t *p_this )
155 sout_mux_t *p_mux = (sout_mux_t*)p_this;
156 sout_mux_sys_t *p_sys = p_mux->p_sys;
158 if( !p_sys->b_write_header && !p_sys->b_error && av_write_trailer( p_sys->oc ) < 0 )
160 msg_Err( p_mux, "could not write trailer" );
163 avformat_free_context(p_sys->oc);
166 free( p_sys->io_buffer );
170 /*****************************************************************************
172 *****************************************************************************/
173 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
175 sout_mux_sys_t *p_sys = p_mux->p_sys;
176 es_format_t *fmt = p_input->p_fmt;
177 AVCodecContext *codec;
181 msg_Dbg( p_mux, "adding input" );
183 if( !GetFfmpegCodec( fmt->i_codec, 0, &i_codec_id, 0 )
184 || i_codec_id == AV_CODEC_ID_NONE )
186 msg_Dbg( p_mux, "couldn't find codec for fourcc '%4.4s'",
187 (char *)&fmt->i_codec );
191 unsigned opus_size[XIPH_MAX_HEADER_COUNT];
192 void *opus_packet[XIPH_MAX_HEADER_COUNT];
193 if( fmt->i_codec == VLC_CODEC_OPUS )
196 /* Only transmits the first packet (OpusHead) */
197 if( xiph_SplitHeaders(opus_size, opus_packet, &count, fmt->i_extra, fmt->p_extra ) ) {
200 if (count != 2 || opus_size[0] < 19) {
201 msg_Err(p_mux, "Invalid Opus header");
206 p_input->p_sys = malloc( sizeof( int ) );
207 *((int *)p_input->p_sys) = p_sys->oc->nb_streams;
209 if( fmt->i_cat != VIDEO_ES && fmt->i_cat != AUDIO_ES)
211 msg_Warn( p_mux, "Unhandled ES category" );
215 stream = avformat_new_stream( p_sys->oc, NULL);
218 free( p_input->p_sys );
221 codec = stream->codec;
223 codec->opaque = p_mux;
228 codec->codec_type = AVMEDIA_TYPE_AUDIO;
229 codec->channels = fmt->audio.i_channels;
230 codec->sample_rate = fmt->audio.i_rate;
231 codec->time_base = (AVRational){1, codec->sample_rate};
232 codec->frame_size = fmt->audio.i_frame_length;
233 if (fmt->i_bitrate == 0) {
234 msg_Warn( p_mux, "Missing audio bitrate, assuming 64k" );
235 fmt->i_bitrate = 64000;
240 if( !fmt->video.i_frame_rate || !fmt->video.i_frame_rate_base ) {
241 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
242 fmt->video.i_frame_rate = 25;
243 fmt->video.i_frame_rate_base = 1;
245 msg_Dbg( p_mux, "Muxing framerate will be %d/%d = %.2f fps",
246 fmt->video.i_frame_rate,
247 fmt->video.i_frame_rate_base,
248 (double)fmt->video.i_frame_rate/(double)fmt->video.i_frame_rate_base );
250 codec->codec_type = AVMEDIA_TYPE_VIDEO;
251 codec->width = fmt->video.i_width;
252 codec->height = fmt->video.i_height;
253 av_reduce( &codec->sample_aspect_ratio.num,
254 &codec->sample_aspect_ratio.den,
255 fmt->video.i_sar_num,
256 fmt->video.i_sar_den, 1 << 30 /* something big */ );
257 msg_Dbg(p_mux, "Muxing aspect ratio will be %d/%d",
258 fmt->video.i_sar_num, fmt->video.i_sar_den);
259 stream->sample_aspect_ratio.den = codec->sample_aspect_ratio.den;
260 stream->sample_aspect_ratio.num = codec->sample_aspect_ratio.num;
261 codec->time_base.den = fmt->video.i_frame_rate;
262 codec->time_base.num = fmt->video.i_frame_rate_base;
263 if (fmt->i_bitrate == 0) {
264 msg_Warn( p_mux, "Missing video bitrate, assuming 512k" );
265 fmt->i_bitrate = 512000;
267 msg_Dbg( p_mux, "Muxing video bitrate will be %d", fmt->i_bitrate );
272 codec->bit_rate = fmt->i_bitrate;
273 codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
274 if( !codec->codec_tag && i_codec_id == AV_CODEC_ID_MP2 )
276 i_codec_id = AV_CODEC_ID_MP3;
277 codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
279 codec->codec_id = i_codec_id;
283 if( fmt->i_codec == VLC_CODEC_OPUS )
285 codec->extradata_size = opus_size[0];
286 codec->extradata = av_malloc( opus_size[0] );
287 memcpy( codec->extradata, opus_packet[0], opus_size[0] );
291 codec->extradata_size = fmt->i_extra;
292 codec->extradata = av_malloc( fmt->i_extra );
293 memcpy( codec->extradata, fmt->p_extra, fmt->i_extra );
300 /*****************************************************************************
302 *****************************************************************************/
303 static void DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
305 msg_Dbg( p_mux, "removing input" );
306 free( p_input->p_sys );
309 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
311 sout_mux_sys_t *p_sys = p_mux->p_sys;
312 block_t *p_data = block_FifoGet( p_input->p_fifo );
313 int i_stream = *((int *)p_input->p_sys);
314 AVStream *p_stream = p_sys->oc->streams[i_stream];
317 memset( &pkt, 0, sizeof(AVPacket) );
319 av_init_packet(&pkt);
320 pkt.data = p_data->p_buffer;
321 pkt.size = p_data->i_buffer;
322 pkt.stream_index = i_stream;
324 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
326 #ifdef AVFMT_ALLOW_FLUSH
327 /* Make sure we don't inadvertedly mark buffered data as keyframes. */
328 if( p_sys->oc->oformat->flags & AVFMT_ALLOW_FLUSH )
329 av_write_frame( p_sys->oc, NULL );
332 p_sys->b_write_keyframe = true;
333 pkt.flags |= AV_PKT_FLAG_KEY;
336 if( p_data->i_pts > 0 )
337 pkt.pts = p_data->i_pts * p_stream->time_base.den /
338 CLOCK_FREQ / p_stream->time_base.num;
339 if( p_data->i_dts > 0 )
340 pkt.dts = p_data->i_dts * p_stream->time_base.den /
341 CLOCK_FREQ / p_stream->time_base.num;
343 /* this is another hack to prevent libavformat from triggering the "non monotone timestamps" check in avformat/utils.c */
344 p_stream->cur_dts = ( p_data->i_dts * p_stream->time_base.den /
345 CLOCK_FREQ / p_stream->time_base.num ) - 1;
347 if( av_write_frame( p_sys->oc, &pkt ) < 0 )
349 msg_Err( p_mux, "could not write frame (pts: %"PRId64", dts: %"PRId64") "
350 "(pkt pts: %"PRId64", dts: %"PRId64")",
351 p_data->i_pts, p_data->i_dts, pkt.pts, pkt.dts );
352 block_Release( p_data );
356 block_Release( p_data );
360 /*****************************************************************************
361 * Mux: multiplex available data in input fifos
362 *****************************************************************************/
363 static int Mux( sout_mux_t *p_mux )
365 sout_mux_sys_t *p_sys = p_mux->p_sys;
367 if( p_sys->b_error ) return VLC_EGENERIC;
369 if( p_sys->b_write_header )
372 msg_Dbg( p_mux, "writing header" );
374 char *psz_opts = var_GetNonEmptyString( p_mux, "sout-avformat-options" );
375 AVDictionary *options = NULL;
376 if (psz_opts && *psz_opts)
377 options = vlc_av_get_options(psz_opts);
379 error = avformat_write_header( p_sys->oc, options ? &options : NULL);
380 AVDictionaryEntry *t = NULL;
381 while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX))) {
382 msg_Err( p_mux, "Unknown option \"%s\"", t->key );
384 av_dict_free(&options);
387 msg_Err( p_mux, "could not write header: %s",
388 vlc_strerror_c(AVUNERROR(error)) );
389 p_sys->b_write_header = false;
390 p_sys->b_error = true;
394 avio_flush( p_sys->oc->pb );
395 p_sys->b_write_header = false;
402 int i_stream = sout_MuxGetStream( p_mux, 1, &i_dts );
406 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
412 /*****************************************************************************
414 *****************************************************************************/
415 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
421 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
422 pb_bool = (bool*)va_arg( args, bool * );
426 case MUX_GET_ADD_STREAM_WAIT:
427 pb_bool = (bool*)va_arg( args, bool * );
433 char **ppsz = (char**)va_arg( args, char ** );
434 *ppsz = strdup( p_mux->p_sys->oc->oformat->mime_type );
443 /*****************************************************************************
444 * I/O wrappers for libavformat
445 *****************************************************************************/
446 static int IOWrite( void *opaque, uint8_t *buf, int buf_size )
448 sout_mux_t *p_mux = opaque;
449 sout_mux_sys_t *p_sys = p_mux->p_sys;
452 #ifdef AVFORMAT_DEBUG
453 msg_Dbg( p_mux, "IOWrite %i bytes", buf_size );
456 block_t *p_buf = block_Alloc( buf_size );
457 if( buf_size > 0 ) memcpy( p_buf->p_buffer, buf, buf_size );
459 if( p_sys->b_write_header )
460 p_buf->i_flags |= BLOCK_FLAG_HEADER;
462 if( p_sys->b_write_keyframe )
464 p_buf->i_flags |= BLOCK_FLAG_TYPE_I;
465 p_sys->b_write_keyframe = false;
468 i_ret = sout_AccessOutWrite( p_mux->p_access, p_buf );
469 return i_ret ? i_ret : -1;
472 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
474 sout_mux_t *p_mux = opaque;
476 #ifdef AVFORMAT_DEBUG
477 msg_Dbg( p_mux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
483 return sout_AccessOutSeek( p_mux->p_access, offset );