1 /*****************************************************************************
2 * vorbis.c: vorbis decoder/encoder/packetizer module using of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 2001-2012 VLC authors and VideoLAN
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * 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>
37 #include <vlc_charset.h>
39 #include <vlc_input.h>
41 #include "../demux/xiph.h"
45 #ifdef MODULE_NAME_IS_tremor
46 # include <tremor/ivorbiscodec.h>
47 # define INTERLEAVE_TYPE int32_t
50 # include <vorbis/codec.h>
51 # define INTERLEAVE_TYPE float
54 # define HAVE_VORBIS_ENCODER
55 # include <vorbis/vorbisenc.h>
56 # ifndef OV_ECTL_RATEMANAGE_AVG
57 # define OV_ECTL_RATEMANAGE_AVG 0x0
62 /*****************************************************************************
63 * decoder_sys_t : vorbis decoder descriptor
64 *****************************************************************************/
75 vorbis_info vi; /* struct that stores all the static vorbis bitstream
77 vorbis_comment vc; /* struct that stores all the bitstream user
79 vorbis_dsp_state vd; /* central working state for the packet->PCM
81 vorbis_block vb; /* local working space for packet->PCM decode */
87 int i_last_block_size;
92 uint8_t pi_chan_table[AOUT_CHAN_MAX];
95 static const int pi_channels_maps[9] =
99 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
100 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
101 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
102 | AOUT_CHAN_REARRIGHT,
103 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
104 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
105 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
106 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
107 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
108 | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
109 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
110 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
111 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
116 ** channel order as defined in http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
119 /* recommended vorbis channel order for 8 channels */
120 static const uint32_t pi_8channels_in[] =
121 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
122 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
123 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE, 0 };
125 /* recommended vorbis channel order for 7 channels */
126 static const uint32_t pi_7channels_in[] =
127 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
128 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
129 AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE, 0 };
131 /* recommended vorbis channel order for 6 channels */
132 static const uint32_t pi_6channels_in[] =
133 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
134 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_LFE, 0 };
136 /* recommended vorbis channel order for 4 channels */
137 static const uint32_t pi_4channels_in[] =
138 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
140 /* recommended vorbis channel order for 3 channels */
141 static const uint32_t pi_3channels_in[] =
142 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
144 /****************************************************************************
146 ****************************************************************************/
147 static int OpenDecoder ( vlc_object_t * );
148 static int OpenPacketizer( vlc_object_t * );
149 static void CloseDecoder ( vlc_object_t * );
150 static block_t *DecodeBlock ( decoder_t *, block_t ** );
152 static int ProcessHeaders( decoder_t * );
153 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
155 static block_t *DecodePacket( decoder_t *, ogg_packet * );
156 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
158 static void ParseVorbisComments( decoder_t * );
160 static void ConfigureChannelOrder(uint8_t *, int, uint32_t, bool );
162 #ifdef HAVE_VORBIS_ENCODER
163 static int OpenEncoder ( vlc_object_t * );
164 static void CloseEncoder ( vlc_object_t * );
165 static block_t *Encode ( encoder_t *, block_t * );
168 /*****************************************************************************
170 *****************************************************************************/
171 #define ENC_QUALITY_TEXT N_("Encoding quality")
172 #define ENC_QUALITY_LONGTEXT N_( \
173 "Enforce a quality between 1 (low) and 10 (high), instead " \
174 "of specifying a particular bitrate. This will produce a VBR stream." )
175 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
176 #define ENC_MAXBR_LONGTEXT N_( \
177 "Maximum bitrate in kbps. This is useful for streaming applications." )
178 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
179 #define ENC_MINBR_LONGTEXT N_( \
180 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
181 #define ENC_CBR_TEXT N_("CBR encoding")
182 #define ENC_CBR_LONGTEXT N_( \
183 "Force a constant bitrate encoding (CBR)." )
186 set_shortname( "Vorbis" )
187 set_description( N_("Vorbis audio decoder") )
188 #ifdef MODULE_NAME_IS_tremor
189 set_capability( "decoder", 90 )
191 set_capability( "decoder", 100 )
193 set_category( CAT_INPUT )
194 set_subcategory( SUBCAT_INPUT_ACODEC )
195 set_callbacks( OpenDecoder, CloseDecoder )
198 set_description( N_("Vorbis audio packetizer") )
199 set_capability( "packetizer", 100 )
200 set_callbacks( OpenPacketizer, CloseDecoder )
202 #ifdef HAVE_VORBIS_ENCODER
203 # define ENC_CFG_PREFIX "sout-vorbis-"
205 set_description( N_("Vorbis audio encoder") )
206 set_capability( "encoder", 130 )
207 set_callbacks( OpenEncoder, CloseEncoder )
209 add_integer( ENC_CFG_PREFIX "quality", 0, ENC_QUALITY_TEXT,
210 ENC_QUALITY_LONGTEXT, false )
211 change_integer_range( 0, 10 )
212 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, ENC_MAXBR_TEXT,
213 ENC_MAXBR_LONGTEXT, false )
214 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, ENC_MINBR_TEXT,
215 ENC_MINBR_LONGTEXT, false )
216 add_bool( ENC_CFG_PREFIX "cbr", false, ENC_CBR_TEXT,
217 ENC_CBR_LONGTEXT, false )
222 #ifdef HAVE_VORBIS_ENCODER
223 static const char *const ppsz_enc_options[] = {
224 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
228 /*****************************************************************************
229 * OpenDecoder: probe the decoder and return score
230 *****************************************************************************/
231 static int OpenDecoder( vlc_object_t *p_this )
233 decoder_t *p_dec = (decoder_t*)p_this;
234 decoder_sys_t *p_sys;
236 if( p_dec->fmt_in.i_codec != VLC_CODEC_VORBIS )
239 /* Allocate the memory needed to store the decoder's structure */
240 p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) );
241 if( unlikely( !p_sys ) )
245 date_Set( &p_sys->end_date, 0 );
246 p_sys->i_last_block_size = 0;
247 p_sys->b_packetizer = false;
248 p_sys->b_has_headers = false;
250 /* Take care of vorbis init */
251 vorbis_info_init( &p_sys->vi );
252 vorbis_comment_init( &p_sys->vc );
254 /* Set output properties */
255 p_dec->fmt_out.i_cat = AUDIO_ES;
256 #ifdef MODULE_NAME_IS_tremor
257 p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
259 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
263 p_dec->pf_decode_audio = DecodeBlock;
264 p_dec->pf_packetize = DecodeBlock;
269 static int OpenPacketizer( vlc_object_t *p_this )
271 decoder_t *p_dec = (decoder_t*)p_this;
273 int i_ret = OpenDecoder( p_this );
275 if( i_ret == VLC_SUCCESS )
277 p_dec->p_sys->b_packetizer = true;
278 p_dec->fmt_out.i_codec = VLC_CODEC_VORBIS;
284 /****************************************************************************
285 * DecodeBlock: the whole thing
286 ****************************************************************************
287 * This function must be fed with ogg packets.
288 ****************************************************************************/
289 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
291 decoder_sys_t *p_sys = p_dec->p_sys;
292 ogg_packet oggpacket;
294 if( !pp_block ) return NULL;
298 /* Block to Ogg packet */
299 oggpacket.packet = (*pp_block)->p_buffer;
300 oggpacket.bytes = (*pp_block)->i_buffer;
304 if( p_sys->b_packetizer ) return NULL;
306 /* Block to Ogg packet */
307 oggpacket.packet = NULL;
311 oggpacket.granulepos = -1;
314 oggpacket.packetno = 0;
316 /* Check for headers */
317 if( !p_sys->b_has_headers )
319 if( ProcessHeaders( p_dec ) )
322 block_Release( *pp_block );
325 p_sys->b_has_headers = true;
328 return ProcessPacket( p_dec, &oggpacket, pp_block );
331 /*****************************************************************************
332 * ProcessHeaders: process Vorbis headers.
333 *****************************************************************************/
334 static int ProcessHeaders( decoder_t *p_dec )
336 decoder_sys_t *p_sys = p_dec->p_sys;
337 ogg_packet oggpacket;
339 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
340 void *pp_data[XIPH_MAX_HEADER_COUNT];
342 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
343 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
348 oggpacket.granulepos = -1;
350 oggpacket.packetno = 0;
352 /* Take care of the initial Vorbis header */
353 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
354 oggpacket.bytes = pi_size[0];
355 oggpacket.packet = pp_data[0];
356 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
358 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
362 /* Setup the format */
363 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
364 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
366 if( p_dec->fmt_out.audio.i_channels >= ARRAY_SIZE(pi_channels_maps) )
368 msg_Err( p_dec, "invalid number of channels (1-%zu): %i",
369 ARRAY_SIZE(pi_channels_maps),
370 p_dec->fmt_out.audio.i_channels );
374 p_dec->fmt_out.audio.i_physical_channels =
375 p_dec->fmt_out.audio.i_original_channels =
376 pi_channels_maps[p_sys->vi.channels];
377 p_dec->fmt_out.i_bitrate = __MAX( 0, (int32_t) p_sys->vi.bitrate_nominal );
379 date_Init( &p_sys->end_date, p_sys->vi.rate, 1 );
381 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ud",
382 p_sys->vi.channels, p_sys->vi.rate, p_dec->fmt_out.i_bitrate );
384 /* The next packet in order is the comments header */
386 oggpacket.bytes = pi_size[1];
387 oggpacket.packet = pp_data[1];
388 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
390 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
393 ParseVorbisComments( p_dec );
395 /* The next packet in order is the codebooks header
396 * We need to watch out that this packet is not missing as a
397 * missing or corrupted header is fatal. */
399 oggpacket.bytes = pi_size[2];
400 oggpacket.packet = pp_data[2];
401 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
403 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
407 if( !p_sys->b_packetizer )
409 /* Initialize the Vorbis packet->PCM decoder */
410 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
411 vorbis_block_init( &p_sys->vd, &p_sys->vb );
415 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
416 p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
417 p_dec->fmt_out.i_extra );
418 memcpy( p_dec->fmt_out.p_extra,
419 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
422 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
423 p_dec->fmt_out.audio.i_physical_channels, true);
428 /*****************************************************************************
429 * ProcessPacket: processes a Vorbis packet.
430 *****************************************************************************/
431 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
434 decoder_sys_t *p_sys = p_dec->p_sys;
435 block_t *p_block = *pp_block;
437 /* Date management */
438 if( p_block && p_block->i_pts > VLC_TS_INVALID &&
439 p_block->i_pts != date_Get( &p_sys->end_date ) )
441 date_Set( &p_sys->end_date, p_block->i_pts );
444 if( !date_Get( &p_sys->end_date ) )
446 /* We've just started the stream, wait for the first PTS. */
447 if( p_block ) block_Release( p_block );
451 *pp_block = NULL; /* To avoid being fed the same packet again */
453 if( p_sys->b_packetizer )
455 return SendPacket( p_dec, p_oggpacket, p_block );
459 block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
461 block_Release( p_block );
462 return p_aout_buffer;
466 /*****************************************************************************
467 * Interleave: helper function to interleave channels
468 *****************************************************************************/
469 static void Interleave( INTERLEAVE_TYPE *p_out, const INTERLEAVE_TYPE **pp_in,
470 int i_nb_channels, int i_samples, uint8_t *pi_chan_table)
472 for( int j = 0; j < i_samples; j++ )
473 for( int i = 0; i < i_nb_channels; i++ )
475 #ifdef MODULE_NAME_IS_tremor
476 union { int32_t i; uint32_t u;} spl;
478 spl.u = ((uint32_t)pp_in[i][j]) << 8;
479 p_out[j * i_nb_channels + pi_chan_table[i]] = spl.i;
481 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
486 /*****************************************************************************
487 * DecodePacket: decodes a Vorbis packet.
488 *****************************************************************************/
489 static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
491 decoder_sys_t *p_sys = p_dec->p_sys;
494 INTERLEAVE_TYPE **pp_pcm;
496 if( p_oggpacket->bytes &&
497 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
498 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
500 /* **pp_pcm is a multichannel float vector. In stereo, for
501 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
502 * the size of each channel. Convert the float values
503 * (-1.<=range<=1.) to whatever PCM format and write it out */
505 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
508 block_t *p_aout_buffer;
511 decoder_NewAudioBuffer( p_dec, i_samples );
513 if( p_aout_buffer == NULL ) return NULL;
515 /* Interleave the samples */
516 Interleave( (INTERLEAVE_TYPE*)p_aout_buffer->p_buffer,
517 (const INTERLEAVE_TYPE**)pp_pcm, p_sys->vi.channels, i_samples,
518 p_sys->pi_chan_table);
520 /* Tell libvorbis how many samples we actually consumed */
521 vorbis_synthesis_read( &p_sys->vd, i_samples );
523 /* Date management */
524 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
525 p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
526 i_samples ) - p_aout_buffer->i_pts;
527 return p_aout_buffer;
535 /*****************************************************************************
536 * SendPacket: send an ogg dated packet to the stream output.
537 *****************************************************************************/
538 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
541 decoder_sys_t *p_sys = p_dec->p_sys;
542 int i_block_size, i_samples;
544 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
545 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
546 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
547 p_sys->i_last_block_size = i_block_size;
549 /* Date management */
550 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->end_date );
552 p_block->i_length = date_Increment( &p_sys->end_date, i_samples ) - p_block->i_pts;
557 /*****************************************************************************
558 * ParseVorbisComments
559 *****************************************************************************/
560 static void ParseVorbisComments( decoder_t *p_dec )
562 char *psz_name, *psz_value, *psz_comment;
565 while( i < p_dec->p_sys->vc.comments )
567 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
570 psz_name = psz_comment;
571 psz_value = strchr( psz_comment, '=' );
572 /* Don't add empty values */
573 if( psz_value && psz_value[1] != '\0')
578 if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
579 !strcasecmp( psz_name, "RG_RADIO" ) )
581 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
583 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
584 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = us_atof( psz_value );
586 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
587 !strcasecmp( psz_name, "RG_PEAK" ) )
589 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
591 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
592 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = us_atof( psz_value );
594 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
595 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
597 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
599 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
600 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( psz_value );
602 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
604 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
606 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
607 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( psz_value );
609 else if( !strcasecmp( psz_name, "METADATA_BLOCK_PICTURE" ) )
610 { /* Do nothing, for now */ }
613 if( !p_dec->p_description )
614 p_dec->p_description = vlc_meta_New();
615 if( p_dec->p_description )
616 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
625 /*****************************************************************************
627 *****************************************************************************/
628 static void ConfigureChannelOrder(uint8_t *pi_chan_table, int i_channels,
629 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,
663 i_channel_mask, pi_chan_table );
665 aout_CheckChannelReorder( NULL, pi_channels_in,
666 i_channel_mask, pi_chan_table );
669 /*****************************************************************************
670 * CloseDecoder: vorbis decoder destruction
671 *****************************************************************************/
672 static void CloseDecoder( vlc_object_t *p_this )
674 decoder_t *p_dec = (decoder_t *)p_this;
675 decoder_sys_t *p_sys = p_dec->p_sys;
677 if( !p_sys->b_packetizer && p_sys->b_has_headers )
679 vorbis_block_clear( &p_sys->vb );
680 vorbis_dsp_clear( &p_sys->vd );
683 vorbis_comment_clear( &p_sys->vc );
684 vorbis_info_clear( &p_sys->vi ); /* must be called last */
689 #ifdef HAVE_VORBIS_ENCODER
690 /*****************************************************************************
691 * encoder_sys_t : vorbis encoder descriptor
692 *****************************************************************************/
698 vorbis_info vi; /* struct that stores all the static vorbis bitstream
700 vorbis_comment vc; /* struct that stores all the bitstream user
702 vorbis_dsp_state vd; /* central working state for the packet->PCM
704 vorbis_block vb; /* local working space for packet->PCM decode */
706 int i_last_block_size;
708 unsigned int i_channels;
711 ** Channel reordering
713 uint8_t pi_chan_table[AOUT_CHAN_MAX];
717 /*****************************************************************************
718 * OpenEncoder: probe the encoder and return score
719 *****************************************************************************/
720 static int OpenEncoder( vlc_object_t *p_this )
722 encoder_t *p_enc = (encoder_t *)p_this;
723 encoder_sys_t *p_sys;
724 int i_quality, i_min_bitrate, i_max_bitrate;
725 ogg_packet header[3];
727 if( p_enc->fmt_out.i_codec != VLC_CODEC_VORBIS &&
733 /* Allocate the memory needed to store the decoder's structure */
734 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
736 p_enc->p_sys = p_sys;
738 p_enc->pf_encode_audio = Encode;
739 p_enc->fmt_in.i_codec = VLC_CODEC_FL32;
740 p_enc->fmt_out.i_codec = VLC_CODEC_VORBIS;
742 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
744 i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
745 if( i_quality > 10 ) i_quality = 10;
746 if( i_quality < 0 ) i_quality = 0;
748 if( var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ) i_quality = 0;
749 i_max_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
750 i_min_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "min-bitrate" );
752 /* Initialize vorbis encoder */
753 vorbis_info_init( &p_sys->vi );
758 if( vorbis_encode_setup_vbr( &p_sys->vi,
759 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
762 vorbis_info_clear( &p_sys->vi );
763 free( p_enc->p_sys );
764 msg_Err( p_enc, "VBR mode initialisation failed" );
768 /* Do we have optional hard quality restrictions? */
769 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
771 struct ovectl_ratemanage_arg ai;
772 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
774 ai.bitrate_hard_min = i_min_bitrate;
775 ai.bitrate_hard_max = i_max_bitrate;
776 ai.management_active = 1;
778 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
783 /* Turn off management entirely */
784 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
789 if( vorbis_encode_setup_managed( &p_sys->vi,
790 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
791 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
792 p_enc->fmt_out.i_bitrate,
793 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
795 vorbis_info_clear( &p_sys->vi );
796 msg_Err( p_enc, "CBR mode initialisation failed" );
797 free( p_enc->p_sys );
802 vorbis_encode_setup_init( &p_sys->vi );
805 vorbis_comment_init( &p_sys->vc);
806 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
808 /* Set up the analysis state and auxiliary encoding storage */
809 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
810 vorbis_block_init( &p_sys->vd, &p_sys->vb );
812 /* Create and store headers */
813 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
814 &header[0], &header[1], &header[2]);
815 for( int i = 0; i < 3; i++ )
817 if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
818 header[i].bytes, header[i].packet ) )
820 p_enc->fmt_out.i_extra = 0;
821 p_enc->fmt_out.p_extra = NULL;
825 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
826 p_sys->i_last_block_size = 0;
827 p_sys->i_samples_delay = 0;
829 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
830 p_enc->fmt_in.audio.i_physical_channels, true);
835 /****************************************************************************
836 * Encode: the whole thing
837 ****************************************************************************
838 * This function spits out ogg packets.
839 ****************************************************************************/
840 static block_t *Encode( encoder_t *p_enc, block_t *p_aout_buf )
842 encoder_sys_t *p_sys = p_enc->p_sys;
843 ogg_packet oggpacket;
844 block_t *p_block, *p_chain = NULL;
847 /* FIXME: flush buffers in here */
848 if( unlikely( !p_aout_buf ) ) return NULL;
850 mtime_t i_pts = p_aout_buf->i_pts -
851 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
852 (mtime_t)p_enc->fmt_in.audio.i_rate;
854 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
856 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
858 /* convert samples to float and uninterleave */
859 for( unsigned int i = 0; i < p_sys->i_channels; i++ )
861 for( unsigned int j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
863 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
864 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
868 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
870 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
874 vorbis_analysis( &p_sys->vb, NULL );
875 vorbis_bitrate_addblock( &p_sys->vb );
877 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
880 p_block = block_Alloc( oggpacket.bytes );
881 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
883 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
885 if( i_block_size < 0 ) i_block_size = 0;
886 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
887 p_sys->i_last_block_size = i_block_size;
889 p_block->i_length = (mtime_t)1000000 *
890 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
892 p_block->i_dts = p_block->i_pts = i_pts;
894 p_sys->i_samples_delay -= i_samples;
897 i_pts += p_block->i_length;
898 block_ChainAppend( &p_chain, p_block );
905 /*****************************************************************************
906 * CloseEncoder: vorbis encoder destruction
907 *****************************************************************************/
908 static void CloseEncoder( vlc_object_t *p_this )
910 encoder_t *p_enc = (encoder_t *)p_this;
911 encoder_sys_t *p_sys = p_enc->p_sys;
913 vorbis_block_clear( &p_sys->vb );
914 vorbis_dsp_clear( &p_sys->vd );
915 vorbis_comment_clear( &p_sys->vc );
916 vorbis_info_clear( &p_sys->vi ); /* must be called last */
921 #endif /* HAVE_VORBIS_ENCODER */