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/codec.h>
50 #ifdef HAVE_VORBIS_VORBISENC_H
51 # include <vorbis/vorbisenc.h>
52 # ifndef OV_ECTL_RATEMANAGE_AVG
53 # define OV_ECTL_RATEMANAGE_AVG 0x0
59 /*****************************************************************************
60 * decoder_sys_t : vorbis decoder descriptor
61 *****************************************************************************/
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 */
86 audio_date_t end_date;
87 int i_last_block_size;
92 int 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_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
109 | AOUT_CHAN_MIDDLERIGHT,
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.ogghelp.com/ogg/glossary.cfm#Audio_Channels
119 /* recommended vorbis channel order for 6 channels */
120 static const uint32_t pi_6channels_in[] =
121 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
122 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
124 /* recommended vorbis channel order for 4 channels */
125 static const uint32_t pi_4channels_in[] =
126 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
128 /* recommended vorbis channel order for 3 channels */
129 static const uint32_t pi_3channels_in[] =
130 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
132 /****************************************************************************
134 ****************************************************************************/
135 static int OpenDecoder ( vlc_object_t * );
136 static int OpenPacketizer( vlc_object_t * );
137 static void CloseDecoder ( vlc_object_t * );
138 static void *DecodeBlock ( decoder_t *, block_t ** );
140 static int ProcessHeaders( decoder_t * );
141 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
143 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
144 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
146 static void ParseVorbisComments( decoder_t * );
148 static void ConfigureChannelOrder(int *, int, uint32_t, bool );
150 #ifdef MODULE_NAME_IS_tremor
151 static void Interleave ( int32_t *, const int32_t **, int, int, int * );
153 static void Interleave ( float *, const float **, int, int, int * );
156 #ifndef MODULE_NAME_IS_tremor
157 static int OpenEncoder ( vlc_object_t * );
158 static void CloseEncoder ( vlc_object_t * );
159 static block_t *Encode ( encoder_t *, aout_buffer_t * );
162 /*****************************************************************************
164 *****************************************************************************/
165 #define ENC_QUALITY_TEXT N_("Encoding quality")
166 #define ENC_QUALITY_LONGTEXT N_( \
167 "Enforce a quality between 1 (low) and 10 (high), instead " \
168 "of specifying a particular bitrate. This will produce a VBR stream." )
169 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
170 #define ENC_MAXBR_LONGTEXT N_( \
171 "Maximum bitrate in kbps. This is useful for streaming applications." )
172 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
173 #define ENC_MINBR_LONGTEXT N_( \
174 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
175 #define ENC_CBR_TEXT N_("CBR encoding")
176 #define ENC_CBR_LONGTEXT N_( \
177 "Force a constant bitrate encoding (CBR)." )
180 set_shortname( "Vorbis" )
181 set_description( N_("Vorbis audio decoder") )
182 #ifdef MODULE_NAME_IS_tremor
183 set_capability( "decoder", 90 )
185 set_capability( "decoder", 100 )
187 set_category( CAT_INPUT )
188 set_subcategory( SUBCAT_INPUT_ACODEC )
189 set_callbacks( OpenDecoder, CloseDecoder )
192 set_description( N_("Vorbis audio packetizer") )
193 set_capability( "packetizer", 100 )
194 set_callbacks( OpenPacketizer, CloseDecoder )
196 #ifndef MODULE_NAME_IS_tremor
197 # define ENC_CFG_PREFIX "sout-vorbis-"
199 set_description( N_("Vorbis audio encoder") )
200 set_capability( "encoder", 100 )
201 #if defined(HAVE_VORBIS_VORBISENC_H)
202 set_callbacks( OpenEncoder, CloseEncoder )
205 add_integer( ENC_CFG_PREFIX "quality", 0, NULL, ENC_QUALITY_TEXT,
206 ENC_QUALITY_LONGTEXT, false )
207 change_integer_range( 0, 10 )
208 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
209 ENC_MAXBR_LONGTEXT, false )
210 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
211 ENC_MINBR_LONGTEXT, false )
212 add_bool( ENC_CFG_PREFIX "cbr", 0, NULL, ENC_CBR_TEXT,
213 ENC_CBR_LONGTEXT, false )
218 #ifndef MODULE_NAME_IS_tremor
219 static const char *const ppsz_enc_options[] = {
220 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
224 /*****************************************************************************
225 * OpenDecoder: probe the decoder and return score
226 *****************************************************************************/
227 static int OpenDecoder( vlc_object_t *p_this )
229 decoder_t *p_dec = (decoder_t*)p_this;
230 decoder_sys_t *p_sys;
232 if( p_dec->fmt_in.i_codec != VLC_CODEC_VORBIS )
237 /* Allocate the memory needed to store the decoder's structure */
238 if( ( p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) ) ) == NULL )
242 aout_DateSet( &p_sys->end_date, 0 );
243 p_sys->i_last_block_size = 0;
244 p_sys->b_packetizer = false;
245 p_sys->i_headers = 0;
247 /* Take care of vorbis init */
248 vorbis_info_init( &p_sys->vi );
249 vorbis_comment_init( &p_sys->vc );
251 /* Set output properties */
252 p_dec->fmt_out.i_cat = AUDIO_ES;
253 #ifdef MODULE_NAME_IS_tremor
254 p_dec->fmt_out.i_codec = VLC_CODEC_FI32;
256 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
260 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
262 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
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 void *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->i_headers == 0 && p_dec->fmt_in.i_extra )
318 /* Headers already available as extra data */
319 msg_Dbg( p_dec, "headers already available as extra data" );
320 p_sys->i_headers = 3;
322 else if( oggpacket.bytes && p_sys->i_headers < 3 )
324 /* Backup headers as extra data */
327 p_dec->fmt_in.p_extra =
328 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
329 oggpacket.bytes + 2 );
330 p_extra = (uint8_t *)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
331 *(p_extra++) = oggpacket.bytes >> 8;
332 *(p_extra++) = oggpacket.bytes & 0xFF;
334 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
335 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
337 block_Release( *pp_block );
342 if( p_sys->i_headers == 3 )
344 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
346 p_sys->i_headers = 0;
347 p_dec->fmt_in.i_extra = 0;
348 block_Release( *pp_block );
351 else p_sys->i_headers++;
354 return ProcessPacket( p_dec, &oggpacket, pp_block );
357 /*****************************************************************************
358 * ProcessHeaders: process Vorbis headers.
359 *****************************************************************************/
360 static int ProcessHeaders( decoder_t *p_dec )
362 decoder_sys_t *p_sys = p_dec->p_sys;
363 ogg_packet oggpacket;
367 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
369 oggpacket.granulepos = -1;
370 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
372 oggpacket.packetno = 0;
373 p_extra = p_dec->fmt_in.p_extra;
374 i_extra = p_dec->fmt_in.i_extra;
376 /* Take care of the initial Vorbis header */
377 oggpacket.bytes = *(p_extra++) << 8;
378 oggpacket.bytes |= (*(p_extra++) & 0xFF);
379 oggpacket.packet = p_extra;
380 p_extra += oggpacket.bytes;
381 i_extra -= (oggpacket.bytes + 2);
384 msg_Err( p_dec, "header data corrupted");
388 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
390 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
394 /* Setup the format */
395 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
396 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
398 if( p_dec->fmt_out.audio.i_channels > 9 )
400 msg_Err( p_dec, "invalid number of channels (not between 1 and 9): %i",
401 p_dec->fmt_out.audio.i_channels );
405 p_dec->fmt_out.audio.i_physical_channels =
406 p_dec->fmt_out.audio.i_original_channels =
407 pi_channels_maps[p_sys->vi.channels];
408 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
410 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
412 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
413 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
415 /* The next packet in order is the comments header */
417 oggpacket.bytes = *(p_extra++) << 8;
418 oggpacket.bytes |= (*(p_extra++) & 0xFF);
419 oggpacket.packet = p_extra;
420 p_extra += oggpacket.bytes;
421 i_extra -= (oggpacket.bytes + 2);
424 msg_Err( p_dec, "header data corrupted");
428 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
430 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
433 ParseVorbisComments( p_dec );
435 /* The next packet in order is the codebooks header
436 * We need to watch out that this packet is not missing as a
437 * missing or corrupted header is fatal. */
438 oggpacket.bytes = *(p_extra++) << 8;
439 oggpacket.bytes |= (*(p_extra++) & 0xFF);
440 oggpacket.packet = p_extra;
441 i_extra -= (oggpacket.bytes + 2);
444 msg_Err( p_dec, "header data corrupted");
448 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
450 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
454 if( !p_sys->b_packetizer )
456 /* Initialize the Vorbis packet->PCM decoder */
457 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
458 vorbis_block_init( &p_sys->vd, &p_sys->vb );
462 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
463 p_dec->fmt_out.p_extra =
464 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
465 memcpy( p_dec->fmt_out.p_extra,
466 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
469 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
470 p_dec->fmt_out.audio.i_physical_channels, true);
475 /*****************************************************************************
476 * ProcessPacket: processes a Vorbis packet.
477 *****************************************************************************/
478 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
481 decoder_sys_t *p_sys = p_dec->p_sys;
482 block_t *p_block = *pp_block;
484 /* Date management */
485 if( p_block && p_block->i_pts > 0 &&
486 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
488 aout_DateSet( &p_sys->end_date, p_block->i_pts );
491 if( !aout_DateGet( &p_sys->end_date ) )
493 /* We've just started the stream, wait for the first PTS. */
494 if( p_block ) block_Release( p_block );
498 *pp_block = NULL; /* To avoid being fed the same packet again */
500 if( p_sys->b_packetizer )
502 return SendPacket( p_dec, p_oggpacket, p_block );
506 aout_buffer_t *p_aout_buffer;
508 if( p_sys->i_headers >= 3 )
509 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
511 p_aout_buffer = NULL;
513 if( p_block ) block_Release( p_block );
514 return p_aout_buffer;
518 /*****************************************************************************
519 * DecodePacket: decodes a Vorbis packet.
520 *****************************************************************************/
521 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
523 decoder_sys_t *p_sys = p_dec->p_sys;
526 #ifdef MODULE_NAME_IS_tremor
532 if( p_oggpacket->bytes &&
533 #ifdef MODULE_NAME_IS_tremor
534 vorbis_synthesis( &p_sys->vb, p_oggpacket, 1 ) == 0 )
536 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
538 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
540 /* **pp_pcm is a multichannel float vector. In stereo, for
541 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
542 * the size of each channel. Convert the float values
543 * (-1.<=range<=1.) to whatever PCM format and write it out */
545 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
548 aout_buffer_t *p_aout_buffer;
551 decoder_NewAudioBuffer( p_dec, i_samples );
553 if( p_aout_buffer == NULL ) return NULL;
555 /* Interleave the samples */
556 #ifdef MODULE_NAME_IS_tremor
557 Interleave( (int32_t *)p_aout_buffer->p_buffer,
558 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
560 Interleave( (float *)p_aout_buffer->p_buffer,
561 (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
564 /* Tell libvorbis how many samples we actually consumed */
565 vorbis_synthesis_read( &p_sys->vd, i_samples );
567 /* Date management */
568 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
569 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date, i_samples );
570 return p_aout_buffer;
578 /*****************************************************************************
579 * SendPacket: send an ogg dated packet to the stream output.
580 *****************************************************************************/
581 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
584 decoder_sys_t *p_sys = p_dec->p_sys;
585 int i_block_size, i_samples;
587 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
588 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
589 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
590 p_sys->i_last_block_size = i_block_size;
592 /* Date management */
593 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
595 if( p_sys->i_headers >= 3 )
596 p_block->i_length = aout_DateIncrement( &p_sys->end_date, i_samples ) - p_block->i_pts;
598 p_block->i_length = 0;
603 /*****************************************************************************
604 * ParseVorbisComments
605 *****************************************************************************/
606 static void ParseVorbisComments( decoder_t *p_dec )
608 char *psz_name, *psz_value, *psz_comment;
611 while( i < p_dec->p_sys->vc.comments )
613 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
616 psz_name = psz_comment;
617 psz_value = strchr( psz_comment, '=' );
623 if( !p_dec->p_description )
624 p_dec->p_description = vlc_meta_New();
625 if( p_dec->p_description )
626 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
628 if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
629 !strcasecmp( psz_name, "RG_RADIO" ) )
631 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
633 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
634 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
636 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
637 !strcasecmp( psz_name, "RG_PEAK" ) )
639 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
641 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
642 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
644 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
645 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
647 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
649 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
650 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
652 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
654 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
656 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
657 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
665 /*****************************************************************************
666 * Interleave: helper function to interleave channels
667 *****************************************************************************/
668 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, bool b_decode)
670 const uint32_t *pi_channels_in;
675 pi_channels_in = pi_6channels_in;
678 pi_channels_in = pi_4channels_in;
681 pi_channels_in = pi_3channels_in;
686 for( i = 0; i< i_channels; ++i )
688 pi_chan_table[i] = i;
695 aout_CheckChannelReorder( pi_channels_in, NULL,
696 i_channel_mask & AOUT_CHAN_PHYSMASK,
700 aout_CheckChannelReorder( NULL, pi_channels_in,
701 i_channel_mask & AOUT_CHAN_PHYSMASK,
706 /*****************************************************************************
707 * Interleave: helper function to interleave channels
708 *****************************************************************************/
709 #ifdef MODULE_NAME_IS_tremor
710 static void Interleave( int32_t *p_out, const int32_t **pp_in,
711 int i_nb_channels, int i_samples, int *pi_chan_table)
715 for ( j = 0; j < i_samples; j++ )
716 for ( i = 0; i < i_nb_channels; i++ )
717 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
720 static void Interleave( float *p_out, const float **pp_in,
721 int i_nb_channels, int i_samples, int *pi_chan_table )
725 for ( j = 0; j < i_samples; j++ )
726 for ( i = 0; i < i_nb_channels; i++ )
727 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
731 /*****************************************************************************
732 * CloseDecoder: vorbis decoder destruction
733 *****************************************************************************/
734 static void CloseDecoder( vlc_object_t *p_this )
736 decoder_t *p_dec = (decoder_t *)p_this;
737 decoder_sys_t *p_sys = p_dec->p_sys;
739 if( !p_sys->b_packetizer && p_sys->i_headers > 3 )
741 vorbis_block_clear( &p_sys->vb );
742 vorbis_dsp_clear( &p_sys->vd );
745 vorbis_comment_clear( &p_sys->vc );
746 vorbis_info_clear( &p_sys->vi ); /* must be called last */
751 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
753 /*****************************************************************************
754 * encoder_sys_t : vorbis encoder descriptor
755 *****************************************************************************/
761 vorbis_info vi; /* struct that stores all the static vorbis bitstream
763 vorbis_comment vc; /* struct that stores all the bitstream user
765 vorbis_dsp_state vd; /* central working state for the packet->PCM
767 vorbis_block vb; /* local working space for packet->PCM decode */
769 int i_last_block_size;
779 ** Channel reordering
781 int pi_chan_table[AOUT_CHAN_MAX];
785 /*****************************************************************************
786 * OpenEncoder: probe the encoder and return score
787 *****************************************************************************/
788 static int OpenEncoder( vlc_object_t *p_this )
790 encoder_t *p_enc = (encoder_t *)p_this;
791 encoder_sys_t *p_sys;
792 int i_quality, i_min_bitrate, i_max_bitrate, i;
793 ogg_packet header[3];
797 if( p_enc->fmt_out.i_codec != VLC_CODEC_VORBIS &&
803 /* Allocate the memory needed to store the decoder's structure */
804 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
806 p_enc->p_sys = p_sys;
808 p_enc->pf_encode_audio = Encode;
809 p_enc->fmt_in.i_codec = VLC_CODEC_FL32;
810 p_enc->fmt_out.i_codec = VLC_CODEC_VORBIS;
812 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
814 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
815 i_quality = val.i_int;
816 if( i_quality > 10 ) i_quality = 10;
817 if( i_quality < 0 ) i_quality = 0;
818 var_Get( p_enc, ENC_CFG_PREFIX "cbr", &val );
819 if( val.b_bool ) i_quality = 0;
820 var_Get( p_enc, ENC_CFG_PREFIX "max-bitrate", &val );
821 i_max_bitrate = val.i_int;
822 var_Get( p_enc, ENC_CFG_PREFIX "min-bitrate", &val );
823 i_min_bitrate = val.i_int;
825 /* Initialize vorbis encoder */
826 vorbis_info_init( &p_sys->vi );
831 if( vorbis_encode_setup_vbr( &p_sys->vi,
832 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
835 vorbis_info_clear( &p_sys->vi );
836 free( p_enc->p_sys );
837 msg_Err( p_enc, "VBR mode initialisation failed" );
841 /* Do we have optional hard quality restrictions? */
842 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
844 struct ovectl_ratemanage_arg ai;
845 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
847 ai.bitrate_hard_min = i_min_bitrate;
848 ai.bitrate_hard_max = i_max_bitrate;
849 ai.management_active = 1;
851 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
856 /* Turn off management entirely */
857 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
862 if( vorbis_encode_setup_managed( &p_sys->vi,
863 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
864 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
865 p_enc->fmt_out.i_bitrate,
866 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
868 vorbis_info_clear( &p_sys->vi );
869 msg_Err( p_enc, "CBR mode initialisation failed" );
870 free( p_enc->p_sys );
875 vorbis_encode_setup_init( &p_sys->vi );
878 vorbis_comment_init( &p_sys->vc);
879 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
881 /* Set up the analysis state and auxiliary encoding storage */
882 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
883 vorbis_block_init( &p_sys->vd, &p_sys->vb );
885 /* Create and store headers */
886 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
887 &header[0], &header[1], &header[2]);
888 p_enc->fmt_out.i_extra = 3 * 2 + header[0].bytes +
889 header[1].bytes + header[2].bytes;
890 p_extra = p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
891 for( i = 0; i < 3; i++ )
893 *(p_extra++) = header[i].bytes >> 8;
894 *(p_extra++) = header[i].bytes & 0xFF;
895 memcpy( p_extra, header[i].packet, header[i].bytes );
896 p_extra += header[i].bytes;
899 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
900 p_sys->i_last_block_size = 0;
901 p_sys->i_samples_delay = 0;
904 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
905 p_enc->fmt_in.audio.i_physical_channels, true);
910 /****************************************************************************
911 * Encode: the whole thing
912 ****************************************************************************
913 * This function spits out ogg packets.
914 ****************************************************************************/
915 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
917 encoder_sys_t *p_sys = p_enc->p_sys;
918 ogg_packet oggpacket;
919 block_t *p_block, *p_chain = NULL;
924 p_sys->i_pts = p_aout_buf->start_date -
925 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
926 (mtime_t)p_enc->fmt_in.audio.i_rate;
928 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
930 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
932 /* convert samples to float and uninterleave */
933 for( i = 0; i < p_sys->i_channels; i++ )
935 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
937 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
938 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
942 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
944 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
948 vorbis_analysis( &p_sys->vb, NULL );
949 vorbis_bitrate_addblock( &p_sys->vb );
951 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
954 p_block = block_New( p_enc, oggpacket.bytes );
955 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
957 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
959 if( i_block_size < 0 ) i_block_size = 0;
960 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
961 p_sys->i_last_block_size = i_block_size;
963 p_block->i_length = (mtime_t)1000000 *
964 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
966 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
968 p_sys->i_samples_delay -= i_samples;
971 p_sys->i_pts += p_block->i_length;
972 block_ChainAppend( &p_chain, p_block );
979 /*****************************************************************************
980 * CloseEncoder: vorbis encoder destruction
981 *****************************************************************************/
982 static void CloseEncoder( vlc_object_t *p_this )
984 encoder_t *p_enc = (encoder_t *)p_this;
985 encoder_sys_t *p_sys = p_enc->p_sys;
987 vorbis_block_clear( &p_sys->vb );
988 vorbis_dsp_clear( &p_sys->vd );
989 vorbis_comment_clear( &p_sys->vc );
990 vorbis_info_clear( &p_sys->vi ); /* must be called last */
995 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */