]> git.sesse.net Git - vlc/blob - modules/stream_out/transcode/audio.c
sout: allow duplicate outputs to be merged
[vlc] / modules / stream_out / transcode / audio.c
1 /*****************************************************************************
2  * audio.c: transcoding stream output module (audio)
3  *****************************************************************************
4  * Copyright (C) 2003-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
10  *          Antoine Cellerier <dionoea at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #include "transcode.h"
32
33 #include <vlc_aout.h>
34 #include <vlc_meta.h>
35
36 static const int pi_channels_maps[6] =
37 {
38     0,
39     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
40     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
41     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
42      | AOUT_CHAN_REARRIGHT,
43     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
44      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
45 };
46
47 static inline void audio_timer_start( encoder_t * p_encoder )
48 {
49     stats_TimerStart( p_encoder, "encoding audio frame",
50                       STATS_TIMER_AUDIO_FRAME_ENCODING );
51 }
52
53 static inline void audio_timer_stop( encoder_t * p_encoder )
54 {
55     stats_TimerStop( p_encoder, STATS_TIMER_AUDIO_FRAME_ENCODING );
56 }
57
58 static inline void audio_timer_close( encoder_t * p_encoder )
59 {
60     stats_TimerDump(  p_encoder, STATS_TIMER_AUDIO_FRAME_ENCODING );
61     stats_TimerClean( p_encoder, STATS_TIMER_AUDIO_FRAME_ENCODING );
62 }
63
64 static block_t *transcode_audio_alloc( filter_t *p_filter, int size )
65 {
66     VLC_UNUSED( p_filter );
67     return block_Alloc( size );
68 }
69
70 static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
71 {
72     block_t *p_block;
73     int i_size;
74
75     if( p_dec->fmt_out.audio.i_bitspersample )
76     {
77         i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
78             p_dec->fmt_out.audio.i_channels;
79     }
80     else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
81              p_dec->fmt_out.audio.i_frame_length )
82     {
83         i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
84             p_dec->fmt_out.audio.i_frame_length;
85     }
86     else
87     {
88         i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
89     }
90
91     p_block = block_New( p_dec, i_size );
92     p_block->i_nb_samples = i_samples;
93     return p_block;
94 }
95
96 static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
97 {
98     VLC_UNUSED(p_dec);
99     block_Release( p_buffer );
100 }
101
102 static int transcode_audio_filter_allocation_init( filter_t *p_filter,
103                                                    void *data )
104 {
105     VLC_UNUSED(data);
106     p_filter->pf_audio_buffer_new = transcode_audio_alloc;
107     return VLC_SUCCESS;
108 }
109
110 static bool transcode_audio_filter_needed( const es_format_t *p_fmt1, const es_format_t *p_fmt2 )
111 {
112     if( p_fmt1->i_codec != p_fmt2->i_codec ||
113         p_fmt1->audio.i_channels != p_fmt2->audio.i_channels ||
114         p_fmt1->audio.i_rate != p_fmt2->audio.i_rate )
115         return true;
116     return false;
117 }
118 static int transcode_audio_filter_chain_build( sout_stream_t *p_stream, filter_chain_t *p_chain,
119                                                const es_format_t *p_dst, const es_format_t *p_src )
120 {
121     if( !transcode_audio_filter_needed( p_dst, p_src ) )
122         return VLC_SUCCESS;
123
124     es_format_t current = *p_src;
125
126     msg_Dbg( p_stream, "Looking for filter "
127              "(%4.4s->%4.4s, channels %d->%d, rate %d->%d)",
128          (const char *)&p_src->i_codec,
129          (const char *)&p_dst->i_codec,
130          p_src->audio.i_channels,
131          p_dst->audio.i_channels,
132          p_src->audio.i_rate,
133          p_dst->audio.i_rate );
134
135     /* If any filter is needed, convert to fl32 */
136     if( current.i_codec != VLC_CODEC_FL32 )
137     {
138         /* First step, convert to fl32 */
139         current.i_codec =
140         current.audio.i_format = VLC_CODEC_FL32;
141
142         if( !filter_chain_AppendFilter( p_chain, NULL, NULL, NULL, &current ) )
143         {
144             msg_Err( p_stream, "Failed to find conversion filter to fl32" );
145             return VLC_EGENERIC;
146         }
147         current = *filter_chain_GetFmtOut( p_chain );
148     }
149
150     /* Fix sample rate */
151     if( current.audio.i_rate != p_dst->audio.i_rate )
152     {
153         current.audio.i_rate = p_dst->audio.i_rate;
154         if( !filter_chain_AppendFilter( p_chain, NULL, NULL, NULL, &current ) )
155         {
156             msg_Err( p_stream, "Failed to find conversion filter for resampling" );
157             return VLC_EGENERIC;
158         }
159         current = *filter_chain_GetFmtOut( p_chain );
160     }
161
162     /* Fix channels */
163     if( current.audio.i_channels != p_dst->audio.i_channels )
164     {
165         current.audio.i_channels = p_dst->audio.i_channels;
166         current.audio.i_physical_channels = p_dst->audio.i_physical_channels;
167         current.audio.i_original_channels = p_dst->audio.i_original_channels;
168
169         if( ( !current.audio.i_physical_channels || !current.audio.i_original_channels ) &&
170             current.audio.i_channels < 6 )
171             current.audio.i_physical_channels =
172             current.audio.i_original_channels = pi_channels_maps[current.audio.i_channels];
173
174         if( !filter_chain_AppendFilter( p_chain, NULL, NULL, NULL, &current ) )
175         {
176             msg_Err( p_stream, "Failed to find conversion filter for channel mixing" );
177             return VLC_EGENERIC;
178         }
179         current = *filter_chain_GetFmtOut( p_chain );
180     }
181     /* And last step, convert to the requested codec */
182     if( current.i_codec != p_dst->i_codec )
183     {
184         current.i_codec = p_dst->i_codec;
185         if( !filter_chain_AppendFilter( p_chain, NULL, NULL, NULL, &current ) )
186         {
187             msg_Err( p_stream, "Failed to find conversion filter to %4.4s",
188                      (const char*)&p_dst->i_codec);
189             return VLC_EGENERIC;
190         }
191         current = *filter_chain_GetFmtOut( p_chain );
192     }
193
194     if( transcode_audio_filter_needed( p_dst, &current ) )
195     {
196         /* Weird case, a filter has side effects, doomed */
197         msg_Err( p_stream, "Failed to create a valid audio filter chain" );
198         return VLC_EGENERIC;
199     }
200
201     msg_Dbg( p_stream, "Got complete audio filter chain" );
202     return VLC_SUCCESS;
203 }
204
205
206 int transcode_audio_new( sout_stream_t *p_stream,
207                                 sout_stream_id_t *id )
208 {
209     sout_stream_sys_t *p_sys = p_stream->p_sys;
210     es_format_t fmt_last;
211
212     /*
213      * Open decoder
214      */
215
216     /* Initialization of decoder structures */
217     id->p_decoder->fmt_out = id->p_decoder->fmt_in;
218     id->p_decoder->fmt_out.i_extra = 0;
219     id->p_decoder->fmt_out.p_extra = 0;
220     id->p_decoder->pf_decode_audio = NULL;
221     id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
222     id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
223     /* id->p_decoder->p_cfg = p_sys->p_audio_cfg; */
224
225     id->p_decoder->p_module =
226         module_need( id->p_decoder, "decoder", "$codec", false );
227     if( !id->p_decoder->p_module )
228     {
229         msg_Err( p_stream, "cannot find audio decoder" );
230         return VLC_EGENERIC;
231     }
232     id->p_decoder->fmt_out.audio.i_bitspersample =
233         aout_BitsPerSample( id->p_decoder->fmt_out.i_codec );
234     fmt_last = id->p_decoder->fmt_out;
235     /* Fix AAC SBR changing number of channels and sampling rate */
236     if( !(id->p_decoder->fmt_in.i_codec == VLC_CODEC_MP4A &&
237         fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate &&
238         fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels) )
239         fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
240
241     /*
242      * Open encoder
243      */
244
245     /* Initialization of encoder format structures */
246     es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
247                     id->p_decoder->fmt_out.i_codec );
248     id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
249
250     id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
251     id->p_encoder->fmt_in.audio.i_physical_channels =
252         id->p_encoder->fmt_out.audio.i_physical_channels;
253     id->p_encoder->fmt_in.audio.i_original_channels =
254         id->p_encoder->fmt_out.audio.i_original_channels;
255     id->p_encoder->fmt_in.audio.i_channels =
256         id->p_encoder->fmt_out.audio.i_channels;
257     id->p_encoder->fmt_in.audio.i_bitspersample =
258         aout_BitsPerSample( id->p_encoder->fmt_in.i_codec );
259
260     id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
261     id->p_encoder->p_module =
262         module_need( id->p_encoder, "encoder", p_sys->psz_aenc, true );
263     if( !id->p_encoder->p_module )
264     {
265         msg_Err( p_stream, "cannot find audio encoder (module:%s fourcc:%4.4s)",
266                  p_sys->psz_aenc ? p_sys->psz_aenc : "any",
267                  (char *)&p_sys->i_acodec );
268         module_unneed( id->p_decoder, id->p_decoder->p_module );
269         id->p_decoder->p_module = NULL;
270         return VLC_EGENERIC;
271     }
272     id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
273     id->p_encoder->fmt_in.audio.i_bitspersample =
274         aout_BitsPerSample( id->p_encoder->fmt_in.i_codec );
275
276     /* Load user specified audio filters */
277     if( p_sys->psz_af )
278     {
279         es_format_t fmt_fl32 = fmt_last;
280         fmt_fl32.i_codec =
281         fmt_fl32.audio.i_format = VLC_CODEC_FL32;
282         if( transcode_audio_filter_chain_build( p_stream, id->p_uf_chain,
283                                                 &fmt_fl32, &fmt_last ) )
284         {
285             transcode_audio_close( id );
286             return VLC_EGENERIC;
287         }
288         fmt_last = fmt_fl32;
289
290         id->p_uf_chain = filter_chain_New( p_stream, "audio filter", false,
291                                            transcode_audio_filter_allocation_init, NULL, NULL );
292         filter_chain_Reset( id->p_uf_chain, &fmt_last, &fmt_fl32 );
293         if( filter_chain_AppendFromString( id->p_uf_chain, p_sys->psz_af ) > 0 )
294             fmt_last = *filter_chain_GetFmtOut( id->p_uf_chain );
295     }
296
297     /* Load conversion filters */
298     id->p_f_chain = filter_chain_New( p_stream, "audio filter", true,
299                     transcode_audio_filter_allocation_init, NULL, NULL );
300     filter_chain_Reset( id->p_f_chain, &fmt_last, &id->p_encoder->fmt_in );
301
302     if( transcode_audio_filter_chain_build( p_stream, id->p_f_chain,
303                                             &id->p_encoder->fmt_in, &fmt_last ) )
304     {
305         transcode_audio_close( id );
306         return VLC_EGENERIC;
307     }
308     fmt_last = id->p_encoder->fmt_in;
309
310     /* */
311     id->p_encoder->fmt_out.i_codec =
312         vlc_fourcc_GetCodec( AUDIO_ES, id->p_encoder->fmt_out.i_codec );
313
314     return VLC_SUCCESS;
315 }
316
317 void transcode_audio_close( sout_stream_id_t *id )
318 {
319     audio_timer_close( id->p_encoder );
320
321     /* Close decoder */
322     if( id->p_decoder->p_module )
323         module_unneed( id->p_decoder, id->p_decoder->p_module );
324     id->p_decoder->p_module = NULL;
325
326     if( id->p_decoder->p_description )
327         vlc_meta_Delete( id->p_decoder->p_description );
328     id->p_decoder->p_description = NULL;
329
330     /* Close encoder */
331     if( id->p_encoder->p_module )
332         module_unneed( id->p_encoder, id->p_encoder->p_module );
333     id->p_encoder->p_module = NULL;
334
335     /* Close filters */
336     if( id->p_uf_chain )
337         filter_chain_Delete( id->p_uf_chain );
338     if( id->p_f_chain )
339         filter_chain_Delete( id->p_f_chain );
340 }
341
342 int transcode_audio_process( sout_stream_t *p_stream,
343                                     sout_stream_id_t *id,
344                                     block_t *in, block_t **out )
345 {
346     sout_stream_sys_t *p_sys = p_stream->p_sys;
347     block_t *p_block, *p_audio_buf;
348     *out = NULL;
349
350     while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
351                                                           &in )) )
352     {
353         sout_UpdateStatistic( p_stream->p_sout, SOUT_STATISTIC_DECODED_AUDIO, 1 );
354         if( p_sys->b_master_sync )
355         {
356             mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
357             if ( p_audio_buf->i_pts - i_dts > MASTER_SYNC_MAX_DRIFT
358                   || p_audio_buf->i_pts - i_dts < -MASTER_SYNC_MAX_DRIFT )
359             {
360                 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
361                 date_Set( &id->interpolated_pts, p_audio_buf->i_pts );
362                 i_dts = p_audio_buf->i_pts + 1;
363             }
364             p_sys->i_master_drift = p_audio_buf->i_pts - i_dts;
365             date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
366             p_audio_buf->i_pts -= p_sys->i_master_drift;
367         }
368
369         p_audio_buf->i_dts = p_audio_buf->i_pts;
370
371         /* Run filter chain */
372         if( id->p_uf_chain )
373         {
374             p_audio_buf = filter_chain_AudioFilter( id->p_uf_chain,
375                                                     p_audio_buf );
376             if( !p_audio_buf )
377                 abort();
378         }
379
380         p_audio_buf = filter_chain_AudioFilter( id->p_f_chain, p_audio_buf );
381         if( !p_audio_buf )
382             abort();
383
384         p_audio_buf->i_pts = p_audio_buf->i_dts;
385
386         audio_timer_start( id->p_encoder );
387         p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
388         audio_timer_stop( id->p_encoder );
389
390         block_ChainAppend( out, p_block );
391         block_Release( p_audio_buf );
392     }
393
394     return VLC_SUCCESS;
395 }
396
397 bool transcode_audio_add( sout_stream_t *p_stream, es_format_t *p_fmt, 
398             sout_stream_id_t *id )
399 {
400     sout_stream_sys_t *p_sys = p_stream->p_sys;
401
402     msg_Dbg( p_stream,
403              "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
404              (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
405
406     /* Complete destination format */
407     id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
408     id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
409         p_sys->i_sample_rate : p_fmt->audio.i_rate;
410     id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
411     id->p_encoder->fmt_out.audio.i_bitspersample =
412         p_fmt->audio.i_bitspersample;
413     id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
414         p_sys->i_channels : p_fmt->audio.i_channels;
415     /* Sanity check for audio channels */
416     id->p_encoder->fmt_out.audio.i_channels =
417         __MIN( id->p_encoder->fmt_out.audio.i_channels,
418                id->p_decoder->fmt_in.audio.i_channels );
419     id->p_encoder->fmt_out.audio.i_original_channels =
420         id->p_decoder->fmt_in.audio.i_physical_channels;
421     if( id->p_decoder->fmt_in.audio.i_channels ==
422         id->p_encoder->fmt_out.audio.i_channels )
423     {
424         id->p_encoder->fmt_out.audio.i_physical_channels =
425             id->p_decoder->fmt_in.audio.i_physical_channels;
426     }
427     else
428     {
429         id->p_encoder->fmt_out.audio.i_physical_channels =
430             pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
431     }
432
433     /* Build decoder -> filter -> encoder chain */
434     if( transcode_audio_new( p_stream, id ) )
435     {
436         msg_Err( p_stream, "cannot create audio chain" );
437         return false;
438     }
439
440     /* Open output stream */
441     id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
442     id->b_transcode = true;
443
444     if( !id->id )
445     {
446         transcode_audio_close( id );
447         return false;
448     }
449
450     date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
451
452     return true;
453 }