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