1 /*****************************************************************************
2 * vorbis.c: vorbis decoder/encoder/packetizer module using of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 2001-2003 the VideoLAN team
5 * Copyright (C) 2007 Société des arts technologiques
6 * Copyright (C) 2007 Savoir-faire Linux
10 * Authors: Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
38 #include <vlc_input.h>
43 #ifdef MODULE_NAME_IS_tremor
44 #include <tremor/ivorbiscodec.h>
47 #include <vorbis/codec.h>
50 #ifdef HAVE_VORBIS_VORBISENC_H
51 # include <vorbis/vorbisenc.h>
52 # ifndef OV_ECTL_RATEMANAGE_AVG
53 # define OV_ECTL_RATEMANAGE_AVG 0x0
59 /*****************************************************************************
60 * decoder_sys_t : vorbis decoder descriptor
61 *****************************************************************************/
75 vorbis_info vi; /* struct that stores all the static vorbis bitstream
77 vorbis_comment vc; /* struct that stores all the bitstream user
79 vorbis_dsp_state vd; /* central working state for the packet->PCM
81 vorbis_block vb; /* local working space for packet->PCM decode */
86 audio_date_t end_date;
87 int i_last_block_size;
92 int pi_chan_table[AOUT_CHAN_MAX];
95 static const int pi_channels_maps[9] =
99 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
100 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
101 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
102 | AOUT_CHAN_REARRIGHT,
103 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
104 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
105 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
106 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
107 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
108 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
109 | AOUT_CHAN_MIDDLERIGHT,
110 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
111 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
116 ** channel order as defined in http://www.ogghelp.com/ogg/glossary.cfm#Audio_Channels
119 /* recommended vorbis channel order for 6 channels */
120 static const uint32_t pi_6channels_in[] =
121 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
122 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
124 /* recommended vorbis channel order for 4 channels */
125 static const uint32_t pi_4channels_in[] =
126 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
128 /* recommended vorbis channel order for 3 channels */
129 static const uint32_t pi_3channels_in[] =
130 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
132 /****************************************************************************
134 ****************************************************************************/
135 static int OpenDecoder ( vlc_object_t * );
136 static int OpenPacketizer( vlc_object_t * );
137 static void CloseDecoder ( vlc_object_t * );
138 static void *DecodeBlock ( decoder_t *, block_t ** );
140 static int ProcessHeaders( decoder_t * );
141 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
143 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
144 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
146 static void ParseVorbisComments( decoder_t * );
148 static void ConfigureChannelOrder(int *, int, uint32_t, bool );
150 #ifdef MODULE_NAME_IS_tremor
151 static void Interleave ( int32_t *, const int32_t **, int, int, int * );
153 static void Interleave ( float *, const float **, int, int, int * );
156 #ifndef MODULE_NAME_IS_tremor
157 static int OpenEncoder ( vlc_object_t * );
158 static void CloseEncoder ( vlc_object_t * );
159 static block_t *Encode ( encoder_t *, aout_buffer_t * );
162 /*****************************************************************************
164 *****************************************************************************/
165 #define ENC_QUALITY_TEXT N_("Encoding quality")
166 #define ENC_QUALITY_LONGTEXT N_( \
167 "Enforce a quality between 1 (low) and 10 (high), instead " \
168 "of specifying a particular bitrate. This will produce a VBR stream." )
169 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
170 #define ENC_MAXBR_LONGTEXT N_( \
171 "Maximum bitrate in kbps. This is useful for streaming applications." )
172 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
173 #define ENC_MINBR_LONGTEXT N_( \
174 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
175 #define ENC_CBR_TEXT N_("CBR encoding")
176 #define ENC_CBR_LONGTEXT N_( \
177 "Force a constant bitrate encoding (CBR)." )
180 set_shortname( "Vorbis" )
181 set_description( N_("Vorbis audio decoder") )
182 #ifdef MODULE_NAME_IS_tremor
183 set_capability( "decoder", 90 )
185 set_capability( "decoder", 100 )
187 set_category( CAT_INPUT )
188 set_subcategory( SUBCAT_INPUT_ACODEC )
189 set_callbacks( OpenDecoder, CloseDecoder )
192 set_description( N_("Vorbis audio packetizer") )
193 set_capability( "packetizer", 100 )
194 set_callbacks( OpenPacketizer, CloseDecoder )
196 #ifndef MODULE_NAME_IS_tremor
197 # define ENC_CFG_PREFIX "sout-vorbis-"
199 set_description( N_("Vorbis audio encoder") )
200 set_capability( "encoder", 100 )
201 #if defined(HAVE_VORBIS_VORBISENC_H)
202 set_callbacks( OpenEncoder, CloseEncoder )
205 add_integer( ENC_CFG_PREFIX "quality", 0, NULL, ENC_QUALITY_TEXT,
206 ENC_QUALITY_LONGTEXT, false );
207 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
208 ENC_MAXBR_LONGTEXT, false );
209 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
210 ENC_MINBR_LONGTEXT, false );
211 add_bool( ENC_CFG_PREFIX "cbr", 0, NULL, ENC_CBR_TEXT,
212 ENC_CBR_LONGTEXT, false );
217 #ifndef MODULE_NAME_IS_tremor
218 static const char *const ppsz_enc_options[] = {
219 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
223 /*****************************************************************************
224 * OpenDecoder: probe the decoder and return score
225 *****************************************************************************/
226 static int OpenDecoder( vlc_object_t *p_this )
228 decoder_t *p_dec = (decoder_t*)p_this;
229 decoder_sys_t *p_sys;
231 if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
236 /* Allocate the memory needed to store the decoder's structure */
237 if( ( p_dec->p_sys = p_sys =
238 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
242 aout_DateSet( &p_sys->end_date, 0 );
243 p_sys->i_last_block_size = 0;
244 p_sys->b_packetizer = false;
245 p_sys->i_headers = 0;
247 /* Take care of vorbis init */
248 vorbis_info_init( &p_sys->vi );
249 vorbis_comment_init( &p_sys->vc );
251 /* Set output properties */
252 p_dec->fmt_out.i_cat = AUDIO_ES;
253 #ifdef MODULE_NAME_IS_tremor
254 p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
256 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
260 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
262 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
268 static int OpenPacketizer( vlc_object_t *p_this )
270 decoder_t *p_dec = (decoder_t*)p_this;
272 int i_ret = OpenDecoder( p_this );
274 if( i_ret == VLC_SUCCESS )
276 p_dec->p_sys->b_packetizer = true;
277 p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
283 /****************************************************************************
284 * DecodeBlock: the whole thing
285 ****************************************************************************
286 * This function must be fed with ogg packets.
287 ****************************************************************************/
288 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
290 decoder_sys_t *p_sys = p_dec->p_sys;
291 ogg_packet oggpacket;
293 if( !pp_block ) return NULL;
297 /* Block to Ogg packet */
298 oggpacket.packet = (*pp_block)->p_buffer;
299 oggpacket.bytes = (*pp_block)->i_buffer;
303 if( p_sys->b_packetizer ) return NULL;
305 /* Block to Ogg packet */
306 oggpacket.packet = NULL;
310 oggpacket.granulepos = -1;
313 oggpacket.packetno = 0;
315 /* Check for headers */
316 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
318 /* Headers already available as extra data */
319 msg_Dbg( p_dec, "headers already available as extra data" );
320 p_sys->i_headers = 3;
322 else if( oggpacket.bytes && p_sys->i_headers < 3 )
324 /* Backup headers as extra data */
327 p_dec->fmt_in.p_extra =
328 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
329 oggpacket.bytes + 2 );
330 p_extra = (uint8_t *)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
331 *(p_extra++) = oggpacket.bytes >> 8;
332 *(p_extra++) = oggpacket.bytes & 0xFF;
334 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
335 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
337 block_Release( *pp_block );
342 if( p_sys->i_headers == 3 )
344 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
346 p_sys->i_headers = 0;
347 p_dec->fmt_in.i_extra = 0;
348 block_Release( *pp_block );
351 else p_sys->i_headers++;
354 return ProcessPacket( p_dec, &oggpacket, pp_block );
357 /*****************************************************************************
358 * ProcessHeaders: process Vorbis headers.
359 *****************************************************************************/
360 static int ProcessHeaders( decoder_t *p_dec )
362 decoder_sys_t *p_sys = p_dec->p_sys;
363 ogg_packet oggpacket;
367 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
369 oggpacket.granulepos = -1;
370 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
372 oggpacket.packetno = 0;
373 p_extra = p_dec->fmt_in.p_extra;
374 i_extra = p_dec->fmt_in.i_extra;
376 /* Take care of the initial Vorbis header */
377 oggpacket.bytes = *(p_extra++) << 8;
378 oggpacket.bytes |= (*(p_extra++) & 0xFF);
379 oggpacket.packet = p_extra;
380 p_extra += oggpacket.bytes;
381 i_extra -= (oggpacket.bytes + 2);
384 msg_Err( p_dec, "header data corrupted");
388 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
390 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
394 /* Setup the format */
395 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
396 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
398 if( p_dec->fmt_out.audio.i_channels > 9 )
400 msg_Err( p_dec, "invalid number of channels (not between 1 and 9): %i",
401 p_dec->fmt_out.audio.i_channels );
405 p_dec->fmt_out.audio.i_physical_channels =
406 p_dec->fmt_out.audio.i_original_channels =
407 pi_channels_maps[p_sys->vi.channels];
408 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
410 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
412 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
413 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
415 /* The next packet in order is the comments header */
417 oggpacket.bytes = *(p_extra++) << 8;
418 oggpacket.bytes |= (*(p_extra++) & 0xFF);
419 oggpacket.packet = p_extra;
420 p_extra += oggpacket.bytes;
421 i_extra -= (oggpacket.bytes + 2);
424 msg_Err( p_dec, "header data corrupted");
428 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
430 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
433 ParseVorbisComments( p_dec );
435 /* The next packet in order is the codebooks header
436 * We need to watch out that this packet is not missing as a
437 * missing or corrupted header is fatal. */
438 oggpacket.bytes = *(p_extra++) << 8;
439 oggpacket.bytes |= (*(p_extra++) & 0xFF);
440 oggpacket.packet = p_extra;
441 i_extra -= (oggpacket.bytes + 2);
444 msg_Err( p_dec, "header data corrupted");
448 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
450 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
454 if( !p_sys->b_packetizer )
456 /* Initialize the Vorbis packet->PCM decoder */
457 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
458 vorbis_block_init( &p_sys->vd, &p_sys->vb );
462 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
463 p_dec->fmt_out.p_extra =
464 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
465 memcpy( p_dec->fmt_out.p_extra,
466 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
469 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
470 p_dec->fmt_out.audio.i_physical_channels, true);
475 /*****************************************************************************
476 * ProcessPacket: processes a Vorbis packet.
477 *****************************************************************************/
478 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
481 decoder_sys_t *p_sys = p_dec->p_sys;
482 block_t *p_block = *pp_block;
484 /* Date management */
485 if( p_block && p_block->i_pts > 0 &&
486 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
488 aout_DateSet( &p_sys->end_date, p_block->i_pts );
491 if( !aout_DateGet( &p_sys->end_date ) )
493 /* We've just started the stream, wait for the first PTS. */
494 if( p_block ) block_Release( p_block );
498 *pp_block = NULL; /* To avoid being fed the same packet again */
500 if( p_sys->b_packetizer )
502 return SendPacket( p_dec, p_oggpacket, p_block );
506 aout_buffer_t *p_aout_buffer;
508 if( p_sys->i_headers >= 3 )
509 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
511 p_aout_buffer = NULL;
513 if( p_block ) block_Release( p_block );
514 return p_aout_buffer;
518 /*****************************************************************************
519 * DecodePacket: decodes a Vorbis packet.
520 *****************************************************************************/
521 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
523 decoder_sys_t *p_sys = p_dec->p_sys;
526 #ifdef MODULE_NAME_IS_tremor
532 if( p_oggpacket->bytes &&
533 #ifdef MODULE_NAME_IS_tremor
534 vorbis_synthesis( &p_sys->vb, p_oggpacket, 1 ) == 0 )
536 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
538 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
540 /* **pp_pcm is a multichannel float vector. In stereo, for
541 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
542 * the size of each channel. Convert the float values
543 * (-1.<=range<=1.) to whatever PCM format and write it out */
545 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
548 aout_buffer_t *p_aout_buffer;
551 decoder_NewAudioBuffer( p_dec, i_samples );
553 if( p_aout_buffer == NULL ) return NULL;
555 /* Interleave the samples */
556 #ifdef MODULE_NAME_IS_tremor
557 Interleave( (int32_t *)p_aout_buffer->p_buffer,
558 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
560 Interleave( (float *)p_aout_buffer->p_buffer,
561 (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
564 /* Tell libvorbis how many samples we actually consumed */
565 vorbis_synthesis_read( &p_sys->vd, i_samples );
567 /* Date management */
568 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
569 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date, i_samples );
570 return p_aout_buffer;
578 /*****************************************************************************
579 * SendPacket: send an ogg dated packet to the stream output.
580 *****************************************************************************/
581 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
584 decoder_sys_t *p_sys = p_dec->p_sys;
585 int i_block_size, i_samples;
587 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
588 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
589 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
590 p_sys->i_last_block_size = i_block_size;
592 /* Date management */
593 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
595 if( p_sys->i_headers >= 3 )
596 p_block->i_length = aout_DateIncrement( &p_sys->end_date, i_samples ) - p_block->i_pts;
598 p_block->i_length = 0;
603 /*****************************************************************************
604 * ParseVorbisComments
605 *****************************************************************************/
606 static void ParseVorbisComments( decoder_t *p_dec )
608 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
609 char *psz_name, *psz_value, *psz_comment;
610 input_item_t *p_item;
613 if( p_input->i_object_type != VLC_OBJECT_INPUT )
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 );
693 /*****************************************************************************
694 * Interleave: helper function to interleave channels
695 *****************************************************************************/
696 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, bool b_decode)
698 const uint32_t *pi_channels_in;
703 pi_channels_in = pi_6channels_in;
706 pi_channels_in = pi_4channels_in;
709 pi_channels_in = pi_3channels_in;
714 for( i = 0; i< i_channels; ++i )
716 pi_chan_table[i] = i;
723 aout_CheckChannelReorder( pi_channels_in, NULL,
724 i_channel_mask & AOUT_CHAN_PHYSMASK,
728 aout_CheckChannelReorder( NULL, pi_channels_in,
729 i_channel_mask & AOUT_CHAN_PHYSMASK,
734 /*****************************************************************************
735 * Interleave: helper function to interleave channels
736 *****************************************************************************/
737 #ifdef MODULE_NAME_IS_tremor
738 static void Interleave( int32_t *p_out, const int32_t **pp_in,
739 int i_nb_channels, int i_samples, int *pi_chan_table)
743 for ( j = 0; j < i_samples; j++ )
744 for ( i = 0; i < i_nb_channels; i++ )
745 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
748 static void Interleave( float *p_out, const float **pp_in,
749 int i_nb_channels, int i_samples, int *pi_chan_table )
753 for ( j = 0; j < i_samples; j++ )
754 for ( i = 0; i < i_nb_channels; i++ )
755 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
759 /*****************************************************************************
760 * CloseDecoder: vorbis decoder destruction
761 *****************************************************************************/
762 static void CloseDecoder( vlc_object_t *p_this )
764 decoder_t *p_dec = (decoder_t *)p_this;
765 decoder_sys_t *p_sys = p_dec->p_sys;
767 if( !p_sys->b_packetizer && p_sys->i_headers > 3 )
769 vorbis_block_clear( &p_sys->vb );
770 vorbis_dsp_clear( &p_sys->vd );
773 vorbis_comment_clear( &p_sys->vc );
774 vorbis_info_clear( &p_sys->vi ); /* must be called last */
779 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
781 /*****************************************************************************
782 * encoder_sys_t : vorbis encoder descriptor
783 *****************************************************************************/
789 vorbis_info vi; /* struct that stores all the static vorbis bitstream
791 vorbis_comment vc; /* struct that stores all the bitstream user
793 vorbis_dsp_state vd; /* central working state for the packet->PCM
795 vorbis_block vb; /* local working space for packet->PCM decode */
797 int i_last_block_size;
807 ** Channel reordering
809 int pi_chan_table[AOUT_CHAN_MAX];
813 /*****************************************************************************
814 * OpenEncoder: probe the encoder and return score
815 *****************************************************************************/
816 static int OpenEncoder( vlc_object_t *p_this )
818 encoder_t *p_enc = (encoder_t *)p_this;
819 encoder_sys_t *p_sys;
820 int i_quality, i_min_bitrate, i_max_bitrate, i;
821 ogg_packet header[3];
825 if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') &&
831 /* Allocate the memory needed to store the decoder's structure */
832 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
834 p_enc->p_sys = p_sys;
836 p_enc->pf_encode_audio = Encode;
837 p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
838 p_enc->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
840 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
842 var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
843 i_quality = val.i_int;
844 if( i_quality > 10 ) i_quality = 10;
845 if( i_quality < 0 ) i_quality = 0;
846 var_Get( p_enc, ENC_CFG_PREFIX "cbr", &val );
847 if( val.b_bool ) i_quality = 0;
848 var_Get( p_enc, ENC_CFG_PREFIX "max-bitrate", &val );
849 i_max_bitrate = val.i_int;
850 var_Get( p_enc, ENC_CFG_PREFIX "min-bitrate", &val );
851 i_min_bitrate = val.i_int;
853 /* Initialize vorbis encoder */
854 vorbis_info_init( &p_sys->vi );
859 if( vorbis_encode_setup_vbr( &p_sys->vi,
860 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
863 vorbis_info_clear( &p_sys->vi );
864 free( p_enc->p_sys );
865 msg_Err( p_enc, "VBR mode initialisation failed" );
869 /* Do we have optional hard quality restrictions? */
870 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
872 struct ovectl_ratemanage_arg ai;
873 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
875 ai.bitrate_hard_min = i_min_bitrate;
876 ai.bitrate_hard_max = i_max_bitrate;
877 ai.management_active = 1;
879 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
884 /* Turn off management entirely */
885 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
890 if( vorbis_encode_setup_managed( &p_sys->vi,
891 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
892 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
893 p_enc->fmt_out.i_bitrate,
894 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
896 vorbis_info_clear( &p_sys->vi );
897 msg_Err( p_enc, "CBR mode initialisation failed" );
898 free( p_enc->p_sys );
903 vorbis_encode_setup_init( &p_sys->vi );
906 vorbis_comment_init( &p_sys->vc);
907 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
909 /* Set up the analysis state and auxiliary encoding storage */
910 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
911 vorbis_block_init( &p_sys->vd, &p_sys->vb );
913 /* Create and store headers */
914 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
915 &header[0], &header[1], &header[2]);
916 p_enc->fmt_out.i_extra = 3 * 2 + header[0].bytes +
917 header[1].bytes + header[2].bytes;
918 p_extra = p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
919 for( i = 0; i < 3; i++ )
921 *(p_extra++) = header[i].bytes >> 8;
922 *(p_extra++) = header[i].bytes & 0xFF;
923 memcpy( p_extra, header[i].packet, header[i].bytes );
924 p_extra += header[i].bytes;
927 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
928 p_sys->i_last_block_size = 0;
929 p_sys->i_samples_delay = 0;
932 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
933 p_enc->fmt_in.audio.i_physical_channels, true);
938 /****************************************************************************
939 * Encode: the whole thing
940 ****************************************************************************
941 * This function spits out ogg packets.
942 ****************************************************************************/
943 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
945 encoder_sys_t *p_sys = p_enc->p_sys;
946 ogg_packet oggpacket;
947 block_t *p_block, *p_chain = NULL;
952 p_sys->i_pts = p_aout_buf->start_date -
953 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
954 (mtime_t)p_enc->fmt_in.audio.i_rate;
956 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
958 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
960 /* convert samples to float and uninterleave */
961 for( i = 0; i < p_sys->i_channels; i++ )
963 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
965 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
966 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
970 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
972 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
976 vorbis_analysis( &p_sys->vb, NULL );
977 vorbis_bitrate_addblock( &p_sys->vb );
979 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
982 p_block = block_New( p_enc, oggpacket.bytes );
983 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
985 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
987 if( i_block_size < 0 ) i_block_size = 0;
988 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
989 p_sys->i_last_block_size = i_block_size;
991 p_block->i_length = (mtime_t)1000000 *
992 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
994 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
996 p_sys->i_samples_delay -= i_samples;
999 p_sys->i_pts += p_block->i_length;
1000 block_ChainAppend( &p_chain, p_block );
1007 /*****************************************************************************
1008 * CloseEncoder: vorbis encoder destruction
1009 *****************************************************************************/
1010 static void CloseEncoder( vlc_object_t *p_this )
1012 encoder_t *p_enc = (encoder_t *)p_this;
1013 encoder_sys_t *p_sys = p_enc->p_sys;
1015 vorbis_block_clear( &p_sys->vb );
1016 vorbis_dsp_clear( &p_sys->vd );
1017 vorbis_comment_clear( &p_sys->vc );
1018 vorbis_info_clear( &p_sys->vi ); /* must be called last */
1023 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */