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
10 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
38 #include <vlc_input.h>
39 #include <vlc_playlist.h>
44 #ifdef MODULE_NAME_IS_tremor
45 #include <tremor/ivorbiscodec.h>
48 #include <vorbis/codec.h>
51 #ifdef HAVE_VORBIS_VORBISENC_H
52 # include <vorbis/vorbisenc.h>
53 # ifndef OV_ECTL_RATEMANAGE_AVG
54 # define OV_ECTL_RATEMANAGE_AVG 0x0
60 /*****************************************************************************
61 * decoder_sys_t : vorbis decoder descriptor
62 *****************************************************************************/
76 vorbis_info vi; /* struct that stores all the static vorbis bitstream
78 vorbis_comment vc; /* struct that stores all the bitstream user
80 vorbis_dsp_state vd; /* central working state for the packet->PCM
82 vorbis_block vb; /* local working space for packet->PCM decode */
87 audio_date_t end_date;
88 int i_last_block_size;
95 int pi_chan_table[AOUT_CHAN_MAX];
98 static int pi_channels_maps[9] =
102 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
103 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
104 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
105 | AOUT_CHAN_REARRIGHT,
106 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
107 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
108 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
109 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
110 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
111 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
112 | AOUT_CHAN_MIDDLERIGHT,
113 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
114 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
119 ** channel order as defined in http://www.ogghelp.com/ogg/glossary.cfm#Audio_Channels
122 /* recommended vorbis channel order for 6 channels */
123 static const uint32_t pi_6channels_in[] =
124 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
125 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
127 /* recommended vorbis channel order for 4 channels */
128 static const uint32_t pi_4channels_in[] =
129 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
131 /* recommended vorbis channel order for 3 channels */
132 static const uint32_t pi_3channels_in[] =
133 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
135 /* our internal channel order (WG-4 order) */
136 static const uint32_t pi_channels_out[] =
137 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
138 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
140 /****************************************************************************
142 ****************************************************************************/
143 static int OpenDecoder ( vlc_object_t * );
144 static int OpenPacketizer( vlc_object_t * );
145 static void CloseDecoder ( vlc_object_t * );
146 static void *DecodeBlock ( decoder_t *, block_t ** );
148 static int ProcessHeaders( decoder_t * );
149 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
151 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
152 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
154 static void ParseVorbisComments( decoder_t * );
156 static void ConfigureChannelOrder(int *, int, uint32_t, bool );
158 #ifdef MODULE_NAME_IS_tremor
159 static void Interleave ( int32_t *, const int32_t **, int, int, int * );
161 static void Interleave ( float *, const float **, int, int, int * );
164 #ifndef MODULE_NAME_IS_tremor
165 static int OpenEncoder ( vlc_object_t * );
166 static void CloseEncoder ( vlc_object_t * );
167 static block_t *Encode ( encoder_t *, aout_buffer_t * );
170 /*****************************************************************************
172 *****************************************************************************/
173 #define ENC_QUALITY_TEXT N_("Encoding quality")
174 #define ENC_QUALITY_LONGTEXT N_( \
175 "Enforce a quality between 1 (low) and 10 (high), instead " \
176 "of specifying a particular bitrate. This will produce a VBR stream." )
177 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
178 #define ENC_MAXBR_LONGTEXT N_( \
179 "Maximum bitrate in kbps. This is useful for streaming applications." )
180 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
181 #define ENC_MINBR_LONGTEXT N_( \
182 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
183 #define ENC_CBR_TEXT N_("CBR encoding")
184 #define ENC_CBR_LONGTEXT N_( \
185 "Force a constant bitrate encoding (CBR)." )
188 set_shortname( "Vorbis" );
189 set_description( N_("Vorbis audio decoder") );
190 #ifdef MODULE_NAME_IS_tremor
191 set_capability( "decoder", 90 );
193 set_capability( "decoder", 100 );
195 set_category( CAT_INPUT );
196 set_subcategory( SUBCAT_INPUT_ACODEC );
197 set_callbacks( OpenDecoder, CloseDecoder );
200 set_description( N_("Vorbis audio packetizer") );
201 set_capability( "packetizer", 100 );
202 set_callbacks( OpenPacketizer, CloseDecoder );
204 #ifndef MODULE_NAME_IS_tremor
205 # define ENC_CFG_PREFIX "sout-vorbis-"
207 set_description( N_("Vorbis audio encoder") );
208 set_capability( "encoder", 100 );
209 #if defined(HAVE_VORBIS_VORBISENC_H)
210 set_callbacks( OpenEncoder, CloseEncoder );
213 add_integer( ENC_CFG_PREFIX "quality", 0, NULL, ENC_QUALITY_TEXT,
214 ENC_QUALITY_LONGTEXT, false );
215 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
216 ENC_MAXBR_LONGTEXT, false );
217 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
218 ENC_MINBR_LONGTEXT, false );
219 add_bool( ENC_CFG_PREFIX "cbr", 0, NULL, ENC_CBR_TEXT,
220 ENC_CBR_LONGTEXT, false );
225 #ifndef MODULE_NAME_IS_tremor
226 static const char *const ppsz_enc_options[] = {
227 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
231 /*****************************************************************************
232 * OpenDecoder: probe the decoder and return score
233 *****************************************************************************/
234 static int OpenDecoder( vlc_object_t *p_this )
236 decoder_t *p_dec = (decoder_t*)p_this;
237 decoder_sys_t *p_sys;
239 if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
244 /* Allocate the memory needed to store the decoder's structure */
245 if( ( p_dec->p_sys = p_sys =
246 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
250 aout_DateSet( &p_sys->end_date, 0 );
251 p_sys->i_last_block_size = 0;
252 p_sys->b_packetizer = false;
253 p_sys->i_headers = 0;
254 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
256 /* Take care of vorbis init */
257 vorbis_info_init( &p_sys->vi );
258 vorbis_comment_init( &p_sys->vc );
260 /* Set output properties */
261 p_dec->fmt_out.i_cat = AUDIO_ES;
262 #ifdef MODULE_NAME_IS_tremor
263 p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
265 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
269 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
271 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
277 static int OpenPacketizer( vlc_object_t *p_this )
279 decoder_t *p_dec = (decoder_t*)p_this;
281 int i_ret = OpenDecoder( p_this );
283 if( i_ret == VLC_SUCCESS )
285 p_dec->p_sys->b_packetizer = true;
286 p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
292 /****************************************************************************
293 * DecodeBlock: the whole thing
294 ****************************************************************************
295 * This function must be fed with ogg packets.
296 ****************************************************************************/
297 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
299 decoder_sys_t *p_sys = p_dec->p_sys;
300 ogg_packet oggpacket;
302 if( !pp_block ) return NULL;
306 /* Block to Ogg packet */
307 oggpacket.packet = (*pp_block)->p_buffer;
308 oggpacket.bytes = (*pp_block)->i_buffer;
310 if( (*pp_block)->i_rate > 0 )
311 p_sys->i_input_rate = (*pp_block)->i_rate;
315 if( p_sys->b_packetizer ) return NULL;
317 /* Block to Ogg packet */
318 oggpacket.packet = NULL;
322 oggpacket.granulepos = -1;
325 oggpacket.packetno = 0;
327 /* Check for headers */
328 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
330 /* Headers already available as extra data */
331 msg_Dbg( p_dec, "headers already available as extra data" );
332 p_sys->i_headers = 3;
334 else if( oggpacket.bytes && p_sys->i_headers < 3 )
336 /* Backup headers as extra data */
339 p_dec->fmt_in.p_extra =
340 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
341 oggpacket.bytes + 2 );
342 p_extra = (uint8_t *)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
343 *(p_extra++) = oggpacket.bytes >> 8;
344 *(p_extra++) = oggpacket.bytes & 0xFF;
346 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
347 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
349 block_Release( *pp_block );
354 if( p_sys->i_headers == 3 )
356 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
358 p_sys->i_headers = 0;
359 p_dec->fmt_in.i_extra = 0;
360 block_Release( *pp_block );
363 else p_sys->i_headers++;
366 return ProcessPacket( p_dec, &oggpacket, pp_block );
369 /*****************************************************************************
370 * ProcessHeaders: process Vorbis headers.
371 *****************************************************************************/
372 static int ProcessHeaders( decoder_t *p_dec )
374 decoder_sys_t *p_sys = p_dec->p_sys;
375 ogg_packet oggpacket;
379 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
381 oggpacket.granulepos = -1;
382 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
384 oggpacket.packetno = 0;
385 p_extra = p_dec->fmt_in.p_extra;
386 i_extra = p_dec->fmt_in.i_extra;
388 /* Take care of the initial Vorbis header */
389 oggpacket.bytes = *(p_extra++) << 8;
390 oggpacket.bytes |= (*(p_extra++) & 0xFF);
391 oggpacket.packet = p_extra;
392 p_extra += oggpacket.bytes;
393 i_extra -= (oggpacket.bytes + 2);
396 msg_Err( p_dec, "header data corrupted");
400 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
402 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
406 /* Setup the format */
407 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
408 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
410 if( p_dec->fmt_out.audio.i_channels > 9 )
412 msg_Err( p_dec, "invalid number of channels (not between 1 and 9): %i",
413 p_dec->fmt_out.audio.i_channels );
417 p_dec->fmt_out.audio.i_physical_channels =
418 p_dec->fmt_out.audio.i_original_channels =
419 pi_channels_maps[p_sys->vi.channels];
420 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
422 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
424 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
425 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
427 /* The next packet in order is the comments header */
429 oggpacket.bytes = *(p_extra++) << 8;
430 oggpacket.bytes |= (*(p_extra++) & 0xFF);
431 oggpacket.packet = p_extra;
432 p_extra += oggpacket.bytes;
433 i_extra -= (oggpacket.bytes + 2);
436 msg_Err( p_dec, "header data corrupted");
440 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
442 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
445 ParseVorbisComments( p_dec );
447 /* The next packet in order is the codebooks header
448 * We need to watch out that this packet is not missing as a
449 * missing or corrupted header is fatal. */
450 oggpacket.bytes = *(p_extra++) << 8;
451 oggpacket.bytes |= (*(p_extra++) & 0xFF);
452 oggpacket.packet = p_extra;
453 i_extra -= (oggpacket.bytes + 2);
456 msg_Err( p_dec, "header data corrupted");
460 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
462 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
466 if( !p_sys->b_packetizer )
468 /* Initialize the Vorbis packet->PCM decoder */
469 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
470 vorbis_block_init( &p_sys->vd, &p_sys->vb );
474 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
475 p_dec->fmt_out.p_extra =
476 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
477 memcpy( p_dec->fmt_out.p_extra,
478 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
481 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
482 p_dec->fmt_out.audio.i_physical_channels, true);
487 /*****************************************************************************
488 * ProcessPacket: processes a Vorbis packet.
489 *****************************************************************************/
490 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
493 decoder_sys_t *p_sys = p_dec->p_sys;
494 block_t *p_block = *pp_block;
496 /* Date management */
497 if( p_block && p_block->i_pts > 0 &&
498 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
500 aout_DateSet( &p_sys->end_date, p_block->i_pts );
503 if( !aout_DateGet( &p_sys->end_date ) )
505 /* We've just started the stream, wait for the first PTS. */
506 if( p_block ) block_Release( p_block );
510 *pp_block = NULL; /* To avoid being fed the same packet again */
512 if( p_sys->b_packetizer )
514 return SendPacket( p_dec, p_oggpacket, p_block );
518 aout_buffer_t *p_aout_buffer;
520 if( p_sys->i_headers >= 3 )
521 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
523 p_aout_buffer = NULL;
525 if( p_block ) block_Release( p_block );
526 return p_aout_buffer;
530 /*****************************************************************************
531 * DecodePacket: decodes a Vorbis packet.
532 *****************************************************************************/
533 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
535 decoder_sys_t *p_sys = p_dec->p_sys;
538 #ifdef MODULE_NAME_IS_tremor
544 if( p_oggpacket->bytes &&
545 #ifdef MODULE_NAME_IS_tremor
546 vorbis_synthesis( &p_sys->vb, p_oggpacket, 1 ) == 0 )
548 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
550 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
552 /* **pp_pcm is a multichannel float vector. In stereo, for
553 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
554 * the size of each channel. Convert the float values
555 * (-1.<=range<=1.) to whatever PCM format and write it out */
557 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
560 aout_buffer_t *p_aout_buffer;
563 p_dec->pf_aout_buffer_new( p_dec, i_samples );
565 if( p_aout_buffer == NULL ) return NULL;
567 /* Interleave the samples */
568 #ifdef MODULE_NAME_IS_tremor
569 Interleave( (int32_t *)p_aout_buffer->p_buffer,
570 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
572 Interleave( (float *)p_aout_buffer->p_buffer,
573 (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
576 /* Tell libvorbis how many samples we actually consumed */
577 vorbis_synthesis_read( &p_sys->vd, i_samples );
579 /* Date management */
580 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
581 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
582 i_samples * p_sys->i_input_rate / INPUT_RATE_DEFAULT );
583 return p_aout_buffer;
591 /*****************************************************************************
592 * SendPacket: send an ogg dated packet to the stream output.
593 *****************************************************************************/
594 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
597 decoder_sys_t *p_sys = p_dec->p_sys;
598 int i_block_size, i_samples;
600 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
601 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
602 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
603 p_sys->i_last_block_size = i_block_size;
605 /* Date management */
606 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
608 if( p_sys->i_headers >= 3 )
609 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
610 i_samples * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
613 p_block->i_length = 0;
618 /*****************************************************************************
619 * ParseVorbisComments
620 *****************************************************************************/
621 static void ParseVorbisComments( decoder_t *p_dec )
623 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
624 char *psz_name, *psz_value, *psz_comment;
625 input_item_t *p_item;
628 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
630 p_item = input_GetItem( p_input );
632 while( i < p_dec->p_sys->vc.comments )
634 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
637 psz_name = psz_comment;
638 psz_value = strchr( psz_comment, '=' );
643 input_Control( p_input, INPUT_ADD_INFO, _("Vorbis comment"),
644 psz_name, "%s", psz_value );
645 /*TODO: dot he test at the beginning and save time !! */
647 if( psz_value && ( *psz_value != '\0' ) )
649 if( !strcasecmp( psz_name, "artist" ) )
650 input_item_SetArtist( p_item, psz_value );
651 else if( !strcasecmp( psz_name, "title" ) )
653 input_item_SetTitle( p_item, psz_value );
654 p_item->psz_name = strdup( psz_value );
656 else if( !strcasecmp( psz_name, "album" ) )
658 input_item_SetAlbum( p_item, psz_value );
660 else if( !strcasecmp( psz_name, "musicbrainz_trackid" ) )
661 input_item_SetTrackID( p_item, psz_value );
663 else if( !strcasecmp( psz_name, "musicbrainz_artistid" ) )
664 vlc_meta_SetArtistID( p_item, psz_value );
665 else if( !strcasecmp( psz_name, "musicbrainz_albumid" ) )
666 input_item_SetAlbumID( p_item, psz_value );
670 if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
671 !strcasecmp( psz_name, "RG_RADIO" ) )
673 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
675 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
676 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
678 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
679 !strcasecmp( psz_name, "RG_PEAK" ) )
681 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
683 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
684 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
686 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
687 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
689 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
691 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
692 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
694 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
696 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
698 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
699 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
702 var_SetInteger( pl_Yield( p_input ), "item-change", p_item->i_id );
703 pl_Release( p_input );
709 /*****************************************************************************
710 * Interleave: helper function to interleave channels
711 *****************************************************************************/
712 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, bool b_decode)
714 const uint32_t *pi_channels_in;
719 pi_channels_in = pi_6channels_in;
722 pi_channels_in = pi_4channels_in;
725 pi_channels_in = pi_3channels_in;
730 for( i = 0; i< i_channels; ++i )
732 pi_chan_table[i] = i;
739 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
740 i_channel_mask & AOUT_CHAN_PHYSMASK,
744 aout_CheckChannelReorder( pi_channels_out, pi_channels_in,
745 i_channel_mask & AOUT_CHAN_PHYSMASK,
750 /*****************************************************************************
751 * Interleave: helper function to interleave channels
752 *****************************************************************************/
753 #ifdef MODULE_NAME_IS_tremor
754 static void Interleave( int32_t *p_out, const int32_t **pp_in,
755 int i_nb_channels, int i_samples, int *pi_chan_table)
759 for ( j = 0; j < i_samples; j++ )
760 for ( i = 0; i < i_nb_channels; i++ )
761 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
764 static void Interleave( float *p_out, const float **pp_in,
765 int i_nb_channels, int i_samples, int *pi_chan_table )
769 for ( j = 0; j < i_samples; j++ )
770 for ( i = 0; i < i_nb_channels; i++ )
771 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
775 /*****************************************************************************
776 * CloseDecoder: vorbis decoder destruction
777 *****************************************************************************/
778 static void CloseDecoder( vlc_object_t *p_this )
780 decoder_t *p_dec = (decoder_t *)p_this;
781 decoder_sys_t *p_sys = p_dec->p_sys;
783 if( !p_sys->b_packetizer && p_sys->i_headers > 3 )
785 vorbis_block_clear( &p_sys->vb );
786 vorbis_dsp_clear( &p_sys->vd );
789 vorbis_comment_clear( &p_sys->vc );
790 vorbis_info_clear( &p_sys->vi ); /* must be called last */
795 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
797 /*****************************************************************************
798 * encoder_sys_t : vorbis encoder descriptor
799 *****************************************************************************/
805 vorbis_info vi; /* struct that stores all the static vorbis bitstream
807 vorbis_comment vc; /* struct that stores all the bitstream user
809 vorbis_dsp_state vd; /* central working state for the packet->PCM
811 vorbis_block vb; /* local working space for packet->PCM decode */
813 int i_last_block_size;
823 ** Channel reordering
825 int pi_chan_table[AOUT_CHAN_MAX];
829 /*****************************************************************************
830 * OpenEncoder: probe the encoder and return score
831 *****************************************************************************/
832 static int OpenEncoder( vlc_object_t *p_this )
834 encoder_t *p_enc = (encoder_t *)p_this;
835 encoder_sys_t *p_sys;
836 int i_quality, i_min_bitrate, i_max_bitrate, i;
837 ogg_packet header[3];
841 if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') &&
847 /* Allocate the memory needed to store the decoder's structure */
848 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
850 p_enc->p_sys = p_sys;
852 p_enc->pf_encode_audio = Encode;
853 p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
854 p_enc->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
856 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
858 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
859 i_quality = val.i_int;
860 if( i_quality > 10 ) i_quality = 10;
861 if( i_quality < 0 ) i_quality = 0;
862 var_Get( p_enc, ENC_CFG_PREFIX "cbr", &val );
863 if( val.b_bool ) i_quality = 0;
864 var_Get( p_enc, ENC_CFG_PREFIX "max-bitrate", &val );
865 i_max_bitrate = val.i_int;
866 var_Get( p_enc, ENC_CFG_PREFIX "min-bitrate", &val );
867 i_min_bitrate = val.i_int;
869 /* Initialize vorbis encoder */
870 vorbis_info_init( &p_sys->vi );
875 if( vorbis_encode_setup_vbr( &p_sys->vi,
876 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
879 vorbis_info_clear( &p_sys->vi );
880 free( p_enc->p_sys );
881 msg_Err( p_enc, "VBR mode initialisation failed" );
885 /* Do we have optional hard quality restrictions? */
886 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
888 struct ovectl_ratemanage_arg ai;
889 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
891 ai.bitrate_hard_min = i_min_bitrate;
892 ai.bitrate_hard_max = i_max_bitrate;
893 ai.management_active = 1;
895 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
900 /* Turn off management entirely */
901 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
906 if( vorbis_encode_setup_managed( &p_sys->vi,
907 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
908 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
909 p_enc->fmt_out.i_bitrate,
910 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
912 vorbis_info_clear( &p_sys->vi );
913 msg_Err( p_enc, "CBR mode initialisation failed" );
914 free( p_enc->p_sys );
919 vorbis_encode_setup_init( &p_sys->vi );
922 vorbis_comment_init( &p_sys->vc);
923 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
925 /* Set up the analysis state and auxiliary encoding storage */
926 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
927 vorbis_block_init( &p_sys->vd, &p_sys->vb );
929 /* Create and store headers */
930 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
931 &header[0], &header[1], &header[2]);
932 p_enc->fmt_out.i_extra = 3 * 2 + header[0].bytes +
933 header[1].bytes + header[2].bytes;
934 p_extra = p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
935 for( i = 0; i < 3; i++ )
937 *(p_extra++) = header[i].bytes >> 8;
938 *(p_extra++) = header[i].bytes & 0xFF;
939 memcpy( p_extra, header[i].packet, header[i].bytes );
940 p_extra += header[i].bytes;
943 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
944 p_sys->i_last_block_size = 0;
945 p_sys->i_samples_delay = 0;
948 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
949 p_enc->fmt_in.audio.i_physical_channels, true);
954 /****************************************************************************
955 * Encode: the whole thing
956 ****************************************************************************
957 * This function spits out ogg packets.
958 ****************************************************************************/
959 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
961 encoder_sys_t *p_sys = p_enc->p_sys;
962 ogg_packet oggpacket;
963 block_t *p_block, *p_chain = NULL;
968 p_sys->i_pts = p_aout_buf->start_date -
969 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
970 (mtime_t)p_enc->fmt_in.audio.i_rate;
972 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
974 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
976 /* convert samples to float and uninterleave */
977 for( i = 0; i < p_sys->i_channels; i++ )
979 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
981 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
982 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
986 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
988 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
992 vorbis_analysis( &p_sys->vb, NULL );
993 vorbis_bitrate_addblock( &p_sys->vb );
995 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
998 p_block = block_New( p_enc, oggpacket.bytes );
999 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
1001 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
1003 if( i_block_size < 0 ) i_block_size = 0;
1004 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
1005 p_sys->i_last_block_size = i_block_size;
1007 p_block->i_length = (mtime_t)1000000 *
1008 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
1010 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
1012 p_sys->i_samples_delay -= i_samples;
1015 p_sys->i_pts += p_block->i_length;
1016 block_ChainAppend( &p_chain, p_block );
1023 /*****************************************************************************
1024 * CloseEncoder: vorbis encoder destruction
1025 *****************************************************************************/
1026 static void CloseEncoder( vlc_object_t *p_this )
1028 encoder_t *p_enc = (encoder_t *)p_this;
1029 encoder_sys_t *p_sys = p_enc->p_sys;
1031 vorbis_block_clear( &p_sys->vb );
1032 vorbis_dsp_clear( &p_sys->vd );
1033 vorbis_comment_clear( &p_sys->vc );
1034 vorbis_info_clear( &p_sys->vi ); /* must be called last */
1039 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */