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