]> git.sesse.net Git - vlc/blob - modules/demux/avformat/mux.c
Force avcodec to be at least 52.25.0 and avfomat 52.30.0...
[vlc] / modules / demux / avformat / mux.c
1 /*****************************************************************************
2  * mux.c: muxer using ffmpeg (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 /* ffmpeg header */
37 #ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
38 #   include <libavformat/avformat.h>
39 #elif defined(HAVE_FFMPEG_AVFORMAT_H)
40 #   include <ffmpeg/avformat.h>
41 #endif
42
43 #include "avformat.h"
44 #include "../../codec/avcodec/avcodec.h"
45 #include "../../codec/avcodec/avutil.h"
46
47 //#define AVFORMAT_DEBUG 1
48
49 static const char *const ppsz_mux_options[] = {
50     "mux", NULL
51 };
52
53 /*****************************************************************************
54  * mux_sys_t: mux descriptor
55  *****************************************************************************/
56 struct sout_mux_sys_t
57 {
58     ByteIOContext   io;
59     int             io_buffer_size;
60     uint8_t        *io_buffer;
61
62     AVFormatContext *oc;
63     URLContext     url;
64     URLProtocol    prot;
65
66     bool     b_write_header;
67     bool     b_error;
68
69     int64_t        i_initial_dts;
70 };
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75 static int Control  ( sout_mux_t *, int, va_list );
76 static int AddStream( sout_mux_t *, sout_input_t * );
77 static int DelStream( sout_mux_t *, sout_input_t * );
78 static int Mux      ( sout_mux_t * );
79
80 static int IOWrite( void *opaque, uint8_t *buf, int buf_size );
81 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
82
83 /*****************************************************************************
84  * Open
85  *****************************************************************************/
86 int OpenMux( vlc_object_t *p_this )
87 {
88     AVOutputFormat *file_oformat;
89     sout_mux_t *p_mux = (sout_mux_t*)p_this;
90     sout_mux_sys_t *p_sys;
91     AVFormatParameters params, *ap = &params;
92     char *psz_mux;
93
94     /* Should we call it only once ? */
95     av_register_all();
96     av_log_set_callback( LibavutilCallback );
97
98     config_ChainParse( p_mux, "ffmpeg-", ppsz_mux_options, p_mux->p_cfg );
99
100     /* Find the requested muxer */
101     psz_mux = var_GetNonEmptyString( p_mux, "ffmpeg-mux" );
102     if( psz_mux )
103     {
104         file_oformat = guess_format( psz_mux, NULL, NULL );
105     }
106     else
107     {
108         file_oformat =
109             guess_format(NULL, p_mux->p_access->psz_path, NULL);
110     }
111     if (!file_oformat)
112     {
113       msg_Err( p_mux, "unable for find a suitable output format" );
114       return VLC_EGENERIC;
115     }
116
117     /* Fill p_mux fields */
118     p_mux->pf_control   = Control;
119     p_mux->pf_addstream = AddStream;
120     p_mux->pf_delstream = DelStream;
121     p_mux->pf_mux       = Mux;
122     p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
123
124     p_sys->oc = av_alloc_format_context();
125     p_sys->oc->oformat = file_oformat;
126
127     /* Create I/O wrapper */
128     p_sys->io_buffer_size = 32768;  /* FIXME */
129     p_sys->io_buffer = malloc( p_sys->io_buffer_size );
130     p_sys->url.priv_data = p_mux;
131     p_sys->url.prot = &p_sys->prot;
132     p_sys->url.prot->name = "VLC I/O wrapper";
133     p_sys->url.prot->url_open = 0;
134     p_sys->url.prot->url_read = 0;
135     p_sys->url.prot->url_write =
136                     (int (*) (URLContext *, unsigned char *, int))IOWrite;
137     p_sys->url.prot->url_seek =
138                     (int64_t (*) (URLContext *, int64_t, int))IOSeek;
139     p_sys->url.prot->url_close = 0;
140     p_sys->url.prot->next = 0;
141     init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
142                    1, &p_sys->url, NULL, IOWrite, IOSeek );
143
144     memset( ap, 0, sizeof(*ap) );
145     if( av_set_parameters( p_sys->oc, ap ) < 0 )
146     {
147         msg_Err( p_mux, "invalid encoding parameters" );
148         av_free( p_sys->oc );
149         free( p_sys->io_buffer );
150         free( p_sys );
151         return VLC_EGENERIC;
152     }
153
154     p_sys->oc->pb = &p_sys->io;
155     p_sys->oc->nb_streams = 0;
156
157     p_sys->b_write_header = true;
158     p_sys->b_error = false;
159     p_sys->i_initial_dts = 0;
160
161     return VLC_SUCCESS;
162 }
163
164 /*****************************************************************************
165  * Close
166  *****************************************************************************/
167 void CloseMux( vlc_object_t *p_this )
168 {
169     sout_mux_t *p_mux = (sout_mux_t*)p_this;
170     sout_mux_sys_t *p_sys = p_mux->p_sys;
171     unsigned int i;
172
173     if( !p_sys->b_write_header && av_write_trailer( p_sys->oc ) < 0 )
174     {
175         msg_Err( p_mux, "could not write trailer" );
176     }
177
178     for( i = 0 ; i < p_sys->oc->nb_streams; i++ )
179     {
180         if( p_sys->oc->streams[i]->codec->extradata )
181             av_free( p_sys->oc->streams[i]->codec->extradata );
182         av_free( p_sys->oc->streams[i]->codec );
183         av_free( p_sys->oc->streams[i] );
184     }
185     av_free( p_sys->oc );
186
187     free( p_sys->io_buffer );
188     free( p_sys );
189 }
190
191 /*****************************************************************************
192  * AddStream
193  *****************************************************************************/
194 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
195 {
196     sout_mux_sys_t *p_sys = p_mux->p_sys;
197     AVCodecContext *codec;
198     AVStream *stream;
199     int i_codec_id;
200
201     msg_Dbg( p_mux, "adding input" );
202
203     if( !GetFfmpegCodec( p_input->p_fmt->i_codec, 0, &i_codec_id, 0 ) )
204     {
205         msg_Dbg( p_mux, "couldn't find codec for fourcc '%4.4s'",
206                  (char *)&p_input->p_fmt->i_codec );
207         return VLC_EGENERIC;
208     }
209
210     p_input->p_sys = malloc( sizeof( int ) );
211     *((int *)p_input->p_sys) = p_sys->oc->nb_streams;
212
213     if( p_input->p_fmt->i_cat != VIDEO_ES && p_input->p_fmt->i_cat != AUDIO_ES)
214     {
215         msg_Warn( p_mux, "Unhandled ES category" );
216         return VLC_EGENERIC;
217     }
218
219     stream = av_new_stream( p_sys->oc, p_sys->oc->nb_streams);
220     if( !stream )
221     {
222         free( p_input->p_sys );
223         return VLC_EGENERIC;
224     }
225     codec = stream->codec;
226
227     /* This is used by LibavutilCallback (avutil.h) to print messages */
228     codec->opaque = (void*)p_mux;
229
230     switch( p_input->p_fmt->i_cat )
231     {
232     case AUDIO_ES:
233         codec->codec_type = CODEC_TYPE_AUDIO;
234         codec->channels = p_input->p_fmt->audio.i_channels;
235         codec->sample_rate = p_input->p_fmt->audio.i_rate;
236         codec->time_base = (AVRational){1, codec->sample_rate};
237         codec->frame_size = p_input->p_fmt->audio.i_frame_length;
238         break;
239
240     case VIDEO_ES:
241         if( !p_input->p_fmt->video.i_frame_rate ||
242             !p_input->p_fmt->video.i_frame_rate_base )
243         {
244             msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
245             p_input->p_fmt->video.i_frame_rate = 25;
246             p_input->p_fmt->video.i_frame_rate_base = 1;
247         }
248         codec->codec_type = CODEC_TYPE_VIDEO;
249         codec->width = p_input->p_fmt->video.i_width;
250         codec->height = p_input->p_fmt->video.i_height;
251         av_reduce( &codec->sample_aspect_ratio.num,
252                    &codec->sample_aspect_ratio.den,
253                    p_input->p_fmt->video.i_sar_num,
254                    p_input->p_fmt->video.i_sar_den, 1 << 30 /* something big */ );
255 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
256         stream->sample_aspect_ratio.num = codec->sample_aspect_ratio.num;
257         stream->sample_aspect_ratio.den = codec->sample_aspect_ratio.den;
258 #endif
259         codec->time_base.den = p_input->p_fmt->video.i_frame_rate;
260         codec->time_base.num = p_input->p_fmt->video.i_frame_rate_base;
261         break;
262
263     }
264
265     codec->bit_rate = p_input->p_fmt->i_bitrate;
266 #if LIBAVFORMAT_VERSION_INT >= ((51<<16)+(8<<8)+0)
267     codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
268     if( !codec->codec_tag && i_codec_id == CODEC_ID_MP2 )
269     {
270         i_codec_id = CODEC_ID_MP3;
271         codec->codec_tag = av_codec_get_tag( p_sys->oc->oformat->codec_tag, i_codec_id );
272     }
273 #else
274 #   warning "WARNING!!!!!!!"
275 #   warning "Using libavformat muxing with versions older than 51.8.0 (r7593) might produce broken files."
276     /* This is a hack */
277     if( i_codec_id == CODEC_ID_MP2 )
278         i_codec_id = CODEC_ID_MP3;
279     codec->codec_tag = p_input->p_fmt->i_original_fourcc ?: p_input->p_fmt->i_codec;
280 #endif
281     codec->codec_id = i_codec_id;
282
283     if( p_input->p_fmt->i_extra )
284     {
285         codec->extradata_size = p_input->p_fmt->i_extra;
286         codec->extradata = av_malloc( p_input->p_fmt->i_extra );
287         memcpy( codec->extradata, p_input->p_fmt->p_extra,
288                 p_input->p_fmt->i_extra );
289     }
290
291     return VLC_SUCCESS;
292 }
293
294 /*****************************************************************************
295  * DelStream
296  *****************************************************************************/
297 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
298 {
299     msg_Dbg( p_mux, "removing input" );
300     free( p_input->p_sys );
301     return VLC_SUCCESS;
302 }
303
304 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
305 {
306     sout_mux_sys_t *p_sys = p_mux->p_sys;
307     block_t *p_data = block_FifoGet( p_input->p_fifo );
308     int i_stream = *((int *)p_input->p_sys);
309     AVStream *p_stream = p_sys->oc->streams[i_stream];
310     AVPacket pkt;
311
312     memset( &pkt, 0, sizeof(AVPacket) );
313
314     av_init_packet(&pkt);
315     pkt.data = p_data->p_buffer;
316     pkt.size = p_data->i_buffer;
317     pkt.stream_index = i_stream;
318
319     if( p_data->i_flags & BLOCK_FLAG_TYPE_I ) pkt.flags |= PKT_FLAG_KEY;
320
321     /* avformat expects pts/dts which start from 0 */
322     p_data->i_dts -= p_mux->p_sys->i_initial_dts;
323     p_data->i_pts -= p_mux->p_sys->i_initial_dts;
324
325     if( p_data->i_pts > 0 )
326         pkt.pts = p_data->i_pts * p_stream->time_base.den /
327             INT64_C(1000000) / p_stream->time_base.num;
328     if( p_data->i_dts > 0 )
329         pkt.dts = p_data->i_dts * p_stream->time_base.den /
330             INT64_C(1000000) / p_stream->time_base.num;
331
332     /* this is another hack to prevent libavformat from triggering the "non monotone timestamps" check in avformat/utils.c */
333     p_stream->cur_dts = ( p_data->i_dts * p_stream->time_base.den /
334             INT64_C(1000000) / p_stream->time_base.num ) - 1;
335
336     if( av_write_frame( p_sys->oc, &pkt ) < 0 )
337     {
338         msg_Err( p_mux, "could not write frame (pts: %"PRId64", dts: %"PRId64") "
339                  "(pkt pts: %"PRId64", dts: %"PRId64")",
340                  p_data->i_pts, p_data->i_dts, pkt.pts, pkt.dts );
341         block_Release( p_data );
342         return VLC_EGENERIC;
343     }
344
345     block_Release( p_data );
346     return VLC_SUCCESS;
347 }
348
349 /*****************************************************************************
350  * Mux: multiplex available data in input fifos
351  *****************************************************************************/
352 static int Mux( sout_mux_t *p_mux )
353 {
354     sout_mux_sys_t *p_sys = p_mux->p_sys;
355
356     if( p_sys->b_error ) return VLC_EGENERIC;
357
358     if( p_sys->b_write_header )
359     {
360         msg_Dbg( p_mux, "writing header" );
361
362         if( av_write_header( p_sys->oc ) < 0 )
363         {
364             msg_Err( p_mux, "could not write header" );
365             p_sys->b_write_header = false;
366             p_sys->b_error = true;
367             return VLC_EGENERIC;
368         }
369
370 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(0<<8)+0)
371         put_flush_packet( p_sys->oc->pb );
372 #else
373         put_flush_packet( &p_sys->oc->pb );
374 #endif
375         p_sys->b_write_header = false;
376     }
377
378     for( ;; )
379     {
380         mtime_t i_dts;
381
382         int i_stream = sout_MuxGetStream( p_mux, 1, &i_dts );
383         if( i_stream < 0 )
384             return VLC_SUCCESS;
385
386         if( !p_mux->p_sys->i_initial_dts )
387             p_mux->p_sys->i_initial_dts = i_dts;
388
389         MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
390     }
391
392     return VLC_SUCCESS;
393 }
394
395 /*****************************************************************************
396  * Control:
397  *****************************************************************************/
398 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
399 {
400     bool *pb_bool;
401
402     switch( i_query )
403     {
404     case MUX_CAN_ADD_STREAM_WHILE_MUXING:
405         pb_bool = (bool*)va_arg( args, bool * );
406         *pb_bool = false;
407         return VLC_SUCCESS;
408
409     case MUX_GET_ADD_STREAM_WAIT:
410         pb_bool = (bool*)va_arg( args, bool * );
411         *pb_bool = true;
412         return VLC_SUCCESS;
413
414     case MUX_GET_MIME:
415     {
416         char **ppsz = (char**)va_arg( args, char ** );
417         *ppsz = strdup( p_mux->p_sys->oc->oformat->mime_type );
418         return VLC_SUCCESS;
419     }
420
421     default:
422         return VLC_EGENERIC;
423     }
424 }
425
426 /*****************************************************************************
427  * I/O wrappers for libavformat
428  *****************************************************************************/
429 static int IOWrite( void *opaque, uint8_t *buf, int buf_size )
430 {
431     URLContext *p_url = opaque;
432     sout_mux_t *p_mux = p_url->priv_data;
433     int i_ret;
434
435 #ifdef AVFORMAT_DEBUG
436     msg_Dbg( p_mux, "IOWrite %i bytes", buf_size );
437 #endif
438
439     block_t *p_buf = block_New( p_mux->p_sout, buf_size );
440     if( buf_size > 0 ) memcpy( p_buf->p_buffer, buf, buf_size );
441
442     if( p_mux->p_sys->b_write_header )
443         p_buf->i_flags |= BLOCK_FLAG_HEADER;
444
445     i_ret = sout_AccessOutWrite( p_mux->p_access, p_buf );
446     return i_ret ? i_ret : -1;
447 }
448
449 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
450 {
451     URLContext *p_url = opaque;
452     sout_mux_t *p_mux = p_url->priv_data;
453
454 #ifdef AVFORMAT_DEBUG
455     msg_Dbg( p_mux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
456 #endif
457
458     switch( whence )
459     {
460     case SEEK_SET:
461         return sout_AccessOutSeek( p_mux->p_access, offset );
462     case SEEK_CUR:
463     case SEEK_END:
464     default:
465         return -1;
466     }
467 }