1 /*****************************************************************************
2 * vorbis.c: vorbis decoder/encoder/packetizer module making use of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/decoder.h>
29 #include <vlc/input.h>
34 #ifdef MODULE_NAME_IS_tremor
35 #include <tremor/ivorbiscodec.h>
38 #include <vorbis/codec.h>
41 #ifdef HAVE_VORBIS_VORBISENC_H
42 # include <vorbis/vorbisenc.h>
43 # ifndef OV_ECTL_RATEMANAGE_AVG
44 # define OV_ECTL_RATEMANAGE_AVG 0x0
50 /*****************************************************************************
51 * decoder_sys_t : vorbis decoder descriptor
52 *****************************************************************************/
56 vlc_bool_t b_packetizer;
66 vorbis_info vi; /* struct that stores all the static vorbis bitstream
68 vorbis_comment vc; /* struct that stores all the bitstream user
70 vorbis_dsp_state vd; /* central working state for the packet->PCM
72 vorbis_block vb; /* local working space for packet->PCM decode */
77 audio_date_t end_date;
78 int i_last_block_size;
83 int pi_chan_table[AOUT_CHAN_MAX];
86 static int pi_channels_maps[7] =
90 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
91 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
92 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
93 | AOUT_CHAN_REARRIGHT,
94 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
95 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
96 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
97 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
101 ** channel order as defined in http://www.ogghelp.com/ogg/glossary.cfm#Audio_Channels
104 /* recommended vorbis channel order for 6 channels */
105 static const uint32_t pi_6channels_in[] =
106 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
107 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
109 /* recommended vorbis channel order for 4 channels */
110 static const uint32_t pi_4channels_in[] =
111 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
113 /* recommended vorbis channel order for 3 channels */
114 static const uint32_t pi_3channels_in[] =
115 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
117 /* our internal channel order (WG-4 order) */
118 static const uint32_t pi_channels_out[] =
119 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
120 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
122 /****************************************************************************
124 ****************************************************************************/
125 static int OpenDecoder ( vlc_object_t * );
126 static int OpenPacketizer( vlc_object_t * );
127 static void CloseDecoder ( vlc_object_t * );
128 static void *DecodeBlock ( decoder_t *, block_t ** );
130 static int ProcessHeaders( decoder_t * );
131 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
133 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
134 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
136 static void ParseVorbisComments( decoder_t * );
138 static void ConfigureChannelOrder(int *, int, uint32_t, vlc_bool_t );
140 #ifdef MODULE_NAME_IS_tremor
141 static void Interleave ( int32_t *, const int32_t **, int, int, int * );
143 static void Interleave ( float *, const float **, int, int, int * );
146 #ifndef MODULE_NAME_IS_tremor
147 static int OpenEncoder ( vlc_object_t * );
148 static void CloseEncoder ( vlc_object_t * );
149 static block_t *Encode ( encoder_t *, aout_buffer_t * );
152 /*****************************************************************************
154 *****************************************************************************/
155 #define ENC_QUALITY_TEXT N_("Encoding quality")
156 #define ENC_QUALITY_LONGTEXT N_( \
157 "Allows you to specify a quality between 1 (low) and 10 (high), instead " \
158 "of specifying a particular bitrate. This will produce a VBR stream." )
159 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
160 #define ENC_MAXBR_LONGTEXT N_( \
161 "Allows you to specify a maximum bitrate in kbps. " \
162 "Useful for streaming applications." )
163 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
164 #define ENC_MINBR_LONGTEXT N_( \
165 "Allows you to specify a minimum bitrate in kbps. " \
166 "Useful for encoding for a fixed-size channel." )
167 #define ENC_CBR_TEXT N_("CBR encoding")
168 #define ENC_CBR_LONGTEXT N_( \
169 "Allows you to force a constant bitrate encoding (CBR)." )
172 set_shortname( "Vorbis" );
173 set_description( _("Vorbis audio decoder") );
174 #ifdef MODULE_NAME_IS_tremor
175 set_capability( "decoder", 90 );
177 set_capability( "decoder", 100 );
179 set_category( CAT_INPUT );
180 set_subcategory( SUBCAT_INPUT_ACODEC );
181 set_callbacks( OpenDecoder, CloseDecoder );
184 set_description( _("Vorbis audio packetizer") );
185 set_capability( "packetizer", 100 );
186 set_callbacks( OpenPacketizer, CloseDecoder );
188 #ifndef MODULE_NAME_IS_tremor
189 # define ENC_CFG_PREFIX "sout-vorbis-"
191 set_description( _("Vorbis audio encoder") );
192 set_capability( "encoder", 100 );
193 #if defined(HAVE_VORBIS_VORBISENC_H)
194 set_callbacks( OpenEncoder, CloseEncoder );
197 add_integer( ENC_CFG_PREFIX "quality", 3, NULL, ENC_QUALITY_TEXT,
198 ENC_QUALITY_LONGTEXT, VLC_FALSE );
199 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
200 ENC_MAXBR_LONGTEXT, VLC_FALSE );
201 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
202 ENC_MINBR_LONGTEXT, VLC_FALSE );
203 add_bool( ENC_CFG_PREFIX "cbr", 0, NULL, ENC_CBR_TEXT,
204 ENC_CBR_LONGTEXT, VLC_FALSE );
209 #ifndef MODULE_NAME_IS_tremor
210 static const char *ppsz_enc_options[] = {
211 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
215 /*****************************************************************************
216 * OpenDecoder: probe the decoder and return score
217 *****************************************************************************/
218 static int OpenDecoder( vlc_object_t *p_this )
220 decoder_t *p_dec = (decoder_t*)p_this;
221 decoder_sys_t *p_sys;
223 if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
228 /* Allocate the memory needed to store the decoder's structure */
229 if( ( p_dec->p_sys = p_sys =
230 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
232 msg_Err( p_dec, "out of memory" );
237 aout_DateSet( &p_sys->end_date, 0 );
238 p_sys->i_last_block_size = 0;
239 p_sys->b_packetizer = VLC_FALSE;
240 p_sys->i_headers = 0;
242 /* Take care of vorbis init */
243 vorbis_info_init( &p_sys->vi );
244 vorbis_comment_init( &p_sys->vc );
246 /* Set output properties */
247 p_dec->fmt_out.i_cat = AUDIO_ES;
248 #ifdef MODULE_NAME_IS_tremor
249 p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
251 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
255 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
257 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
263 static int OpenPacketizer( vlc_object_t *p_this )
265 decoder_t *p_dec = (decoder_t*)p_this;
267 int i_ret = OpenDecoder( p_this );
269 if( i_ret == VLC_SUCCESS )
271 p_dec->p_sys->b_packetizer = VLC_TRUE;
272 p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
278 /****************************************************************************
279 * DecodeBlock: the whole thing
280 ****************************************************************************
281 * This function must be fed with ogg packets.
282 ****************************************************************************/
283 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
285 decoder_sys_t *p_sys = p_dec->p_sys;
286 ogg_packet oggpacket;
288 if( !pp_block ) return NULL;
292 /* Block to Ogg packet */
293 oggpacket.packet = (*pp_block)->p_buffer;
294 oggpacket.bytes = (*pp_block)->i_buffer;
298 if( p_sys->b_packetizer ) return NULL;
300 /* Block to Ogg packet */
301 oggpacket.packet = NULL;
305 oggpacket.granulepos = -1;
308 oggpacket.packetno = 0;
310 /* Check for headers */
311 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
313 /* 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 =
322 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
323 oggpacket.bytes + 2 );
324 p_extra = (uint8_t *)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
325 *(p_extra++) = oggpacket.bytes >> 8;
326 *(p_extra++) = oggpacket.bytes & 0xFF;
328 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
329 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
331 block_Release( *pp_block );
336 if( p_sys->i_headers == 3 )
338 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
340 p_sys->i_headers = 0;
341 p_dec->fmt_in.i_extra = 0;
342 block_Release( *pp_block );
345 else p_sys->i_headers++;
348 return ProcessPacket( p_dec, &oggpacket, pp_block );
351 /*****************************************************************************
352 * ProcessHeaders: process Vorbis headers.
353 *****************************************************************************/
354 static int ProcessHeaders( decoder_t *p_dec )
356 decoder_sys_t *p_sys = p_dec->p_sys;
357 ogg_packet oggpacket;
361 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
363 oggpacket.granulepos = -1;
364 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
366 oggpacket.packetno = 0;
367 p_extra = p_dec->fmt_in.p_extra;
368 i_extra = p_dec->fmt_in.i_extra;
370 /* Take care of the initial Vorbis header */
371 oggpacket.bytes = *(p_extra++) << 8;
372 oggpacket.bytes |= (*(p_extra++) & 0xFF);
373 oggpacket.packet = p_extra;
374 p_extra += oggpacket.bytes;
375 i_extra -= (oggpacket.bytes + 2);
378 msg_Err( p_dec, "header data corrupted");
382 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
384 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
388 /* Setup the format */
389 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
390 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
391 p_dec->fmt_out.audio.i_physical_channels =
392 p_dec->fmt_out.audio.i_original_channels =
393 pi_channels_maps[p_sys->vi.channels];
394 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
396 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
397 aout_DateSet( &p_sys->end_date, 0 );
399 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
400 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
402 /* The next packet in order is the comments header */
404 oggpacket.bytes = *(p_extra++) << 8;
405 oggpacket.bytes |= (*(p_extra++) & 0xFF);
406 oggpacket.packet = p_extra;
407 p_extra += oggpacket.bytes;
408 i_extra -= (oggpacket.bytes + 2);
411 msg_Err( p_dec, "header data corrupted");
415 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
417 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
420 ParseVorbisComments( p_dec );
422 /* The next packet in order is the codebooks header
423 * We need to watch out that this packet is not missing as a
424 * missing or corrupted header is fatal. */
425 oggpacket.bytes = *(p_extra++) << 8;
426 oggpacket.bytes |= (*(p_extra++) & 0xFF);
427 oggpacket.packet = p_extra;
428 i_extra -= (oggpacket.bytes + 2);
431 msg_Err( p_dec, "header data corrupted");
435 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
437 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
441 if( !p_sys->b_packetizer )
443 /* Initialize the Vorbis packet->PCM decoder */
444 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
445 vorbis_block_init( &p_sys->vd, &p_sys->vb );
449 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
450 p_dec->fmt_out.p_extra =
451 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
452 memcpy( p_dec->fmt_out.p_extra,
453 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
456 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
457 p_dec->fmt_out.audio.i_physical_channels, VLC_TRUE);
462 /*****************************************************************************
463 * ProcessPacket: processes a Vorbis packet.
464 *****************************************************************************/
465 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
468 decoder_sys_t *p_sys = p_dec->p_sys;
469 block_t *p_block = *pp_block;
471 /* Date management */
472 if( p_block && p_block->i_pts > 0 &&
473 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
475 aout_DateSet( &p_sys->end_date, p_block->i_pts );
478 if( !aout_DateGet( &p_sys->end_date ) )
480 /* We've just started the stream, wait for the first PTS. */
481 if( p_block ) block_Release( p_block );
485 *pp_block = NULL; /* To avoid being fed the same packet again */
487 if( p_sys->b_packetizer )
489 return SendPacket( p_dec, p_oggpacket, p_block );
493 aout_buffer_t *p_aout_buffer;
495 if( p_sys->i_headers >= 3 )
496 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
498 p_aout_buffer = NULL;
500 if( p_block ) block_Release( p_block );
501 return p_aout_buffer;
505 /*****************************************************************************
506 * DecodePacket: decodes a Vorbis packet.
507 *****************************************************************************/
508 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
510 decoder_sys_t *p_sys = p_dec->p_sys;
513 #ifdef MODULE_NAME_IS_tremor
519 if( p_oggpacket->bytes &&
520 #ifdef MODULE_NAME_IS_tremor
521 vorbis_synthesis( &p_sys->vb, p_oggpacket, 1 ) == 0 )
523 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
525 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
527 /* **pp_pcm is a multichannel float vector. In stereo, for
528 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
529 * the size of each channel. Convert the float values
530 * (-1.<=range<=1.) to whatever PCM format and write it out */
532 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
535 aout_buffer_t *p_aout_buffer;
538 p_dec->pf_aout_buffer_new( p_dec, i_samples );
540 if( p_aout_buffer == NULL ) return NULL;
542 /* Interleave the samples */
543 #ifdef MODULE_NAME_IS_tremor
544 Interleave( (int32_t *)p_aout_buffer->p_buffer,
545 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
547 Interleave( (float *)p_aout_buffer->p_buffer,
548 (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
551 /* Tell libvorbis how many samples we actually consumed */
552 vorbis_synthesis_read( &p_sys->vd, i_samples );
554 /* Date management */
555 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
556 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
558 return p_aout_buffer;
566 /*****************************************************************************
567 * SendPacket: send an ogg dated packet to the stream output.
568 *****************************************************************************/
569 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
572 decoder_sys_t *p_sys = p_dec->p_sys;
573 int i_block_size, i_samples;
575 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
576 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
577 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
578 p_sys->i_last_block_size = i_block_size;
580 /* Date management */
581 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
583 if( p_sys->i_headers >= 3 )
584 p_block->i_length = aout_DateIncrement( &p_sys->end_date, i_samples ) -
587 p_block->i_length = 0;
592 /*****************************************************************************
593 * ParseVorbisComments: FIXME should be done in demuxer
594 *****************************************************************************/
595 static void ParseVorbisComments( decoder_t *p_dec )
597 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
598 char *psz_name, *psz_value, *psz_comment;
601 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
603 while( i < p_dec->p_sys->vc.comments )
605 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
608 msg_Warn( p_dec, "out of memory" );
611 psz_name = psz_comment;
612 psz_value = strchr( psz_comment, '=' );
617 input_Control( p_input, INPUT_ADD_INFO, _("Vorbis comment"),
618 psz_name, psz_value );
619 /* HACK, we should use meta */
620 if( strstr( psz_name, "artist" ) )
622 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
623 _("Artist"), psz_value );
625 else if( strstr( psz_name, "title" ) )
627 p_input->input.p_item->psz_name = strdup( psz_value );
631 var_SetInteger( p_input, "item-change", p_input->input.p_item->i_id );
637 /*****************************************************************************
638 * Interleave: helper function to interleave channels
639 *****************************************************************************/
640 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, vlc_bool_t b_decode)
642 const uint32_t *pi_channels_in;
647 pi_channels_in = pi_6channels_in;
650 pi_channels_in = pi_4channels_in;
653 pi_channels_in = pi_3channels_in;
658 for( i = 0; i< i_channels; ++i )
660 pi_chan_table[i] = i;
667 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
668 i_channel_mask & AOUT_CHAN_PHYSMASK,
672 aout_CheckChannelReorder( pi_channels_out, pi_channels_in,
673 i_channel_mask & AOUT_CHAN_PHYSMASK,
678 /*****************************************************************************
679 * Interleave: helper function to interleave channels
680 *****************************************************************************/
681 #ifdef MODULE_NAME_IS_tremor
682 static void Interleave( int32_t *p_out, const int32_t **pp_in,
683 int i_nb_channels, int i_samples, int *pi_chan_table)
687 for ( j = 0; j < i_samples; j++ )
688 for ( i = 0; i < i_nb_channels; i++ )
689 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
692 static void Interleave( float *p_out, const float **pp_in,
693 int i_nb_channels, int i_samples, int *pi_chan_table )
697 for ( j = 0; j < i_samples; j++ )
698 for ( i = 0; i < i_nb_channels; i++ )
699 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
703 /*****************************************************************************
704 * CloseDecoder: vorbis decoder destruction
705 *****************************************************************************/
706 static void CloseDecoder( vlc_object_t *p_this )
708 decoder_t *p_dec = (decoder_t *)p_this;
709 decoder_sys_t *p_sys = p_dec->p_sys;
711 if( !p_sys->b_packetizer && p_sys->i_headers >= 3 )
713 vorbis_block_clear( &p_sys->vb );
714 vorbis_dsp_clear( &p_sys->vd );
717 vorbis_comment_clear( &p_sys->vc );
718 vorbis_info_clear( &p_sys->vi ); /* must be called last */
723 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
725 /*****************************************************************************
726 * encoder_sys_t : vorbis encoder descriptor
727 *****************************************************************************/
733 vorbis_info vi; /* struct that stores all the static vorbis bitstream
735 vorbis_comment vc; /* struct that stores all the bitstream user
737 vorbis_dsp_state vd; /* central working state for the packet->PCM
739 vorbis_block vb; /* local working space for packet->PCM decode */
741 int i_last_block_size;
751 ** Channel reordering
753 int pi_chan_table[AOUT_CHAN_MAX];
757 /*****************************************************************************
758 * OpenEncoder: probe the encoder and return score
759 *****************************************************************************/
760 static int OpenEncoder( vlc_object_t *p_this )
762 encoder_t *p_enc = (encoder_t *)p_this;
763 encoder_sys_t *p_sys;
764 int i_quality, i_min_bitrate, i_max_bitrate, i;
765 ogg_packet header[3];
769 if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') &&
775 /* Allocate the memory needed to store the decoder's structure */
776 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
778 msg_Err( p_enc, "out of memory" );
781 p_enc->p_sys = p_sys;
783 p_enc->pf_encode_audio = Encode;
784 p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
785 p_enc->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
787 sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
789 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
790 i_quality = val.i_int;
791 if( i_quality > 10 ) i_quality = 10;
792 if( i_quality < 0 ) i_quality = 0;
793 var_Get( p_enc, ENC_CFG_PREFIX "cbr", &val );
794 if( val.b_bool ) i_quality = 0;
795 var_Get( p_enc, ENC_CFG_PREFIX "max-bitrate", &val );
796 i_max_bitrate = val.i_int;
797 var_Get( p_enc, ENC_CFG_PREFIX "min-bitrate", &val );
798 i_min_bitrate = val.i_int;
800 /* Initialize vorbis encoder */
801 vorbis_info_init( &p_sys->vi );
806 if( vorbis_encode_setup_vbr( &p_sys->vi,
807 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
810 vorbis_info_clear( &p_sys->vi );
811 free( p_enc->p_sys );
812 msg_Err( p_enc, "VBR mode initialisation failed" );
816 /* Do we have optional hard quality restrictions? */
817 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
819 struct ovectl_ratemanage_arg ai;
820 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
822 ai.bitrate_hard_min = i_min_bitrate;
823 ai.bitrate_hard_max = i_max_bitrate;
824 ai.management_active = 1;
826 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
831 /* Turn off management entirely */
832 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
837 if( vorbis_encode_setup_managed( &p_sys->vi,
838 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
839 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
840 p_enc->fmt_out.i_bitrate,
841 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
843 vorbis_info_clear( &p_sys->vi );
844 msg_Err( p_enc, "CBR mode initialisation failed" );
845 free( p_enc->p_sys );
850 vorbis_encode_setup_init( &p_sys->vi );
853 vorbis_comment_init( &p_sys->vc);
854 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
856 /* Set up the analysis state and auxiliary encoding storage */
857 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
858 vorbis_block_init( &p_sys->vd, &p_sys->vb );
860 /* Create and store headers */
861 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
862 &header[0], &header[1], &header[2]);
863 p_enc->fmt_out.i_extra = 3 * 2 + header[0].bytes +
864 header[1].bytes + header[2].bytes;
865 p_extra = p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
866 for( i = 0; i < 3; i++ )
868 *(p_extra++) = header[i].bytes >> 8;
869 *(p_extra++) = header[i].bytes & 0xFF;
870 memcpy( p_extra, header[i].packet, header[i].bytes );
871 p_extra += header[i].bytes;
874 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
875 p_sys->i_last_block_size = 0;
876 p_sys->i_samples_delay = 0;
879 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
880 p_enc->fmt_in.audio.i_physical_channels, VLC_TRUE);
885 /****************************************************************************
886 * Encode: the whole thing
887 ****************************************************************************
888 * This function spits out ogg packets.
889 ****************************************************************************/
890 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
892 encoder_sys_t *p_sys = p_enc->p_sys;
893 ogg_packet oggpacket;
894 block_t *p_block, *p_chain = NULL;
899 p_sys->i_pts = p_aout_buf->start_date -
900 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
901 (mtime_t)p_enc->fmt_in.audio.i_rate;
903 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
905 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
907 /* convert samples to float and uninterleave */
908 for( i = 0; i < p_sys->i_channels; i++ )
910 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
912 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
913 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
917 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
919 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
923 vorbis_analysis( &p_sys->vb, NULL );
924 vorbis_bitrate_addblock( &p_sys->vb );
926 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
929 p_block = block_New( p_enc, oggpacket.bytes );
930 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
932 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
934 if( i_block_size < 0 ) i_block_size = 0;
935 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
936 p_sys->i_last_block_size = i_block_size;
938 p_block->i_length = (mtime_t)1000000 *
939 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
941 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
943 p_sys->i_samples_delay -= i_samples;
946 p_sys->i_pts += p_block->i_length;
947 block_ChainAppend( &p_chain, p_block );
954 /*****************************************************************************
955 * CloseEncoder: vorbis encoder destruction
956 *****************************************************************************/
957 static void CloseEncoder( vlc_object_t *p_this )
959 encoder_t *p_enc = (encoder_t *)p_this;
960 encoder_sys_t *p_sys = p_enc->p_sys;
962 vorbis_block_clear( &p_sys->vb );
963 vorbis_dsp_clear( &p_sys->vd );
964 vorbis_comment_clear( &p_sys->vc );
965 vorbis_info_clear( &p_sys->vi ); /* must be called last */
970 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */