1 /*****************************************************************************
2 * vorbis.c: vorbis decoder/encoder/packetizer module using of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 2001-2012 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>
46 # define INTERLEAVE_TYPE int32_t
49 # include <vorbis/codec.h>
50 # define INTERLEAVE_TYPE float
53 # define HAVE_VORBIS_ENCODER
54 # include <vorbis/vorbisenc.h>
55 # ifndef OV_ECTL_RATEMANAGE_AVG
56 # define OV_ECTL_RATEMANAGE_AVG 0x0
61 /*****************************************************************************
62 * decoder_sys_t : vorbis decoder descriptor
63 *****************************************************************************/
74 vorbis_info vi; /* struct that stores all the static vorbis bitstream
76 vorbis_comment vc; /* struct that stores all the bitstream user
78 vorbis_dsp_state vd; /* central working state for the packet->PCM
80 vorbis_block vb; /* local working space for packet->PCM decode */
86 int i_last_block_size;
91 int pi_chan_table[AOUT_CHAN_MAX];
94 static const int pi_channels_maps[9] =
98 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
99 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
100 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
101 | AOUT_CHAN_REARRIGHT,
102 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
103 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
104 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
105 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
106 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
107 | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
108 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
109 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
110 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
115 ** channel order as defined in http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
118 /* recommended vorbis channel order for 8 channels */
119 static const uint32_t pi_8channels_in[] =
120 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
121 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
122 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
124 /* recommended vorbis channel order for 7 channels */
125 static const uint32_t pi_7channels_in[] =
126 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
127 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
128 AOUT_CHAN_REARCENTER,AOUT_CHAN_LFE,0 };
130 /* recommended vorbis channel order for 6 channels */
131 static const uint32_t pi_6channels_in[] =
132 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
133 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
135 /* recommended vorbis channel order for 4 channels */
136 static const uint32_t pi_4channels_in[] =
137 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
139 /* recommended vorbis channel order for 3 channels */
140 static const uint32_t pi_3channels_in[] =
141 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
143 /****************************************************************************
145 ****************************************************************************/
146 static int OpenDecoder ( vlc_object_t * );
147 static int OpenPacketizer( vlc_object_t * );
148 static void CloseDecoder ( vlc_object_t * );
149 static block_t *DecodeBlock ( decoder_t *, block_t ** );
151 static int ProcessHeaders( decoder_t * );
152 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
154 static block_t *DecodePacket( decoder_t *, ogg_packet * );
155 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
157 static void ParseVorbisComments( decoder_t * );
159 static void ConfigureChannelOrder(int *, int, uint32_t, bool );
161 #ifdef HAVE_VORBIS_ENCODER
162 static int OpenEncoder ( vlc_object_t * );
163 static void CloseEncoder ( vlc_object_t * );
164 static block_t *Encode ( encoder_t *, block_t * );
167 /*****************************************************************************
169 *****************************************************************************/
170 #define ENC_QUALITY_TEXT N_("Encoding quality")
171 #define ENC_QUALITY_LONGTEXT N_( \
172 "Enforce a quality between 1 (low) and 10 (high), instead " \
173 "of specifying a particular bitrate. This will produce a VBR stream." )
174 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
175 #define ENC_MAXBR_LONGTEXT N_( \
176 "Maximum bitrate in kbps. This is useful for streaming applications." )
177 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
178 #define ENC_MINBR_LONGTEXT N_( \
179 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
180 #define ENC_CBR_TEXT N_("CBR encoding")
181 #define ENC_CBR_LONGTEXT N_( \
182 "Force a constant bitrate encoding (CBR)." )
185 set_shortname( "Vorbis" )
186 set_description( N_("Vorbis audio decoder") )
187 #ifdef MODULE_NAME_IS_tremor
188 set_capability( "decoder", 90 )
190 set_capability( "decoder", 100 )
192 set_category( CAT_INPUT )
193 set_subcategory( SUBCAT_INPUT_ACODEC )
194 set_callbacks( OpenDecoder, CloseDecoder )
197 set_description( N_("Vorbis audio packetizer") )
198 set_capability( "packetizer", 100 )
199 set_callbacks( OpenPacketizer, CloseDecoder )
201 #ifdef HAVE_VORBIS_ENCODER
202 # define ENC_CFG_PREFIX "sout-vorbis-"
204 set_description( N_("Vorbis audio encoder") )
205 set_capability( "encoder", 130 )
206 set_callbacks( OpenEncoder, CloseEncoder )
208 add_integer( ENC_CFG_PREFIX "quality", 0, ENC_QUALITY_TEXT,
209 ENC_QUALITY_LONGTEXT, false )
210 change_integer_range( 0, 10 )
211 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, ENC_MAXBR_TEXT,
212 ENC_MAXBR_LONGTEXT, false )
213 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, ENC_MINBR_TEXT,
214 ENC_MINBR_LONGTEXT, false )
215 add_bool( ENC_CFG_PREFIX "cbr", false, ENC_CBR_TEXT,
216 ENC_CBR_LONGTEXT, false )
221 #ifdef HAVE_VORBIS_ENCODER
222 static const char *const ppsz_enc_options[] = {
223 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
227 /*****************************************************************************
228 * OpenDecoder: probe the decoder and return score
229 *****************************************************************************/
230 static int OpenDecoder( vlc_object_t *p_this )
232 decoder_t *p_dec = (decoder_t*)p_this;
233 decoder_sys_t *p_sys;
235 if( p_dec->fmt_in.i_codec != VLC_CODEC_VORBIS )
238 /* Allocate the memory needed to store the decoder's structure */
239 p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) );
240 if( unlikely( !p_sys ) )
244 date_Set( &p_sys->end_date, 0 );
245 p_sys->i_last_block_size = 0;
246 p_sys->b_packetizer = false;
247 p_sys->b_has_headers = false;
249 /* Take care of vorbis init */
250 vorbis_info_init( &p_sys->vi );
251 vorbis_comment_init( &p_sys->vc );
253 /* Set output properties */
254 p_dec->fmt_out.i_cat = AUDIO_ES;
255 #ifdef MODULE_NAME_IS_tremor
256 p_dec->fmt_out.i_codec = VLC_CODEC_FI32;
258 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
262 p_dec->pf_decode_audio = DecodeBlock;
263 p_dec->pf_packetize = DecodeBlock;
268 static int OpenPacketizer( vlc_object_t *p_this )
270 decoder_t *p_dec = (decoder_t*)p_this;
272 int i_ret = OpenDecoder( p_this );
274 if( i_ret == VLC_SUCCESS )
276 p_dec->p_sys->b_packetizer = true;
277 p_dec->fmt_out.i_codec = VLC_CODEC_VORBIS;
283 /****************************************************************************
284 * DecodeBlock: the whole thing
285 ****************************************************************************
286 * This function must be fed with ogg packets.
287 ****************************************************************************/
288 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
290 decoder_sys_t *p_sys = p_dec->p_sys;
291 ogg_packet oggpacket;
293 if( !pp_block ) return NULL;
297 /* Block to Ogg packet */
298 oggpacket.packet = (*pp_block)->p_buffer;
299 oggpacket.bytes = (*pp_block)->i_buffer;
303 if( p_sys->b_packetizer ) return NULL;
305 /* Block to Ogg packet */
306 oggpacket.packet = NULL;
310 oggpacket.granulepos = -1;
313 oggpacket.packetno = 0;
315 /* Check for headers */
316 if( !p_sys->b_has_headers )
318 if( ProcessHeaders( p_dec ) )
320 block_Release( *pp_block );
323 p_sys->b_has_headers = true;
326 return ProcessPacket( p_dec, &oggpacket, pp_block );
329 /*****************************************************************************
330 * ProcessHeaders: process Vorbis headers.
331 *****************************************************************************/
332 static int ProcessHeaders( decoder_t *p_dec )
334 decoder_sys_t *p_sys = p_dec->p_sys;
335 ogg_packet oggpacket;
337 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
338 void *pp_data[XIPH_MAX_HEADER_COUNT];
340 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
341 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
346 oggpacket.granulepos = -1;
348 oggpacket.packetno = 0;
350 /* Take care of the initial Vorbis header */
351 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
352 oggpacket.bytes = pi_size[0];
353 oggpacket.packet = pp_data[0];
354 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
356 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
360 /* Setup the format */
361 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
362 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
364 if( p_dec->fmt_out.audio.i_channels > 9 )
366 msg_Err( p_dec, "invalid number of channels (not between 1 and 9): %i",
367 p_dec->fmt_out.audio.i_channels );
371 p_dec->fmt_out.audio.i_physical_channels =
372 p_dec->fmt_out.audio.i_original_channels =
373 pi_channels_maps[p_sys->vi.channels];
374 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
376 date_Init( &p_sys->end_date, p_sys->vi.rate, 1 );
378 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
379 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
381 /* The next packet in order is the comments header */
383 oggpacket.bytes = pi_size[1];
384 oggpacket.packet = pp_data[1];
385 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
387 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
390 ParseVorbisComments( p_dec );
392 /* The next packet in order is the codebooks header
393 * We need to watch out that this packet is not missing as a
394 * missing or corrupted header is fatal. */
396 oggpacket.bytes = pi_size[2];
397 oggpacket.packet = pp_data[2];
398 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
400 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
404 if( !p_sys->b_packetizer )
406 /* Initialize the Vorbis packet->PCM decoder */
407 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
408 vorbis_block_init( &p_sys->vd, &p_sys->vb );
412 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
413 p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
414 p_dec->fmt_out.i_extra );
415 memcpy( p_dec->fmt_out.p_extra,
416 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
419 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
420 p_dec->fmt_out.audio.i_physical_channels, true);
422 for( unsigned i = 0; i < i_count; i++ )
427 for( unsigned i = 0; i < i_count; i++ )
432 /*****************************************************************************
433 * ProcessPacket: processes a Vorbis packet.
434 *****************************************************************************/
435 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
438 decoder_sys_t *p_sys = p_dec->p_sys;
439 block_t *p_block = *pp_block;
441 /* Date management */
442 if( p_block && p_block->i_pts > VLC_TS_INVALID &&
443 p_block->i_pts != date_Get( &p_sys->end_date ) )
445 date_Set( &p_sys->end_date, p_block->i_pts );
448 if( !date_Get( &p_sys->end_date ) )
450 /* We've just started the stream, wait for the first PTS. */
451 if( p_block ) block_Release( p_block );
455 *pp_block = NULL; /* To avoid being fed the same packet again */
457 if( p_sys->b_packetizer )
459 return SendPacket( p_dec, p_oggpacket, p_block );
463 block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
465 block_Release( p_block );
466 return p_aout_buffer;
470 /*****************************************************************************
471 * Interleave: helper function to interleave channels
472 *****************************************************************************/
473 static void Interleave( INTERLEAVE_TYPE *p_out, const INTERLEAVE_TYPE **pp_in,
474 int i_nb_channels, int i_samples, int *pi_chan_table)
476 for( int j = 0; j < i_samples; j++ )
477 for( int i = 0; i < i_nb_channels; i++ )
478 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j]
479 #ifdef MODULE_NAME_IS_tremor
480 * (FIXED32_ONE >> 24)
485 /*****************************************************************************
486 * DecodePacket: decodes a Vorbis packet.
487 *****************************************************************************/
488 static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
490 decoder_sys_t *p_sys = p_dec->p_sys;
493 INTERLEAVE_TYPE **pp_pcm;
495 if( p_oggpacket->bytes &&
496 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
497 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
499 /* **pp_pcm is a multichannel float vector. In stereo, for
500 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
501 * the size of each channel. Convert the float values
502 * (-1.<=range<=1.) to whatever PCM format and write it out */
504 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
507 block_t *p_aout_buffer;
510 decoder_NewAudioBuffer( p_dec, i_samples );
512 if( p_aout_buffer == NULL ) return NULL;
514 /* Interleave the samples */
515 Interleave( (INTERLEAVE_TYPE*)p_aout_buffer->p_buffer,
516 (const INTERLEAVE_TYPE**)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
518 /* Tell libvorbis how many samples we actually consumed */
519 vorbis_synthesis_read( &p_sys->vd, i_samples );
521 /* Date management */
522 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
523 p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
524 i_samples ) - p_aout_buffer->i_pts;
525 return p_aout_buffer;
533 /*****************************************************************************
534 * SendPacket: send an ogg dated packet to the stream output.
535 *****************************************************************************/
536 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
539 decoder_sys_t *p_sys = p_dec->p_sys;
540 int i_block_size, i_samples;
542 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
543 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
544 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
545 p_sys->i_last_block_size = i_block_size;
547 /* Date management */
548 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->end_date );
550 p_block->i_length = date_Increment( &p_sys->end_date, i_samples ) - p_block->i_pts;
555 /*****************************************************************************
556 * ParseVorbisComments
557 *****************************************************************************/
558 static void ParseVorbisComments( decoder_t *p_dec )
560 char *psz_name, *psz_value, *psz_comment;
563 while( i < p_dec->p_sys->vc.comments )
565 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
568 psz_name = psz_comment;
569 psz_value = strchr( psz_comment, '=' );
575 /* Don't add empty values */
576 if( *psz_value == '\0' )
579 if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
580 !strcasecmp( psz_name, "RG_RADIO" ) )
582 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
584 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
585 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
587 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
588 !strcasecmp( psz_name, "RG_PEAK" ) )
590 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
592 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
593 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
595 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
596 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
598 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
600 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
601 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
603 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
605 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
607 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
608 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
610 else if( !strcasecmp( psz_name, "METADATA_BLOCK_PICTURE" ) )
611 { /* Do nothing, for now */ }
614 if( !p_dec->p_description )
615 p_dec->p_description = vlc_meta_New();
616 if( p_dec->p_description )
617 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
626 /*****************************************************************************
628 *****************************************************************************/
629 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, bool b_decode)
631 const uint32_t *pi_channels_in;
635 pi_channels_in = pi_8channels_in;
638 pi_channels_in = pi_7channels_in;
642 pi_channels_in = pi_6channels_in;
645 pi_channels_in = pi_4channels_in;
648 pi_channels_in = pi_3channels_in;
653 for( i = 0; i< i_channels; ++i )
655 pi_chan_table[i] = i;
662 aout_CheckChannelReorder( pi_channels_in, NULL,
667 aout_CheckChannelReorder( NULL, pi_channels_in,
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 #ifdef HAVE_VORBIS_ENCODER
694 /*****************************************************************************
695 * encoder_sys_t : vorbis encoder descriptor
696 *****************************************************************************/
702 vorbis_info vi; /* struct that stores all the static vorbis bitstream
704 vorbis_comment vc; /* struct that stores all the bitstream user
706 vorbis_dsp_state vd; /* central working state for the packet->PCM
708 vorbis_block vb; /* local working space for packet->PCM decode */
710 int i_last_block_size;
712 unsigned int i_channels;
715 ** Channel reordering
717 int pi_chan_table[AOUT_CHAN_MAX];
721 /*****************************************************************************
722 * OpenEncoder: probe the encoder and return score
723 *****************************************************************************/
724 static int OpenEncoder( vlc_object_t *p_this )
726 encoder_t *p_enc = (encoder_t *)p_this;
727 encoder_sys_t *p_sys;
728 int i_quality, i_min_bitrate, i_max_bitrate;
729 ogg_packet header[3];
731 if( p_enc->fmt_out.i_codec != VLC_CODEC_VORBIS &&
737 /* Allocate the memory needed to store the decoder's structure */
738 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
740 p_enc->p_sys = p_sys;
742 p_enc->pf_encode_audio = Encode;
743 p_enc->fmt_in.i_codec = VLC_CODEC_FL32;
744 p_enc->fmt_out.i_codec = VLC_CODEC_VORBIS;
746 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
748 i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
749 if( i_quality > 10 ) i_quality = 10;
750 if( i_quality < 0 ) i_quality = 0;
752 if( var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ) i_quality = 0;
753 i_max_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
754 i_min_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "min-bitrate" );
756 /* Initialize vorbis encoder */
757 vorbis_info_init( &p_sys->vi );
762 if( vorbis_encode_setup_vbr( &p_sys->vi,
763 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
766 vorbis_info_clear( &p_sys->vi );
767 free( p_enc->p_sys );
768 msg_Err( p_enc, "VBR mode initialisation failed" );
772 /* Do we have optional hard quality restrictions? */
773 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
775 struct ovectl_ratemanage_arg ai;
776 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
778 ai.bitrate_hard_min = i_min_bitrate;
779 ai.bitrate_hard_max = i_max_bitrate;
780 ai.management_active = 1;
782 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
787 /* Turn off management entirely */
788 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
793 if( vorbis_encode_setup_managed( &p_sys->vi,
794 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
795 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
796 p_enc->fmt_out.i_bitrate,
797 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
799 vorbis_info_clear( &p_sys->vi );
800 msg_Err( p_enc, "CBR mode initialisation failed" );
801 free( p_enc->p_sys );
806 vorbis_encode_setup_init( &p_sys->vi );
809 vorbis_comment_init( &p_sys->vc);
810 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
812 /* Set up the analysis state and auxiliary encoding storage */
813 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
814 vorbis_block_init( &p_sys->vd, &p_sys->vb );
816 /* Create and store headers */
817 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
818 &header[0], &header[1], &header[2]);
819 for( int i = 0; i < 3; i++ )
821 if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
822 header[i].bytes, header[i].packet ) )
824 p_enc->fmt_out.i_extra = 0;
825 p_enc->fmt_out.p_extra = NULL;
829 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
830 p_sys->i_last_block_size = 0;
831 p_sys->i_samples_delay = 0;
833 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
834 p_enc->fmt_in.audio.i_physical_channels, true);
839 /****************************************************************************
840 * Encode: the whole thing
841 ****************************************************************************
842 * This function spits out ogg packets.
843 ****************************************************************************/
844 static block_t *Encode( encoder_t *p_enc, block_t *p_aout_buf )
846 encoder_sys_t *p_sys = p_enc->p_sys;
847 ogg_packet oggpacket;
848 block_t *p_block, *p_chain = NULL;
851 mtime_t i_pts = p_aout_buf->i_pts -
852 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
853 (mtime_t)p_enc->fmt_in.audio.i_rate;
855 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
857 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
859 /* convert samples to float and uninterleave */
860 for( unsigned int i = 0; i < p_sys->i_channels; i++ )
862 for( unsigned int j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
864 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
865 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
869 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
871 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
875 vorbis_analysis( &p_sys->vb, NULL );
876 vorbis_bitrate_addblock( &p_sys->vb );
878 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
881 p_block = block_New( p_enc, oggpacket.bytes );
882 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
884 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
886 if( i_block_size < 0 ) i_block_size = 0;
887 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
888 p_sys->i_last_block_size = i_block_size;
890 p_block->i_length = (mtime_t)1000000 *
891 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
893 p_block->i_dts = p_block->i_pts = i_pts;
895 p_sys->i_samples_delay -= i_samples;
898 i_pts += p_block->i_length;
899 block_ChainAppend( &p_chain, p_block );
906 /*****************************************************************************
907 * CloseEncoder: vorbis encoder destruction
908 *****************************************************************************/
909 static void CloseEncoder( vlc_object_t *p_this )
911 encoder_t *p_enc = (encoder_t *)p_this;
912 encoder_sys_t *p_sys = p_enc->p_sys;
914 vorbis_block_clear( &p_sys->vb );
915 vorbis_dsp_clear( &p_sys->vd );
916 vorbis_comment_clear( &p_sys->vc );
917 vorbis_info_clear( &p_sys->vi ); /* must be called last */
922 #endif /* HAVE_VORBIS_ENCODER */