1 /*****************************************************************************
2 * vorbis.c: vorbis decoder/encoder/packetizer module making use of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 2001-2003 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 "Enforce 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 "Maximum bitrate in kbps. This is useful for streaming applications." )
162 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
163 #define ENC_MINBR_LONGTEXT N_( \
164 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
165 #define ENC_CBR_TEXT N_("CBR encoding")
166 #define ENC_CBR_LONGTEXT N_( \
167 "Force a constant bitrate encoding (CBR)." )
170 set_shortname( "Vorbis" );
171 set_description( _("Vorbis audio decoder") );
172 #ifdef MODULE_NAME_IS_tremor
173 set_capability( "decoder", 90 );
175 set_capability( "decoder", 100 );
177 set_category( CAT_INPUT );
178 set_subcategory( SUBCAT_INPUT_ACODEC );
179 set_callbacks( OpenDecoder, CloseDecoder );
182 set_description( _("Vorbis audio packetizer") );
183 set_capability( "packetizer", 100 );
184 set_callbacks( OpenPacketizer, CloseDecoder );
186 #ifndef MODULE_NAME_IS_tremor
187 # define ENC_CFG_PREFIX "sout-vorbis-"
189 set_description( _("Vorbis audio encoder") );
190 set_capability( "encoder", 100 );
191 #if defined(HAVE_VORBIS_VORBISENC_H)
192 set_callbacks( OpenEncoder, CloseEncoder );
195 add_integer( ENC_CFG_PREFIX "quality", 0, NULL, ENC_QUALITY_TEXT,
196 ENC_QUALITY_LONGTEXT, VLC_FALSE );
197 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
198 ENC_MAXBR_LONGTEXT, VLC_FALSE );
199 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
200 ENC_MINBR_LONGTEXT, VLC_FALSE );
201 add_bool( ENC_CFG_PREFIX "cbr", 0, NULL, ENC_CBR_TEXT,
202 ENC_CBR_LONGTEXT, VLC_FALSE );
207 #ifndef MODULE_NAME_IS_tremor
208 static const char *ppsz_enc_options[] = {
209 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
213 /*****************************************************************************
214 * OpenDecoder: probe the decoder and return score
215 *****************************************************************************/
216 static int OpenDecoder( vlc_object_t *p_this )
218 decoder_t *p_dec = (decoder_t*)p_this;
219 decoder_sys_t *p_sys;
221 if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
226 /* Allocate the memory needed to store the decoder's structure */
227 if( ( p_dec->p_sys = p_sys =
228 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
230 msg_Err( p_dec, "out of memory" );
235 aout_DateSet( &p_sys->end_date, 0 );
236 p_sys->i_last_block_size = 0;
237 p_sys->b_packetizer = VLC_FALSE;
238 p_sys->i_headers = 0;
240 /* Take care of vorbis init */
241 vorbis_info_init( &p_sys->vi );
242 vorbis_comment_init( &p_sys->vc );
244 /* Set output properties */
245 p_dec->fmt_out.i_cat = AUDIO_ES;
246 #ifdef MODULE_NAME_IS_tremor
247 p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
249 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
253 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
255 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
261 static int OpenPacketizer( vlc_object_t *p_this )
263 decoder_t *p_dec = (decoder_t*)p_this;
265 int i_ret = OpenDecoder( p_this );
267 if( i_ret == VLC_SUCCESS )
269 p_dec->p_sys->b_packetizer = VLC_TRUE;
270 p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
276 /****************************************************************************
277 * DecodeBlock: the whole thing
278 ****************************************************************************
279 * This function must be fed with ogg packets.
280 ****************************************************************************/
281 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
283 decoder_sys_t *p_sys = p_dec->p_sys;
284 ogg_packet oggpacket;
286 if( !pp_block ) return NULL;
290 /* Block to Ogg packet */
291 oggpacket.packet = (*pp_block)->p_buffer;
292 oggpacket.bytes = (*pp_block)->i_buffer;
296 if( p_sys->b_packetizer ) return NULL;
298 /* Block to Ogg packet */
299 oggpacket.packet = NULL;
303 oggpacket.granulepos = -1;
306 oggpacket.packetno = 0;
308 /* Check for headers */
309 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
311 /* Headers already available as extra data */
312 msg_Dbg( p_dec, "headers already available as extra data" );
313 p_sys->i_headers = 3;
315 else if( oggpacket.bytes && p_sys->i_headers < 3 )
317 /* Backup headers as extra data */
320 p_dec->fmt_in.p_extra =
321 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
322 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 < 0 ||
392 p_dec->fmt_out.audio.i_channels > 6 )
394 msg_Err( p_dec, "invalid number of channels (not between 1 and 6): %i",
395 p_dec->fmt_out.audio.i_channels );
399 p_dec->fmt_out.audio.i_physical_channels =
400 p_dec->fmt_out.audio.i_original_channels =
401 pi_channels_maps[p_sys->vi.channels];
402 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
404 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
405 aout_DateSet( &p_sys->end_date, 0 );
407 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
408 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
410 /* The next packet in order is the comments header */
412 oggpacket.bytes = *(p_extra++) << 8;
413 oggpacket.bytes |= (*(p_extra++) & 0xFF);
414 oggpacket.packet = p_extra;
415 p_extra += oggpacket.bytes;
416 i_extra -= (oggpacket.bytes + 2);
419 msg_Err( p_dec, "header data corrupted");
423 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
425 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
428 ParseVorbisComments( p_dec );
430 /* The next packet in order is the codebooks header
431 * We need to watch out that this packet is not missing as a
432 * missing or corrupted header is fatal. */
433 oggpacket.bytes = *(p_extra++) << 8;
434 oggpacket.bytes |= (*(p_extra++) & 0xFF);
435 oggpacket.packet = p_extra;
436 i_extra -= (oggpacket.bytes + 2);
439 msg_Err( p_dec, "header data corrupted");
443 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
445 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
449 if( !p_sys->b_packetizer )
451 /* Initialize the Vorbis packet->PCM decoder */
452 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
453 vorbis_block_init( &p_sys->vd, &p_sys->vb );
457 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
458 p_dec->fmt_out.p_extra =
459 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
460 memcpy( p_dec->fmt_out.p_extra,
461 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
464 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
465 p_dec->fmt_out.audio.i_physical_channels, VLC_TRUE);
470 /*****************************************************************************
471 * ProcessPacket: processes a Vorbis packet.
472 *****************************************************************************/
473 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
476 decoder_sys_t *p_sys = p_dec->p_sys;
477 block_t *p_block = *pp_block;
479 /* Date management */
480 if( p_block && p_block->i_pts > 0 &&
481 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
483 aout_DateSet( &p_sys->end_date, p_block->i_pts );
486 if( !aout_DateGet( &p_sys->end_date ) )
488 /* We've just started the stream, wait for the first PTS. */
489 if( p_block ) block_Release( p_block );
493 *pp_block = NULL; /* To avoid being fed the same packet again */
495 if( p_sys->b_packetizer )
497 return SendPacket( p_dec, p_oggpacket, p_block );
501 aout_buffer_t *p_aout_buffer;
503 if( p_sys->i_headers >= 3 )
504 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
506 p_aout_buffer = NULL;
508 if( p_block ) block_Release( p_block );
509 return p_aout_buffer;
513 /*****************************************************************************
514 * DecodePacket: decodes a Vorbis packet.
515 *****************************************************************************/
516 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
518 decoder_sys_t *p_sys = p_dec->p_sys;
521 #ifdef MODULE_NAME_IS_tremor
527 if( p_oggpacket->bytes &&
528 #ifdef MODULE_NAME_IS_tremor
529 vorbis_synthesis( &p_sys->vb, p_oggpacket, 1 ) == 0 )
531 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
533 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
535 /* **pp_pcm is a multichannel float vector. In stereo, for
536 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
537 * the size of each channel. Convert the float values
538 * (-1.<=range<=1.) to whatever PCM format and write it out */
540 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
543 aout_buffer_t *p_aout_buffer;
546 p_dec->pf_aout_buffer_new( p_dec, i_samples );
548 if( p_aout_buffer == NULL ) return NULL;
550 /* Interleave the samples */
551 #ifdef MODULE_NAME_IS_tremor
552 Interleave( (int32_t *)p_aout_buffer->p_buffer,
553 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
555 Interleave( (float *)p_aout_buffer->p_buffer,
556 (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
559 /* Tell libvorbis how many samples we actually consumed */
560 vorbis_synthesis_read( &p_sys->vd, i_samples );
562 /* Date management */
563 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
564 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
566 return p_aout_buffer;
574 /*****************************************************************************
575 * SendPacket: send an ogg dated packet to the stream output.
576 *****************************************************************************/
577 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
580 decoder_sys_t *p_sys = p_dec->p_sys;
581 int i_block_size, i_samples;
583 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
584 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
585 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
586 p_sys->i_last_block_size = i_block_size;
588 /* Date management */
589 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
591 if( p_sys->i_headers >= 3 )
592 p_block->i_length = aout_DateIncrement( &p_sys->end_date, i_samples ) -
595 p_block->i_length = 0;
600 /*****************************************************************************
601 * ParseVorbisComments: FIXME should be done in demuxer
602 *****************************************************************************/
603 static void ParseVorbisComments( decoder_t *p_dec )
605 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
606 char *psz_name, *psz_value, *psz_comment;
609 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
611 while( i < p_dec->p_sys->vc.comments )
613 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
616 msg_Warn( p_dec, "out of memory" );
619 psz_name = psz_comment;
620 psz_value = strchr( psz_comment, '=' );
625 input_Control( p_input, INPUT_ADD_INFO, _("Vorbis comment"),
626 psz_name, psz_value );
627 if( !strcasecmp( psz_name, "artist" ) )
629 vlc_meta_SetArtist( p_input->input.p_item->p_meta,
631 input_ItemAddInfo( p_input->input.p_item,
632 _(VLC_META_INFO_CAT), _(VLC_META_ARTIST),
635 else if( !strcasecmp( psz_name, "title" ) )
637 vlc_meta_SetTitle( p_input->input.p_item->p_meta,
639 p_input->input.p_item->psz_name = strdup( psz_value );
641 else if( !strcasecmp( psz_name, "album" ) )
643 vlc_meta_SetAlbum( p_input->input.p_item->p_meta,
646 else if( !strcasecmp( psz_name, "musicbrainz_trackid" ) )
648 vlc_meta_SetTrackID( p_input->input.p_item->p_meta,
651 else if( !strcasecmp( psz_name, "musicbrainz_artistid" ) )
653 vlc_meta_SetArtistID( p_input->input.p_item->p_meta,
656 else if( !strcasecmp( psz_name, "musicbrainz_albumid" ) )
658 vlc_meta_SetAlbumID( p_input->input.p_item->p_meta,
663 var_SetInteger( p_input, "item-change", p_input->input.p_item->i_id );
669 /*****************************************************************************
670 * Interleave: helper function to interleave channels
671 *****************************************************************************/
672 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, vlc_bool_t b_decode)
674 const uint32_t *pi_channels_in;
679 pi_channels_in = pi_6channels_in;
682 pi_channels_in = pi_4channels_in;
685 pi_channels_in = pi_3channels_in;
690 for( i = 0; i< i_channels; ++i )
692 pi_chan_table[i] = i;
699 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
700 i_channel_mask & AOUT_CHAN_PHYSMASK,
704 aout_CheckChannelReorder( pi_channels_out, pi_channels_in,
705 i_channel_mask & AOUT_CHAN_PHYSMASK,
710 /*****************************************************************************
711 * Interleave: helper function to interleave channels
712 *****************************************************************************/
713 #ifdef MODULE_NAME_IS_tremor
714 static void Interleave( int32_t *p_out, const int32_t **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] * (FIXED32_ONE >> 24);
724 static void Interleave( float *p_out, const float **pp_in,
725 int i_nb_channels, int i_samples, int *pi_chan_table )
729 for ( j = 0; j < i_samples; j++ )
730 for ( i = 0; i < i_nb_channels; i++ )
731 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
735 /*****************************************************************************
736 * CloseDecoder: vorbis decoder destruction
737 *****************************************************************************/
738 static void CloseDecoder( vlc_object_t *p_this )
740 decoder_t *p_dec = (decoder_t *)p_this;
741 decoder_sys_t *p_sys = p_dec->p_sys;
743 if( !p_sys->b_packetizer && p_sys->i_headers > 3 )
745 vorbis_block_clear( &p_sys->vb );
746 vorbis_dsp_clear( &p_sys->vd );
749 vorbis_comment_clear( &p_sys->vc );
750 vorbis_info_clear( &p_sys->vi ); /* must be called last */
755 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
757 /*****************************************************************************
758 * encoder_sys_t : vorbis encoder descriptor
759 *****************************************************************************/
765 vorbis_info vi; /* struct that stores all the static vorbis bitstream
767 vorbis_comment vc; /* struct that stores all the bitstream user
769 vorbis_dsp_state vd; /* central working state for the packet->PCM
771 vorbis_block vb; /* local working space for packet->PCM decode */
773 int i_last_block_size;
783 ** Channel reordering
785 int pi_chan_table[AOUT_CHAN_MAX];
789 /*****************************************************************************
790 * OpenEncoder: probe the encoder and return score
791 *****************************************************************************/
792 static int OpenEncoder( vlc_object_t *p_this )
794 encoder_t *p_enc = (encoder_t *)p_this;
795 encoder_sys_t *p_sys;
796 int i_quality, i_min_bitrate, i_max_bitrate, i;
797 ogg_packet header[3];
801 if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') &&
807 /* Allocate the memory needed to store the decoder's structure */
808 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
810 msg_Err( p_enc, "out of memory" );
813 p_enc->p_sys = p_sys;
815 p_enc->pf_encode_audio = Encode;
816 p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
817 p_enc->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
819 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
821 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
822 i_quality = val.i_int;
823 if( i_quality > 10 ) i_quality = 10;
824 if( i_quality < 0 ) i_quality = 0;
825 var_Get( p_enc, ENC_CFG_PREFIX "cbr", &val );
826 if( val.b_bool ) i_quality = 0;
827 var_Get( p_enc, ENC_CFG_PREFIX "max-bitrate", &val );
828 i_max_bitrate = val.i_int;
829 var_Get( p_enc, ENC_CFG_PREFIX "min-bitrate", &val );
830 i_min_bitrate = val.i_int;
832 /* Initialize vorbis encoder */
833 vorbis_info_init( &p_sys->vi );
838 if( vorbis_encode_setup_vbr( &p_sys->vi,
839 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
842 vorbis_info_clear( &p_sys->vi );
843 free( p_enc->p_sys );
844 msg_Err( p_enc, "VBR mode initialisation failed" );
848 /* Do we have optional hard quality restrictions? */
849 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
851 struct ovectl_ratemanage_arg ai;
852 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
854 ai.bitrate_hard_min = i_min_bitrate;
855 ai.bitrate_hard_max = i_max_bitrate;
856 ai.management_active = 1;
858 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
863 /* Turn off management entirely */
864 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
869 if( vorbis_encode_setup_managed( &p_sys->vi,
870 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
871 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
872 p_enc->fmt_out.i_bitrate,
873 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
875 vorbis_info_clear( &p_sys->vi );
876 msg_Err( p_enc, "CBR mode initialisation failed" );
877 free( p_enc->p_sys );
882 vorbis_encode_setup_init( &p_sys->vi );
885 vorbis_comment_init( &p_sys->vc);
886 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
888 /* Set up the analysis state and auxiliary encoding storage */
889 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
890 vorbis_block_init( &p_sys->vd, &p_sys->vb );
892 /* Create and store headers */
893 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
894 &header[0], &header[1], &header[2]);
895 p_enc->fmt_out.i_extra = 3 * 2 + header[0].bytes +
896 header[1].bytes + header[2].bytes;
897 p_extra = p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
898 for( i = 0; i < 3; i++ )
900 *(p_extra++) = header[i].bytes >> 8;
901 *(p_extra++) = header[i].bytes & 0xFF;
902 memcpy( p_extra, header[i].packet, header[i].bytes );
903 p_extra += header[i].bytes;
906 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
907 p_sys->i_last_block_size = 0;
908 p_sys->i_samples_delay = 0;
911 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
912 p_enc->fmt_in.audio.i_physical_channels, VLC_TRUE);
917 /****************************************************************************
918 * Encode: the whole thing
919 ****************************************************************************
920 * This function spits out ogg packets.
921 ****************************************************************************/
922 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
924 encoder_sys_t *p_sys = p_enc->p_sys;
925 ogg_packet oggpacket;
926 block_t *p_block, *p_chain = NULL;
931 p_sys->i_pts = p_aout_buf->start_date -
932 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
933 (mtime_t)p_enc->fmt_in.audio.i_rate;
935 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
937 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
939 /* convert samples to float and uninterleave */
940 for( i = 0; i < p_sys->i_channels; i++ )
942 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
944 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
945 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
949 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
951 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
955 vorbis_analysis( &p_sys->vb, NULL );
956 vorbis_bitrate_addblock( &p_sys->vb );
958 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
961 p_block = block_New( p_enc, oggpacket.bytes );
962 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
964 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
966 if( i_block_size < 0 ) i_block_size = 0;
967 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
968 p_sys->i_last_block_size = i_block_size;
970 p_block->i_length = (mtime_t)1000000 *
971 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
973 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
975 p_sys->i_samples_delay -= i_samples;
978 p_sys->i_pts += p_block->i_length;
979 block_ChainAppend( &p_chain, p_block );
986 /*****************************************************************************
987 * CloseEncoder: vorbis encoder destruction
988 *****************************************************************************/
989 static void CloseEncoder( vlc_object_t *p_this )
991 encoder_t *p_enc = (encoder_t *)p_this;
992 encoder_sys_t *p_sys = p_enc->p_sys;
994 vorbis_block_clear( &p_sys->vb );
995 vorbis_dsp_clear( &p_sys->vd );
996 vorbis_comment_clear( &p_sys->vc );
997 vorbis_info_clear( &p_sys->vi ); /* must be called last */
1002 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */