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