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 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_codec.h>
40 #include <vlc_input.h>
42 #include <vlc_memory.h>
46 #ifdef MODULE_NAME_IS_tremor
47 #include <tremor/ivorbiscodec.h>
50 #include <vorbis/vorbisenc.h>
52 # ifndef OV_ECTL_RATEMANAGE_AVG
53 # define OV_ECTL_RATEMANAGE_AVG 0x0
58 /*****************************************************************************
59 * decoder_sys_t : vorbis decoder descriptor
60 *****************************************************************************/
74 vorbis_info vi; /* struct that stores all the static vorbis bitstream
76 vorbis_comment vc; /* struct that stores all the bitstream user
78 vorbis_dsp_state vd; /* central working state for the packet->PCM
80 vorbis_block vb; /* local working space for packet->PCM decode */
86 int i_last_block_size;
91 int pi_chan_table[AOUT_CHAN_MAX];
94 static const int pi_channels_maps[9] =
98 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
99 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
100 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
101 | AOUT_CHAN_REARRIGHT,
102 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
103 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
104 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
105 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
106 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
107 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
108 | AOUT_CHAN_MIDDLERIGHT,
109 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
110 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
115 ** channel order as defined in http://www.ogghelp.com/ogg/glossary.cfm#Audio_Channels
118 /* recommended vorbis channel order for 6 channels */
119 static const uint32_t pi_6channels_in[] =
120 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
121 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
123 /* recommended vorbis channel order for 4 channels */
124 static const uint32_t pi_4channels_in[] =
125 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
127 /* recommended vorbis channel order for 3 channels */
128 static const uint32_t pi_3channels_in[] =
129 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
131 /****************************************************************************
133 ****************************************************************************/
134 static int OpenDecoder ( vlc_object_t * );
135 static int OpenPacketizer( vlc_object_t * );
136 static void CloseDecoder ( vlc_object_t * );
137 static void *DecodeBlock ( decoder_t *, block_t ** );
139 static int ProcessHeaders( decoder_t * );
140 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
142 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
143 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
145 static void ParseVorbisComments( decoder_t * );
147 static void ConfigureChannelOrder(int *, int, uint32_t, bool );
149 #ifdef MODULE_NAME_IS_tremor
150 static void Interleave ( int32_t *, const int32_t **, int, int, int * );
152 static void Interleave ( float *, const float **, int, int, int * );
155 #ifndef MODULE_NAME_IS_tremor
156 static int OpenEncoder ( vlc_object_t * );
157 static void CloseEncoder ( vlc_object_t * );
158 static block_t *Encode ( encoder_t *, aout_buffer_t * );
161 /*****************************************************************************
163 *****************************************************************************/
164 #define ENC_QUALITY_TEXT N_("Encoding quality")
165 #define ENC_QUALITY_LONGTEXT N_( \
166 "Enforce a quality between 1 (low) and 10 (high), instead " \
167 "of specifying a particular bitrate. This will produce a VBR stream." )
168 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
169 #define ENC_MAXBR_LONGTEXT N_( \
170 "Maximum bitrate in kbps. This is useful for streaming applications." )
171 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
172 #define ENC_MINBR_LONGTEXT N_( \
173 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
174 #define ENC_CBR_TEXT N_("CBR encoding")
175 #define ENC_CBR_LONGTEXT N_( \
176 "Force a constant bitrate encoding (CBR)." )
179 set_shortname( "Vorbis" )
180 set_description( N_("Vorbis audio decoder") )
181 #ifdef MODULE_NAME_IS_tremor
182 set_capability( "decoder", 90 )
184 set_capability( "decoder", 100 )
186 set_category( CAT_INPUT )
187 set_subcategory( SUBCAT_INPUT_ACODEC )
188 set_callbacks( OpenDecoder, CloseDecoder )
191 set_description( N_("Vorbis audio packetizer") )
192 set_capability( "packetizer", 100 )
193 set_callbacks( OpenPacketizer, CloseDecoder )
195 #ifndef MODULE_NAME_IS_tremor
196 # define ENC_CFG_PREFIX "sout-vorbis-"
198 set_description( N_("Vorbis audio encoder") )
199 set_capability( "encoder", 100 )
200 set_callbacks( OpenEncoder, CloseEncoder )
202 add_integer( ENC_CFG_PREFIX "quality", 0, NULL, ENC_QUALITY_TEXT,
203 ENC_QUALITY_LONGTEXT, false )
204 change_integer_range( 0, 10 )
205 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
206 ENC_MAXBR_LONGTEXT, false )
207 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
208 ENC_MINBR_LONGTEXT, false )
209 add_bool( ENC_CFG_PREFIX "cbr", false, NULL, ENC_CBR_TEXT,
210 ENC_CBR_LONGTEXT, false )
215 #ifndef MODULE_NAME_IS_tremor
216 static const char *const ppsz_enc_options[] = {
217 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
221 /*****************************************************************************
222 * OpenDecoder: probe the decoder and return score
223 *****************************************************************************/
224 static int OpenDecoder( vlc_object_t *p_this )
226 decoder_t *p_dec = (decoder_t*)p_this;
227 decoder_sys_t *p_sys;
229 if( p_dec->fmt_in.i_codec != VLC_CODEC_VORBIS )
234 /* Allocate the memory needed to store the decoder's structure */
235 if( ( p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) ) ) == NULL )
239 date_Set( &p_sys->end_date, 0 );
240 p_sys->i_last_block_size = 0;
241 p_sys->b_packetizer = false;
242 p_sys->i_headers = 0;
244 /* Take care of vorbis init */
245 vorbis_info_init( &p_sys->vi );
246 vorbis_comment_init( &p_sys->vc );
248 /* Set output properties */
249 p_dec->fmt_out.i_cat = AUDIO_ES;
250 #ifdef MODULE_NAME_IS_tremor
251 p_dec->fmt_out.i_codec = VLC_CODEC_FI32;
253 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
257 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
259 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
265 static int OpenPacketizer( vlc_object_t *p_this )
267 decoder_t *p_dec = (decoder_t*)p_this;
269 int i_ret = OpenDecoder( p_this );
271 if( i_ret == VLC_SUCCESS )
273 p_dec->p_sys->b_packetizer = true;
274 p_dec->fmt_out.i_codec = VLC_CODEC_VORBIS;
280 /****************************************************************************
281 * DecodeBlock: the whole thing
282 ****************************************************************************
283 * This function must be fed with ogg packets.
284 ****************************************************************************/
285 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
287 decoder_sys_t *p_sys = p_dec->p_sys;
288 ogg_packet oggpacket;
290 if( !pp_block ) return NULL;
294 /* Block to Ogg packet */
295 oggpacket.packet = (*pp_block)->p_buffer;
296 oggpacket.bytes = (*pp_block)->i_buffer;
300 if( p_sys->b_packetizer ) return NULL;
302 /* Block to Ogg packet */
303 oggpacket.packet = NULL;
307 oggpacket.granulepos = -1;
310 oggpacket.packetno = 0;
312 /* Check for headers */
313 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
315 /* Headers already available as extra data */
316 msg_Dbg( p_dec, "headers already available as extra data" );
317 p_sys->i_headers = 3;
319 else if( oggpacket.bytes && p_sys->i_headers < 3 )
321 /* Backup headers as extra data */
324 p_dec->fmt_in.p_extra = realloc_or_free( p_dec->fmt_in.p_extra,
325 p_dec->fmt_in.i_extra + oggpacket.bytes + 2 );
326 assert( p_dec->fmt_in.p_extra );
327 p_extra = (uint8_t *)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
328 *(p_extra++) = oggpacket.bytes >> 8;
329 *(p_extra++) = oggpacket.bytes & 0xFF;
331 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
332 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
334 block_Release( *pp_block );
339 if( p_sys->i_headers == 3 )
341 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
343 p_sys->i_headers = 0;
344 p_dec->fmt_in.i_extra = 0;
345 block_Release( *pp_block );
348 else p_sys->i_headers++;
351 return ProcessPacket( p_dec, &oggpacket, pp_block );
354 /*****************************************************************************
355 * ProcessHeaders: process Vorbis headers.
356 *****************************************************************************/
357 static int ProcessHeaders( decoder_t *p_dec )
359 decoder_sys_t *p_sys = p_dec->p_sys;
360 ogg_packet oggpacket;
364 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
366 oggpacket.granulepos = -1;
367 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
369 oggpacket.packetno = 0;
370 p_extra = p_dec->fmt_in.p_extra;
371 i_extra = p_dec->fmt_in.i_extra;
373 /* Take care of the initial Vorbis header */
374 oggpacket.bytes = *(p_extra++) << 8;
375 oggpacket.bytes |= (*(p_extra++) & 0xFF);
376 oggpacket.packet = p_extra;
377 p_extra += oggpacket.bytes;
378 i_extra -= (oggpacket.bytes + 2);
381 msg_Err( p_dec, "header data corrupted");
385 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
387 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
391 /* Setup the format */
392 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
393 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
395 if( p_dec->fmt_out.audio.i_channels > 9 )
397 msg_Err( p_dec, "invalid number of channels (not between 1 and 9): %i",
398 p_dec->fmt_out.audio.i_channels );
402 p_dec->fmt_out.audio.i_physical_channels =
403 p_dec->fmt_out.audio.i_original_channels =
404 pi_channels_maps[p_sys->vi.channels];
405 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
407 date_Init( &p_sys->end_date, p_sys->vi.rate, 1 );
409 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
410 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
412 /* The next packet in order is the comments header */
414 oggpacket.bytes = *(p_extra++) << 8;
415 oggpacket.bytes |= (*(p_extra++) & 0xFF);
416 oggpacket.packet = p_extra;
417 p_extra += oggpacket.bytes;
418 i_extra -= (oggpacket.bytes + 2);
421 msg_Err( p_dec, "header data corrupted");
425 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
427 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
430 ParseVorbisComments( p_dec );
432 /* The next packet in order is the codebooks header
433 * We need to watch out that this packet is not missing as a
434 * missing or corrupted header is fatal. */
435 oggpacket.bytes = *(p_extra++) << 8;
436 oggpacket.bytes |= (*(p_extra++) & 0xFF);
437 oggpacket.packet = p_extra;
438 i_extra -= (oggpacket.bytes + 2);
441 msg_Err( p_dec, "header data corrupted");
445 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
447 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
451 if( !p_sys->b_packetizer )
453 /* Initialize the Vorbis packet->PCM decoder */
454 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
455 vorbis_block_init( &p_sys->vd, &p_sys->vb );
459 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
460 p_dec->fmt_out.p_extra = realloc_or_free( p_dec->fmt_out.p_extra,
461 p_dec->fmt_out.i_extra );
462 assert( p_dec->fmt_out.p_extra );
463 memcpy( p_dec->fmt_out.p_extra,
464 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
467 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
468 p_dec->fmt_out.audio.i_physical_channels, true);
473 /*****************************************************************************
474 * ProcessPacket: processes a Vorbis packet.
475 *****************************************************************************/
476 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
479 decoder_sys_t *p_sys = p_dec->p_sys;
480 block_t *p_block = *pp_block;
482 /* Date management */
483 if( p_block && p_block->i_pts > 0 &&
484 p_block->i_pts != date_Get( &p_sys->end_date ) )
486 date_Set( &p_sys->end_date, p_block->i_pts );
489 if( !date_Get( &p_sys->end_date ) )
491 /* We've just started the stream, wait for the first PTS. */
492 if( p_block ) block_Release( p_block );
496 *pp_block = NULL; /* To avoid being fed the same packet again */
498 if( p_sys->b_packetizer )
500 return SendPacket( p_dec, p_oggpacket, p_block );
504 aout_buffer_t *p_aout_buffer;
506 if( p_sys->i_headers >= 3 )
507 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
509 p_aout_buffer = NULL;
511 if( p_block ) block_Release( p_block );
512 return p_aout_buffer;
516 /*****************************************************************************
517 * DecodePacket: decodes a Vorbis packet.
518 *****************************************************************************/
519 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
521 decoder_sys_t *p_sys = p_dec->p_sys;
524 #ifdef MODULE_NAME_IS_tremor
530 if( p_oggpacket->bytes &&
531 #ifdef MODULE_NAME_IS_tremor
532 vorbis_synthesis( &p_sys->vb, p_oggpacket, 1 ) == 0 )
534 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
536 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
538 /* **pp_pcm is a multichannel float vector. In stereo, for
539 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
540 * the size of each channel. Convert the float values
541 * (-1.<=range<=1.) to whatever PCM format and write it out */
543 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
546 aout_buffer_t *p_aout_buffer;
549 decoder_NewAudioBuffer( p_dec, i_samples );
551 if( p_aout_buffer == NULL ) return NULL;
553 /* Interleave the samples */
554 #ifdef MODULE_NAME_IS_tremor
555 Interleave( (int32_t *)p_aout_buffer->p_buffer,
556 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
558 Interleave( (float *)p_aout_buffer->p_buffer,
559 (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
562 /* Tell libvorbis how many samples we actually consumed */
563 vorbis_synthesis_read( &p_sys->vd, i_samples );
565 /* Date management */
566 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
567 p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
568 i_samples ) - p_aout_buffer->i_pts;
569 return p_aout_buffer;
577 /*****************************************************************************
578 * SendPacket: send an ogg dated packet to the stream output.
579 *****************************************************************************/
580 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
583 decoder_sys_t *p_sys = p_dec->p_sys;
584 int i_block_size, i_samples;
586 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
587 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
588 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
589 p_sys->i_last_block_size = i_block_size;
591 /* Date management */
592 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->end_date );
594 if( p_sys->i_headers >= 3 )
595 p_block->i_length = date_Increment( &p_sys->end_date, i_samples ) - p_block->i_pts;
597 p_block->i_length = 0;
602 /*****************************************************************************
603 * ParseVorbisComments
604 *****************************************************************************/
605 static void ParseVorbisComments( decoder_t *p_dec )
607 char *psz_name, *psz_value, *psz_comment;
610 while( i < p_dec->p_sys->vc.comments )
612 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
615 psz_name = psz_comment;
616 psz_value = strchr( psz_comment, '=' );
622 if( !p_dec->p_description )
623 p_dec->p_description = vlc_meta_New();
624 if( p_dec->p_description )
625 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
627 if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
628 !strcasecmp( psz_name, "RG_RADIO" ) )
630 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
632 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
633 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
635 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
636 !strcasecmp( psz_name, "RG_PEAK" ) )
638 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
640 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
641 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
643 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
644 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
646 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
648 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
649 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
651 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
653 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
655 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
656 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
664 /*****************************************************************************
665 * Interleave: helper function to interleave channels
666 *****************************************************************************/
667 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, bool b_decode)
669 const uint32_t *pi_channels_in;
674 pi_channels_in = pi_6channels_in;
677 pi_channels_in = pi_4channels_in;
680 pi_channels_in = pi_3channels_in;
685 for( i = 0; i< i_channels; ++i )
687 pi_chan_table[i] = i;
694 aout_CheckChannelReorder( pi_channels_in, NULL,
695 i_channel_mask & AOUT_CHAN_PHYSMASK,
699 aout_CheckChannelReorder( NULL, pi_channels_in,
700 i_channel_mask & AOUT_CHAN_PHYSMASK,
705 /*****************************************************************************
706 * Interleave: helper function to interleave channels
707 *****************************************************************************/
708 #ifdef MODULE_NAME_IS_tremor
709 static void Interleave( int32_t *p_out, const int32_t **pp_in,
710 int i_nb_channels, int i_samples, int *pi_chan_table)
714 for ( j = 0; j < i_samples; j++ )
715 for ( i = 0; i < i_nb_channels; i++ )
716 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
719 static void Interleave( float *p_out, const float **pp_in,
720 int i_nb_channels, int i_samples, int *pi_chan_table )
724 for ( j = 0; j < i_samples; j++ )
725 for ( i = 0; i < i_nb_channels; i++ )
726 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
730 /*****************************************************************************
731 * CloseDecoder: vorbis decoder destruction
732 *****************************************************************************/
733 static void CloseDecoder( vlc_object_t *p_this )
735 decoder_t *p_dec = (decoder_t *)p_this;
736 decoder_sys_t *p_sys = p_dec->p_sys;
738 if( !p_sys->b_packetizer && p_sys->i_headers > 3 )
740 vorbis_block_clear( &p_sys->vb );
741 vorbis_dsp_clear( &p_sys->vd );
744 vorbis_comment_clear( &p_sys->vc );
745 vorbis_info_clear( &p_sys->vi ); /* must be called last */
750 #ifndef MODULE_NAME_IS_tremor
752 /*****************************************************************************
753 * encoder_sys_t : vorbis encoder descriptor
754 *****************************************************************************/
760 vorbis_info vi; /* struct that stores all the static vorbis bitstream
762 vorbis_comment vc; /* struct that stores all the bitstream user
764 vorbis_dsp_state vd; /* central working state for the packet->PCM
766 vorbis_block vb; /* local working space for packet->PCM decode */
768 int i_last_block_size;
778 ** Channel reordering
780 int pi_chan_table[AOUT_CHAN_MAX];
784 /*****************************************************************************
785 * OpenEncoder: probe the encoder and return score
786 *****************************************************************************/
787 static int OpenEncoder( vlc_object_t *p_this )
789 encoder_t *p_enc = (encoder_t *)p_this;
790 encoder_sys_t *p_sys;
791 int i_quality, i_min_bitrate, i_max_bitrate, i;
792 ogg_packet header[3];
795 if( p_enc->fmt_out.i_codec != VLC_CODEC_VORBIS &&
801 /* Allocate the memory needed to store the decoder's structure */
802 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
804 p_enc->p_sys = p_sys;
806 p_enc->pf_encode_audio = Encode;
807 p_enc->fmt_in.i_codec = VLC_CODEC_FL32;
808 p_enc->fmt_out.i_codec = VLC_CODEC_VORBIS;
810 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
812 i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
813 if( i_quality > 10 ) i_quality = 10;
814 if( i_quality < 0 ) i_quality = 0;
816 if( var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ) i_quality = 0;
817 i_max_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
818 i_min_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "min-bitrate" );
820 /* Initialize vorbis encoder */
821 vorbis_info_init( &p_sys->vi );
826 if( vorbis_encode_setup_vbr( &p_sys->vi,
827 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
830 vorbis_info_clear( &p_sys->vi );
831 free( p_enc->p_sys );
832 msg_Err( p_enc, "VBR mode initialisation failed" );
836 /* Do we have optional hard quality restrictions? */
837 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
839 struct ovectl_ratemanage_arg ai;
840 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
842 ai.bitrate_hard_min = i_min_bitrate;
843 ai.bitrate_hard_max = i_max_bitrate;
844 ai.management_active = 1;
846 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
851 /* Turn off management entirely */
852 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
857 if( vorbis_encode_setup_managed( &p_sys->vi,
858 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
859 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
860 p_enc->fmt_out.i_bitrate,
861 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
863 vorbis_info_clear( &p_sys->vi );
864 msg_Err( p_enc, "CBR mode initialisation failed" );
865 free( p_enc->p_sys );
870 vorbis_encode_setup_init( &p_sys->vi );
873 vorbis_comment_init( &p_sys->vc);
874 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
876 /* Set up the analysis state and auxiliary encoding storage */
877 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
878 vorbis_block_init( &p_sys->vd, &p_sys->vb );
880 /* Create and store headers */
881 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
882 &header[0], &header[1], &header[2]);
883 p_enc->fmt_out.i_extra = 3 * 2 + header[0].bytes +
884 header[1].bytes + header[2].bytes;
885 p_extra = p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
887 for( i = 0; i < 3; i++ )
889 *(p_extra++) = header[i].bytes >> 8;
890 *(p_extra++) = header[i].bytes & 0xFF;
891 memcpy( p_extra, header[i].packet, header[i].bytes );
892 p_extra += header[i].bytes;
895 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
896 p_sys->i_last_block_size = 0;
897 p_sys->i_samples_delay = 0;
900 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
901 p_enc->fmt_in.audio.i_physical_channels, true);
906 /****************************************************************************
907 * Encode: the whole thing
908 ****************************************************************************
909 * This function spits out ogg packets.
910 ****************************************************************************/
911 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
913 encoder_sys_t *p_sys = p_enc->p_sys;
914 ogg_packet oggpacket;
915 block_t *p_block, *p_chain = NULL;
920 p_sys->i_pts = p_aout_buf->i_pts -
921 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
922 (mtime_t)p_enc->fmt_in.audio.i_rate;
924 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
926 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
928 /* convert samples to float and uninterleave */
929 for( i = 0; i < p_sys->i_channels; i++ )
931 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
933 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
934 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
938 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
940 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
944 vorbis_analysis( &p_sys->vb, NULL );
945 vorbis_bitrate_addblock( &p_sys->vb );
947 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
950 p_block = block_New( p_enc, oggpacket.bytes );
951 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
953 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
955 if( i_block_size < 0 ) i_block_size = 0;
956 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
957 p_sys->i_last_block_size = i_block_size;
959 p_block->i_length = (mtime_t)1000000 *
960 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
962 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
964 p_sys->i_samples_delay -= i_samples;
967 p_sys->i_pts += p_block->i_length;
968 block_ChainAppend( &p_chain, p_block );
975 /*****************************************************************************
976 * CloseEncoder: vorbis encoder destruction
977 *****************************************************************************/
978 static void CloseEncoder( vlc_object_t *p_this )
980 encoder_t *p_enc = (encoder_t *)p_this;
981 encoder_sys_t *p_sys = p_enc->p_sys;
983 vorbis_block_clear( &p_sys->vb );
984 vorbis_dsp_clear( &p_sys->vd );
985 vorbis_comment_clear( &p_sys->vc );
986 vorbis_info_clear( &p_sys->vi ); /* must be called last */
991 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */