Removed unused variable (avformat)
[vlc.git] / modules / codec / vorbis.c
blob6b42f9882c63ea96f002a9a0471e252d282bddae
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
8 * $Id$
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 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_aout.h>
38 #include <vlc_input.h>
39 #include <vlc_sout.h>
40 #include "../demux/xiph.h"
42 #include <ogg/ogg.h>
44 #ifdef MODULE_NAME_IS_tremor
45 #include <tremor/ivorbiscodec.h>
47 #else
48 #include <vorbis/vorbisenc.h>
50 # ifndef OV_ECTL_RATEMANAGE_AVG
51 # define OV_ECTL_RATEMANAGE_AVG 0x0
52 # endif
54 #endif
56 /*****************************************************************************
57 * decoder_sys_t : vorbis decoder descriptor
58 *****************************************************************************/
59 struct decoder_sys_t
61 /* Module mode */
62 bool b_packetizer;
64 bool b_has_headers;
67 * Vorbis properties
69 vorbis_info vi; /* struct that stores all the static vorbis bitstream
70 settings */
71 vorbis_comment vc; /* struct that stores all the bitstream user
72 * comments */
73 vorbis_dsp_state vd; /* central working state for the packet->PCM
74 * decoder */
75 vorbis_block vb; /* local working space for packet->PCM decode */
78 * Common properties
80 date_t end_date;
81 int i_last_block_size;
84 ** Channel reordering
86 int pi_chan_table[AOUT_CHAN_MAX];
89 static const int pi_channels_maps[9] =
92 AOUT_CHAN_CENTER,
93 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
94 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
95 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
96 | AOUT_CHAN_REARRIGHT,
97 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
98 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
99 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
100 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
101 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
102 | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
103 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
104 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
105 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
106 | AOUT_CHAN_LFE,
110 ** channel order as defined in http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
113 /* recommended vorbis channel order for 8 channels */
114 static const uint32_t pi_8channels_in[] =
115 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
116 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
117 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
119 /* recommended vorbis channel order for 7 channels */
120 static const uint32_t pi_7channels_in[] =
121 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
122 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
123 AOUT_CHAN_REARCENTER,AOUT_CHAN_LFE,0 };
125 /* recommended vorbis channel order for 6 channels */
126 static const uint32_t pi_6channels_in[] =
127 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
128 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
130 /* recommended vorbis channel order for 4 channels */
131 static const uint32_t pi_4channels_in[] =
132 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
134 /* recommended vorbis channel order for 3 channels */
135 static const uint32_t pi_3channels_in[] =
136 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
138 /****************************************************************************
139 * Local prototypes
140 ****************************************************************************/
141 static int OpenDecoder ( vlc_object_t * );
142 static int OpenPacketizer( vlc_object_t * );
143 static void CloseDecoder ( vlc_object_t * );
144 static block_t *DecodeBlock ( decoder_t *, block_t ** );
146 static int ProcessHeaders( decoder_t * );
147 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
149 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
150 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
152 static void ParseVorbisComments( decoder_t * );
154 static void ConfigureChannelOrder(int *, int, uint32_t, bool );
156 #ifdef MODULE_NAME_IS_tremor
157 static void Interleave ( int32_t *, const int32_t **, int, int, int * );
158 #else
159 static void Interleave ( float *, const float **, int, int, int * );
160 #endif
162 #ifndef MODULE_NAME_IS_tremor
163 static int OpenEncoder ( vlc_object_t * );
164 static void CloseEncoder ( vlc_object_t * );
165 static block_t *Encode ( encoder_t *, aout_buffer_t * );
166 #endif
168 /*****************************************************************************
169 * Module descriptor
170 *****************************************************************************/
171 #define ENC_QUALITY_TEXT N_("Encoding quality")
172 #define ENC_QUALITY_LONGTEXT N_( \
173 "Enforce a quality between 1 (low) and 10 (high), instead " \
174 "of specifying a particular bitrate. This will produce a VBR stream." )
175 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
176 #define ENC_MAXBR_LONGTEXT N_( \
177 "Maximum bitrate in kbps. This is useful for streaming applications." )
178 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
179 #define ENC_MINBR_LONGTEXT N_( \
180 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
181 #define ENC_CBR_TEXT N_("CBR encoding")
182 #define ENC_CBR_LONGTEXT N_( \
183 "Force a constant bitrate encoding (CBR)." )
185 vlc_module_begin ()
186 set_shortname( "Vorbis" )
187 set_description( N_("Vorbis audio decoder") )
188 #ifdef MODULE_NAME_IS_tremor
189 set_capability( "decoder", 90 )
190 #else
191 set_capability( "decoder", 100 )
192 #endif
193 set_category( CAT_INPUT )
194 set_subcategory( SUBCAT_INPUT_ACODEC )
195 set_callbacks( OpenDecoder, CloseDecoder )
197 add_submodule ()
198 set_description( N_("Vorbis audio packetizer") )
199 set_capability( "packetizer", 100 )
200 set_callbacks( OpenPacketizer, CloseDecoder )
202 #ifndef MODULE_NAME_IS_tremor
203 # define ENC_CFG_PREFIX "sout-vorbis-"
204 add_submodule ()
205 set_description( N_("Vorbis audio encoder") )
206 set_capability( "encoder", 130 )
207 set_callbacks( OpenEncoder, CloseEncoder )
209 add_integer( ENC_CFG_PREFIX "quality", 0, ENC_QUALITY_TEXT,
210 ENC_QUALITY_LONGTEXT, false )
211 change_integer_range( 0, 10 )
212 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, ENC_MAXBR_TEXT,
213 ENC_MAXBR_LONGTEXT, false )
214 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, ENC_MINBR_TEXT,
215 ENC_MINBR_LONGTEXT, false )
216 add_bool( ENC_CFG_PREFIX "cbr", false, ENC_CBR_TEXT,
217 ENC_CBR_LONGTEXT, false )
218 #endif
220 vlc_module_end ()
222 #ifndef MODULE_NAME_IS_tremor
223 static const char *const ppsz_enc_options[] = {
224 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
226 #endif
228 /*****************************************************************************
229 * OpenDecoder: probe the decoder and return score
230 *****************************************************************************/
231 static int OpenDecoder( vlc_object_t *p_this )
233 decoder_t *p_dec = (decoder_t*)p_this;
234 decoder_sys_t *p_sys;
236 if( p_dec->fmt_in.i_codec != VLC_CODEC_VORBIS )
238 return VLC_EGENERIC;
241 /* Allocate the memory needed to store the decoder's structure */
242 if( ( p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) ) ) == NULL )
243 return VLC_ENOMEM;
245 /* Misc init */
246 date_Set( &p_sys->end_date, 0 );
247 p_sys->i_last_block_size = 0;
248 p_sys->b_packetizer = false;
249 p_sys->b_has_headers = false;
251 /* Take care of vorbis init */
252 vorbis_info_init( &p_sys->vi );
253 vorbis_comment_init( &p_sys->vc );
255 /* Set output properties */
256 p_dec->fmt_out.i_cat = AUDIO_ES;
257 #ifdef MODULE_NAME_IS_tremor
258 p_dec->fmt_out.i_codec = VLC_CODEC_FI32;
259 #else
260 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
261 #endif
263 /* Set callbacks */
264 p_dec->pf_decode_audio = DecodeBlock;
265 p_dec->pf_packetize = DecodeBlock;
267 return VLC_SUCCESS;
270 static int OpenPacketizer( vlc_object_t *p_this )
272 decoder_t *p_dec = (decoder_t*)p_this;
274 int i_ret = OpenDecoder( p_this );
276 if( i_ret == VLC_SUCCESS )
278 p_dec->p_sys->b_packetizer = true;
279 p_dec->fmt_out.i_codec = VLC_CODEC_VORBIS;
282 return i_ret;
285 /****************************************************************************
286 * DecodeBlock: the whole thing
287 ****************************************************************************
288 * This function must be fed with ogg packets.
289 ****************************************************************************/
290 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
292 decoder_sys_t *p_sys = p_dec->p_sys;
293 ogg_packet oggpacket;
295 if( !pp_block ) return NULL;
297 if( *pp_block )
299 /* Block to Ogg packet */
300 oggpacket.packet = (*pp_block)->p_buffer;
301 oggpacket.bytes = (*pp_block)->i_buffer;
303 else
305 if( p_sys->b_packetizer ) return NULL;
307 /* Block to Ogg packet */
308 oggpacket.packet = NULL;
309 oggpacket.bytes = 0;
312 oggpacket.granulepos = -1;
313 oggpacket.b_o_s = 0;
314 oggpacket.e_o_s = 0;
315 oggpacket.packetno = 0;
317 /* Check for headers */
318 if( !p_sys->b_has_headers )
320 if( ProcessHeaders( p_dec ) )
322 block_Release( *pp_block );
323 return NULL;
325 p_sys->b_has_headers = true;
328 return ProcessPacket( p_dec, &oggpacket, pp_block );
331 /*****************************************************************************
332 * ProcessHeaders: process Vorbis headers.
333 *****************************************************************************/
334 static int ProcessHeaders( decoder_t *p_dec )
336 decoder_sys_t *p_sys = p_dec->p_sys;
337 ogg_packet oggpacket;
339 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
340 void *pp_data[XIPH_MAX_HEADER_COUNT];
341 unsigned i_count;
342 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
343 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
344 return VLC_EGENERIC;
345 if( i_count < 3 )
346 goto error;
348 oggpacket.granulepos = -1;
349 oggpacket.e_o_s = 0;
350 oggpacket.packetno = 0;
352 /* Take care of the initial Vorbis header */
353 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
354 oggpacket.bytes = pi_size[0];
355 oggpacket.packet = pp_data[0];
356 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
358 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
359 goto error;
362 /* Setup the format */
363 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
364 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
366 if( p_dec->fmt_out.audio.i_channels > 9 )
368 msg_Err( p_dec, "invalid number of channels (not between 1 and 9): %i",
369 p_dec->fmt_out.audio.i_channels );
370 goto error;
373 p_dec->fmt_out.audio.i_physical_channels =
374 p_dec->fmt_out.audio.i_original_channels =
375 pi_channels_maps[p_sys->vi.channels];
376 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
378 date_Init( &p_sys->end_date, p_sys->vi.rate, 1 );
380 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
381 p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
383 /* The next packet in order is the comments header */
384 oggpacket.b_o_s = 0;
385 oggpacket.bytes = pi_size[1];
386 oggpacket.packet = pp_data[1];
387 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
389 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
390 goto error;
392 ParseVorbisComments( p_dec );
394 /* The next packet in order is the codebooks header
395 * We need to watch out that this packet is not missing as a
396 * missing or corrupted header is fatal. */
397 oggpacket.b_o_s = 0;
398 oggpacket.bytes = pi_size[2];
399 oggpacket.packet = pp_data[2];
400 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
402 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
403 return VLC_EGENERIC;
406 if( !p_sys->b_packetizer )
408 /* Initialize the Vorbis packet->PCM decoder */
409 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
410 vorbis_block_init( &p_sys->vd, &p_sys->vb );
412 else
414 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
415 p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
416 p_dec->fmt_out.i_extra );
417 memcpy( p_dec->fmt_out.p_extra,
418 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
421 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
422 p_dec->fmt_out.audio.i_physical_channels, true);
424 for( unsigned i = 0; i < i_count; i++ )
425 free( pp_data[i] );
426 return VLC_SUCCESS;
428 error:
429 for( unsigned i = 0; i < i_count; i++ )
430 free( pp_data[i] );
431 return VLC_EGENERIC;
434 /*****************************************************************************
435 * ProcessPacket: processes a Vorbis packet.
436 *****************************************************************************/
437 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
438 block_t **pp_block )
440 decoder_sys_t *p_sys = p_dec->p_sys;
441 block_t *p_block = *pp_block;
443 /* Date management */
444 if( p_block && p_block->i_pts > VLC_TS_INVALID &&
445 p_block->i_pts != date_Get( &p_sys->end_date ) )
447 date_Set( &p_sys->end_date, p_block->i_pts );
450 if( !date_Get( &p_sys->end_date ) )
452 /* We've just started the stream, wait for the first PTS. */
453 if( p_block ) block_Release( p_block );
454 return NULL;
457 *pp_block = NULL; /* To avoid being fed the same packet again */
459 if( p_sys->b_packetizer )
461 return SendPacket( p_dec, p_oggpacket, p_block );
463 else
465 aout_buffer_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
466 if( p_block )
467 block_Release( p_block );
468 return p_aout_buffer;
472 /*****************************************************************************
473 * DecodePacket: decodes a Vorbis packet.
474 *****************************************************************************/
475 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
477 decoder_sys_t *p_sys = p_dec->p_sys;
478 int i_samples;
480 #ifdef MODULE_NAME_IS_tremor
481 int32_t **pp_pcm;
482 #else
483 float **pp_pcm;
484 #endif
486 if( p_oggpacket->bytes &&
487 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
488 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
490 /* **pp_pcm is a multichannel float vector. In stereo, for
491 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
492 * the size of each channel. Convert the float values
493 * (-1.<=range<=1.) to whatever PCM format and write it out */
495 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
498 aout_buffer_t *p_aout_buffer;
500 p_aout_buffer =
501 decoder_NewAudioBuffer( p_dec, i_samples );
503 if( p_aout_buffer == NULL ) return NULL;
505 /* Interleave the samples */
506 #ifdef MODULE_NAME_IS_tremor
507 Interleave( (int32_t *)p_aout_buffer->p_buffer,
508 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
509 #else
510 Interleave( (float *)p_aout_buffer->p_buffer,
511 (const float **)pp_pcm, p_sys->vi.channels, i_samples, p_sys->pi_chan_table);
512 #endif
514 /* Tell libvorbis how many samples we actually consumed */
515 vorbis_synthesis_read( &p_sys->vd, i_samples );
517 /* Date management */
518 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
519 p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
520 i_samples ) - p_aout_buffer->i_pts;
521 return p_aout_buffer;
523 else
525 return NULL;
529 /*****************************************************************************
530 * SendPacket: send an ogg dated packet to the stream output.
531 *****************************************************************************/
532 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
533 block_t *p_block )
535 decoder_sys_t *p_sys = p_dec->p_sys;
536 int i_block_size, i_samples;
538 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
539 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
540 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
541 p_sys->i_last_block_size = i_block_size;
543 /* Date management */
544 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->end_date );
546 p_block->i_length = date_Increment( &p_sys->end_date, i_samples ) - p_block->i_pts;
548 return p_block;
551 /*****************************************************************************
552 * ParseVorbisComments
553 *****************************************************************************/
554 static void ParseVorbisComments( decoder_t *p_dec )
556 char *psz_name, *psz_value, *psz_comment;
557 int i = 0;
559 while( i < p_dec->p_sys->vc.comments )
561 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
562 if( !psz_comment )
563 break;
564 psz_name = psz_comment;
565 psz_value = strchr( psz_comment, '=' );
566 if( psz_value )
568 *psz_value = '\0';
569 psz_value++;
571 /* Don't add empty values */
572 if( *psz_value == '\0' )
573 break;
575 if( !p_dec->p_description )
576 p_dec->p_description = vlc_meta_New();
577 if( p_dec->p_description )
578 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
580 if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
581 !strcasecmp( psz_name, "RG_RADIO" ) )
583 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
585 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
586 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
588 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
589 !strcasecmp( psz_name, "RG_PEAK" ) )
591 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
593 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
594 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = atof( psz_value );
596 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
597 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
599 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
601 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
602 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
604 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
606 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
608 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
609 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = atof( psz_value );
612 free( psz_comment );
613 i++;
617 /*****************************************************************************
618 * Interleave: helper function to interleave channels
619 *****************************************************************************/
620 static void ConfigureChannelOrder(int *pi_chan_table, int i_channels, uint32_t i_channel_mask, bool b_decode)
622 const uint32_t *pi_channels_in;
623 switch( i_channels )
625 case 8:
626 pi_channels_in = pi_8channels_in;
627 break;
628 case 7:
629 pi_channels_in = pi_7channels_in;
630 break;
631 case 6:
632 case 5:
633 pi_channels_in = pi_6channels_in;
634 break;
635 case 4:
636 pi_channels_in = pi_4channels_in;
637 break;
638 case 3:
639 pi_channels_in = pi_3channels_in;
640 break;
641 default:
643 int i;
644 for( i = 0; i< i_channels; ++i )
646 pi_chan_table[i] = i;
648 return;
652 if( b_decode )
653 aout_CheckChannelReorder( pi_channels_in, NULL,
654 i_channel_mask & AOUT_CHAN_PHYSMASK,
655 i_channels,
656 pi_chan_table );
657 else
658 aout_CheckChannelReorder( NULL, pi_channels_in,
659 i_channel_mask & AOUT_CHAN_PHYSMASK,
660 i_channels,
661 pi_chan_table );
664 /*****************************************************************************
665 * Interleave: helper function to interleave channels
666 *****************************************************************************/
667 #ifdef MODULE_NAME_IS_tremor
668 static void Interleave( int32_t *p_out, const int32_t **pp_in,
669 int i_nb_channels, int i_samples, int *pi_chan_table)
671 int i, j;
673 for ( j = 0; j < i_samples; j++ )
674 for ( i = 0; i < i_nb_channels; i++ )
675 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j] * (FIXED32_ONE >> 24);
677 #else
678 static void Interleave( float *p_out, const float **pp_in,
679 int i_nb_channels, int i_samples, int *pi_chan_table )
681 int i, j;
683 for ( j = 0; j < i_samples; j++ )
684 for ( i = 0; i < i_nb_channels; i++ )
685 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
687 #endif
689 /*****************************************************************************
690 * CloseDecoder: vorbis decoder destruction
691 *****************************************************************************/
692 static void CloseDecoder( vlc_object_t *p_this )
694 decoder_t *p_dec = (decoder_t *)p_this;
695 decoder_sys_t *p_sys = p_dec->p_sys;
697 if( !p_sys->b_packetizer && p_sys->b_has_headers )
699 vorbis_block_clear( &p_sys->vb );
700 vorbis_dsp_clear( &p_sys->vd );
703 vorbis_comment_clear( &p_sys->vc );
704 vorbis_info_clear( &p_sys->vi ); /* must be called last */
706 free( p_sys );
709 #ifndef MODULE_NAME_IS_tremor
711 /*****************************************************************************
712 * encoder_sys_t : vorbis encoder descriptor
713 *****************************************************************************/
714 struct encoder_sys_t
717 * Vorbis properties
719 vorbis_info vi; /* struct that stores all the static vorbis bitstream
720 settings */
721 vorbis_comment vc; /* struct that stores all the bitstream user
722 * comments */
723 vorbis_dsp_state vd; /* central working state for the packet->PCM
724 * decoder */
725 vorbis_block vb; /* local working space for packet->PCM decode */
727 int i_last_block_size;
728 int i_samples_delay;
729 int i_channels;
732 ** Channel reordering
734 int pi_chan_table[AOUT_CHAN_MAX];
738 /*****************************************************************************
739 * OpenEncoder: probe the encoder and return score
740 *****************************************************************************/
741 static int OpenEncoder( vlc_object_t *p_this )
743 encoder_t *p_enc = (encoder_t *)p_this;
744 encoder_sys_t *p_sys;
745 int i_quality, i_min_bitrate, i_max_bitrate;
746 ogg_packet header[3];
748 if( p_enc->fmt_out.i_codec != VLC_CODEC_VORBIS &&
749 !p_enc->b_force )
751 return VLC_EGENERIC;
754 /* Allocate the memory needed to store the decoder's structure */
755 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
756 return VLC_ENOMEM;
757 p_enc->p_sys = p_sys;
759 p_enc->pf_encode_audio = Encode;
760 p_enc->fmt_in.i_codec = VLC_CODEC_FL32;
761 p_enc->fmt_out.i_codec = VLC_CODEC_VORBIS;
763 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
765 i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
766 if( i_quality > 10 ) i_quality = 10;
767 if( i_quality < 0 ) i_quality = 0;
769 if( var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ) i_quality = 0;
770 i_max_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
771 i_min_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "min-bitrate" );
773 /* Initialize vorbis encoder */
774 vorbis_info_init( &p_sys->vi );
776 if( i_quality > 0 )
778 /* VBR mode */
779 if( vorbis_encode_setup_vbr( &p_sys->vi,
780 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
781 i_quality * 0.1 ) )
783 vorbis_info_clear( &p_sys->vi );
784 free( p_enc->p_sys );
785 msg_Err( p_enc, "VBR mode initialisation failed" );
786 return VLC_EGENERIC;
789 /* Do we have optional hard quality restrictions? */
790 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
792 struct ovectl_ratemanage_arg ai;
793 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
795 ai.bitrate_hard_min = i_min_bitrate;
796 ai.bitrate_hard_max = i_max_bitrate;
797 ai.management_active = 1;
799 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
802 else
804 /* Turn off management entirely */
805 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
808 else
810 if( vorbis_encode_setup_managed( &p_sys->vi,
811 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
812 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
813 p_enc->fmt_out.i_bitrate,
814 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
816 vorbis_info_clear( &p_sys->vi );
817 msg_Err( p_enc, "CBR mode initialisation failed" );
818 free( p_enc->p_sys );
819 return VLC_EGENERIC;
823 vorbis_encode_setup_init( &p_sys->vi );
825 /* Add a comment */
826 vorbis_comment_init( &p_sys->vc);
827 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
829 /* Set up the analysis state and auxiliary encoding storage */
830 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
831 vorbis_block_init( &p_sys->vd, &p_sys->vb );
833 /* Create and store headers */
834 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
835 &header[0], &header[1], &header[2]);
836 for( int i = 0; i < 3; i++ )
838 if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
839 header[i].bytes, header[i].packet ) )
841 p_enc->fmt_out.i_extra = 0;
842 p_enc->fmt_out.p_extra = NULL;
846 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
847 p_sys->i_last_block_size = 0;
848 p_sys->i_samples_delay = 0;
850 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
851 p_enc->fmt_in.audio.i_physical_channels, true);
853 return VLC_SUCCESS;
856 /****************************************************************************
857 * Encode: the whole thing
858 ****************************************************************************
859 * This function spits out ogg packets.
860 ****************************************************************************/
861 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
863 encoder_sys_t *p_sys = p_enc->p_sys;
864 ogg_packet oggpacket;
865 block_t *p_block, *p_chain = NULL;
866 float **buffer;
867 int i;
868 unsigned int j;
870 mtime_t i_pts = p_aout_buf->i_pts -
871 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
872 (mtime_t)p_enc->fmt_in.audio.i_rate;
874 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
876 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
878 /* convert samples to float and uninterleave */
879 for( i = 0; i < p_sys->i_channels; i++ )
881 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
883 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
884 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
888 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
890 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
892 int i_samples;
894 vorbis_analysis( &p_sys->vb, NULL );
895 vorbis_bitrate_addblock( &p_sys->vb );
897 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
899 int i_block_size;
900 p_block = block_New( p_enc, oggpacket.bytes );
901 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
903 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
905 if( i_block_size < 0 ) i_block_size = 0;
906 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
907 p_sys->i_last_block_size = i_block_size;
909 p_block->i_length = (mtime_t)1000000 *
910 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
912 p_block->i_dts = p_block->i_pts = i_pts;
914 p_sys->i_samples_delay -= i_samples;
916 /* Update pts */
917 i_pts += p_block->i_length;
918 block_ChainAppend( &p_chain, p_block );
922 return p_chain;
925 /*****************************************************************************
926 * CloseEncoder: vorbis encoder destruction
927 *****************************************************************************/
928 static void CloseEncoder( vlc_object_t *p_this )
930 encoder_t *p_enc = (encoder_t *)p_this;
931 encoder_sys_t *p_sys = p_enc->p_sys;
933 vorbis_block_clear( &p_sys->vb );
934 vorbis_dsp_clear( &p_sys->vd );
935 vorbis_comment_clear( &p_sys->vc );
936 vorbis_info_clear( &p_sys->vi ); /* must be called last */
938 free( p_sys );
941 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */