1 /*****************************************************************************
2 * vorbis.c: vorbis decoder/encoder/packetizer module making use of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 2001-2003 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc_codec.h>
30 #include <vlc_input.h>
35 #ifdef MODULE_NAME_IS_tremor
36 #include <tremor/ivorbiscodec.h>
39 #include <vorbis/codec.h>
42 #ifdef HAVE_VORBIS_VORBISENC_H
43 # include <vorbis/vorbisenc.h>
44 # ifndef OV_ECTL_RATEMANAGE_AVG
45 # define OV_ECTL_RATEMANAGE_AVG 0x0
51 /*****************************************************************************
52 * decoder_sys_t : vorbis decoder descriptor
53 *****************************************************************************/
57 vlc_bool_t b_packetizer;
67 vorbis_info vi; /* struct that stores all the static vorbis bitstream
69 vorbis_comment vc; /* struct that stores all the bitstream user
71 vorbis_dsp_state vd; /* central working state for the packet->PCM
73 vorbis_block vb; /* local working space for packet->PCM decode */
78 audio_date_t end_date;
79 int i_last_block_size;
86 int pi_chan_table[AOUT_CHAN_MAX];
89 static int pi_channels_maps[7] =
93 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
94 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
95 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
96 | AOUT_CHAN_REARRIGHT,
97 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
98 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
99 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
100 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
104 ** channel order as defined in http://www.ogghelp.com/ogg/glossary.cfm#Audio_Channels
107 /* recommended vorbis channel order for 6 channels */
108 static const uint32_t pi_6channels_in[] =
109 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
110 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
112 /* recommended vorbis channel order for 4 channels */
113 static const uint32_t pi_4channels_in[] =
114 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
116 /* recommended vorbis channel order for 3 channels */
117 static const uint32_t pi_3channels_in[] =
118 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
120 /* our internal channel order (WG-4 order) */
121 static const uint32_t pi_channels_out[] =
122 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
123 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
125 /****************************************************************************
127 ****************************************************************************/
128 static int OpenDecoder ( vlc_object_t * );
129 static int OpenPacketizer( vlc_object_t * );
130 static void CloseDecoder ( vlc_object_t * );
131 static void *DecodeBlock ( decoder_t *, block_t ** );
133 static int ProcessHeaders( decoder_t * );
134 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
136 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
137 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
140 static void ParseVorbisComments( decoder_t * );
143 static void ConfigureChannelOrder(int *, int, uint32_t, vlc_bool_t );
145 #ifdef MODULE_NAME_IS_tremor
146 static void Interleave ( int32_t *, const int32_t **, int, int, int * );
148 static void Interleave ( float *, const float **, int, int, int * );
151 #ifndef MODULE_NAME_IS_tremor
152 static int OpenEncoder ( vlc_object_t * );
153 static void CloseEncoder ( vlc_object_t * );
154 static block_t *Encode ( encoder_t *, aout_buffer_t * );
157 /*****************************************************************************
159 *****************************************************************************/
160 #define ENC_QUALITY_TEXT N_("Encoding quality")
161 #define ENC_QUALITY_LONGTEXT N_( \
162 "Enforce a quality between 1 (low) and 10 (high), instead " \
163 "of specifying a particular bitrate. This will produce a VBR stream." )
164 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
165 #define ENC_MAXBR_LONGTEXT N_( \
166 "Maximum bitrate in kbps. This is useful for streaming applications." )
167 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
168 #define ENC_MINBR_LONGTEXT N_( \
169 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
170 #define ENC_CBR_TEXT N_("CBR encoding")
171 #define ENC_CBR_LONGTEXT N_( \
172 "Force a constant bitrate encoding (CBR)." )
175 set_shortname( "Vorbis" );
176 set_description( _("Vorbis audio decoder") );
177 #ifdef MODULE_NAME_IS_tremor
178 set_capability( "decoder", 90 );
180 set_capability( "decoder", 100 );
182 set_category( CAT_INPUT );
183 set_subcategory( SUBCAT_INPUT_ACODEC );
184 set_callbacks( OpenDecoder, CloseDecoder );
187 set_description( _("Vorbis audio packetizer") );
188 set_capability( "packetizer", 100 );
189 set_callbacks( OpenPacketizer, CloseDecoder );
191 #ifndef MODULE_NAME_IS_tremor
192 # define ENC_CFG_PREFIX "sout-vorbis-"
194 set_description( _("Vorbis audio encoder") );
195 set_capability( "encoder", 100 );
196 #if defined(HAVE_VORBIS_VORBISENC_H)
197 set_callbacks( OpenEncoder, CloseEncoder );
200 add_integer( ENC_CFG_PREFIX "quality", 0, NULL, ENC_QUALITY_TEXT,
201 ENC_QUALITY_LONGTEXT, VLC_FALSE );
202 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
203 ENC_MAXBR_LONGTEXT, VLC_FALSE );
204 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
205 ENC_MINBR_LONGTEXT, VLC_FALSE );
206 add_bool( ENC_CFG_PREFIX "cbr", 0, NULL, ENC_CBR_TEXT,
207 ENC_CBR_LONGTEXT, VLC_FALSE );
212 #ifndef MODULE_NAME_IS_tremor
213 static const char *ppsz_enc_options[] = {
214 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
218 /*****************************************************************************
219 * OpenDecoder: probe the decoder and return score
220 *****************************************************************************/
221 static int OpenDecoder( vlc_object_t *p_this )
223 decoder_t *p_dec = (decoder_t*)p_this;
224 decoder_sys_t *p_sys;
226 if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
231 /* Allocate the memory needed to store the decoder's structure */
232 if( ( p_dec->p_sys = p_sys =
233 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
235 msg_Err( p_dec, "out of memory" );
240 aout_DateSet( &p_sys->end_date, 0 );
241 p_sys->i_last_block_size = 0;
242 p_sys->b_packetizer = VLC_FALSE;
243 p_sys->i_headers = 0;
244 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
246 /* Take care of vorbis init */
247 vorbis_info_init( &p_sys->vi );
248 vorbis_comment_init( &p_sys->vc );
250 /* Set output properties */
251 p_dec->fmt_out.i_cat = AUDIO_ES;
252 #ifdef MODULE_NAME_IS_tremor
253 p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
255 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
259 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
261 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
267 static int OpenPacketizer( vlc_object_t *p_this )
269 decoder_t *p_dec = (decoder_t*)p_this;
271 int i_ret = OpenDecoder( p_this );
273 if( i_ret == VLC_SUCCESS )
275 p_dec->p_sys->b_packetizer = VLC_TRUE;
276 p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
282 /****************************************************************************
283 * DecodeBlock: the whole thing
284 ****************************************************************************
285 * This function must be fed with ogg packets.
286 ****************************************************************************/
287 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
289 decoder_sys_t *p_sys = p_dec->p_sys;
290 ogg_packet oggpacket;
292 if( !pp_block ) return NULL;
296 /* Block to Ogg packet */
297 oggpacket.packet = (*pp_block)->p_buffer;
298 oggpacket.bytes = (*pp_block)->i_buffer;
300 if( (*pp_block)->i_rate > 0 )
301 p_sys->i_input_rate = (*pp_block)->i_rate;
305 if( p_sys->b_packetizer ) return NULL;
307 /* Block to Ogg packet */
308 oggpacket.packet = NULL;
312 oggpacket.granulepos = -1;
315 oggpacket.packetno = 0;
317 /* Check for headers */
318 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
320 /* Headers already available as extra data */
321 msg_Dbg( p_dec, "headers already available as extra data" );
322 p_sys->i_headers = 3;
324 else if( oggpacket.bytes && p_sys->i_headers < 3 )
326 /* Backup headers as extra data */
329 p_dec->fmt_in.p_extra =
330 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
331 oggpacket.bytes + 2 );
332 p_extra = (uint8_t *)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
333 *(p_extra++) = oggpacket.bytes >> 8;
334 *(p_extra++) = oggpacket.bytes & 0xFF;
336 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
337 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
339 block_Release( *pp_block );
344 if( p_sys->i_headers == 3 )
346 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
348 p_sys->i_headers = 0;
349 p_dec->fmt_in.i_extra = 0;
350 block_Release( *pp_block );
353 else p_sys->i_headers++;
356 return ProcessPacket( p_dec, &oggpacket, pp_block );
359 /*****************************************************************************
360 * ProcessHeaders: process Vorbis headers.
361 *****************************************************************************/
362 static int ProcessHeaders( decoder_t *p_dec )
364 decoder_sys_t *p_sys = p_dec->p_sys;
365 ogg_packet oggpacket;
369 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
371 oggpacket.granulepos = -1;
372 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
374 oggpacket.packetno = 0;
375 p_extra = p_dec->fmt_in.p_extra;
376 i_extra = p_dec->fmt_in.i_extra;
378 /* Take care of the initial Vorbis header */
379 oggpacket.bytes = *(p_extra++) << 8;
380 oggpacket.bytes |= (*(p_extra++) & 0xFF);
381 oggpacket.packet = p_extra;
382 p_extra += oggpacket.bytes;
383 i_extra -= (oggpacket.bytes + 2);
386 msg_Err( p_dec, "header data corrupted");
390 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
392 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
396 /* Setup the format */
397 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
398 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
400 if( p_dec->fmt_out.audio.i_channels < 0 ||
401 p_dec->fmt_out.audio.i_channels > 6 )
403 msg_Err( p_dec, "invalid number of channels (not between 1 and 6): %i",
404 p_dec->fmt_out.audio.i_channels );
408 p_dec->fmt_out.audio.i_physical_channels =
409 p_dec->fmt_out.audio.i_original_channels =
410 pi_channels_maps[p_sys->vi.channels];
411 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
413 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
414 aout_DateSet( &p_sys->end_date, 0 );
416 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
417 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
419 /* The next packet in order is the comments header */
421 oggpacket.bytes = *(p_extra++) << 8;
422 oggpacket.bytes |= (*(p_extra++) & 0xFF);
423 oggpacket.packet = p_extra;
424 p_extra += oggpacket.bytes;
425 i_extra -= (oggpacket.bytes + 2);
428 msg_Err( p_dec, "header data corrupted");
432 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
434 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
438 ParseVorbisComments( p_dec );
441 /* The next packet in order is the codebooks header
442 * We need to watch out that this packet is not missing as a
443 * missing or corrupted header is fatal. */
444 oggpacket.bytes = *(p_extra++) << 8;
445 oggpacket.bytes |= (*(p_extra++) & 0xFF);
446 oggpacket.packet = p_extra;
447 i_extra -= (oggpacket.bytes + 2);
450 msg_Err( p_dec, "header data corrupted");
454 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
456 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
460 if( !p_sys->b_packetizer )
462 /* Initialize the Vorbis packet->PCM decoder */
463 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
464 vorbis_block_init( &p_sys->vd, &p_sys->vb );
468 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
469 p_dec->fmt_out.p_extra =
470 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
471 memcpy( p_dec->fmt_out.p_extra,
472 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
475 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
476 p_dec->fmt_out.audio.i_physical_channels, VLC_TRUE);
481 /*****************************************************************************
482 * ProcessPacket: processes a Vorbis packet.
483 *****************************************************************************/
484 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
487 decoder_sys_t *p_sys = p_dec->p_sys;
488 block_t *p_block = *pp_block;
490 /* Date management */
491 if( p_block && p_block->i_pts > 0 &&
492 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
494 aout_DateSet( &p_sys->end_date, p_block->i_pts );
497 if( !aout_DateGet( &p_sys->end_date ) )
499 /* We've just started the stream, wait for the first PTS. */
500 if( p_block ) block_Release( p_block );
504 *pp_block = NULL; /* To avoid being fed the same packet again */
506 if( p_sys->b_packetizer )
508 return SendPacket( p_dec, p_oggpacket, p_block );
512 aout_buffer_t *p_aout_buffer;
514 if( p_sys->i_headers >= 3 )
515 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
517 p_aout_buffer = NULL;
519 if( p_block ) block_Release( p_block );
520 return p_aout_buffer;
524 /*****************************************************************************
525 * DecodePacket: decodes a Vorbis packet.
526 *****************************************************************************/
527 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
529 decoder_sys_t *p_sys = p_dec->p_sys;
532 #ifdef MODULE_NAME_IS_tremor
538 if( p_oggpacket->bytes &&
539 #ifdef MODULE_NAME_IS_tremor
540 vorbis_synthesis( &p_sys->vb, p_oggpacket, 1 ) == 0 )
542 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
544 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
546 /* **pp_pcm is a multichannel float vector. In stereo, for
547 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
548 * the size of each channel. Convert the float values
549 * (-1.<=range<=1.) to whatever PCM format and write it out */
551 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
554 aout_buffer_t *p_aout_buffer;
557 p_dec->pf_aout_buffer_new( p_dec, i_samples );
559 if( p_aout_buffer == NULL ) return NULL;
561 /* Interleave the samples */
562 #ifdef MODULE_NAME_IS_tremor
563 Interleave( (int32_t *)p_aout_buffer->p_buffer,
564 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
566 Interleave( (float *)p_aout_buffer->p_buffer,
567 (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
570 /* Tell libvorbis how many samples we actually consumed */
571 vorbis_synthesis_read( &p_sys->vd, i_samples );
573 /* Date management */
574 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
575 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
576 i_samples * p_sys->i_input_rate / INPUT_RATE_DEFAULT );
577 return p_aout_buffer;
585 /*****************************************************************************
586 * SendPacket: send an ogg dated packet to the stream output.
587 *****************************************************************************/
588 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
591 decoder_sys_t *p_sys = p_dec->p_sys;
592 int i_block_size, i_samples;
594 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
595 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
596 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
597 p_sys->i_last_block_size = i_block_size;
599 /* Date management */
600 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
602 if( p_sys->i_headers >= 3 )
603 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
604 i_samples * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
607 p_block->i_length = 0;
613 /*****************************************************************************
614 * ParseVorbisComments
615 *****************************************************************************/
616 static void ParseVorbisComments( decoder_t *p_dec )
618 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
619 char *psz_name, *psz_value, *psz_comment;
620 input_item_t *p_item;
623 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
625 p_item = input_GetItem( p_input );
627 while( i < p_dec->p_sys->vc.comments )
629 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
632 msg_Warn( p_dec, "out of memory" );
635 psz_name = psz_comment;
636 psz_value = strchr( psz_comment, '=' );
641 input_Control( p_input, INPUT_ADD_INFO, _("Vorbis comment"),
642 psz_name, "%s", psz_value );
643 if( !strcasecmp( psz_name, "artist" ) )
645 if( psz_value && ( *psz_value != '\0' ) )
647 input_item_SetArtist( p_item, psz_value );
648 input_ItemAddInfo( p_item,
649 _(VLC_META_INFO_CAT),
654 else if( !strcasecmp( psz_name, "title" ) )
656 if( psz_value && ( *psz_value != '\0' ) )
658 input_item_SetTitle( p_item, psz_value );
659 p_item->psz_name = strdup( psz_value );
662 else if( !strcasecmp( psz_name, "album" ) )
664 if( psz_value && ( *psz_value != '\0' ) )
666 input_item_SetAlbum( p_item, psz_value );
669 else if( !strcasecmp( psz_name, "musicbrainz_trackid" ) )
671 if( psz_value && ( *psz_value != '\0' ) )
673 input_item_SetTrackID( p_item, psz_value );
676 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
677 !strcasecmp( psz_name, "RG_RADIO" ) )
679 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
681 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
682 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
684 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
685 !strcasecmp( psz_name, "RG_PEAK" ) )
687 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
689 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
690 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
692 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
693 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
695 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
697 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
698 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
700 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
702 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
704 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
705 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
708 else if( !strcasecmp( psz_name, "musicbrainz_artistid" ) )
710 if( psz_value && ( *psz_value != '\0' ) )
712 vlc_meta_SetArtistID( p_item, psz_value );
715 else if( !strcasecmp( psz_name, "musicbrainz_albumid" ) )
717 if( psz_value && ( *psz_value != '\0' ) )
719 input_item_SetAlbumID( p_item, psz_value );
725 var_SetInteger( p_input, "item-change", p_item->i_id );
732 /*****************************************************************************
733 * Interleave: helper function to interleave channels
734 *****************************************************************************/
735 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, vlc_bool_t b_decode)
737 const uint32_t *pi_channels_in;
742 pi_channels_in = pi_6channels_in;
745 pi_channels_in = pi_4channels_in;
748 pi_channels_in = pi_3channels_in;
753 for( i = 0; i< i_channels; ++i )
755 pi_chan_table[i] = i;
762 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
763 i_channel_mask & AOUT_CHAN_PHYSMASK,
767 aout_CheckChannelReorder( pi_channels_out, pi_channels_in,
768 i_channel_mask & AOUT_CHAN_PHYSMASK,
773 /*****************************************************************************
774 * Interleave: helper function to interleave channels
775 *****************************************************************************/
776 #ifdef MODULE_NAME_IS_tremor
777 static void Interleave( int32_t *p_out, const int32_t **pp_in,
778 int i_nb_channels, int i_samples, int *pi_chan_table)
782 for ( j = 0; j < i_samples; j++ )
783 for ( i = 0; i < i_nb_channels; i++ )
784 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
787 static void Interleave( float *p_out, const float **pp_in,
788 int i_nb_channels, int i_samples, int *pi_chan_table )
792 for ( j = 0; j < i_samples; j++ )
793 for ( i = 0; i < i_nb_channels; i++ )
794 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
798 /*****************************************************************************
799 * CloseDecoder: vorbis decoder destruction
800 *****************************************************************************/
801 static void CloseDecoder( vlc_object_t *p_this )
803 decoder_t *p_dec = (decoder_t *)p_this;
804 decoder_sys_t *p_sys = p_dec->p_sys;
806 if( !p_sys->b_packetizer && p_sys->i_headers > 3 )
808 vorbis_block_clear( &p_sys->vb );
809 vorbis_dsp_clear( &p_sys->vd );
812 vorbis_comment_clear( &p_sys->vc );
813 vorbis_info_clear( &p_sys->vi ); /* must be called last */
818 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
820 /*****************************************************************************
821 * encoder_sys_t : vorbis encoder descriptor
822 *****************************************************************************/
828 vorbis_info vi; /* struct that stores all the static vorbis bitstream
830 vorbis_comment vc; /* struct that stores all the bitstream user
832 vorbis_dsp_state vd; /* central working state for the packet->PCM
834 vorbis_block vb; /* local working space for packet->PCM decode */
836 int i_last_block_size;
846 ** Channel reordering
848 int pi_chan_table[AOUT_CHAN_MAX];
852 /*****************************************************************************
853 * OpenEncoder: probe the encoder and return score
854 *****************************************************************************/
855 static int OpenEncoder( vlc_object_t *p_this )
857 encoder_t *p_enc = (encoder_t *)p_this;
858 encoder_sys_t *p_sys;
859 int i_quality, i_min_bitrate, i_max_bitrate, i;
860 ogg_packet header[3];
864 if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') &&
870 /* Allocate the memory needed to store the decoder's structure */
871 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
873 msg_Err( p_enc, "out of memory" );
876 p_enc->p_sys = p_sys;
878 p_enc->pf_encode_audio = Encode;
879 p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
880 p_enc->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
882 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
884 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
885 i_quality = val.i_int;
886 if( i_quality > 10 ) i_quality = 10;
887 if( i_quality < 0 ) i_quality = 0;
888 var_Get( p_enc, ENC_CFG_PREFIX "cbr", &val );
889 if( val.b_bool ) i_quality = 0;
890 var_Get( p_enc, ENC_CFG_PREFIX "max-bitrate", &val );
891 i_max_bitrate = val.i_int;
892 var_Get( p_enc, ENC_CFG_PREFIX "min-bitrate", &val );
893 i_min_bitrate = val.i_int;
895 /* Initialize vorbis encoder */
896 vorbis_info_init( &p_sys->vi );
901 if( vorbis_encode_setup_vbr( &p_sys->vi,
902 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
905 vorbis_info_clear( &p_sys->vi );
906 free( p_enc->p_sys );
907 msg_Err( p_enc, "VBR mode initialisation failed" );
911 /* Do we have optional hard quality restrictions? */
912 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
914 struct ovectl_ratemanage_arg ai;
915 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
917 ai.bitrate_hard_min = i_min_bitrate;
918 ai.bitrate_hard_max = i_max_bitrate;
919 ai.management_active = 1;
921 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
926 /* Turn off management entirely */
927 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
932 if( vorbis_encode_setup_managed( &p_sys->vi,
933 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
934 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
935 p_enc->fmt_out.i_bitrate,
936 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
938 vorbis_info_clear( &p_sys->vi );
939 msg_Err( p_enc, "CBR mode initialisation failed" );
940 free( p_enc->p_sys );
945 vorbis_encode_setup_init( &p_sys->vi );
948 vorbis_comment_init( &p_sys->vc);
949 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
951 /* Set up the analysis state and auxiliary encoding storage */
952 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
953 vorbis_block_init( &p_sys->vd, &p_sys->vb );
955 /* Create and store headers */
956 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
957 &header[0], &header[1], &header[2]);
958 p_enc->fmt_out.i_extra = 3 * 2 + header[0].bytes +
959 header[1].bytes + header[2].bytes;
960 p_extra = p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
961 for( i = 0; i < 3; i++ )
963 *(p_extra++) = header[i].bytes >> 8;
964 *(p_extra++) = header[i].bytes & 0xFF;
965 memcpy( p_extra, header[i].packet, header[i].bytes );
966 p_extra += header[i].bytes;
969 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
970 p_sys->i_last_block_size = 0;
971 p_sys->i_samples_delay = 0;
974 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
975 p_enc->fmt_in.audio.i_physical_channels, VLC_TRUE);
980 /****************************************************************************
981 * Encode: the whole thing
982 ****************************************************************************
983 * This function spits out ogg packets.
984 ****************************************************************************/
985 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
987 encoder_sys_t *p_sys = p_enc->p_sys;
988 ogg_packet oggpacket;
989 block_t *p_block, *p_chain = NULL;
994 p_sys->i_pts = p_aout_buf->start_date -
995 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
996 (mtime_t)p_enc->fmt_in.audio.i_rate;
998 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
1000 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
1002 /* convert samples to float and uninterleave */
1003 for( i = 0; i < p_sys->i_channels; i++ )
1005 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
1007 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
1008 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
1012 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
1014 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
1018 vorbis_analysis( &p_sys->vb, NULL );
1019 vorbis_bitrate_addblock( &p_sys->vb );
1021 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
1024 p_block = block_New( p_enc, oggpacket.bytes );
1025 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
1027 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
1029 if( i_block_size < 0 ) i_block_size = 0;
1030 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
1031 p_sys->i_last_block_size = i_block_size;
1033 p_block->i_length = (mtime_t)1000000 *
1034 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
1036 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
1038 p_sys->i_samples_delay -= i_samples;
1041 p_sys->i_pts += p_block->i_length;
1042 block_ChainAppend( &p_chain, p_block );
1049 /*****************************************************************************
1050 * CloseEncoder: vorbis encoder destruction
1051 *****************************************************************************/
1052 static void CloseEncoder( vlc_object_t *p_this )
1054 encoder_t *p_enc = (encoder_t *)p_this;
1055 encoder_sys_t *p_sys = p_enc->p_sys;
1057 vorbis_block_clear( &p_sys->vb );
1058 vorbis_dsp_clear( &p_sys->vd );
1059 vorbis_comment_clear( &p_sys->vc );
1060 vorbis_info_clear( &p_sys->vi ); /* must be called last */
1065 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */