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>
43 #ifdef MODULE_NAME_IS_tremor
44 #include <tremor/ivorbiscodec.h>
47 #include <vorbis/vorbisenc.h>
49 # ifndef OV_ECTL_RATEMANAGE_AVG
50 # define OV_ECTL_RATEMANAGE_AVG 0x0
55 /*****************************************************************************
56 * decoder_sys_t : vorbis decoder descriptor
57 *****************************************************************************/
71 vorbis_info vi; /* struct that stores all the static vorbis bitstream
73 vorbis_comment vc; /* struct that stores all the bitstream user
75 vorbis_dsp_state vd; /* central working state for the packet->PCM
77 vorbis_block vb; /* local working space for packet->PCM decode */
83 int i_last_block_size;
88 int pi_chan_table[AOUT_CHAN_MAX];
91 static const int pi_channels_maps[9] =
95 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
96 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
97 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
98 | AOUT_CHAN_REARRIGHT,
99 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
100 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
101 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
102 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
103 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
104 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
105 | AOUT_CHAN_MIDDLERIGHT,
106 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
107 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
112 ** channel order as defined in http://www.ogghelp.com/ogg/glossary.cfm#Audio_Channels
115 /* recommended vorbis channel order for 6 channels */
116 static const uint32_t pi_6channels_in[] =
117 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
118 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
120 /* recommended vorbis channel order for 4 channels */
121 static const uint32_t pi_4channels_in[] =
122 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
124 /* recommended vorbis channel order for 3 channels */
125 static const uint32_t pi_3channels_in[] =
126 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
128 /****************************************************************************
130 ****************************************************************************/
131 static int OpenDecoder ( vlc_object_t * );
132 static int OpenPacketizer( vlc_object_t * );
133 static void CloseDecoder ( vlc_object_t * );
134 static void *DecodeBlock ( decoder_t *, block_t ** );
136 static int ProcessHeaders( decoder_t * );
137 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
139 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
140 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
142 static void ParseVorbisComments( decoder_t * );
144 static void ConfigureChannelOrder(int *, int, uint32_t, bool );
146 #ifdef MODULE_NAME_IS_tremor
147 static void Interleave ( int32_t *, const int32_t **, int, int, int * );
149 static void Interleave ( float *, const float **, int, int, int * );
152 #ifndef MODULE_NAME_IS_tremor
153 static int OpenEncoder ( vlc_object_t * );
154 static void CloseEncoder ( vlc_object_t * );
155 static block_t *Encode ( encoder_t *, aout_buffer_t * );
158 /*****************************************************************************
160 *****************************************************************************/
161 #define ENC_QUALITY_TEXT N_("Encoding quality")
162 #define ENC_QUALITY_LONGTEXT N_( \
163 "Enforce a quality between 1 (low) and 10 (high), instead " \
164 "of specifying a particular bitrate. This will produce a VBR stream." )
165 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
166 #define ENC_MAXBR_LONGTEXT N_( \
167 "Maximum bitrate in kbps. This is useful for streaming applications." )
168 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
169 #define ENC_MINBR_LONGTEXT N_( \
170 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
171 #define ENC_CBR_TEXT N_("CBR encoding")
172 #define ENC_CBR_LONGTEXT N_( \
173 "Force a constant bitrate encoding (CBR)." )
176 set_shortname( "Vorbis" )
177 set_description( N_("Vorbis audio decoder") )
178 #ifdef MODULE_NAME_IS_tremor
179 set_capability( "decoder", 90 )
181 set_capability( "decoder", 100 )
183 set_category( CAT_INPUT )
184 set_subcategory( SUBCAT_INPUT_ACODEC )
185 set_callbacks( OpenDecoder, CloseDecoder )
188 set_description( N_("Vorbis audio packetizer") )
189 set_capability( "packetizer", 100 )
190 set_callbacks( OpenPacketizer, CloseDecoder )
192 #ifndef MODULE_NAME_IS_tremor
193 # define ENC_CFG_PREFIX "sout-vorbis-"
195 set_description( N_("Vorbis audio encoder") )
196 set_capability( "encoder", 100 )
197 set_callbacks( OpenEncoder, CloseEncoder )
199 add_integer( ENC_CFG_PREFIX "quality", 0, NULL, ENC_QUALITY_TEXT,
200 ENC_QUALITY_LONGTEXT, false )
201 change_integer_range( 0, 10 )
202 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
203 ENC_MAXBR_LONGTEXT, false )
204 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
205 ENC_MINBR_LONGTEXT, false )
206 add_bool( ENC_CFG_PREFIX "cbr", false, NULL, ENC_CBR_TEXT,
207 ENC_CBR_LONGTEXT, false )
212 #ifndef MODULE_NAME_IS_tremor
213 static const char *const ppsz_enc_options[] = {
214 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
218 /*****************************************************************************
219 * OpenDecoder: probe the decoder and return score
220 *****************************************************************************/
221 static int OpenDecoder( vlc_object_t *p_this )
223 decoder_t *p_dec = (decoder_t*)p_this;
224 decoder_sys_t *p_sys;
226 if( p_dec->fmt_in.i_codec != VLC_CODEC_VORBIS )
231 /* Allocate the memory needed to store the decoder's structure */
232 if( ( p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) ) ) == NULL )
236 date_Set( &p_sys->end_date, 0 );
237 p_sys->i_last_block_size = 0;
238 p_sys->b_packetizer = false;
239 p_sys->i_headers = 0;
241 /* Take care of vorbis init */
242 vorbis_info_init( &p_sys->vi );
243 vorbis_comment_init( &p_sys->vc );
245 /* Set output properties */
246 p_dec->fmt_out.i_cat = AUDIO_ES;
247 #ifdef MODULE_NAME_IS_tremor
248 p_dec->fmt_out.i_codec = VLC_CODEC_FI32;
250 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
254 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
256 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
262 static int OpenPacketizer( vlc_object_t *p_this )
264 decoder_t *p_dec = (decoder_t*)p_this;
266 int i_ret = OpenDecoder( p_this );
268 if( i_ret == VLC_SUCCESS )
270 p_dec->p_sys->b_packetizer = true;
271 p_dec->fmt_out.i_codec = VLC_CODEC_VORBIS;
277 /****************************************************************************
278 * DecodeBlock: the whole thing
279 ****************************************************************************
280 * This function must be fed with ogg packets.
281 ****************************************************************************/
282 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
284 decoder_sys_t *p_sys = p_dec->p_sys;
285 ogg_packet oggpacket;
287 if( !pp_block ) return NULL;
291 /* Block to Ogg packet */
292 oggpacket.packet = (*pp_block)->p_buffer;
293 oggpacket.bytes = (*pp_block)->i_buffer;
297 if( p_sys->b_packetizer ) return NULL;
299 /* Block to Ogg packet */
300 oggpacket.packet = NULL;
304 oggpacket.granulepos = -1;
307 oggpacket.packetno = 0;
309 /* Check for headers */
310 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
312 /* Headers already available as extra data */
313 msg_Dbg( p_dec, "headers already available as extra data" );
314 p_sys->i_headers = 3;
316 else if( oggpacket.bytes && p_sys->i_headers < 3 )
318 /* Backup headers as extra data */
321 p_dec->fmt_in.p_extra = xrealloc( p_dec->fmt_in.p_extra,
322 p_dec->fmt_in.i_extra + oggpacket.bytes + 2 );
323 p_extra = (uint8_t *)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
324 *(p_extra++) = oggpacket.bytes >> 8;
325 *(p_extra++) = oggpacket.bytes & 0xFF;
327 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
328 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
330 block_Release( *pp_block );
335 if( p_sys->i_headers == 3 )
337 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
339 p_sys->i_headers = 0;
340 p_dec->fmt_in.i_extra = 0;
341 block_Release( *pp_block );
344 else p_sys->i_headers++;
347 return ProcessPacket( p_dec, &oggpacket, pp_block );
350 /*****************************************************************************
351 * ProcessHeaders: process Vorbis headers.
352 *****************************************************************************/
353 static int ProcessHeaders( decoder_t *p_dec )
355 decoder_sys_t *p_sys = p_dec->p_sys;
356 ogg_packet oggpacket;
360 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
362 oggpacket.granulepos = -1;
363 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
365 oggpacket.packetno = 0;
366 p_extra = p_dec->fmt_in.p_extra;
367 i_extra = p_dec->fmt_in.i_extra;
369 /* Take care of the initial Vorbis header */
370 oggpacket.bytes = *(p_extra++) << 8;
371 oggpacket.bytes |= (*(p_extra++) & 0xFF);
372 oggpacket.packet = p_extra;
373 p_extra += oggpacket.bytes;
374 i_extra -= (oggpacket.bytes + 2);
377 msg_Err( p_dec, "header data corrupted");
381 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
383 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
387 /* Setup the format */
388 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
389 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
391 if( p_dec->fmt_out.audio.i_channels > 9 )
393 msg_Err( p_dec, "invalid number of channels (not between 1 and 9): %i",
394 p_dec->fmt_out.audio.i_channels );
398 p_dec->fmt_out.audio.i_physical_channels =
399 p_dec->fmt_out.audio.i_original_channels =
400 pi_channels_maps[p_sys->vi.channels];
401 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
403 date_Init( &p_sys->end_date, p_sys->vi.rate, 1 );
405 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
406 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
408 /* The next packet in order is the comments header */
410 oggpacket.bytes = *(p_extra++) << 8;
411 oggpacket.bytes |= (*(p_extra++) & 0xFF);
412 oggpacket.packet = p_extra;
413 p_extra += oggpacket.bytes;
414 i_extra -= (oggpacket.bytes + 2);
417 msg_Err( p_dec, "header data corrupted");
421 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
423 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
426 ParseVorbisComments( p_dec );
428 /* The next packet in order is the codebooks header
429 * We need to watch out that this packet is not missing as a
430 * missing or corrupted header is fatal. */
431 oggpacket.bytes = *(p_extra++) << 8;
432 oggpacket.bytes |= (*(p_extra++) & 0xFF);
433 oggpacket.packet = p_extra;
434 i_extra -= (oggpacket.bytes + 2);
437 msg_Err( p_dec, "header data corrupted");
441 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
443 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
447 if( !p_sys->b_packetizer )
449 /* Initialize the Vorbis packet->PCM decoder */
450 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
451 vorbis_block_init( &p_sys->vd, &p_sys->vb );
455 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
456 p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
457 p_dec->fmt_out.i_extra );
458 memcpy( p_dec->fmt_out.p_extra,
459 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
462 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
463 p_dec->fmt_out.audio.i_physical_channels, true);
468 /*****************************************************************************
469 * ProcessPacket: processes a Vorbis packet.
470 *****************************************************************************/
471 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
474 decoder_sys_t *p_sys = p_dec->p_sys;
475 block_t *p_block = *pp_block;
477 /* Date management */
478 if( p_block && p_block->i_pts > VLC_TS_INVALID &&
479 p_block->i_pts != date_Get( &p_sys->end_date ) )
481 date_Set( &p_sys->end_date, p_block->i_pts );
484 if( !date_Get( &p_sys->end_date ) )
486 /* We've just started the stream, wait for the first PTS. */
487 if( p_block ) block_Release( p_block );
491 *pp_block = NULL; /* To avoid being fed the same packet again */
493 if( p_sys->b_packetizer )
495 return SendPacket( p_dec, p_oggpacket, p_block );
499 aout_buffer_t *p_aout_buffer;
501 if( p_sys->i_headers >= 3 )
502 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
504 p_aout_buffer = NULL;
506 if( p_block ) block_Release( p_block );
507 return p_aout_buffer;
511 /*****************************************************************************
512 * DecodePacket: decodes a Vorbis packet.
513 *****************************************************************************/
514 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
516 decoder_sys_t *p_sys = p_dec->p_sys;
519 #ifdef MODULE_NAME_IS_tremor
525 if( p_oggpacket->bytes &&
526 #ifdef MODULE_NAME_IS_tremor
527 vorbis_synthesis( &p_sys->vb, p_oggpacket, 1 ) == 0 )
529 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
531 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
533 /* **pp_pcm is a multichannel float vector. In stereo, for
534 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
535 * the size of each channel. Convert the float values
536 * (-1.<=range<=1.) to whatever PCM format and write it out */
538 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
541 aout_buffer_t *p_aout_buffer;
544 decoder_NewAudioBuffer( p_dec, i_samples );
546 if( p_aout_buffer == NULL ) return NULL;
548 /* Interleave the samples */
549 #ifdef MODULE_NAME_IS_tremor
550 Interleave( (int32_t *)p_aout_buffer->p_buffer,
551 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
553 Interleave( (float *)p_aout_buffer->p_buffer,
554 (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
557 /* Tell libvorbis how many samples we actually consumed */
558 vorbis_synthesis_read( &p_sys->vd, i_samples );
560 /* Date management */
561 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
562 p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
563 i_samples ) - p_aout_buffer->i_pts;
564 return p_aout_buffer;
572 /*****************************************************************************
573 * SendPacket: send an ogg dated packet to the stream output.
574 *****************************************************************************/
575 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
578 decoder_sys_t *p_sys = p_dec->p_sys;
579 int i_block_size, i_samples;
581 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
582 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
583 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
584 p_sys->i_last_block_size = i_block_size;
586 /* Date management */
587 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->end_date );
589 if( p_sys->i_headers >= 3 )
590 p_block->i_length = date_Increment( &p_sys->end_date, i_samples ) - p_block->i_pts;
592 p_block->i_length = 0;
597 /*****************************************************************************
598 * ParseVorbisComments
599 *****************************************************************************/
600 static void ParseVorbisComments( decoder_t *p_dec )
602 char *psz_name, *psz_value, *psz_comment;
605 while( i < p_dec->p_sys->vc.comments )
607 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
610 psz_name = psz_comment;
611 psz_value = strchr( psz_comment, '=' );
617 if( !p_dec->p_description )
618 p_dec->p_description = vlc_meta_New();
619 if( p_dec->p_description )
620 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
622 if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
623 !strcasecmp( psz_name, "RG_RADIO" ) )
625 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
627 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
628 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
630 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
631 !strcasecmp( psz_name, "RG_PEAK" ) )
633 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
635 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
636 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
638 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
639 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
641 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
643 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
644 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
646 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
648 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
650 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
651 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
659 /*****************************************************************************
660 * Interleave: helper function to interleave channels
661 *****************************************************************************/
662 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, bool b_decode)
664 const uint32_t *pi_channels_in;
669 pi_channels_in = pi_6channels_in;
672 pi_channels_in = pi_4channels_in;
675 pi_channels_in = pi_3channels_in;
680 for( i = 0; i< i_channels; ++i )
682 pi_chan_table[i] = i;
689 aout_CheckChannelReorder( pi_channels_in, NULL,
690 i_channel_mask & AOUT_CHAN_PHYSMASK,
694 aout_CheckChannelReorder( NULL, pi_channels_in,
695 i_channel_mask & AOUT_CHAN_PHYSMASK,
700 /*****************************************************************************
701 * Interleave: helper function to interleave channels
702 *****************************************************************************/
703 #ifdef MODULE_NAME_IS_tremor
704 static void Interleave( int32_t *p_out, const int32_t **pp_in,
705 int i_nb_channels, int i_samples, int *pi_chan_table)
709 for ( j = 0; j < i_samples; j++ )
710 for ( i = 0; i < i_nb_channels; i++ )
711 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
714 static void Interleave( float *p_out, const float **pp_in,
715 int i_nb_channels, int i_samples, int *pi_chan_table )
719 for ( j = 0; j < i_samples; j++ )
720 for ( i = 0; i < i_nb_channels; i++ )
721 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
725 /*****************************************************************************
726 * CloseDecoder: vorbis decoder destruction
727 *****************************************************************************/
728 static void CloseDecoder( vlc_object_t *p_this )
730 decoder_t *p_dec = (decoder_t *)p_this;
731 decoder_sys_t *p_sys = p_dec->p_sys;
733 if( !p_sys->b_packetizer && p_sys->i_headers > 3 )
735 vorbis_block_clear( &p_sys->vb );
736 vorbis_dsp_clear( &p_sys->vd );
739 vorbis_comment_clear( &p_sys->vc );
740 vorbis_info_clear( &p_sys->vi ); /* must be called last */
745 #ifndef MODULE_NAME_IS_tremor
747 /*****************************************************************************
748 * encoder_sys_t : vorbis encoder descriptor
749 *****************************************************************************/
755 vorbis_info vi; /* struct that stores all the static vorbis bitstream
757 vorbis_comment vc; /* struct that stores all the bitstream user
759 vorbis_dsp_state vd; /* central working state for the packet->PCM
761 vorbis_block vb; /* local working space for packet->PCM decode */
763 int i_last_block_size;
768 ** Channel reordering
770 int pi_chan_table[AOUT_CHAN_MAX];
774 /*****************************************************************************
775 * OpenEncoder: probe the encoder and return score
776 *****************************************************************************/
777 static int OpenEncoder( vlc_object_t *p_this )
779 encoder_t *p_enc = (encoder_t *)p_this;
780 encoder_sys_t *p_sys;
781 int i_quality, i_min_bitrate, i_max_bitrate, i;
782 ogg_packet header[3];
785 if( p_enc->fmt_out.i_codec != VLC_CODEC_VORBIS &&
791 /* Allocate the memory needed to store the decoder's structure */
792 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
794 p_enc->p_sys = p_sys;
796 p_enc->pf_encode_audio = Encode;
797 p_enc->fmt_in.i_codec = VLC_CODEC_FL32;
798 p_enc->fmt_out.i_codec = VLC_CODEC_VORBIS;
800 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
802 i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
803 if( i_quality > 10 ) i_quality = 10;
804 if( i_quality < 0 ) i_quality = 0;
806 if( var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ) i_quality = 0;
807 i_max_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
808 i_min_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "min-bitrate" );
810 /* Initialize vorbis encoder */
811 vorbis_info_init( &p_sys->vi );
816 if( vorbis_encode_setup_vbr( &p_sys->vi,
817 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
820 vorbis_info_clear( &p_sys->vi );
821 free( p_enc->p_sys );
822 msg_Err( p_enc, "VBR mode initialisation failed" );
826 /* Do we have optional hard quality restrictions? */
827 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
829 struct ovectl_ratemanage_arg ai;
830 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
832 ai.bitrate_hard_min = i_min_bitrate;
833 ai.bitrate_hard_max = i_max_bitrate;
834 ai.management_active = 1;
836 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
841 /* Turn off management entirely */
842 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
847 if( vorbis_encode_setup_managed( &p_sys->vi,
848 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
849 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
850 p_enc->fmt_out.i_bitrate,
851 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
853 vorbis_info_clear( &p_sys->vi );
854 msg_Err( p_enc, "CBR mode initialisation failed" );
855 free( p_enc->p_sys );
860 vorbis_encode_setup_init( &p_sys->vi );
863 vorbis_comment_init( &p_sys->vc);
864 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
866 /* Set up the analysis state and auxiliary encoding storage */
867 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
868 vorbis_block_init( &p_sys->vd, &p_sys->vb );
870 /* Create and store headers */
871 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
872 &header[0], &header[1], &header[2]);
873 p_enc->fmt_out.i_extra = 3 * 2 + header[0].bytes +
874 header[1].bytes + header[2].bytes;
875 p_extra = p_enc->fmt_out.p_extra = xmalloc( p_enc->fmt_out.i_extra );
876 for( i = 0; i < 3; i++ )
878 *(p_extra++) = header[i].bytes >> 8;
879 *(p_extra++) = header[i].bytes & 0xFF;
880 memcpy( p_extra, header[i].packet, header[i].bytes );
881 p_extra += header[i].bytes;
884 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
885 p_sys->i_last_block_size = 0;
886 p_sys->i_samples_delay = 0;
888 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
889 p_enc->fmt_in.audio.i_physical_channels, true);
894 /****************************************************************************
895 * Encode: the whole thing
896 ****************************************************************************
897 * This function spits out ogg packets.
898 ****************************************************************************/
899 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
901 encoder_sys_t *p_sys = p_enc->p_sys;
902 ogg_packet oggpacket;
903 block_t *p_block, *p_chain = NULL;
908 mtime_t i_pts = p_aout_buf->i_pts -
909 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
910 (mtime_t)p_enc->fmt_in.audio.i_rate;
912 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
914 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
916 /* convert samples to float and uninterleave */
917 for( i = 0; i < p_sys->i_channels; i++ )
919 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
921 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
922 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
926 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
928 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
932 vorbis_analysis( &p_sys->vb, NULL );
933 vorbis_bitrate_addblock( &p_sys->vb );
935 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
938 p_block = block_New( p_enc, oggpacket.bytes );
939 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
941 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
943 if( i_block_size < 0 ) i_block_size = 0;
944 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
945 p_sys->i_last_block_size = i_block_size;
947 p_block->i_length = (mtime_t)1000000 *
948 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
950 p_block->i_dts = p_block->i_pts = i_pts;
952 p_sys->i_samples_delay -= i_samples;
955 i_pts += p_block->i_length;
956 block_ChainAppend( &p_chain, p_block );
963 /*****************************************************************************
964 * CloseEncoder: vorbis encoder destruction
965 *****************************************************************************/
966 static void CloseEncoder( vlc_object_t *p_this )
968 encoder_t *p_enc = (encoder_t *)p_this;
969 encoder_sys_t *p_sys = p_enc->p_sys;
971 vorbis_block_clear( &p_sys->vb );
972 vorbis_dsp_clear( &p_sys->vd );
973 vorbis_comment_clear( &p_sys->vc );
974 vorbis_info_clear( &p_sys->vi ); /* must be called last */
979 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */