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>
40 #include "../demux/xiph.h"
44 #ifdef MODULE_NAME_IS_tremor
45 #include <tremor/ivorbiscodec.h>
48 #include <vorbis/vorbisenc.h>
50 # ifndef OV_ECTL_RATEMANAGE_AVG
51 # define OV_ECTL_RATEMANAGE_AVG 0x0
56 /*****************************************************************************
57 * decoder_sys_t : vorbis decoder descriptor
58 *****************************************************************************/
69 vorbis_info vi; /* struct that stores all the static vorbis bitstream
71 vorbis_comment vc; /* struct that stores all the bitstream user
73 vorbis_dsp_state vd; /* central working state for the packet->PCM
75 vorbis_block vb; /* local working space for packet->PCM decode */
81 int i_last_block_size;
86 int pi_chan_table[AOUT_CHAN_MAX];
89 static const int pi_channels_maps[9] =
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,
101 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
102 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
103 | AOUT_CHAN_MIDDLERIGHT,
104 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
105 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
110 ** channel order as defined in http://www.ogghelp.com/ogg/glossary.cfm#Audio_Channels
113 /* recommended vorbis channel order for 6 channels */
114 static const uint32_t pi_6channels_in[] =
115 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
116 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
118 /* recommended vorbis channel order for 4 channels */
119 static const uint32_t pi_4channels_in[] =
120 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
122 /* recommended vorbis channel order for 3 channels */
123 static const uint32_t pi_3channels_in[] =
124 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
126 /****************************************************************************
128 ****************************************************************************/
129 static int OpenDecoder ( vlc_object_t * );
130 static int OpenPacketizer( vlc_object_t * );
131 static void CloseDecoder ( vlc_object_t * );
132 static void *DecodeBlock ( decoder_t *, block_t ** );
134 static int ProcessHeaders( decoder_t * );
135 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
137 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
138 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
140 static void ParseVorbisComments( decoder_t * );
142 static void ConfigureChannelOrder(int *, int, uint32_t, bool );
144 #ifdef MODULE_NAME_IS_tremor
145 static void Interleave ( int32_t *, const int32_t **, int, int, int * );
147 static void Interleave ( float *, const float **, int, int, int * );
150 #ifndef MODULE_NAME_IS_tremor
151 static int OpenEncoder ( vlc_object_t * );
152 static void CloseEncoder ( vlc_object_t * );
153 static block_t *Encode ( encoder_t *, aout_buffer_t * );
156 /*****************************************************************************
158 *****************************************************************************/
159 #define ENC_QUALITY_TEXT N_("Encoding quality")
160 #define ENC_QUALITY_LONGTEXT N_( \
161 "Enforce a quality between 1 (low) and 10 (high), instead " \
162 "of specifying a particular bitrate. This will produce a VBR stream." )
163 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
164 #define ENC_MAXBR_LONGTEXT N_( \
165 "Maximum bitrate in kbps. This is useful for streaming applications." )
166 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
167 #define ENC_MINBR_LONGTEXT N_( \
168 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
169 #define ENC_CBR_TEXT N_("CBR encoding")
170 #define ENC_CBR_LONGTEXT N_( \
171 "Force a constant bitrate encoding (CBR)." )
174 set_shortname( "Vorbis" )
175 set_description( N_("Vorbis audio decoder") )
176 #ifdef MODULE_NAME_IS_tremor
177 set_capability( "decoder", 90 )
179 set_capability( "decoder", 100 )
181 set_category( CAT_INPUT )
182 set_subcategory( SUBCAT_INPUT_ACODEC )
183 set_callbacks( OpenDecoder, CloseDecoder )
186 set_description( N_("Vorbis audio packetizer") )
187 set_capability( "packetizer", 100 )
188 set_callbacks( OpenPacketizer, CloseDecoder )
190 #ifndef MODULE_NAME_IS_tremor
191 # define ENC_CFG_PREFIX "sout-vorbis-"
193 set_description( N_("Vorbis audio encoder") )
194 set_capability( "encoder", 100 )
195 set_callbacks( OpenEncoder, CloseEncoder )
197 add_integer( ENC_CFG_PREFIX "quality", 0, NULL, ENC_QUALITY_TEXT,
198 ENC_QUALITY_LONGTEXT, false )
199 change_integer_range( 0, 10 )
200 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
201 ENC_MAXBR_LONGTEXT, false )
202 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
203 ENC_MINBR_LONGTEXT, false )
204 add_bool( ENC_CFG_PREFIX "cbr", false, NULL, ENC_CBR_TEXT,
205 ENC_CBR_LONGTEXT, false )
210 #ifndef MODULE_NAME_IS_tremor
211 static const char *const ppsz_enc_options[] = {
212 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
216 /*****************************************************************************
217 * OpenDecoder: probe the decoder and return score
218 *****************************************************************************/
219 static int OpenDecoder( vlc_object_t *p_this )
221 decoder_t *p_dec = (decoder_t*)p_this;
222 decoder_sys_t *p_sys;
224 if( p_dec->fmt_in.i_codec != VLC_CODEC_VORBIS )
229 /* Allocate the memory needed to store the decoder's structure */
230 if( ( p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) ) ) == NULL )
234 date_Set( &p_sys->end_date, 0 );
235 p_sys->i_last_block_size = 0;
236 p_sys->b_packetizer = false;
237 p_sys->b_has_headers = false;
239 /* Take care of vorbis init */
240 vorbis_info_init( &p_sys->vi );
241 vorbis_comment_init( &p_sys->vc );
243 /* Set output properties */
244 p_dec->fmt_out.i_cat = AUDIO_ES;
245 #ifdef MODULE_NAME_IS_tremor
246 p_dec->fmt_out.i_codec = VLC_CODEC_FI32;
248 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
252 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
254 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
260 static int OpenPacketizer( vlc_object_t *p_this )
262 decoder_t *p_dec = (decoder_t*)p_this;
264 int i_ret = OpenDecoder( p_this );
266 if( i_ret == VLC_SUCCESS )
268 p_dec->p_sys->b_packetizer = true;
269 p_dec->fmt_out.i_codec = VLC_CODEC_VORBIS;
275 /****************************************************************************
276 * DecodeBlock: the whole thing
277 ****************************************************************************
278 * This function must be fed with ogg packets.
279 ****************************************************************************/
280 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
282 decoder_sys_t *p_sys = p_dec->p_sys;
283 ogg_packet oggpacket;
285 if( !pp_block ) return NULL;
289 /* Block to Ogg packet */
290 oggpacket.packet = (*pp_block)->p_buffer;
291 oggpacket.bytes = (*pp_block)->i_buffer;
295 if( p_sys->b_packetizer ) return NULL;
297 /* Block to Ogg packet */
298 oggpacket.packet = NULL;
302 oggpacket.granulepos = -1;
305 oggpacket.packetno = 0;
307 /* Check for headers */
308 if( !p_sys->b_has_headers )
310 if( ProcessHeaders( p_dec ) )
312 block_Release( *pp_block );
315 p_sys->b_has_headers = true;
318 return ProcessPacket( p_dec, &oggpacket, pp_block );
321 /*****************************************************************************
322 * ProcessHeaders: process Vorbis headers.
323 *****************************************************************************/
324 static int ProcessHeaders( decoder_t *p_dec )
326 decoder_sys_t *p_sys = p_dec->p_sys;
327 ogg_packet oggpacket;
329 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
330 void *pp_data[XIPH_MAX_HEADER_COUNT];
332 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
333 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
338 oggpacket.granulepos = -1;
340 oggpacket.packetno = 0;
342 /* Take care of the initial Vorbis header */
343 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
344 oggpacket.bytes = pi_size[0];
345 oggpacket.packet = pp_data[0];
346 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
348 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
352 /* Setup the format */
353 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
354 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
356 if( p_dec->fmt_out.audio.i_channels > 9 )
358 msg_Err( p_dec, "invalid number of channels (not between 1 and 9): %i",
359 p_dec->fmt_out.audio.i_channels );
363 p_dec->fmt_out.audio.i_physical_channels =
364 p_dec->fmt_out.audio.i_original_channels =
365 pi_channels_maps[p_sys->vi.channels];
366 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
368 date_Init( &p_sys->end_date, p_sys->vi.rate, 1 );
370 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
371 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
373 /* The next packet in order is the comments header */
375 oggpacket.bytes = pi_size[1];
376 oggpacket.packet = pp_data[1];
377 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
379 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
382 ParseVorbisComments( p_dec );
384 /* The next packet in order is the codebooks header
385 * We need to watch out that this packet is not missing as a
386 * missing or corrupted header is fatal. */
388 oggpacket.bytes = pi_size[2];
389 oggpacket.packet = pp_data[2];
390 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
392 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
396 if( !p_sys->b_packetizer )
398 /* Initialize the Vorbis packet->PCM decoder */
399 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
400 vorbis_block_init( &p_sys->vd, &p_sys->vb );
404 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
405 p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
406 p_dec->fmt_out.i_extra );
407 memcpy( p_dec->fmt_out.p_extra,
408 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
411 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
412 p_dec->fmt_out.audio.i_physical_channels, true);
414 for( unsigned i = 0; i < i_count; i++ )
419 for( unsigned i = 0; i < i_count; i++ )
424 /*****************************************************************************
425 * ProcessPacket: processes a Vorbis packet.
426 *****************************************************************************/
427 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
430 decoder_sys_t *p_sys = p_dec->p_sys;
431 block_t *p_block = *pp_block;
433 /* Date management */
434 if( p_block && p_block->i_pts > VLC_TS_INVALID &&
435 p_block->i_pts != date_Get( &p_sys->end_date ) )
437 date_Set( &p_sys->end_date, p_block->i_pts );
440 if( !date_Get( &p_sys->end_date ) )
442 /* We've just started the stream, wait for the first PTS. */
443 if( p_block ) block_Release( p_block );
447 *pp_block = NULL; /* To avoid being fed the same packet again */
449 if( p_sys->b_packetizer )
451 return SendPacket( p_dec, p_oggpacket, p_block );
455 aout_buffer_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
457 block_Release( p_block );
458 return p_aout_buffer;
462 /*****************************************************************************
463 * DecodePacket: decodes a Vorbis packet.
464 *****************************************************************************/
465 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
467 decoder_sys_t *p_sys = p_dec->p_sys;
470 #ifdef MODULE_NAME_IS_tremor
476 if( p_oggpacket->bytes &&
477 #ifdef MODULE_NAME_IS_tremor
478 vorbis_synthesis( &p_sys->vb, p_oggpacket, 1 ) == 0 )
480 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
482 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
484 /* **pp_pcm is a multichannel float vector. In stereo, for
485 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
486 * the size of each channel. Convert the float values
487 * (-1.<=range<=1.) to whatever PCM format and write it out */
489 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
492 aout_buffer_t *p_aout_buffer;
495 decoder_NewAudioBuffer( p_dec, i_samples );
497 if( p_aout_buffer == NULL ) return NULL;
499 /* Interleave the samples */
500 #ifdef MODULE_NAME_IS_tremor
501 Interleave( (int32_t *)p_aout_buffer->p_buffer,
502 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
504 Interleave( (float *)p_aout_buffer->p_buffer,
505 (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
508 /* Tell libvorbis how many samples we actually consumed */
509 vorbis_synthesis_read( &p_sys->vd, i_samples );
511 /* Date management */
512 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
513 p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
514 i_samples ) - p_aout_buffer->i_pts;
515 return p_aout_buffer;
523 /*****************************************************************************
524 * SendPacket: send an ogg dated packet to the stream output.
525 *****************************************************************************/
526 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
529 decoder_sys_t *p_sys = p_dec->p_sys;
530 int i_block_size, i_samples;
532 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
533 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
534 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
535 p_sys->i_last_block_size = i_block_size;
537 /* Date management */
538 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->end_date );
540 p_block->i_length = date_Increment( &p_sys->end_date, i_samples ) - p_block->i_pts;
545 /*****************************************************************************
546 * ParseVorbisComments
547 *****************************************************************************/
548 static void ParseVorbisComments( decoder_t *p_dec )
550 char *psz_name, *psz_value, *psz_comment;
553 while( i < p_dec->p_sys->vc.comments )
555 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
558 psz_name = psz_comment;
559 psz_value = strchr( psz_comment, '=' );
565 if( !p_dec->p_description )
566 p_dec->p_description = vlc_meta_New();
567 if( p_dec->p_description )
568 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
570 if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
571 !strcasecmp( psz_name, "RG_RADIO" ) )
573 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
575 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
576 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
578 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
579 !strcasecmp( psz_name, "RG_PEAK" ) )
581 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
583 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
584 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
586 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
587 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
589 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
591 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
592 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
594 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
596 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
598 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
599 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
607 /*****************************************************************************
608 * Interleave: helper function to interleave channels
609 *****************************************************************************/
610 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, bool b_decode)
612 const uint32_t *pi_channels_in;
617 pi_channels_in = pi_6channels_in;
620 pi_channels_in = pi_4channels_in;
623 pi_channels_in = pi_3channels_in;
628 for( i = 0; i< i_channels; ++i )
630 pi_chan_table[i] = i;
637 aout_CheckChannelReorder( pi_channels_in, NULL,
638 i_channel_mask & AOUT_CHAN_PHYSMASK,
642 aout_CheckChannelReorder( NULL, pi_channels_in,
643 i_channel_mask & AOUT_CHAN_PHYSMASK,
648 /*****************************************************************************
649 * Interleave: helper function to interleave channels
650 *****************************************************************************/
651 #ifdef MODULE_NAME_IS_tremor
652 static void Interleave( int32_t *p_out, const int32_t **pp_in,
653 int i_nb_channels, int i_samples, int *pi_chan_table)
657 for ( j = 0; j < i_samples; j++ )
658 for ( i = 0; i < i_nb_channels; i++ )
659 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
662 static void Interleave( float *p_out, const float **pp_in,
663 int i_nb_channels, int i_samples, int *pi_chan_table )
667 for ( j = 0; j < i_samples; j++ )
668 for ( i = 0; i < i_nb_channels; i++ )
669 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
673 /*****************************************************************************
674 * CloseDecoder: vorbis decoder destruction
675 *****************************************************************************/
676 static void CloseDecoder( vlc_object_t *p_this )
678 decoder_t *p_dec = (decoder_t *)p_this;
679 decoder_sys_t *p_sys = p_dec->p_sys;
681 if( !p_sys->b_packetizer && p_sys->b_has_headers )
683 vorbis_block_clear( &p_sys->vb );
684 vorbis_dsp_clear( &p_sys->vd );
687 vorbis_comment_clear( &p_sys->vc );
688 vorbis_info_clear( &p_sys->vi ); /* must be called last */
693 #ifndef MODULE_NAME_IS_tremor
695 /*****************************************************************************
696 * encoder_sys_t : vorbis encoder descriptor
697 *****************************************************************************/
703 vorbis_info vi; /* struct that stores all the static vorbis bitstream
705 vorbis_comment vc; /* struct that stores all the bitstream user
707 vorbis_dsp_state vd; /* central working state for the packet->PCM
709 vorbis_block vb; /* local working space for packet->PCM decode */
711 int i_last_block_size;
716 ** Channel reordering
718 int pi_chan_table[AOUT_CHAN_MAX];
722 /*****************************************************************************
723 * OpenEncoder: probe the encoder and return score
724 *****************************************************************************/
725 static int OpenEncoder( vlc_object_t *p_this )
727 encoder_t *p_enc = (encoder_t *)p_this;
728 encoder_sys_t *p_sys;
729 int i_quality, i_min_bitrate, i_max_bitrate, i;
730 ogg_packet header[3];
733 if( p_enc->fmt_out.i_codec != VLC_CODEC_VORBIS &&
739 /* Allocate the memory needed to store the decoder's structure */
740 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
742 p_enc->p_sys = p_sys;
744 p_enc->pf_encode_audio = Encode;
745 p_enc->fmt_in.i_codec = VLC_CODEC_FL32;
746 p_enc->fmt_out.i_codec = VLC_CODEC_VORBIS;
748 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
750 i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
751 if( i_quality > 10 ) i_quality = 10;
752 if( i_quality < 0 ) i_quality = 0;
754 if( var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ) i_quality = 0;
755 i_max_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
756 i_min_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "min-bitrate" );
758 /* Initialize vorbis encoder */
759 vorbis_info_init( &p_sys->vi );
764 if( vorbis_encode_setup_vbr( &p_sys->vi,
765 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
768 vorbis_info_clear( &p_sys->vi );
769 free( p_enc->p_sys );
770 msg_Err( p_enc, "VBR mode initialisation failed" );
774 /* Do we have optional hard quality restrictions? */
775 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
777 struct ovectl_ratemanage_arg ai;
778 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
780 ai.bitrate_hard_min = i_min_bitrate;
781 ai.bitrate_hard_max = i_max_bitrate;
782 ai.management_active = 1;
784 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
789 /* Turn off management entirely */
790 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
795 if( vorbis_encode_setup_managed( &p_sys->vi,
796 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
797 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
798 p_enc->fmt_out.i_bitrate,
799 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
801 vorbis_info_clear( &p_sys->vi );
802 msg_Err( p_enc, "CBR mode initialisation failed" );
803 free( p_enc->p_sys );
808 vorbis_encode_setup_init( &p_sys->vi );
811 vorbis_comment_init( &p_sys->vc);
812 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
814 /* Set up the analysis state and auxiliary encoding storage */
815 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
816 vorbis_block_init( &p_sys->vd, &p_sys->vb );
818 /* Create and store headers */
819 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
820 &header[0], &header[1], &header[2]);
821 for( int i = 0; i < 3; i++ )
823 if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
824 header[i].bytes, header[i].packet ) )
826 p_enc->fmt_out.i_extra = 0;
827 p_enc->fmt_out.p_extra = NULL;
831 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
832 p_sys->i_last_block_size = 0;
833 p_sys->i_samples_delay = 0;
835 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
836 p_enc->fmt_in.audio.i_physical_channels, true);
841 /****************************************************************************
842 * Encode: the whole thing
843 ****************************************************************************
844 * This function spits out ogg packets.
845 ****************************************************************************/
846 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
848 encoder_sys_t *p_sys = p_enc->p_sys;
849 ogg_packet oggpacket;
850 block_t *p_block, *p_chain = NULL;
855 mtime_t i_pts = p_aout_buf->i_pts -
856 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
857 (mtime_t)p_enc->fmt_in.audio.i_rate;
859 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
861 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
863 /* convert samples to float and uninterleave */
864 for( i = 0; i < p_sys->i_channels; i++ )
866 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
868 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
869 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
873 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
875 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
879 vorbis_analysis( &p_sys->vb, NULL );
880 vorbis_bitrate_addblock( &p_sys->vb );
882 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
885 p_block = block_New( p_enc, oggpacket.bytes );
886 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
888 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
890 if( i_block_size < 0 ) i_block_size = 0;
891 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
892 p_sys->i_last_block_size = i_block_size;
894 p_block->i_length = (mtime_t)1000000 *
895 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
897 p_block->i_dts = p_block->i_pts = i_pts;
899 p_sys->i_samples_delay -= i_samples;
902 i_pts += p_block->i_length;
903 block_ChainAppend( &p_chain, p_block );
910 /*****************************************************************************
911 * CloseEncoder: vorbis encoder destruction
912 *****************************************************************************/
913 static void CloseEncoder( vlc_object_t *p_this )
915 encoder_t *p_enc = (encoder_t *)p_this;
916 encoder_sys_t *p_sys = p_enc->p_sys;
918 vorbis_block_clear( &p_sys->vb );
919 vorbis_dsp_clear( &p_sys->vd );
920 vorbis_comment_clear( &p_sys->vc );
921 vorbis_info_clear( &p_sys->vi ); /* must be called last */
926 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */