]> git.sesse.net Git - vlc/blob - modules/codec/vorbis.c
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / codec / vorbis.c
1 /*****************************************************************************
2  * vorbis.c: vorbis decoder/encoder/packetizer module using of libvorbis.
3  *****************************************************************************
4  * Copyright (C) 2001-2003 the VideoLAN team
5  * Copyright (C) 2007 Société des arts technologiques
6  * Copyright (C) 2007 Savoir-faire Linux
7  *
8  * $Id$
9  *
10  * Authors: Gildas Bazin <gbazin@videolan.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 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_aout.h>
38 #include <vlc_input.h>
39 #include <vlc_sout.h>
40 #include "../demux/xiph.h"
41
42 #include <ogg/ogg.h>
43
44 #ifdef MODULE_NAME_IS_tremor
45 #include <tremor/ivorbiscodec.h>
46
47 #else
48 #include <vorbis/vorbisenc.h>
49
50 # ifndef OV_ECTL_RATEMANAGE_AVG
51 #   define OV_ECTL_RATEMANAGE_AVG 0x0
52 # endif
53
54 #endif
55
56 /*****************************************************************************
57  * decoder_sys_t : vorbis decoder descriptor
58  *****************************************************************************/
59 struct decoder_sys_t
60 {
61     /* Module mode */
62     bool b_packetizer;
63
64     bool            b_has_headers;
65
66     /*
67      * Vorbis properties
68      */
69     vorbis_info      vi; /* struct that stores all the static vorbis bitstream
70                             settings */
71     vorbis_comment   vc; /* struct that stores all the bitstream user
72                           * comments */
73     vorbis_dsp_state vd; /* central working state for the packet->PCM
74                           * decoder */
75     vorbis_block     vb; /* local working space for packet->PCM decode */
76
77     /*
78      * Common properties
79      */
80     date_t       end_date;
81     int          i_last_block_size;
82
83     /*
84     ** Channel reordering
85     */
86     int pi_chan_table[AOUT_CHAN_MAX];
87 };
88
89 static const int pi_channels_maps[9] =
90 {
91     0,
92     AOUT_CHAN_CENTER,
93     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
94     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
95     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
96      | AOUT_CHAN_REARRIGHT,
97     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
98      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
99     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
100      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
101     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
102      | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
103      | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
104     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
105      | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
106      | AOUT_CHAN_LFE,
107 };
108
109 /*
110 **  channel order as defined in http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
111 */
112
113 /* recommended vorbis channel order for 8 channels */
114 static const uint32_t pi_8channels_in[] =
115 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
116   AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
117   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
118
119 /* recommended vorbis channel order for 7 channels */
120 static const uint32_t pi_7channels_in[] =
121 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
122   AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
123   AOUT_CHAN_REARCENTER,AOUT_CHAN_LFE,0 };
124
125 /* recommended vorbis channel order for 6 channels */
126 static const uint32_t pi_6channels_in[] =
127 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
128   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
129
130 /* recommended vorbis channel order for 4 channels */
131 static const uint32_t pi_4channels_in[] =
132 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
133
134 /* recommended vorbis channel order for 3 channels */
135 static const uint32_t pi_3channels_in[] =
136 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
137
138 /****************************************************************************
139  * Local prototypes
140  ****************************************************************************/
141 static int  OpenDecoder   ( vlc_object_t * );
142 static int  OpenPacketizer( vlc_object_t * );
143 static void CloseDecoder  ( vlc_object_t * );
144 static void *DecodeBlock  ( decoder_t *, block_t ** );
145
146 static int  ProcessHeaders( decoder_t * );
147 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
148
149 static aout_buffer_t *DecodePacket  ( decoder_t *, ogg_packet * );
150 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
151
152 static void ParseVorbisComments( decoder_t * );
153
154 static void ConfigureChannelOrder(int *, int, uint32_t, bool );
155
156 #ifdef MODULE_NAME_IS_tremor
157 static void Interleave   ( int32_t *, const int32_t **, int, int, int * );
158 #else
159 static void Interleave   ( float *, const float **, int, int, int * );
160 #endif
161
162 #ifndef MODULE_NAME_IS_tremor
163 static int OpenEncoder   ( vlc_object_t * );
164 static void CloseEncoder ( vlc_object_t * );
165 static block_t *Encode   ( encoder_t *, aout_buffer_t * );
166 #endif
167
168 /*****************************************************************************
169  * Module descriptor
170  *****************************************************************************/
171 #define ENC_QUALITY_TEXT N_("Encoding quality")
172 #define ENC_QUALITY_LONGTEXT N_( \
173   "Enforce a quality between 1 (low) and 10 (high), instead " \
174   "of specifying a particular bitrate. This will produce a VBR stream." )
175 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
176 #define ENC_MAXBR_LONGTEXT N_( \
177   "Maximum bitrate in kbps. This is useful for streaming applications." )
178 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
179 #define ENC_MINBR_LONGTEXT N_( \
180   "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
181 #define ENC_CBR_TEXT N_("CBR encoding")
182 #define ENC_CBR_LONGTEXT N_( \
183   "Force a constant bitrate encoding (CBR)." )
184
185 vlc_module_begin ()
186     set_shortname( "Vorbis" )
187     set_description( N_("Vorbis audio decoder") )
188 #ifdef MODULE_NAME_IS_tremor
189     set_capability( "decoder", 90 )
190 #else
191     set_capability( "decoder", 100 )
192 #endif
193     set_category( CAT_INPUT )
194     set_subcategory( SUBCAT_INPUT_ACODEC )
195     set_callbacks( OpenDecoder, CloseDecoder )
196
197     add_submodule ()
198     set_description( N_("Vorbis audio packetizer") )
199     set_capability( "packetizer", 100 )
200     set_callbacks( OpenPacketizer, CloseDecoder )
201
202 #ifndef MODULE_NAME_IS_tremor
203 #   define ENC_CFG_PREFIX "sout-vorbis-"
204     add_submodule ()
205     set_description( N_("Vorbis audio encoder") )
206     set_capability( "encoder", 130 )
207     set_callbacks( OpenEncoder, CloseEncoder )
208
209     add_integer( ENC_CFG_PREFIX "quality", 0, NULL, ENC_QUALITY_TEXT,
210                  ENC_QUALITY_LONGTEXT, false )
211         change_integer_range( 0, 10 )
212     add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
213                  ENC_MAXBR_LONGTEXT, false )
214     add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
215                  ENC_MINBR_LONGTEXT, false )
216     add_bool( ENC_CFG_PREFIX "cbr", false, NULL, ENC_CBR_TEXT,
217                  ENC_CBR_LONGTEXT, false )
218 #endif
219
220 vlc_module_end ()
221
222 #ifndef MODULE_NAME_IS_tremor
223 static const char *const ppsz_enc_options[] = {
224     "quality", "max-bitrate", "min-bitrate", "cbr", NULL
225 };
226 #endif
227
228 /*****************************************************************************
229  * OpenDecoder: probe the decoder and return score
230  *****************************************************************************/
231 static int OpenDecoder( vlc_object_t *p_this )
232 {
233     decoder_t *p_dec = (decoder_t*)p_this;
234     decoder_sys_t *p_sys;
235
236     if( p_dec->fmt_in.i_codec != VLC_CODEC_VORBIS )
237     {
238         return VLC_EGENERIC;
239     }
240
241     /* Allocate the memory needed to store the decoder's structure */
242     if( ( p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) ) ) == NULL )
243         return VLC_ENOMEM;
244
245     /* Misc init */
246     date_Set( &p_sys->end_date, 0 );
247     p_sys->i_last_block_size = 0;
248     p_sys->b_packetizer = false;
249     p_sys->b_has_headers = false;
250
251     /* Take care of vorbis init */
252     vorbis_info_init( &p_sys->vi );
253     vorbis_comment_init( &p_sys->vc );
254
255     /* Set output properties */
256     p_dec->fmt_out.i_cat = AUDIO_ES;
257 #ifdef MODULE_NAME_IS_tremor
258     p_dec->fmt_out.i_codec = VLC_CODEC_FI32;
259 #else
260     p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
261 #endif
262
263     /* Set callbacks */
264     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
265         DecodeBlock;
266     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
267         DecodeBlock;
268
269     return VLC_SUCCESS;
270 }
271
272 static int OpenPacketizer( vlc_object_t *p_this )
273 {
274     decoder_t *p_dec = (decoder_t*)p_this;
275
276     int i_ret = OpenDecoder( p_this );
277
278     if( i_ret == VLC_SUCCESS )
279     {
280         p_dec->p_sys->b_packetizer = true;
281         p_dec->fmt_out.i_codec = VLC_CODEC_VORBIS;
282     }
283
284     return i_ret;
285 }
286
287 /****************************************************************************
288  * DecodeBlock: the whole thing
289  ****************************************************************************
290  * This function must be fed with ogg packets.
291  ****************************************************************************/
292 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
293 {
294     decoder_sys_t *p_sys = p_dec->p_sys;
295     ogg_packet oggpacket;
296
297     if( !pp_block ) return NULL;
298
299     if( *pp_block )
300     {
301         /* Block to Ogg packet */
302         oggpacket.packet = (*pp_block)->p_buffer;
303         oggpacket.bytes = (*pp_block)->i_buffer;
304     }
305     else
306     {
307         if( p_sys->b_packetizer ) return NULL;
308
309         /* Block to Ogg packet */
310         oggpacket.packet = NULL;
311         oggpacket.bytes = 0;
312     }
313
314     oggpacket.granulepos = -1;
315     oggpacket.b_o_s = 0;
316     oggpacket.e_o_s = 0;
317     oggpacket.packetno = 0;
318
319     /* Check for headers */
320     if( !p_sys->b_has_headers )
321     {
322         if( ProcessHeaders( p_dec ) )
323         {
324             block_Release( *pp_block );
325             return NULL;
326         }
327         p_sys->b_has_headers = true;
328     }
329
330     return ProcessPacket( p_dec, &oggpacket, pp_block );
331 }
332
333 /*****************************************************************************
334  * ProcessHeaders: process Vorbis headers.
335  *****************************************************************************/
336 static int ProcessHeaders( decoder_t *p_dec )
337 {
338     decoder_sys_t *p_sys = p_dec->p_sys;
339     ogg_packet oggpacket;
340
341     unsigned pi_size[XIPH_MAX_HEADER_COUNT];
342     void     *pp_data[XIPH_MAX_HEADER_COUNT];
343     unsigned i_count;
344     if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
345                            p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
346         return VLC_EGENERIC;
347     if( i_count < 3 )
348         goto error;
349
350     oggpacket.granulepos = -1;
351     oggpacket.e_o_s = 0;
352     oggpacket.packetno = 0;
353
354     /* Take care of the initial Vorbis header */
355     oggpacket.b_o_s  = 1; /* yes this actually is a b_o_s packet :) */
356     oggpacket.bytes  = pi_size[0];
357     oggpacket.packet = pp_data[0];
358     if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
359     {
360         msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
361         goto error;
362     }
363
364     /* Setup the format */
365     p_dec->fmt_out.audio.i_rate     = p_sys->vi.rate;
366     p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
367
368     if( p_dec->fmt_out.audio.i_channels > 9 )
369     {
370         msg_Err( p_dec, "invalid number of channels (not between 1 and 9): %i",
371                  p_dec->fmt_out.audio.i_channels );
372         goto error;
373     }
374
375     p_dec->fmt_out.audio.i_physical_channels =
376         p_dec->fmt_out.audio.i_original_channels =
377             pi_channels_maps[p_sys->vi.channels];
378     p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
379
380     date_Init( &p_sys->end_date, p_sys->vi.rate, 1 );
381
382     msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
383              p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
384
385     /* The next packet in order is the comments header */
386     oggpacket.b_o_s  = 0;
387     oggpacket.bytes  = pi_size[1];
388     oggpacket.packet = pp_data[1];
389     if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
390     {
391         msg_Err( p_dec, "2nd Vorbis header is corrupted" );
392         goto error;
393     }
394     ParseVorbisComments( p_dec );
395
396     /* The next packet in order is the codebooks header
397      * We need to watch out that this packet is not missing as a
398      * missing or corrupted header is fatal. */
399     oggpacket.b_o_s  = 0;
400     oggpacket.bytes  = pi_size[2];
401     oggpacket.packet = pp_data[2];
402     if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
403     {
404         msg_Err( p_dec, "3rd Vorbis header is corrupted" );
405         return VLC_EGENERIC;
406     }
407
408     if( !p_sys->b_packetizer )
409     {
410         /* Initialize the Vorbis packet->PCM decoder */
411         vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
412         vorbis_block_init( &p_sys->vd, &p_sys->vb );
413     }
414     else
415     {
416         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
417         p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
418                                            p_dec->fmt_out.i_extra );
419         memcpy( p_dec->fmt_out.p_extra,
420                 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
421     }
422
423     ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
424             p_dec->fmt_out.audio.i_physical_channels, true);
425
426     for( unsigned i = 0; i < i_count; i++ )
427         free( pp_data[i] );
428     return VLC_SUCCESS;
429
430 error:
431     for( unsigned i = 0; i < i_count; i++ )
432         free( pp_data[i] );
433     return VLC_EGENERIC;
434 }
435
436 /*****************************************************************************
437  * ProcessPacket: processes a Vorbis packet.
438  *****************************************************************************/
439 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
440                             block_t **pp_block )
441 {
442     decoder_sys_t *p_sys = p_dec->p_sys;
443     block_t *p_block = *pp_block;
444
445     /* Date management */
446     if( p_block && p_block->i_pts > VLC_TS_INVALID &&
447         p_block->i_pts != date_Get( &p_sys->end_date ) )
448     {
449         date_Set( &p_sys->end_date, p_block->i_pts );
450     }
451
452     if( !date_Get( &p_sys->end_date ) )
453     {
454         /* We've just started the stream, wait for the first PTS. */
455         if( p_block ) block_Release( p_block );
456         return NULL;
457     }
458
459     *pp_block = NULL; /* To avoid being fed the same packet again */
460
461     if( p_sys->b_packetizer )
462     {
463         return SendPacket( p_dec, p_oggpacket, p_block );
464     }
465     else
466     {
467         aout_buffer_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
468         if( p_block )
469             block_Release( p_block );
470         return p_aout_buffer;
471     }
472 }
473
474 /*****************************************************************************
475  * DecodePacket: decodes a Vorbis packet.
476  *****************************************************************************/
477 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
478 {
479     decoder_sys_t *p_sys = p_dec->p_sys;
480     int           i_samples;
481
482 #ifdef MODULE_NAME_IS_tremor
483     int32_t       **pp_pcm;
484 #else
485     float         **pp_pcm;
486 #endif
487
488     if( p_oggpacket->bytes &&
489 #ifdef MODULE_NAME_IS_tremor
490         vorbis_synthesis( &p_sys->vb, p_oggpacket, 1 ) == 0 )
491 #else
492         vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
493 #endif
494         vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
495
496     /* **pp_pcm is a multichannel float vector. In stereo, for
497      * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
498      * the size of each channel. Convert the float values
499      * (-1.<=range<=1.) to whatever PCM format and write it out */
500
501     if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
502     {
503
504         aout_buffer_t *p_aout_buffer;
505
506         p_aout_buffer =
507             decoder_NewAudioBuffer( p_dec, i_samples );
508
509         if( p_aout_buffer == NULL ) return NULL;
510
511         /* Interleave the samples */
512 #ifdef MODULE_NAME_IS_tremor
513         Interleave( (int32_t *)p_aout_buffer->p_buffer,
514                     (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
515 #else
516         Interleave( (float *)p_aout_buffer->p_buffer,
517                     (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
518 #endif
519
520         /* Tell libvorbis how many samples we actually consumed */
521         vorbis_synthesis_read( &p_sys->vd, i_samples );
522
523         /* Date management */
524         p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
525         p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
526                                            i_samples ) - p_aout_buffer->i_pts;
527         return p_aout_buffer;
528     }
529     else
530     {
531         return NULL;
532     }
533 }
534
535 /*****************************************************************************
536  * SendPacket: send an ogg dated packet to the stream output.
537  *****************************************************************************/
538 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
539                             block_t *p_block )
540 {
541     decoder_sys_t *p_sys = p_dec->p_sys;
542     int i_block_size, i_samples;
543
544     i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
545     if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
546     i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
547     p_sys->i_last_block_size = i_block_size;
548
549     /* Date management */
550     p_block->i_dts = p_block->i_pts = date_Get( &p_sys->end_date );
551
552     p_block->i_length = date_Increment( &p_sys->end_date, i_samples ) - p_block->i_pts;
553
554     return p_block;
555 }
556
557 /*****************************************************************************
558  * ParseVorbisComments
559  *****************************************************************************/
560 static void ParseVorbisComments( decoder_t *p_dec )
561 {
562     char *psz_name, *psz_value, *psz_comment;
563     int i = 0;
564
565     while( i < p_dec->p_sys->vc.comments )
566     {
567         psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
568         if( !psz_comment )
569             break;
570         psz_name = psz_comment;
571         psz_value = strchr( psz_comment, '=' );
572         if( psz_value )
573         {
574             *psz_value = '\0';
575             psz_value++;
576
577             if( !p_dec->p_description )
578                 p_dec->p_description = vlc_meta_New();
579             if( p_dec->p_description )
580                 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
581
582             if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
583                      !strcasecmp( psz_name, "RG_RADIO" ) )
584             {
585                 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
586
587                 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
588                 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
589             }
590             else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
591                      !strcasecmp( psz_name, "RG_PEAK" ) )
592             {
593                 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
594
595                 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
596                 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
597             }
598             else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
599                      !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
600             {
601                 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
602
603                 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
604                 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
605             }
606             else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
607             {
608                 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
609
610                 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
611                 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
612             }
613         }
614         free( psz_comment );
615         i++;
616     }
617 }
618
619 /*****************************************************************************
620  * Interleave: helper function to interleave channels
621  *****************************************************************************/
622 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, bool b_decode)
623 {
624     const uint32_t *pi_channels_in;
625     switch( i_channels )
626     {
627         case 8:
628             pi_channels_in = pi_8channels_in;
629             break;
630         case 7:
631             pi_channels_in = pi_7channels_in;
632             break;
633         case 6:
634         case 5:
635             pi_channels_in = pi_6channels_in;
636             break;
637         case 4:
638             pi_channels_in = pi_4channels_in;
639             break;
640         case 3:
641             pi_channels_in = pi_3channels_in;
642             break;
643         default:
644             {
645                 int i;
646                 for( i = 0; i< i_channels; ++i )
647                 {
648                     pi_chan_table[i] = i;
649                 }
650                 return;
651             }
652     }
653
654     if( b_decode )
655         aout_CheckChannelReorder( pi_channels_in, NULL,
656                                   i_channel_mask & AOUT_CHAN_PHYSMASK,
657                                   i_channels,
658                                   pi_chan_table );
659     else
660         aout_CheckChannelReorder( NULL, pi_channels_in,
661                                   i_channel_mask & AOUT_CHAN_PHYSMASK,
662                                   i_channels,
663                                   pi_chan_table );
664 }
665
666 /*****************************************************************************
667  * Interleave: helper function to interleave channels
668  *****************************************************************************/
669 #ifdef MODULE_NAME_IS_tremor
670 static void Interleave( int32_t *p_out, const int32_t **pp_in,
671                         int i_nb_channels, int i_samples, int *pi_chan_table)
672 {
673     int i, j;
674
675     for ( j = 0; j < i_samples; j++ )
676         for ( i = 0; i < i_nb_channels; i++ )
677             p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
678 }
679 #else
680 static void Interleave( float *p_out, const float **pp_in,
681                         int i_nb_channels, int i_samples, int *pi_chan_table )
682 {
683     int i, j;
684
685     for ( j = 0; j < i_samples; j++ )
686         for ( i = 0; i < i_nb_channels; i++ )
687             p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
688 }
689 #endif
690
691 /*****************************************************************************
692  * CloseDecoder: vorbis decoder destruction
693  *****************************************************************************/
694 static void CloseDecoder( vlc_object_t *p_this )
695 {
696     decoder_t *p_dec = (decoder_t *)p_this;
697     decoder_sys_t *p_sys = p_dec->p_sys;
698
699     if( !p_sys->b_packetizer && p_sys->b_has_headers )
700     {
701         vorbis_block_clear( &p_sys->vb );
702         vorbis_dsp_clear( &p_sys->vd );
703     }
704
705     vorbis_comment_clear( &p_sys->vc );
706     vorbis_info_clear( &p_sys->vi );  /* must be called last */
707
708     free( p_sys );
709 }
710
711 #ifndef MODULE_NAME_IS_tremor
712
713 /*****************************************************************************
714  * encoder_sys_t : vorbis encoder descriptor
715  *****************************************************************************/
716 struct encoder_sys_t
717 {
718     /*
719      * Vorbis properties
720      */
721     vorbis_info      vi; /* struct that stores all the static vorbis bitstream
722                             settings */
723     vorbis_comment   vc; /* struct that stores all the bitstream user
724                           * comments */
725     vorbis_dsp_state vd; /* central working state for the packet->PCM
726                           * decoder */
727     vorbis_block     vb; /* local working space for packet->PCM decode */
728
729     int i_last_block_size;
730     int i_samples_delay;
731     int i_channels;
732
733     /*
734     ** Channel reordering
735     */
736     int pi_chan_table[AOUT_CHAN_MAX];
737
738 };
739
740 /*****************************************************************************
741  * OpenEncoder: probe the encoder and return score
742  *****************************************************************************/
743 static int OpenEncoder( vlc_object_t *p_this )
744 {
745     encoder_t *p_enc = (encoder_t *)p_this;
746     encoder_sys_t *p_sys;
747     int i_quality, i_min_bitrate, i_max_bitrate;
748     ogg_packet header[3];
749
750     if( p_enc->fmt_out.i_codec != VLC_CODEC_VORBIS &&
751         !p_enc->b_force )
752     {
753         return VLC_EGENERIC;
754     }
755
756     /* Allocate the memory needed to store the decoder's structure */
757     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
758         return VLC_ENOMEM;
759     p_enc->p_sys = p_sys;
760
761     p_enc->pf_encode_audio = Encode;
762     p_enc->fmt_in.i_codec = VLC_CODEC_FL32;
763     p_enc->fmt_out.i_codec = VLC_CODEC_VORBIS;
764
765     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
766
767     i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
768     if( i_quality > 10 ) i_quality = 10;
769     if( i_quality < 0 ) i_quality = 0;
770
771     if( var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ) i_quality = 0;
772     i_max_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
773     i_min_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "min-bitrate" );
774
775     /* Initialize vorbis encoder */
776     vorbis_info_init( &p_sys->vi );
777
778     if( i_quality > 0 )
779     {
780         /* VBR mode */
781         if( vorbis_encode_setup_vbr( &p_sys->vi,
782               p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
783               i_quality * 0.1 ) )
784         {
785             vorbis_info_clear( &p_sys->vi );
786             free( p_enc->p_sys );
787             msg_Err( p_enc, "VBR mode initialisation failed" );
788             return VLC_EGENERIC;
789         }
790
791         /* Do we have optional hard quality restrictions? */
792         if( i_max_bitrate > 0 || i_min_bitrate > 0 )
793         {
794             struct ovectl_ratemanage_arg ai;
795             vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
796
797             ai.bitrate_hard_min = i_min_bitrate;
798             ai.bitrate_hard_max = i_max_bitrate;
799             ai.management_active = 1;
800
801             vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
802
803         }
804         else
805         {
806             /* Turn off management entirely */
807             vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
808         }
809     }
810     else
811     {
812         if( vorbis_encode_setup_managed( &p_sys->vi,
813               p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
814               i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
815               p_enc->fmt_out.i_bitrate,
816               i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
817           {
818               vorbis_info_clear( &p_sys->vi );
819               msg_Err( p_enc, "CBR mode initialisation failed" );
820               free( p_enc->p_sys );
821               return VLC_EGENERIC;
822           }
823     }
824
825     vorbis_encode_setup_init( &p_sys->vi );
826
827     /* Add a comment */
828     vorbis_comment_init( &p_sys->vc);
829     vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
830
831     /* Set up the analysis state and auxiliary encoding storage */
832     vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
833     vorbis_block_init( &p_sys->vd, &p_sys->vb );
834
835     /* Create and store headers */
836     vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
837                                &header[0], &header[1], &header[2]);
838     for( int i = 0; i < 3; i++ )
839     {
840         if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
841                                 header[i].bytes, header[i].packet ) )
842         {
843             p_enc->fmt_out.i_extra = 0;
844             p_enc->fmt_out.p_extra = NULL;
845         }
846     }
847
848     p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
849     p_sys->i_last_block_size = 0;
850     p_sys->i_samples_delay = 0;
851
852     ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
853             p_enc->fmt_in.audio.i_physical_channels, true);
854
855     return VLC_SUCCESS;
856 }
857
858 /****************************************************************************
859  * Encode: the whole thing
860  ****************************************************************************
861  * This function spits out ogg packets.
862  ****************************************************************************/
863 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
864 {
865     encoder_sys_t *p_sys = p_enc->p_sys;
866     ogg_packet oggpacket;
867     block_t *p_block, *p_chain = NULL;
868     float **buffer;
869     int i;
870     unsigned int j;
871
872     mtime_t i_pts = p_aout_buf->i_pts -
873                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
874                 (mtime_t)p_enc->fmt_in.audio.i_rate;
875
876     p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
877
878     buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
879
880     /* convert samples to float and uninterleave */
881     for( i = 0; i < p_sys->i_channels; i++ )
882     {
883         for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
884         {
885             buffer[i][j]= ((float *)p_aout_buf->p_buffer)
886                                     [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
887         }
888     }
889
890     vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
891
892     while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
893     {
894         int i_samples;
895
896         vorbis_analysis( &p_sys->vb, NULL );
897         vorbis_bitrate_addblock( &p_sys->vb );
898
899         while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
900         {
901             int i_block_size;
902             p_block = block_New( p_enc, oggpacket.bytes );
903             memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
904
905             i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
906
907             if( i_block_size < 0 ) i_block_size = 0;
908             i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
909             p_sys->i_last_block_size = i_block_size;
910
911             p_block->i_length = (mtime_t)1000000 *
912                 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
913
914             p_block->i_dts = p_block->i_pts = i_pts;
915
916             p_sys->i_samples_delay -= i_samples;
917
918             /* Update pts */
919             i_pts += p_block->i_length;
920             block_ChainAppend( &p_chain, p_block );
921         }
922     }
923
924     return p_chain;
925 }
926
927 /*****************************************************************************
928  * CloseEncoder: vorbis encoder destruction
929  *****************************************************************************/
930 static void CloseEncoder( vlc_object_t *p_this )
931 {
932     encoder_t *p_enc = (encoder_t *)p_this;
933     encoder_sys_t *p_sys = p_enc->p_sys;
934
935     vorbis_block_clear( &p_sys->vb );
936     vorbis_dsp_clear( &p_sys->vd );
937     vorbis_comment_clear( &p_sys->vc );
938     vorbis_info_clear( &p_sys->vi );  /* must be called last */
939
940     free( p_sys );
941 }
942
943 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */