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