]> git.sesse.net Git - vlc/blob - modules/codec/vorbis.c
remove npvlc.dll.manifest and axvlc.dll.manifest while uninstalling
[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     add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
208                  ENC_MAXBR_LONGTEXT, false );
209     add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
210                  ENC_MINBR_LONGTEXT, false );
211     add_bool( ENC_CFG_PREFIX "cbr", 0, NULL, ENC_CBR_TEXT,
212                  ENC_CBR_LONGTEXT, false );
213 #endif
214
215 vlc_module_end ()
216
217 #ifndef MODULE_NAME_IS_tremor
218 static const char *const ppsz_enc_options[] = {
219     "quality", "max-bitrate", "min-bitrate", "cbr", NULL
220 };
221 #endif
222
223 /*****************************************************************************
224  * OpenDecoder: probe the decoder and return score
225  *****************************************************************************/
226 static int OpenDecoder( vlc_object_t *p_this )
227 {
228     decoder_t *p_dec = (decoder_t*)p_this;
229     decoder_sys_t *p_sys;
230
231     if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
232     {
233         return VLC_EGENERIC;
234     }
235
236     /* Allocate the memory needed to store the decoder's structure */
237     if( ( p_dec->p_sys = p_sys =
238           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == 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     input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
609     char *psz_name, *psz_value, *psz_comment;
610     input_item_t *p_item;
611     int i = 0;
612
613     if( p_input->i_object_type != VLC_OBJECT_INPUT )
614         return;
615
616     p_item = input_GetItem( p_input );
617
618     while( i < p_dec->p_sys->vc.comments )
619     {
620         psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
621         if( !psz_comment )
622             break;
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 /*TODO: dot he test at the beginning and save time !! */
632 #ifndef HAVE_TAGLIB
633             if( psz_value && ( *psz_value != '\0' ) )
634             {
635                 if( !strcasecmp( psz_name, "artist" ) )
636                     input_item_SetArtist( p_item, psz_value );
637                 else if( !strcasecmp( psz_name, "title" ) )
638                 {
639                     input_item_SetTitle( p_item, psz_value );
640                     p_item->psz_name = strdup( psz_value );
641                 }
642                 else if( !strcasecmp( psz_name, "album" ) )
643                 {
644                     input_item_SetAlbum( p_item, psz_value );
645                 }
646                 else if( !strcasecmp( psz_name, "musicbrainz_trackid" ) )
647                     input_item_SetTrackID( p_item, psz_value );
648 #if 0 //not used
649                 else if( !strcasecmp( psz_name, "musicbrainz_artistid" ) )
650                     vlc_meta_SetArtistID( p_item, psz_value );
651                 else if( !strcasecmp( psz_name, "musicbrainz_albumid" ) )
652                     input_item_SetAlbumID( p_item, psz_value );
653 #endif
654             }
655 #endif
656             if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
657                      !strcasecmp( psz_name, "RG_RADIO" ) )
658             {
659                 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
660
661                 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
662                 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
663             }
664             else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
665                      !strcasecmp( psz_name, "RG_PEAK" ) )
666             {
667                 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
668
669                 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
670                 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
671             }
672             else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
673                      !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
674             {
675                 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
676
677                 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
678                 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
679             }
680             else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
681             {
682                 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
683
684                 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
685                 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
686             }
687         }
688         free( psz_comment );
689         i++;
690     }
691 }
692
693 /*****************************************************************************
694  * Interleave: helper function to interleave channels
695  *****************************************************************************/
696 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, bool b_decode)
697 {
698     const uint32_t *pi_channels_in;
699     switch( i_channels )
700     {
701         case 6:
702         case 5:
703             pi_channels_in = pi_6channels_in;
704             break;
705         case 4:
706             pi_channels_in = pi_4channels_in;
707             break;
708         case 3:
709             pi_channels_in = pi_3channels_in;
710             break;
711         default:
712             {
713                 int i;
714                 for( i = 0; i< i_channels; ++i )
715                 {
716                     pi_chan_table[i] = i;
717                 }
718                 return;
719             }
720     }
721
722     if( b_decode )
723         aout_CheckChannelReorder( pi_channels_in, NULL,
724                                   i_channel_mask & AOUT_CHAN_PHYSMASK,
725                                   i_channels,
726                                   pi_chan_table );
727     else
728         aout_CheckChannelReorder( NULL, pi_channels_in,
729                                   i_channel_mask & AOUT_CHAN_PHYSMASK,
730                                   i_channels,
731                                   pi_chan_table );
732 }
733
734 /*****************************************************************************
735  * Interleave: helper function to interleave channels
736  *****************************************************************************/
737 #ifdef MODULE_NAME_IS_tremor
738 static void Interleave( int32_t *p_out, const int32_t **pp_in,
739                         int i_nb_channels, int i_samples, int *pi_chan_table)
740 {
741     int i, j;
742
743     for ( j = 0; j < i_samples; j++ )
744         for ( i = 0; i < i_nb_channels; i++ )
745             p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
746 }
747 #else
748 static void Interleave( float *p_out, const float **pp_in,
749                         int i_nb_channels, int i_samples, int *pi_chan_table )
750 {
751     int i, j;
752
753     for ( j = 0; j < i_samples; j++ )
754         for ( i = 0; i < i_nb_channels; i++ )
755             p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
756 }
757 #endif
758
759 /*****************************************************************************
760  * CloseDecoder: vorbis decoder destruction
761  *****************************************************************************/
762 static void CloseDecoder( vlc_object_t *p_this )
763 {
764     decoder_t *p_dec = (decoder_t *)p_this;
765     decoder_sys_t *p_sys = p_dec->p_sys;
766
767     if( !p_sys->b_packetizer && p_sys->i_headers > 3 )
768     {
769         vorbis_block_clear( &p_sys->vb );
770         vorbis_dsp_clear( &p_sys->vd );
771     }
772
773     vorbis_comment_clear( &p_sys->vc );
774     vorbis_info_clear( &p_sys->vi );  /* must be called last */
775
776     free( p_sys );
777 }
778
779 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
780
781 /*****************************************************************************
782  * encoder_sys_t : vorbis encoder descriptor
783  *****************************************************************************/
784 struct encoder_sys_t
785 {
786     /*
787      * Vorbis properties
788      */
789     vorbis_info      vi; /* struct that stores all the static vorbis bitstream
790                             settings */
791     vorbis_comment   vc; /* struct that stores all the bitstream user
792                           * comments */
793     vorbis_dsp_state vd; /* central working state for the packet->PCM
794                           * decoder */
795     vorbis_block     vb; /* local working space for packet->PCM decode */
796
797     int i_last_block_size;
798     int i_samples_delay;
799     int i_channels;
800
801     /*
802      * Common properties
803      */
804     mtime_t i_pts;
805
806     /*
807     ** Channel reordering
808     */
809     int pi_chan_table[AOUT_CHAN_MAX];
810
811 };
812
813 /*****************************************************************************
814  * OpenEncoder: probe the encoder and return score
815  *****************************************************************************/
816 static int OpenEncoder( vlc_object_t *p_this )
817 {
818     encoder_t *p_enc = (encoder_t *)p_this;
819     encoder_sys_t *p_sys;
820     int i_quality, i_min_bitrate, i_max_bitrate, i;
821     ogg_packet header[3];
822     vlc_value_t val;
823     uint8_t *p_extra;
824
825     if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') &&
826         !p_enc->b_force )
827     {
828         return VLC_EGENERIC;
829     }
830
831     /* Allocate the memory needed to store the decoder's structure */
832     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
833         return VLC_ENOMEM;
834     p_enc->p_sys = p_sys;
835
836     p_enc->pf_encode_audio = Encode;
837     p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
838     p_enc->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
839
840     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
841
842     var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
843     i_quality = val.i_int;
844     if( i_quality > 10 ) i_quality = 10;
845     if( i_quality < 0 ) i_quality = 0;
846     var_Get( p_enc, ENC_CFG_PREFIX "cbr", &val );
847     if( val.b_bool ) i_quality = 0;
848     var_Get( p_enc, ENC_CFG_PREFIX "max-bitrate", &val );
849     i_max_bitrate = val.i_int;
850     var_Get( p_enc, ENC_CFG_PREFIX "min-bitrate", &val );
851     i_min_bitrate = val.i_int;
852
853     /* Initialize vorbis encoder */
854     vorbis_info_init( &p_sys->vi );
855
856     if( i_quality > 0 )
857     {
858         /* VBR mode */
859         if( vorbis_encode_setup_vbr( &p_sys->vi,
860               p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
861               i_quality * 0.1 ) )
862         {
863             vorbis_info_clear( &p_sys->vi );
864             free( p_enc->p_sys );
865             msg_Err( p_enc, "VBR mode initialisation failed" );
866             return VLC_EGENERIC;
867         }
868
869         /* Do we have optional hard quality restrictions? */
870         if( i_max_bitrate > 0 || i_min_bitrate > 0 )
871         {
872             struct ovectl_ratemanage_arg ai;
873             vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
874
875             ai.bitrate_hard_min = i_min_bitrate;
876             ai.bitrate_hard_max = i_max_bitrate;
877             ai.management_active = 1;
878
879             vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
880
881         }
882         else
883         {
884             /* Turn off management entirely */
885             vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
886         }
887     }
888     else
889     {
890         if( vorbis_encode_setup_managed( &p_sys->vi,
891               p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
892               i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
893               p_enc->fmt_out.i_bitrate,
894               i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
895           {
896               vorbis_info_clear( &p_sys->vi );
897               msg_Err( p_enc, "CBR mode initialisation failed" );
898               free( p_enc->p_sys );
899               return VLC_EGENERIC;
900           }
901     }
902
903     vorbis_encode_setup_init( &p_sys->vi );
904
905     /* Add a comment */
906     vorbis_comment_init( &p_sys->vc);
907     vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
908
909     /* Set up the analysis state and auxiliary encoding storage */
910     vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
911     vorbis_block_init( &p_sys->vd, &p_sys->vb );
912
913     /* Create and store headers */
914     vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
915                                &header[0], &header[1], &header[2]);
916     p_enc->fmt_out.i_extra = 3 * 2 + header[0].bytes +
917        header[1].bytes + header[2].bytes;
918     p_extra = p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
919     for( i = 0; i < 3; i++ )
920     {
921         *(p_extra++) = header[i].bytes >> 8;
922         *(p_extra++) = header[i].bytes & 0xFF;
923         memcpy( p_extra, header[i].packet, header[i].bytes );
924         p_extra += header[i].bytes;
925     }
926
927     p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
928     p_sys->i_last_block_size = 0;
929     p_sys->i_samples_delay = 0;
930     p_sys->i_pts = 0;
931
932     ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
933             p_enc->fmt_in.audio.i_physical_channels, true);
934
935     return VLC_SUCCESS;
936 }
937
938 /****************************************************************************
939  * Encode: the whole thing
940  ****************************************************************************
941  * This function spits out ogg packets.
942  ****************************************************************************/
943 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
944 {
945     encoder_sys_t *p_sys = p_enc->p_sys;
946     ogg_packet oggpacket;
947     block_t *p_block, *p_chain = NULL;
948     float **buffer;
949     int i;
950     unsigned int j;
951
952     p_sys->i_pts = p_aout_buf->start_date -
953                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
954                 (mtime_t)p_enc->fmt_in.audio.i_rate;
955
956     p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
957
958     buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
959
960     /* convert samples to float and uninterleave */
961     for( i = 0; i < p_sys->i_channels; i++ )
962     {
963         for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
964         {
965             buffer[i][j]= ((float *)p_aout_buf->p_buffer)
966                                     [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
967         }
968     }
969
970     vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
971
972     while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
973     {
974         int i_samples;
975
976         vorbis_analysis( &p_sys->vb, NULL );
977         vorbis_bitrate_addblock( &p_sys->vb );
978
979         while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
980         {
981             int i_block_size;
982             p_block = block_New( p_enc, oggpacket.bytes );
983             memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
984
985             i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
986
987             if( i_block_size < 0 ) i_block_size = 0;
988             i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
989             p_sys->i_last_block_size = i_block_size;
990
991             p_block->i_length = (mtime_t)1000000 *
992                 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
993
994             p_block->i_dts = p_block->i_pts = p_sys->i_pts;
995
996             p_sys->i_samples_delay -= i_samples;
997
998             /* Update pts */
999             p_sys->i_pts += p_block->i_length;
1000             block_ChainAppend( &p_chain, p_block );
1001         }
1002     }
1003
1004     return p_chain;
1005 }
1006
1007 /*****************************************************************************
1008  * CloseEncoder: vorbis encoder destruction
1009  *****************************************************************************/
1010 static void CloseEncoder( vlc_object_t *p_this )
1011 {
1012     encoder_t *p_enc = (encoder_t *)p_this;
1013     encoder_sys_t *p_sys = p_enc->p_sys;
1014
1015     vorbis_block_clear( &p_sys->vb );
1016     vorbis_dsp_clear( &p_sys->vd );
1017     vorbis_comment_clear( &p_sys->vc );
1018     vorbis_info_clear( &p_sys->vi );  /* must be called last */
1019
1020     free( p_sys );
1021 }
1022
1023 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */