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>
39 #include <vlc_playlist.h>
44 #ifdef MODULE_NAME_IS_tremor
45 #include <tremor/ivorbiscodec.h>
48 #include <vorbis/codec.h>
51 #ifdef HAVE_VORBIS_VORBISENC_H
52 # include <vorbis/vorbisenc.h>
53 # ifndef OV_ECTL_RATEMANAGE_AVG
54 # define OV_ECTL_RATEMANAGE_AVG 0x0
60 /*****************************************************************************
61 * decoder_sys_t : vorbis decoder descriptor
62 *****************************************************************************/
76 vorbis_info vi; /* struct that stores all the static vorbis bitstream
78 vorbis_comment vc; /* struct that stores all the bitstream user
80 vorbis_dsp_state vd; /* central working state for the packet->PCM
82 vorbis_block vb; /* local working space for packet->PCM decode */
87 audio_date_t end_date;
88 int i_last_block_size;
93 int pi_chan_table[AOUT_CHAN_MAX];
96 static const int pi_channels_maps[9] =
100 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
101 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
102 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
103 | AOUT_CHAN_REARRIGHT,
104 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
105 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
106 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
107 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
108 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
109 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
110 | AOUT_CHAN_MIDDLERIGHT,
111 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
112 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
117 ** channel order as defined in http://www.ogghelp.com/ogg/glossary.cfm#Audio_Channels
120 /* recommended vorbis channel order for 6 channels */
121 static const uint32_t pi_6channels_in[] =
122 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
123 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
125 /* recommended vorbis channel order for 4 channels */
126 static const uint32_t pi_4channels_in[] =
127 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
129 /* recommended vorbis channel order for 3 channels */
130 static const uint32_t pi_3channels_in[] =
131 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
133 /****************************************************************************
135 ****************************************************************************/
136 static int OpenDecoder ( vlc_object_t * );
137 static int OpenPacketizer( vlc_object_t * );
138 static void CloseDecoder ( vlc_object_t * );
139 static void *DecodeBlock ( decoder_t *, block_t ** );
141 static int ProcessHeaders( decoder_t * );
142 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
144 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
145 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
147 static void ParseVorbisComments( decoder_t * );
149 static void ConfigureChannelOrder(int *, int, uint32_t, bool );
151 #ifdef MODULE_NAME_IS_tremor
152 static void Interleave ( int32_t *, const int32_t **, int, int, int * );
154 static void Interleave ( float *, const float **, int, int, int * );
157 #ifndef MODULE_NAME_IS_tremor
158 static int OpenEncoder ( vlc_object_t * );
159 static void CloseEncoder ( vlc_object_t * );
160 static block_t *Encode ( encoder_t *, aout_buffer_t * );
163 /*****************************************************************************
165 *****************************************************************************/
166 #define ENC_QUALITY_TEXT N_("Encoding quality")
167 #define ENC_QUALITY_LONGTEXT N_( \
168 "Enforce a quality between 1 (low) and 10 (high), instead " \
169 "of specifying a particular bitrate. This will produce a VBR stream." )
170 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
171 #define ENC_MAXBR_LONGTEXT N_( \
172 "Maximum bitrate in kbps. This is useful for streaming applications." )
173 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
174 #define ENC_MINBR_LONGTEXT N_( \
175 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
176 #define ENC_CBR_TEXT N_("CBR encoding")
177 #define ENC_CBR_LONGTEXT N_( \
178 "Force a constant bitrate encoding (CBR)." )
181 set_shortname( "Vorbis" );
182 set_description( N_("Vorbis audio decoder") );
183 #ifdef MODULE_NAME_IS_tremor
184 set_capability( "decoder", 90 );
186 set_capability( "decoder", 100 );
188 set_category( CAT_INPUT );
189 set_subcategory( SUBCAT_INPUT_ACODEC );
190 set_callbacks( OpenDecoder, CloseDecoder );
193 set_description( N_("Vorbis audio packetizer") );
194 set_capability( "packetizer", 100 );
195 set_callbacks( OpenPacketizer, CloseDecoder );
197 #ifndef MODULE_NAME_IS_tremor
198 # define ENC_CFG_PREFIX "sout-vorbis-"
200 set_description( N_("Vorbis audio encoder") );
201 set_capability( "encoder", 100 );
202 #if defined(HAVE_VORBIS_VORBISENC_H)
203 set_callbacks( OpenEncoder, CloseEncoder );
206 add_integer( ENC_CFG_PREFIX "quality", 0, NULL, ENC_QUALITY_TEXT,
207 ENC_QUALITY_LONGTEXT, false );
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_FOURCC('v','o','r','b') )
237 /* Allocate the memory needed to store the decoder's structure */
238 if( ( p_dec->p_sys = p_sys =
239 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
243 aout_DateSet( &p_sys->end_date, 0 );
244 p_sys->i_last_block_size = 0;
245 p_sys->b_packetizer = false;
246 p_sys->i_headers = 0;
248 /* Take care of vorbis init */
249 vorbis_info_init( &p_sys->vi );
250 vorbis_comment_init( &p_sys->vc );
252 /* Set output properties */
253 p_dec->fmt_out.i_cat = AUDIO_ES;
254 #ifdef MODULE_NAME_IS_tremor
255 p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
257 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
261 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
263 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
269 static int OpenPacketizer( vlc_object_t *p_this )
271 decoder_t *p_dec = (decoder_t*)p_this;
273 int i_ret = OpenDecoder( p_this );
275 if( i_ret == VLC_SUCCESS )
277 p_dec->p_sys->b_packetizer = true;
278 p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
284 /****************************************************************************
285 * DecodeBlock: the whole thing
286 ****************************************************************************
287 * This function must be fed with ogg packets.
288 ****************************************************************************/
289 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
291 decoder_sys_t *p_sys = p_dec->p_sys;
292 ogg_packet oggpacket;
294 if( !pp_block ) return NULL;
298 /* Block to Ogg packet */
299 oggpacket.packet = (*pp_block)->p_buffer;
300 oggpacket.bytes = (*pp_block)->i_buffer;
304 if( p_sys->b_packetizer ) return NULL;
306 /* Block to Ogg packet */
307 oggpacket.packet = NULL;
311 oggpacket.granulepos = -1;
314 oggpacket.packetno = 0;
316 /* Check for headers */
317 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
319 /* Headers already available as extra data */
320 msg_Dbg( p_dec, "headers already available as extra data" );
321 p_sys->i_headers = 3;
323 else if( oggpacket.bytes && p_sys->i_headers < 3 )
325 /* Backup headers as extra data */
328 p_dec->fmt_in.p_extra =
329 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
330 oggpacket.bytes + 2 );
331 p_extra = (uint8_t *)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
332 *(p_extra++) = oggpacket.bytes >> 8;
333 *(p_extra++) = oggpacket.bytes & 0xFF;
335 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
336 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
338 block_Release( *pp_block );
343 if( p_sys->i_headers == 3 )
345 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
347 p_sys->i_headers = 0;
348 p_dec->fmt_in.i_extra = 0;
349 block_Release( *pp_block );
352 else p_sys->i_headers++;
355 return ProcessPacket( p_dec, &oggpacket, pp_block );
358 /*****************************************************************************
359 * ProcessHeaders: process Vorbis headers.
360 *****************************************************************************/
361 static int ProcessHeaders( decoder_t *p_dec )
363 decoder_sys_t *p_sys = p_dec->p_sys;
364 ogg_packet oggpacket;
368 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
370 oggpacket.granulepos = -1;
371 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
373 oggpacket.packetno = 0;
374 p_extra = p_dec->fmt_in.p_extra;
375 i_extra = p_dec->fmt_in.i_extra;
377 /* Take care of the initial Vorbis header */
378 oggpacket.bytes = *(p_extra++) << 8;
379 oggpacket.bytes |= (*(p_extra++) & 0xFF);
380 oggpacket.packet = p_extra;
381 p_extra += oggpacket.bytes;
382 i_extra -= (oggpacket.bytes + 2);
385 msg_Err( p_dec, "header data corrupted");
389 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
391 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
395 /* Setup the format */
396 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
397 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
399 if( p_dec->fmt_out.audio.i_channels > 9 )
401 msg_Err( p_dec, "invalid number of channels (not between 1 and 9): %i",
402 p_dec->fmt_out.audio.i_channels );
406 p_dec->fmt_out.audio.i_physical_channels =
407 p_dec->fmt_out.audio.i_original_channels =
408 pi_channels_maps[p_sys->vi.channels];
409 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
411 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
413 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
414 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
416 /* The next packet in order is the comments header */
418 oggpacket.bytes = *(p_extra++) << 8;
419 oggpacket.bytes |= (*(p_extra++) & 0xFF);
420 oggpacket.packet = p_extra;
421 p_extra += oggpacket.bytes;
422 i_extra -= (oggpacket.bytes + 2);
425 msg_Err( p_dec, "header data corrupted");
429 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
431 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
434 ParseVorbisComments( p_dec );
436 /* The next packet in order is the codebooks header
437 * We need to watch out that this packet is not missing as a
438 * missing or corrupted header is fatal. */
439 oggpacket.bytes = *(p_extra++) << 8;
440 oggpacket.bytes |= (*(p_extra++) & 0xFF);
441 oggpacket.packet = p_extra;
442 i_extra -= (oggpacket.bytes + 2);
445 msg_Err( p_dec, "header data corrupted");
449 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
451 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
455 if( !p_sys->b_packetizer )
457 /* Initialize the Vorbis packet->PCM decoder */
458 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
459 vorbis_block_init( &p_sys->vd, &p_sys->vb );
463 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
464 p_dec->fmt_out.p_extra =
465 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
466 memcpy( p_dec->fmt_out.p_extra,
467 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
470 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
471 p_dec->fmt_out.audio.i_physical_channels, true);
476 /*****************************************************************************
477 * ProcessPacket: processes a Vorbis packet.
478 *****************************************************************************/
479 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
482 decoder_sys_t *p_sys = p_dec->p_sys;
483 block_t *p_block = *pp_block;
485 /* Date management */
486 if( p_block && p_block->i_pts > 0 &&
487 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
489 aout_DateSet( &p_sys->end_date, p_block->i_pts );
492 if( !aout_DateGet( &p_sys->end_date ) )
494 /* We've just started the stream, wait for the first PTS. */
495 if( p_block ) block_Release( p_block );
499 *pp_block = NULL; /* To avoid being fed the same packet again */
501 if( p_sys->b_packetizer )
503 return SendPacket( p_dec, p_oggpacket, p_block );
507 aout_buffer_t *p_aout_buffer;
509 if( p_sys->i_headers >= 3 )
510 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
512 p_aout_buffer = NULL;
514 if( p_block ) block_Release( p_block );
515 return p_aout_buffer;
519 /*****************************************************************************
520 * DecodePacket: decodes a Vorbis packet.
521 *****************************************************************************/
522 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
524 decoder_sys_t *p_sys = p_dec->p_sys;
527 #ifdef MODULE_NAME_IS_tremor
533 if( p_oggpacket->bytes &&
534 #ifdef MODULE_NAME_IS_tremor
535 vorbis_synthesis( &p_sys->vb, p_oggpacket, 1 ) == 0 )
537 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
539 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
541 /* **pp_pcm is a multichannel float vector. In stereo, for
542 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
543 * the size of each channel. Convert the float values
544 * (-1.<=range<=1.) to whatever PCM format and write it out */
546 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
549 aout_buffer_t *p_aout_buffer;
552 decoder_NewAudioBuffer( p_dec, i_samples );
554 if( p_aout_buffer == NULL ) return NULL;
556 /* Interleave the samples */
557 #ifdef MODULE_NAME_IS_tremor
558 Interleave( (int32_t *)p_aout_buffer->p_buffer,
559 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
561 Interleave( (float *)p_aout_buffer->p_buffer,
562 (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
565 /* Tell libvorbis how many samples we actually consumed */
566 vorbis_synthesis_read( &p_sys->vd, i_samples );
568 /* Date management */
569 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
570 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date, i_samples );
571 return p_aout_buffer;
579 /*****************************************************************************
580 * SendPacket: send an ogg dated packet to the stream output.
581 *****************************************************************************/
582 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
585 decoder_sys_t *p_sys = p_dec->p_sys;
586 int i_block_size, i_samples;
588 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
589 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
590 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
591 p_sys->i_last_block_size = i_block_size;
593 /* Date management */
594 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
596 if( p_sys->i_headers >= 3 )
597 p_block->i_length = aout_DateIncrement( &p_sys->end_date, i_samples ) - p_block->i_pts;
599 p_block->i_length = 0;
604 /*****************************************************************************
605 * ParseVorbisComments
606 *****************************************************************************/
607 static void ParseVorbisComments( decoder_t *p_dec )
609 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
610 char *psz_name, *psz_value, *psz_comment;
611 input_item_t *p_item;
614 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
616 p_item = input_GetItem( p_input );
618 while( i < p_dec->p_sys->vc.comments )
620 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
623 psz_name = psz_comment;
624 psz_value = strchr( psz_comment, '=' );
629 input_Control( p_input, INPUT_ADD_INFO, _("Vorbis comment"),
630 psz_name, "%s", psz_value );
631 /*TODO: dot he test at the beginning and save time !! */
633 if( psz_value && ( *psz_value != '\0' ) )
635 if( !strcasecmp( psz_name, "artist" ) )
636 input_item_SetArtist( p_item, psz_value );
637 else if( !strcasecmp( psz_name, "title" ) )
639 input_item_SetTitle( p_item, psz_value );
640 p_item->psz_name = strdup( psz_value );
642 else if( !strcasecmp( psz_name, "album" ) )
644 input_item_SetAlbum( p_item, psz_value );
646 else if( !strcasecmp( psz_name, "musicbrainz_trackid" ) )
647 input_item_SetTrackID( p_item, psz_value );
649 else if( !strcasecmp( psz_name, "musicbrainz_artistid" ) )
650 vlc_meta_SetArtistID( p_item, psz_value );
651 else if( !strcasecmp( psz_name, "musicbrainz_albumid" ) )
652 input_item_SetAlbumID( p_item, psz_value );
656 if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
657 !strcasecmp( psz_name, "RG_RADIO" ) )
659 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
661 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
662 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
664 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
665 !strcasecmp( psz_name, "RG_PEAK" ) )
667 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
669 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
670 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
672 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
673 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
675 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
677 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
678 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
680 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
682 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
684 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
685 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
688 var_SetInteger( pl_Hold( p_input ), "item-change", p_item->i_id );
689 pl_Release( p_input );
695 /*****************************************************************************
696 * Interleave: helper function to interleave channels
697 *****************************************************************************/
698 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, bool b_decode)
700 const uint32_t *pi_channels_in;
705 pi_channels_in = pi_6channels_in;
708 pi_channels_in = pi_4channels_in;
711 pi_channels_in = pi_3channels_in;
716 for( i = 0; i< i_channels; ++i )
718 pi_chan_table[i] = i;
725 aout_CheckChannelReorder( pi_channels_in, NULL,
726 i_channel_mask & AOUT_CHAN_PHYSMASK,
730 aout_CheckChannelReorder( NULL, pi_channels_in,
731 i_channel_mask & AOUT_CHAN_PHYSMASK,
736 /*****************************************************************************
737 * Interleave: helper function to interleave channels
738 *****************************************************************************/
739 #ifdef MODULE_NAME_IS_tremor
740 static void Interleave( int32_t *p_out, const int32_t **pp_in,
741 int i_nb_channels, int i_samples, int *pi_chan_table)
745 for ( j = 0; j < i_samples; j++ )
746 for ( i = 0; i < i_nb_channels; i++ )
747 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
750 static void Interleave( float *p_out, const float **pp_in,
751 int i_nb_channels, int i_samples, int *pi_chan_table )
755 for ( j = 0; j < i_samples; j++ )
756 for ( i = 0; i < i_nb_channels; i++ )
757 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
761 /*****************************************************************************
762 * CloseDecoder: vorbis decoder destruction
763 *****************************************************************************/
764 static void CloseDecoder( vlc_object_t *p_this )
766 decoder_t *p_dec = (decoder_t *)p_this;
767 decoder_sys_t *p_sys = p_dec->p_sys;
769 if( !p_sys->b_packetizer && p_sys->i_headers > 3 )
771 vorbis_block_clear( &p_sys->vb );
772 vorbis_dsp_clear( &p_sys->vd );
775 vorbis_comment_clear( &p_sys->vc );
776 vorbis_info_clear( &p_sys->vi ); /* must be called last */
781 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
783 /*****************************************************************************
784 * encoder_sys_t : vorbis encoder descriptor
785 *****************************************************************************/
791 vorbis_info vi; /* struct that stores all the static vorbis bitstream
793 vorbis_comment vc; /* struct that stores all the bitstream user
795 vorbis_dsp_state vd; /* central working state for the packet->PCM
797 vorbis_block vb; /* local working space for packet->PCM decode */
799 int i_last_block_size;
809 ** Channel reordering
811 int pi_chan_table[AOUT_CHAN_MAX];
815 /*****************************************************************************
816 * OpenEncoder: probe the encoder and return score
817 *****************************************************************************/
818 static int OpenEncoder( vlc_object_t *p_this )
820 encoder_t *p_enc = (encoder_t *)p_this;
821 encoder_sys_t *p_sys;
822 int i_quality, i_min_bitrate, i_max_bitrate, i;
823 ogg_packet header[3];
827 if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') &&
833 /* Allocate the memory needed to store the decoder's structure */
834 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
836 p_enc->p_sys = p_sys;
838 p_enc->pf_encode_audio = Encode;
839 p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
840 p_enc->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
842 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
844 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
845 i_quality = val.i_int;
846 if( i_quality > 10 ) i_quality = 10;
847 if( i_quality < 0 ) i_quality = 0;
848 var_Get( p_enc, ENC_CFG_PREFIX "cbr", &val );
849 if( val.b_bool ) i_quality = 0;
850 var_Get( p_enc, ENC_CFG_PREFIX "max-bitrate", &val );
851 i_max_bitrate = val.i_int;
852 var_Get( p_enc, ENC_CFG_PREFIX "min-bitrate", &val );
853 i_min_bitrate = val.i_int;
855 /* Initialize vorbis encoder */
856 vorbis_info_init( &p_sys->vi );
861 if( vorbis_encode_setup_vbr( &p_sys->vi,
862 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
865 vorbis_info_clear( &p_sys->vi );
866 free( p_enc->p_sys );
867 msg_Err( p_enc, "VBR mode initialisation failed" );
871 /* Do we have optional hard quality restrictions? */
872 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
874 struct ovectl_ratemanage_arg ai;
875 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
877 ai.bitrate_hard_min = i_min_bitrate;
878 ai.bitrate_hard_max = i_max_bitrate;
879 ai.management_active = 1;
881 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
886 /* Turn off management entirely */
887 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
892 if( vorbis_encode_setup_managed( &p_sys->vi,
893 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
894 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
895 p_enc->fmt_out.i_bitrate,
896 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
898 vorbis_info_clear( &p_sys->vi );
899 msg_Err( p_enc, "CBR mode initialisation failed" );
900 free( p_enc->p_sys );
905 vorbis_encode_setup_init( &p_sys->vi );
908 vorbis_comment_init( &p_sys->vc);
909 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
911 /* Set up the analysis state and auxiliary encoding storage */
912 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
913 vorbis_block_init( &p_sys->vd, &p_sys->vb );
915 /* Create and store headers */
916 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
917 &header[0], &header[1], &header[2]);
918 p_enc->fmt_out.i_extra = 3 * 2 + header[0].bytes +
919 header[1].bytes + header[2].bytes;
920 p_extra = p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
921 for( i = 0; i < 3; i++ )
923 *(p_extra++) = header[i].bytes >> 8;
924 *(p_extra++) = header[i].bytes & 0xFF;
925 memcpy( p_extra, header[i].packet, header[i].bytes );
926 p_extra += header[i].bytes;
929 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
930 p_sys->i_last_block_size = 0;
931 p_sys->i_samples_delay = 0;
934 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
935 p_enc->fmt_in.audio.i_physical_channels, true);
940 /****************************************************************************
941 * Encode: the whole thing
942 ****************************************************************************
943 * This function spits out ogg packets.
944 ****************************************************************************/
945 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
947 encoder_sys_t *p_sys = p_enc->p_sys;
948 ogg_packet oggpacket;
949 block_t *p_block, *p_chain = NULL;
954 p_sys->i_pts = p_aout_buf->start_date -
955 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
956 (mtime_t)p_enc->fmt_in.audio.i_rate;
958 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
960 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
962 /* convert samples to float and uninterleave */
963 for( i = 0; i < p_sys->i_channels; i++ )
965 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
967 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
968 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
972 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
974 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
978 vorbis_analysis( &p_sys->vb, NULL );
979 vorbis_bitrate_addblock( &p_sys->vb );
981 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
984 p_block = block_New( p_enc, oggpacket.bytes );
985 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
987 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
989 if( i_block_size < 0 ) i_block_size = 0;
990 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
991 p_sys->i_last_block_size = i_block_size;
993 p_block->i_length = (mtime_t)1000000 *
994 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
996 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
998 p_sys->i_samples_delay -= i_samples;
1001 p_sys->i_pts += p_block->i_length;
1002 block_ChainAppend( &p_chain, p_block );
1009 /*****************************************************************************
1010 * CloseEncoder: vorbis encoder destruction
1011 *****************************************************************************/
1012 static void CloseEncoder( vlc_object_t *p_this )
1014 encoder_t *p_enc = (encoder_t *)p_this;
1015 encoder_sys_t *p_sys = p_enc->p_sys;
1017 vorbis_block_clear( &p_sys->vb );
1018 vorbis_dsp_clear( &p_sys->vd );
1019 vorbis_comment_clear( &p_sys->vc );
1020 vorbis_info_clear( &p_sys->vi ); /* must be called last */
1025 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */