1 /*****************************************************************************
2 * mux.c: muxer using ffmpeg (libavformat).
3 *****************************************************************************
4 * Copyright (C) 2006 VideoLAN
5 * $Id: demux.c 8444 2004-08-17 08:21:07Z gbazin $
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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
34 #ifdef HAVE_FFMPEG_AVFORMAT_H
35 # include <ffmpeg/avformat.h>
37 # include <avformat.h>
42 //#define AVFORMAT_DEBUG 1
44 /* Version checking */
45 #if (LIBAVFORMAT_BUILD >= 4687) && (defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_TREE))
47 /*****************************************************************************
48 * mux_sys_t: mux descriptor
49 *****************************************************************************/
60 vlc_bool_t b_write_header;
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 offset_t IOSeek( void *opaque, offset_t offset, int whence );
77 /*****************************************************************************
79 *****************************************************************************/
80 int E_(OpenMux)( vlc_object_t *p_this )
82 sout_mux_t *p_mux = (sout_mux_t*)p_this;
83 sout_mux_sys_t *p_sys;
84 AVFormatParameters params, *ap = ¶ms;
86 /* Should we call it only once ? */
89 /* Find the requested muxer */
90 AVOutputFormat *file_oformat =
91 guess_format(NULL, p_mux->p_access->psz_name, NULL);
94 msg_Err( p_mux, "unable for find a suitable output format" );
98 /* Fill p_mux fields */
99 p_mux->pf_control = Control;
100 p_mux->pf_addstream = AddStream;
101 p_mux->pf_delstream = DelStream;
103 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
105 p_sys->oc = av_alloc_format_context();
106 p_sys->oc->oformat = file_oformat;
108 /* Create I/O wrapper */
109 p_sys->io_buffer_size = 32768; /* FIXME */
110 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
111 p_sys->url.priv_data = p_mux;
112 p_sys->url.prot = &p_sys->prot;
113 p_sys->url.prot->name = "VLC I/O wrapper";
114 p_sys->url.prot->url_open = 0;
115 p_sys->url.prot->url_read = 0;
116 p_sys->url.prot->url_write =
117 (int (*) (URLContext *, unsigned char *, int))IOWrite;
118 p_sys->url.prot->url_seek =
119 (offset_t (*) (URLContext *, offset_t, int))IOSeek;
120 p_sys->url.prot->url_close = 0;
121 p_sys->url.prot->next = 0;
122 init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
123 1, &p_sys->url, NULL, IOWrite, IOSeek );
125 memset( ap, 0, sizeof(*ap) );
126 if( av_set_parameters( p_sys->oc, ap ) < 0 )
128 msg_Err( p_mux, "invalid encoding parameters" );
129 av_free( p_sys->oc );
130 free( p_sys->io_buffer );
135 p_sys->oc->pb = p_sys->io;
136 p_sys->oc->nb_streams = 0;
138 p_sys->b_write_header = VLC_TRUE;
139 p_sys->b_error = VLC_FALSE;
140 p_sys->i_initial_dts = 0;
145 /*****************************************************************************
147 *****************************************************************************/
148 void E_(CloseMux)( vlc_object_t *p_this )
150 sout_mux_t *p_mux = (sout_mux_t*)p_this;
151 sout_mux_sys_t *p_sys = p_mux->p_sys;
154 if( av_write_trailer( p_sys->oc ) < 0 )
156 msg_Err( p_mux, "could not write trailer" );
159 for( i = 0 ; i < p_sys->oc->nb_streams; i++ )
161 if( p_sys->oc->streams[i]->codec->extradata )
162 av_free( p_sys->oc->streams[i]->codec->extradata );
163 av_free( p_sys->oc->streams[i]->codec );
164 av_free( p_sys->oc->streams[i] );
166 av_free( p_sys->oc );
168 free( p_sys->io_buffer );
172 /*****************************************************************************
174 *****************************************************************************/
175 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
177 sout_mux_sys_t *p_sys = p_mux->p_sys;
178 AVCodecContext *codec;
180 int i_codec_id, i_aspect_num, i_aspect_den;
182 msg_Dbg( p_mux, "adding input" );
184 if( !E_(GetFfmpegCodec)( p_input->p_fmt->i_codec, 0, &i_codec_id, 0 ) )
186 msg_Dbg( p_mux, "couldn't find codec for fourcc '%4.4s'",
187 (char *)&p_input->p_fmt->i_codec );
191 p_input->p_sys = malloc( sizeof( int ) );
192 *((int *)p_input->p_sys) = p_sys->oc->nb_streams;
194 stream = av_new_stream( p_sys->oc, p_sys->oc->nb_streams);
197 free( p_input->p_sys );
200 codec = stream->codec;
202 switch( p_input->p_fmt->i_cat )
205 codec->codec_type = CODEC_TYPE_AUDIO;
206 codec->channels = p_input->p_fmt->audio.i_channels;
207 codec->sample_rate = p_input->p_fmt->audio.i_rate;
208 codec->time_base = (AVRational){1, codec->sample_rate};
212 if( !p_input->p_fmt->video.i_frame_rate ||
213 !p_input->p_fmt->video.i_frame_rate_base )
215 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
216 p_input->p_fmt->video.i_frame_rate = 25;
217 p_input->p_fmt->video.i_frame_rate_base = 1;
219 codec->codec_type = CODEC_TYPE_VIDEO;
220 codec->width = p_input->p_fmt->video.i_width;
221 codec->height = p_input->p_fmt->video.i_height;
222 av_reduce( &i_aspect_num, &i_aspect_den,
223 p_input->p_fmt->video.i_aspect,
224 VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
225 av_reduce( &codec->sample_aspect_ratio.num,
226 &codec->sample_aspect_ratio.den,
227 i_aspect_num * (int64_t)codec->height,
228 i_aspect_den * (int64_t)codec->width, 1 << 30 );
229 codec->time_base.den = p_input->p_fmt->video.i_frame_rate;
230 codec->time_base.num = p_input->p_fmt->video.i_frame_rate_base;
234 codec->bit_rate = p_input->p_fmt->i_bitrate;
235 codec->codec_tag = p_input->p_fmt->i_codec;
236 codec->codec_id = i_codec_id;
238 if( p_input->p_fmt->i_extra )
240 codec->extradata_size = p_input->p_fmt->i_extra;
241 codec->extradata = av_malloc( p_input->p_fmt->i_extra );
242 memcpy( codec->extradata, p_input->p_fmt->p_extra,
243 p_input->p_fmt->i_extra );
249 /*****************************************************************************
251 *****************************************************************************/
252 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
254 msg_Dbg( p_mux, "removing input" );
255 free( p_input->p_sys );
260 * TODO move this function to src/stream_output.c (used by nearly all muxers)
262 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
267 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
269 block_fifo_t *p_fifo;
271 p_fifo = p_mux->pp_inputs[i]->p_fifo;
273 /* We don't really need to have anything in the SPU fifo */
274 if( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
275 p_fifo->i_depth == 0 ) continue;
277 if( p_fifo->i_depth )
281 p_buf = block_FifoShow( p_fifo );
282 if( i_stream < 0 || p_buf->i_dts < i_dts )
284 i_dts = p_buf->i_dts;
291 if( pi_stream ) *pi_stream = i_stream;
292 if( pi_dts ) *pi_dts = i_dts;
293 if( !p_mux->p_sys->i_initial_dts ) p_mux->p_sys->i_initial_dts = i_dts;
297 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
299 sout_mux_sys_t *p_sys = p_mux->p_sys;
300 block_t *p_data = block_FifoGet( p_input->p_fifo );
301 int i_stream = *((int *)p_input->p_sys);
302 AVStream *p_stream = p_sys->oc->streams[i_stream];
305 av_init_packet(&pkt);
306 pkt.data = p_data->p_buffer;
307 pkt.size = p_data->i_buffer;
308 pkt.stream_index = i_stream;
310 if( p_data->i_flags & BLOCK_FLAG_TYPE_I ) pkt.flags |= PKT_FLAG_KEY;
312 /* avformat expects pts/dts which start from 0 */
313 p_data->i_dts -= p_mux->p_sys->i_initial_dts;
314 p_data->i_pts -= p_mux->p_sys->i_initial_dts;
316 if( p_data->i_pts > 0 )
317 pkt.pts = p_data->i_pts * p_stream->time_base.den /
318 I64C(1000000) / p_stream->time_base.num;
319 if( p_data->i_dts > 0 )
320 pkt.dts = p_data->i_dts * p_stream->time_base.den /
321 I64C(1000000) / p_stream->time_base.num;
323 if( av_write_frame( p_sys->oc, &pkt ) < 0 )
325 msg_Err( p_mux, "could not write frame (pts: "I64Fd", dts: "I64Fd") "
326 "(pkt pts: "I64Fd", dts: "I64Fd")",
327 p_data->i_pts, p_data->i_dts, pkt.pts, pkt.dts );
328 block_Release( p_data );
332 block_Release( p_data );
336 /*****************************************************************************
337 * Mux: multiplex available data in input fifos
338 *****************************************************************************/
339 static int Mux( sout_mux_t *p_mux )
341 sout_mux_sys_t *p_sys = p_mux->p_sys;
344 if( p_sys->b_error ) return VLC_EGENERIC;
346 if( p_sys->b_write_header )
348 msg_Dbg( p_mux, "writing header" );
350 p_sys->b_write_header = VLC_FALSE;
352 if( av_write_header( p_sys->oc ) < 0 )
354 msg_Err( p_mux, "could not write header" );
355 p_sys->b_error = VLC_TRUE;
362 if( MuxGetStream( p_mux, &i_stream, 0 ) < 0 ) return VLC_SUCCESS;
363 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
369 /*****************************************************************************
371 *****************************************************************************/
372 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
378 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
379 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
380 *pb_bool = VLC_FALSE;
383 case MUX_GET_ADD_STREAM_WAIT:
384 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
394 /*****************************************************************************
395 * I/O wrappers for libavformat
396 *****************************************************************************/
397 static int IOWrite( void *opaque, uint8_t *buf, int buf_size )
399 URLContext *p_url = opaque;
400 sout_mux_t *p_mux = p_url->priv_data;
403 #ifdef AVFORMAT_DEBUG
404 msg_Dbg( p_mux, "IOWrite %i bytes", buf_size );
407 block_t *p_buf = block_New( p_mux->p_sout, buf_size );
408 if( buf_size > 0 ) memcpy( p_buf->p_buffer, buf, buf_size );
410 i_ret = sout_AccessOutWrite( p_mux->p_access, p_buf );
411 return i_ret ? i_ret : -1;
414 static offset_t IOSeek( void *opaque, offset_t offset, int whence )
416 URLContext *p_url = opaque;
417 sout_mux_t *p_mux = p_url->priv_data;
420 #ifdef AVFORMAT_DEBUG
421 msg_Dbg( p_mux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
435 if( sout_AccessOutSeek( p_mux->p_access, i_absolute ) )
443 #else /* LIBAVFORMAT_BUILD >= 4687 */
445 int E_(OpenMux)( vlc_object_t *p_this )
450 void E_(CloseMux)( vlc_object_t *p_this )
454 #endif /* LIBAVFORMAT_BUILD >= 4687 */