]> git.sesse.net Git - vlc/blob - modules/demux/avformat/mux.c
avformat mux: check correctly for *info member
[vlc] / modules / demux / avformat / mux.c
1 /*****************************************************************************
2  * mux.c: muxer using libavformat
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_block.h>
34 #include <vlc_sout.h>
35
36 #include <libavformat/avformat.h>
37
38 #include "avformat.h"
39 #include "../../codec/avcodec/avcodec.h"
40 #include "../../codec/avcodec/avutil.h"
41
42 //#define AVFORMAT_DEBUG 1
43
44 static const char *const ppsz_mux_options[] = {
45     "mux", NULL
46 };
47
48 /*****************************************************************************
49  * mux_sys_t: mux descriptor
50  *****************************************************************************/
51 struct sout_mux_sys_t
52 {
53     ByteIOContext   io;
54     int             io_buffer_size;
55     uint8_t        *io_buffer;
56
57     AVFormatContext *oc;
58     URLContext     url;
59     URLProtocol    prot;
60
61     bool     b_write_header;
62     bool     b_error;
63
64     int64_t        i_initial_dts;
65 };
66
67 /*****************************************************************************
68  * Local prototypes
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 * );
74
75 static int IOWrite( void *opaque, uint8_t *buf, int buf_size );
76 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
77
78 /*****************************************************************************
79  * Open
80  *****************************************************************************/
81 int OpenMux( vlc_object_t *p_this )
82 {
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 = &params;
87     char *psz_mux;
88
89     /* Should we call it only once ? */
90     av_register_all();
91     av_log_set_callback( LibavutilCallback );
92
93     config_ChainParse( p_mux, "ffmpeg-", ppsz_mux_options, p_mux->p_cfg );
94
95     /* Find the requested muxer */
96     psz_mux = var_GetNonEmptyString( p_mux, "ffmpeg-mux" );
97     if( psz_mux )
98     {
99 #if( LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT( 52, 45, 0 ) )
100         file_oformat = av_guess_format( psz_mux, NULL, NULL );
101 #else
102         file_oformat = guess_format( psz_mux, NULL, NULL );
103 #endif
104         free( psz_mux );
105     }
106     else
107     {
108         file_oformat =
109 #if( LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT( 52, 45, 0 ) )
110             av_guess_format( NULL, p_mux->p_access->psz_path, NULL);
111 #else
112             guess_format( NULL, p_mux->p_access->psz_path, NULL);
113 #endif
114     }
115     if (!file_oformat)
116     {
117       msg_Err( p_mux, "unable for find a suitable output format" );
118       return VLC_EGENERIC;
119     }
120
121     /* Fill p_mux fields */
122     p_mux->pf_control   = Control;
123     p_mux->pf_addstream = AddStream;
124     p_mux->pf_delstream = DelStream;
125     p_mux->pf_mux       = Mux;
126     p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
127
128     p_sys->oc = avformat_alloc_context();
129     p_sys->oc->oformat = file_oformat;
130
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 );
147
148     memset( ap, 0, sizeof(*ap) );
149     if( av_set_parameters( p_sys->oc, ap ) < 0 )
150     {
151         msg_Err( p_mux, "invalid encoding parameters" );
152         av_free( p_sys->oc );
153         free( p_sys->io_buffer );
154         free( p_sys );
155         return VLC_EGENERIC;
156     }
157
158     p_sys->oc->pb = &p_sys->io;
159     p_sys->oc->nb_streams = 0;
160
161     p_sys->b_write_header = true;
162     p_sys->b_error = false;
163     p_sys->i_initial_dts = 0;
164
165     return VLC_SUCCESS;
166 }
167
168 /*****************************************************************************
169  * Close
170  *****************************************************************************/
171 void CloseMux( vlc_object_t *p_this )
172 {
173     sout_mux_t *p_mux = (sout_mux_t*)p_this;
174     sout_mux_sys_t *p_sys = p_mux->p_sys;
175
176     if( !p_sys->b_write_header && av_write_trailer( p_sys->oc ) < 0 )
177     {
178         msg_Err( p_mux, "could not write trailer" );
179     }
180
181 #if( LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT( 52, 96, 0 ) )
182     avformat_free_context(p_sys->oc);
183 #else
184     for( unsigned i = 0 ; i < p_sys->oc->nb_streams; i++ )
185     {
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 );
190 #endif
191         av_free( p_sys->oc->streams[i] );
192     }
193     av_free( p_sys->oc->streams );
194     av_free( p_sys->oc );
195 #endif
196
197     free( p_sys->io_buffer );
198     free( p_sys );
199 }
200
201 /*****************************************************************************
202  * AddStream
203  *****************************************************************************/
204 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
205 {
206     sout_mux_sys_t *p_sys = p_mux->p_sys;
207     AVCodecContext *codec;
208     AVStream *stream;
209     int i_codec_id;
210
211     msg_Dbg( p_mux, "adding input" );
212
213     if( !GetFfmpegCodec( p_input->p_fmt->i_codec, 0, &i_codec_id, 0 ) )
214     {
215         msg_Dbg( p_mux, "couldn't find codec for fourcc '%4.4s'",
216                  (char *)&p_input->p_fmt->i_codec );
217         return VLC_EGENERIC;
218     }
219
220     p_input->p_sys = malloc( sizeof( int ) );
221     *((int *)p_input->p_sys) = p_sys->oc->nb_streams;
222
223     if( p_input->p_fmt->i_cat != VIDEO_ES && p_input->p_fmt->i_cat != AUDIO_ES)
224     {
225         msg_Warn( p_mux, "Unhandled ES category" );
226         return VLC_EGENERIC;
227     }
228
229     stream = av_new_stream( p_sys->oc, p_sys->oc->nb_streams);
230     if( !stream )
231     {
232         free( p_input->p_sys );
233         return VLC_EGENERIC;
234     }
235     codec = stream->codec;
236
237     /* This is used by LibavutilCallback (avutil.h) to print messages */
238     codec->opaque = (void*)p_mux;
239
240     switch( p_input->p_fmt->i_cat )
241     {
242     case AUDIO_ES:
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;
248         break;
249
250     case VIDEO_ES:
251         if( !p_input->p_fmt->video.i_frame_rate ||
252             !p_input->p_fmt->video.i_frame_rate_base )
253         {
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;
257         }
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;
268 #endif
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;
271         break;
272
273     }
274
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 )
279     {
280         i_codec_id = CODEC_ID_MP3;
281         codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
282     }
283 #else
284 #   warning "WARNING!!!!!!!"
285 #   warning "Using libavformat muxing with versions older than 51.8.0 (r7593) might produce broken files."
286     /* This is a hack */
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;
290 #endif
291     codec->codec_id = i_codec_id;
292
293     if( p_input->p_fmt->i_extra )
294     {
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 );
299     }
300
301     return VLC_SUCCESS;
302 }
303
304 /*****************************************************************************
305  * DelStream
306  *****************************************************************************/
307 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
308 {
309     msg_Dbg( p_mux, "removing input" );
310     free( p_input->p_sys );
311     return VLC_SUCCESS;
312 }
313
314 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
315 {
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];
320     AVPacket pkt;
321
322     memset( &pkt, 0, sizeof(AVPacket) );
323
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;
328
329     if( p_data->i_flags & BLOCK_FLAG_TYPE_I ) pkt.flags |= AV_PKT_FLAG_KEY;
330
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;
334
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;
341
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;
345
346     if( av_write_frame( p_sys->oc, &pkt ) < 0 )
347     {
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 );
352         return VLC_EGENERIC;
353     }
354
355     block_Release( p_data );
356     return VLC_SUCCESS;
357 }
358
359 /*****************************************************************************
360  * Mux: multiplex available data in input fifos
361  *****************************************************************************/
362 static int Mux( sout_mux_t *p_mux )
363 {
364     sout_mux_sys_t *p_sys = p_mux->p_sys;
365
366     if( p_sys->b_error ) return VLC_EGENERIC;
367
368     if( p_sys->b_write_header )
369     {
370         msg_Dbg( p_mux, "writing header" );
371
372         if( av_write_header( p_sys->oc ) < 0 )
373         {
374             msg_Err( p_mux, "could not write header" );
375             p_sys->b_write_header = false;
376             p_sys->b_error = true;
377             return VLC_EGENERIC;
378         }
379
380 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
381         put_flush_packet( p_sys->oc->pb );
382 #else
383         put_flush_packet( &p_sys->oc->pb );
384 #endif
385         p_sys->b_write_header = false;
386     }
387
388     for( ;; )
389     {
390         mtime_t i_dts;
391
392         int i_stream = sout_MuxGetStream( p_mux, 1, &i_dts );
393         if( i_stream < 0 )
394             return VLC_SUCCESS;
395
396         if( !p_mux->p_sys->i_initial_dts )
397             p_mux->p_sys->i_initial_dts = i_dts;
398
399         MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
400     }
401
402     return VLC_SUCCESS;
403 }
404
405 /*****************************************************************************
406  * Control:
407  *****************************************************************************/
408 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
409 {
410     bool *pb_bool;
411
412     switch( i_query )
413     {
414     case MUX_CAN_ADD_STREAM_WHILE_MUXING:
415         pb_bool = (bool*)va_arg( args, bool * );
416         *pb_bool = false;
417         return VLC_SUCCESS;
418
419     case MUX_GET_ADD_STREAM_WAIT:
420         pb_bool = (bool*)va_arg( args, bool * );
421         *pb_bool = true;
422         return VLC_SUCCESS;
423
424     case MUX_GET_MIME:
425     {
426         char **ppsz = (char**)va_arg( args, char ** );
427         *ppsz = strdup( p_mux->p_sys->oc->oformat->mime_type );
428         return VLC_SUCCESS;
429     }
430
431     default:
432         return VLC_EGENERIC;
433     }
434 }
435
436 /*****************************************************************************
437  * I/O wrappers for libavformat
438  *****************************************************************************/
439 static int IOWrite( void *opaque, uint8_t *buf, int buf_size )
440 {
441     URLContext *p_url = opaque;
442     sout_mux_t *p_mux = p_url->priv_data;
443     int i_ret;
444
445 #ifdef AVFORMAT_DEBUG
446     msg_Dbg( p_mux, "IOWrite %i bytes", buf_size );
447 #endif
448
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 );
451
452     if( p_mux->p_sys->b_write_header )
453         p_buf->i_flags |= BLOCK_FLAG_HEADER;
454
455     i_ret = sout_AccessOutWrite( p_mux->p_access, p_buf );
456     return i_ret ? i_ret : -1;
457 }
458
459 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
460 {
461     URLContext *p_url = opaque;
462     sout_mux_t *p_mux = p_url->priv_data;
463
464 #ifdef AVFORMAT_DEBUG
465     msg_Dbg( p_mux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
466 #endif
467
468     switch( whence )
469     {
470     case SEEK_SET:
471         return sout_AccessOutSeek( p_mux->p_access, offset );
472     case SEEK_CUR:
473     case SEEK_END:
474     default:
475         return -1;
476     }
477 }