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 *****************************************************************************/
31 #include <vlc_codec.h>
33 #include <vlc_input.h>
38 #ifdef MODULE_NAME_IS_tremor
39 #include <tremor/ivorbiscodec.h>
42 #include <vorbis/codec.h>
45 #ifdef HAVE_VORBIS_VORBISENC_H
46 # include <vorbis/vorbisenc.h>
47 # ifndef OV_ECTL_RATEMANAGE_AVG
48 # define OV_ECTL_RATEMANAGE_AVG 0x0
54 /*****************************************************************************
55 * decoder_sys_t : vorbis decoder descriptor
56 *****************************************************************************/
60 vlc_bool_t b_packetizer;
70 vorbis_info vi; /* struct that stores all the static vorbis bitstream
72 vorbis_comment vc; /* struct that stores all the bitstream user
74 vorbis_dsp_state vd; /* central working state for the packet->PCM
76 vorbis_block vb; /* local working space for packet->PCM decode */
81 audio_date_t end_date;
82 int i_last_block_size;
89 int pi_chan_table[AOUT_CHAN_MAX];
92 static int pi_channels_maps[9] =
96 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
97 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
98 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
99 | AOUT_CHAN_REARRIGHT,
100 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
101 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
102 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
103 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
104 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
105 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
106 | AOUT_CHAN_MIDDLERIGHT,
107 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
108 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
113 ** channel order as defined in http://www.ogghelp.com/ogg/glossary.cfm#Audio_Channels
116 /* recommended vorbis channel order for 6 channels */
117 static const uint32_t pi_6channels_in[] =
118 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
119 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
121 /* recommended vorbis channel order for 4 channels */
122 static const uint32_t pi_4channels_in[] =
123 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
125 /* recommended vorbis channel order for 3 channels */
126 static const uint32_t pi_3channels_in[] =
127 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
129 /* our internal channel order (WG-4 order) */
130 static const uint32_t pi_channels_out[] =
131 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
132 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
134 /****************************************************************************
136 ****************************************************************************/
137 static int OpenDecoder ( vlc_object_t * );
138 static int OpenPacketizer( vlc_object_t * );
139 static void CloseDecoder ( vlc_object_t * );
140 static void *DecodeBlock ( decoder_t *, block_t ** );
142 static int ProcessHeaders( decoder_t * );
143 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
145 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
146 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
148 static void ParseVorbisComments( decoder_t * );
150 static void ConfigureChannelOrder(int *, int, uint32_t, vlc_bool_t );
152 #ifdef MODULE_NAME_IS_tremor
153 static void Interleave ( int32_t *, const int32_t **, int, int, int * );
155 static void Interleave ( float *, const float **, int, int, int * );
158 #ifndef MODULE_NAME_IS_tremor
159 static int OpenEncoder ( vlc_object_t * );
160 static void CloseEncoder ( vlc_object_t * );
161 static block_t *Encode ( encoder_t *, aout_buffer_t * );
164 /*****************************************************************************
166 *****************************************************************************/
167 #define ENC_QUALITY_TEXT N_("Encoding quality")
168 #define ENC_QUALITY_LONGTEXT N_( \
169 "Enforce a quality between 1 (low) and 10 (high), instead " \
170 "of specifying a particular bitrate. This will produce a VBR stream." )
171 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
172 #define ENC_MAXBR_LONGTEXT N_( \
173 "Maximum bitrate in kbps. This is useful for streaming applications." )
174 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
175 #define ENC_MINBR_LONGTEXT N_( \
176 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
177 #define ENC_CBR_TEXT N_("CBR encoding")
178 #define ENC_CBR_LONGTEXT N_( \
179 "Force a constant bitrate encoding (CBR)." )
182 set_shortname( "Vorbis" );
183 set_description( _("Vorbis audio decoder") );
184 #ifdef MODULE_NAME_IS_tremor
185 set_capability( "decoder", 90 );
187 set_capability( "decoder", 100 );
189 set_category( CAT_INPUT );
190 set_subcategory( SUBCAT_INPUT_ACODEC );
191 set_callbacks( OpenDecoder, CloseDecoder );
194 set_description( _("Vorbis audio packetizer") );
195 set_capability( "packetizer", 100 );
196 set_callbacks( OpenPacketizer, CloseDecoder );
198 #ifndef MODULE_NAME_IS_tremor
199 # define ENC_CFG_PREFIX "sout-vorbis-"
201 set_description( _("Vorbis audio encoder") );
202 set_capability( "encoder", 100 );
203 #if defined(HAVE_VORBIS_VORBISENC_H)
204 set_callbacks( OpenEncoder, CloseEncoder );
207 add_integer( ENC_CFG_PREFIX "quality", 0, NULL, ENC_QUALITY_TEXT,
208 ENC_QUALITY_LONGTEXT, VLC_FALSE );
209 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
210 ENC_MAXBR_LONGTEXT, VLC_FALSE );
211 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
212 ENC_MINBR_LONGTEXT, VLC_FALSE );
213 add_bool( ENC_CFG_PREFIX "cbr", 0, NULL, ENC_CBR_TEXT,
214 ENC_CBR_LONGTEXT, VLC_FALSE );
219 #ifndef MODULE_NAME_IS_tremor
220 static const char *ppsz_enc_options[] = {
221 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
225 /*****************************************************************************
226 * OpenDecoder: probe the decoder and return score
227 *****************************************************************************/
228 static int OpenDecoder( vlc_object_t *p_this )
230 decoder_t *p_dec = (decoder_t*)p_this;
231 decoder_sys_t *p_sys;
233 if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
238 /* Allocate the memory needed to store the decoder's structure */
239 if( ( p_dec->p_sys = p_sys =
240 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
242 msg_Err( p_dec, "out of memory" );
247 aout_DateSet( &p_sys->end_date, 0 );
248 p_sys->i_last_block_size = 0;
249 p_sys->b_packetizer = VLC_FALSE;
250 p_sys->i_headers = 0;
251 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
253 /* Take care of vorbis init */
254 vorbis_info_init( &p_sys->vi );
255 vorbis_comment_init( &p_sys->vc );
257 /* Set output properties */
258 p_dec->fmt_out.i_cat = AUDIO_ES;
259 #ifdef MODULE_NAME_IS_tremor
260 p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
262 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
266 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
268 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
274 static int OpenPacketizer( vlc_object_t *p_this )
276 decoder_t *p_dec = (decoder_t*)p_this;
278 int i_ret = OpenDecoder( p_this );
280 if( i_ret == VLC_SUCCESS )
282 p_dec->p_sys->b_packetizer = VLC_TRUE;
283 p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
289 /****************************************************************************
290 * DecodeBlock: the whole thing
291 ****************************************************************************
292 * This function must be fed with ogg packets.
293 ****************************************************************************/
294 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
296 decoder_sys_t *p_sys = p_dec->p_sys;
297 ogg_packet oggpacket;
299 if( !pp_block ) return NULL;
303 /* Block to Ogg packet */
304 oggpacket.packet = (*pp_block)->p_buffer;
305 oggpacket.bytes = (*pp_block)->i_buffer;
307 if( (*pp_block)->i_rate > 0 )
308 p_sys->i_input_rate = (*pp_block)->i_rate;
312 if( p_sys->b_packetizer ) return NULL;
314 /* Block to Ogg packet */
315 oggpacket.packet = NULL;
319 oggpacket.granulepos = -1;
322 oggpacket.packetno = 0;
324 /* Check for headers */
325 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
327 /* Headers already available as extra data */
328 msg_Dbg( p_dec, "headers already available as extra data" );
329 p_sys->i_headers = 3;
331 else if( oggpacket.bytes && p_sys->i_headers < 3 )
333 /* Backup headers as extra data */
336 p_dec->fmt_in.p_extra =
337 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
338 oggpacket.bytes + 2 );
339 p_extra = (uint8_t *)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
340 *(p_extra++) = oggpacket.bytes >> 8;
341 *(p_extra++) = oggpacket.bytes & 0xFF;
343 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
344 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
346 block_Release( *pp_block );
351 if( p_sys->i_headers == 3 )
353 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
355 p_sys->i_headers = 0;
356 p_dec->fmt_in.i_extra = 0;
357 block_Release( *pp_block );
360 else p_sys->i_headers++;
363 return ProcessPacket( p_dec, &oggpacket, pp_block );
366 /*****************************************************************************
367 * ProcessHeaders: process Vorbis headers.
368 *****************************************************************************/
369 static int ProcessHeaders( decoder_t *p_dec )
371 decoder_sys_t *p_sys = p_dec->p_sys;
372 ogg_packet oggpacket;
376 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
378 oggpacket.granulepos = -1;
379 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
381 oggpacket.packetno = 0;
382 p_extra = p_dec->fmt_in.p_extra;
383 i_extra = p_dec->fmt_in.i_extra;
385 /* Take care of the initial Vorbis header */
386 oggpacket.bytes = *(p_extra++) << 8;
387 oggpacket.bytes |= (*(p_extra++) & 0xFF);
388 oggpacket.packet = p_extra;
389 p_extra += oggpacket.bytes;
390 i_extra -= (oggpacket.bytes + 2);
393 msg_Err( p_dec, "header data corrupted");
397 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
399 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
403 /* Setup the format */
404 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
405 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
407 if( p_dec->fmt_out.audio.i_channels > 9 )
409 msg_Err( p_dec, "invalid number of channels (not between 1 and 9): %i",
410 p_dec->fmt_out.audio.i_channels );
414 p_dec->fmt_out.audio.i_physical_channels =
415 p_dec->fmt_out.audio.i_original_channels =
416 pi_channels_maps[p_sys->vi.channels];
417 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
419 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
421 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
422 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
424 /* The next packet in order is the comments header */
426 oggpacket.bytes = *(p_extra++) << 8;
427 oggpacket.bytes |= (*(p_extra++) & 0xFF);
428 oggpacket.packet = p_extra;
429 p_extra += oggpacket.bytes;
430 i_extra -= (oggpacket.bytes + 2);
433 msg_Err( p_dec, "header data corrupted");
437 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
439 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
442 ParseVorbisComments( p_dec );
444 /* The next packet in order is the codebooks header
445 * We need to watch out that this packet is not missing as a
446 * missing or corrupted header is fatal. */
447 oggpacket.bytes = *(p_extra++) << 8;
448 oggpacket.bytes |= (*(p_extra++) & 0xFF);
449 oggpacket.packet = p_extra;
450 i_extra -= (oggpacket.bytes + 2);
453 msg_Err( p_dec, "header data corrupted");
457 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
459 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
463 if( !p_sys->b_packetizer )
465 /* Initialize the Vorbis packet->PCM decoder */
466 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
467 vorbis_block_init( &p_sys->vd, &p_sys->vb );
471 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
472 p_dec->fmt_out.p_extra =
473 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
474 memcpy( p_dec->fmt_out.p_extra,
475 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
478 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
479 p_dec->fmt_out.audio.i_physical_channels, VLC_TRUE);
484 /*****************************************************************************
485 * ProcessPacket: processes a Vorbis packet.
486 *****************************************************************************/
487 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
490 decoder_sys_t *p_sys = p_dec->p_sys;
491 block_t *p_block = *pp_block;
493 /* Date management */
494 if( p_block && p_block->i_pts > 0 &&
495 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
497 aout_DateSet( &p_sys->end_date, p_block->i_pts );
500 if( !aout_DateGet( &p_sys->end_date ) )
502 /* We've just started the stream, wait for the first PTS. */
503 if( p_block ) block_Release( p_block );
507 *pp_block = NULL; /* To avoid being fed the same packet again */
509 if( p_sys->b_packetizer )
511 return SendPacket( p_dec, p_oggpacket, p_block );
515 aout_buffer_t *p_aout_buffer;
517 if( p_sys->i_headers >= 3 )
518 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
520 p_aout_buffer = NULL;
522 if( p_block ) block_Release( p_block );
523 return p_aout_buffer;
527 /*****************************************************************************
528 * DecodePacket: decodes a Vorbis packet.
529 *****************************************************************************/
530 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
532 decoder_sys_t *p_sys = p_dec->p_sys;
535 #ifdef MODULE_NAME_IS_tremor
541 if( p_oggpacket->bytes &&
542 #ifdef MODULE_NAME_IS_tremor
543 vorbis_synthesis( &p_sys->vb, p_oggpacket, 1 ) == 0 )
545 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
547 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
549 /* **pp_pcm is a multichannel float vector. In stereo, for
550 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
551 * the size of each channel. Convert the float values
552 * (-1.<=range<=1.) to whatever PCM format and write it out */
554 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
557 aout_buffer_t *p_aout_buffer;
560 p_dec->pf_aout_buffer_new( p_dec, i_samples );
562 if( p_aout_buffer == NULL ) return NULL;
564 /* Interleave the samples */
565 #ifdef MODULE_NAME_IS_tremor
566 Interleave( (int32_t *)p_aout_buffer->p_buffer,
567 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
569 Interleave( (float *)p_aout_buffer->p_buffer,
570 (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
573 /* Tell libvorbis how many samples we actually consumed */
574 vorbis_synthesis_read( &p_sys->vd, i_samples );
576 /* Date management */
577 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
578 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
579 i_samples * p_sys->i_input_rate / INPUT_RATE_DEFAULT );
580 return p_aout_buffer;
588 /*****************************************************************************
589 * SendPacket: send an ogg dated packet to the stream output.
590 *****************************************************************************/
591 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
594 decoder_sys_t *p_sys = p_dec->p_sys;
595 int i_block_size, i_samples;
597 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
598 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
599 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
600 p_sys->i_last_block_size = i_block_size;
602 /* Date management */
603 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
605 if( p_sys->i_headers >= 3 )
606 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
607 i_samples * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
610 p_block->i_length = 0;
615 /*****************************************************************************
616 * ParseVorbisComments
617 *****************************************************************************/
618 static void ParseVorbisComments( decoder_t *p_dec )
620 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
621 char *psz_name, *psz_value, *psz_comment;
622 input_item_t *p_item;
625 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
627 p_item = input_GetItem( p_input );
629 while( i < p_dec->p_sys->vc.comments )
631 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
634 msg_Warn( p_dec, "out of memory" );
637 psz_name = psz_comment;
638 psz_value = strchr( psz_comment, '=' );
643 input_Control( p_input, INPUT_ADD_INFO, _("Vorbis comment"),
644 psz_name, "%s", psz_value );
646 if( !strcasecmp( psz_name, "artist" ) )
648 if( psz_value && ( *psz_value != '\0' ) )
650 input_item_SetArtist( p_item, psz_value );
651 input_ItemAddInfo( p_item,
652 _(VLC_META_INFO_CAT),
657 else if( !strcasecmp( psz_name, "title" ) )
659 if( psz_value && ( *psz_value != '\0' ) )
661 input_item_SetTitle( p_item, psz_value );
662 p_item->psz_name = strdup( psz_value );
665 else if( !strcasecmp( psz_name, "album" ) )
667 if( psz_value && ( *psz_value != '\0' ) )
669 input_item_SetAlbum( p_item, psz_value );
672 else if( !strcasecmp( psz_name, "musicbrainz_trackid" ) )
674 if( psz_value && ( *psz_value != '\0' ) )
676 input_item_SetTrackID( p_item, psz_value );
680 else if( !strcasecmp( psz_name, "musicbrainz_artistid" ) )
682 if( psz_value && ( *psz_value != '\0' ) )
684 vlc_meta_SetArtistID( p_item, psz_value );
687 else if( !strcasecmp( psz_name, "musicbrainz_albumid" ) )
689 if( psz_value && ( *psz_value != '\0' ) )
691 input_item_SetAlbumID( p_item, psz_value );
696 if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
697 !strcasecmp( psz_name, "RG_RADIO" ) )
699 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
701 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
702 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
704 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
705 !strcasecmp( psz_name, "RG_PEAK" ) )
707 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
709 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = VLC_TRUE;
710 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
712 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
713 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
715 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
717 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
718 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
720 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
722 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
724 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = VLC_TRUE;
725 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
729 var_SetInteger( p_input, "item-change", p_item->i_id );
735 /*****************************************************************************
736 * Interleave: helper function to interleave channels
737 *****************************************************************************/
738 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, vlc_bool_t b_decode)
740 const uint32_t *pi_channels_in;
745 pi_channels_in = pi_6channels_in;
748 pi_channels_in = pi_4channels_in;
751 pi_channels_in = pi_3channels_in;
756 for( i = 0; i< i_channels; ++i )
758 pi_chan_table[i] = i;
765 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
766 i_channel_mask & AOUT_CHAN_PHYSMASK,
770 aout_CheckChannelReorder( pi_channels_out, pi_channels_in,
771 i_channel_mask & AOUT_CHAN_PHYSMASK,
776 /*****************************************************************************
777 * Interleave: helper function to interleave channels
778 *****************************************************************************/
779 #ifdef MODULE_NAME_IS_tremor
780 static void Interleave( int32_t *p_out, const int32_t **pp_in,
781 int i_nb_channels, int i_samples, int *pi_chan_table)
785 for ( j = 0; j < i_samples; j++ )
786 for ( i = 0; i < i_nb_channels; i++ )
787 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
790 static void Interleave( float *p_out, const float **pp_in,
791 int i_nb_channels, int i_samples, int *pi_chan_table )
795 for ( j = 0; j < i_samples; j++ )
796 for ( i = 0; i < i_nb_channels; i++ )
797 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
801 /*****************************************************************************
802 * CloseDecoder: vorbis decoder destruction
803 *****************************************************************************/
804 static void CloseDecoder( vlc_object_t *p_this )
806 decoder_t *p_dec = (decoder_t *)p_this;
807 decoder_sys_t *p_sys = p_dec->p_sys;
809 if( !p_sys->b_packetizer && p_sys->i_headers > 3 )
811 vorbis_block_clear( &p_sys->vb );
812 vorbis_dsp_clear( &p_sys->vd );
815 vorbis_comment_clear( &p_sys->vc );
816 vorbis_info_clear( &p_sys->vi ); /* must be called last */
821 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
823 /*****************************************************************************
824 * encoder_sys_t : vorbis encoder descriptor
825 *****************************************************************************/
831 vorbis_info vi; /* struct that stores all the static vorbis bitstream
833 vorbis_comment vc; /* struct that stores all the bitstream user
835 vorbis_dsp_state vd; /* central working state for the packet->PCM
837 vorbis_block vb; /* local working space for packet->PCM decode */
839 int i_last_block_size;
849 ** Channel reordering
851 int pi_chan_table[AOUT_CHAN_MAX];
855 /*****************************************************************************
856 * OpenEncoder: probe the encoder and return score
857 *****************************************************************************/
858 static int OpenEncoder( vlc_object_t *p_this )
860 encoder_t *p_enc = (encoder_t *)p_this;
861 encoder_sys_t *p_sys;
862 int i_quality, i_min_bitrate, i_max_bitrate, i;
863 ogg_packet header[3];
867 if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') &&
873 /* Allocate the memory needed to store the decoder's structure */
874 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
876 msg_Err( p_enc, "out of memory" );
879 p_enc->p_sys = p_sys;
881 p_enc->pf_encode_audio = Encode;
882 p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
883 p_enc->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
885 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
887 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
888 i_quality = val.i_int;
889 if( i_quality > 10 ) i_quality = 10;
890 if( i_quality < 0 ) i_quality = 0;
891 var_Get( p_enc, ENC_CFG_PREFIX "cbr", &val );
892 if( val.b_bool ) i_quality = 0;
893 var_Get( p_enc, ENC_CFG_PREFIX "max-bitrate", &val );
894 i_max_bitrate = val.i_int;
895 var_Get( p_enc, ENC_CFG_PREFIX "min-bitrate", &val );
896 i_min_bitrate = val.i_int;
898 /* Initialize vorbis encoder */
899 vorbis_info_init( &p_sys->vi );
904 if( vorbis_encode_setup_vbr( &p_sys->vi,
905 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
908 vorbis_info_clear( &p_sys->vi );
909 free( p_enc->p_sys );
910 msg_Err( p_enc, "VBR mode initialisation failed" );
914 /* Do we have optional hard quality restrictions? */
915 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
917 struct ovectl_ratemanage_arg ai;
918 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
920 ai.bitrate_hard_min = i_min_bitrate;
921 ai.bitrate_hard_max = i_max_bitrate;
922 ai.management_active = 1;
924 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
929 /* Turn off management entirely */
930 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
935 if( vorbis_encode_setup_managed( &p_sys->vi,
936 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
937 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
938 p_enc->fmt_out.i_bitrate,
939 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
941 vorbis_info_clear( &p_sys->vi );
942 msg_Err( p_enc, "CBR mode initialisation failed" );
943 free( p_enc->p_sys );
948 vorbis_encode_setup_init( &p_sys->vi );
951 vorbis_comment_init( &p_sys->vc);
952 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
954 /* Set up the analysis state and auxiliary encoding storage */
955 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
956 vorbis_block_init( &p_sys->vd, &p_sys->vb );
958 /* Create and store headers */
959 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
960 &header[0], &header[1], &header[2]);
961 p_enc->fmt_out.i_extra = 3 * 2 + header[0].bytes +
962 header[1].bytes + header[2].bytes;
963 p_extra = p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
964 for( i = 0; i < 3; i++ )
966 *(p_extra++) = header[i].bytes >> 8;
967 *(p_extra++) = header[i].bytes & 0xFF;
968 memcpy( p_extra, header[i].packet, header[i].bytes );
969 p_extra += header[i].bytes;
972 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
973 p_sys->i_last_block_size = 0;
974 p_sys->i_samples_delay = 0;
977 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
978 p_enc->fmt_in.audio.i_physical_channels, VLC_TRUE);
983 /****************************************************************************
984 * Encode: the whole thing
985 ****************************************************************************
986 * This function spits out ogg packets.
987 ****************************************************************************/
988 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
990 encoder_sys_t *p_sys = p_enc->p_sys;
991 ogg_packet oggpacket;
992 block_t *p_block, *p_chain = NULL;
997 p_sys->i_pts = p_aout_buf->start_date -
998 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
999 (mtime_t)p_enc->fmt_in.audio.i_rate;
1001 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
1003 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
1005 /* convert samples to float and uninterleave */
1006 for( i = 0; i < p_sys->i_channels; i++ )
1008 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
1010 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
1011 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
1015 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
1017 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
1021 vorbis_analysis( &p_sys->vb, NULL );
1022 vorbis_bitrate_addblock( &p_sys->vb );
1024 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
1027 p_block = block_New( p_enc, oggpacket.bytes );
1028 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
1030 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
1032 if( i_block_size < 0 ) i_block_size = 0;
1033 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
1034 p_sys->i_last_block_size = i_block_size;
1036 p_block->i_length = (mtime_t)1000000 *
1037 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
1039 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
1041 p_sys->i_samples_delay -= i_samples;
1044 p_sys->i_pts += p_block->i_length;
1045 block_ChainAppend( &p_chain, p_block );
1052 /*****************************************************************************
1053 * CloseEncoder: vorbis encoder destruction
1054 *****************************************************************************/
1055 static void CloseEncoder( vlc_object_t *p_this )
1057 encoder_t *p_enc = (encoder_t *)p_this;
1058 encoder_sys_t *p_sys = p_enc->p_sys;
1060 vorbis_block_clear( &p_sys->vb );
1061 vorbis_dsp_clear( &p_sys->vd );
1062 vorbis_comment_clear( &p_sys->vc );
1063 vorbis_info_clear( &p_sys->vi ); /* must be called last */
1068 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */