input: add input_SetProgramId
[vlc.git] / modules / codec / vorbis.c
blob39133e5ff24bef1567a35ec721f308438cfe6458
1 /*****************************************************************************
2 * vorbis.c: vorbis decoder/encoder/packetizer module using of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 2001-2012 VLC authors and VideoLAN
5 * Copyright (C) 2007 Société des arts technologiques
6 * Copyright (C) 2007 Savoir-faire Linux
9 * Authors: Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_charset.h>
37 #include <vlc_aout.h>
38 #include <vlc_input_item.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>
46 # define INTERLEAVE_TYPE int32_t
48 #else
49 # include <vorbis/codec.h>
50 # define INTERLEAVE_TYPE float
52 # ifdef ENABLE_SOUT
53 # define HAVE_VORBIS_ENCODER
54 # include <vorbis/vorbisenc.h>
55 # ifndef OV_ECTL_RATEMANAGE_AVG
56 # define OV_ECTL_RATEMANAGE_AVG 0x0
57 # endif
58 # endif
59 #endif
61 /*****************************************************************************
62 * decoder_sys_t : vorbis decoder descriptor
63 *****************************************************************************/
64 typedef struct
66 /* Module mode */
67 bool b_packetizer;
69 bool b_has_headers;
72 * Vorbis properties
74 vorbis_info vi; /* struct that stores all the static vorbis bitstream
75 settings */
76 vorbis_comment vc; /* struct that stores all the bitstream user
77 * comments */
78 vorbis_dsp_state vd; /* central working state for the packet->PCM
79 * decoder */
80 vorbis_block vb; /* local working space for packet->PCM decode */
83 * Common properties
85 date_t end_date;
86 int i_last_block_size;
89 ** Channel reordering
91 uint8_t pi_chan_table[AOUT_CHAN_MAX];
92 } decoder_sys_t;
94 static const int pi_channels_maps[9] =
97 AOUT_CHAN_CENTER,
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_REARCENTER | AOUT_CHAN_MIDDLELEFT
108 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
109 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
110 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
111 | AOUT_CHAN_LFE,
115 ** channel order as defined in http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
118 /* recommended vorbis channel order for 8 channels */
119 static const uint32_t pi_8channels_in[] =
120 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
121 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
122 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE, 0 };
124 /* recommended vorbis channel order for 7 channels */
125 static const uint32_t pi_7channels_in[] =
126 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
127 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
128 AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE, 0 };
130 /* recommended vorbis channel order for 6 channels */
131 static const uint32_t pi_6channels_in[] =
132 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
133 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_LFE, 0 };
135 /* recommended vorbis channel order for 4 channels */
136 static const uint32_t pi_4channels_in[] =
137 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
139 /* recommended vorbis channel order for 3 channels */
140 static const uint32_t pi_3channels_in[] =
141 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
143 /****************************************************************************
144 * Local prototypes
145 ****************************************************************************/
146 static int OpenDecoder ( vlc_object_t * );
147 static int OpenPacketizer( vlc_object_t * );
148 static void CloseDecoder ( vlc_object_t * );
149 static int DecodeAudio ( decoder_t *, block_t * );
150 static block_t *Packetize ( decoder_t *, block_t ** );
151 static void Flush( decoder_t * );
153 static int ProcessHeaders( decoder_t * );
154 static block_t *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
156 static block_t *DecodePacket( decoder_t *, ogg_packet * );
157 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
159 static void ParseVorbisComments( decoder_t * );
161 static void ConfigureChannelOrder(uint8_t *, int, uint32_t );
163 #ifdef HAVE_VORBIS_ENCODER
164 static int OpenEncoder ( vlc_object_t * );
165 static void CloseEncoder ( vlc_object_t * );
166 static block_t *Encode ( encoder_t *, block_t * );
167 #endif
169 /*****************************************************************************
170 * Module descriptor
171 *****************************************************************************/
172 #define ENC_QUALITY_TEXT N_("Encoding quality")
173 #define ENC_QUALITY_LONGTEXT N_( \
174 "Enforce a quality between 1 (low) and 10 (high), instead " \
175 "of specifying a particular bitrate. This will produce a VBR stream." )
176 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
177 #define ENC_MAXBR_LONGTEXT N_( \
178 "Maximum bitrate in kbps. This is useful for streaming applications." )
179 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
180 #define ENC_MINBR_LONGTEXT N_( \
181 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
182 #define ENC_CBR_TEXT N_("CBR encoding")
183 #define ENC_CBR_LONGTEXT N_( \
184 "Force a constant bitrate encoding (CBR)." )
186 vlc_module_begin ()
187 set_shortname( "Vorbis" )
188 set_description( N_("Vorbis audio decoder") )
189 #ifdef MODULE_NAME_IS_tremor
190 set_capability( "audio decoder", 90 )
191 #else
192 set_capability( "audio decoder", 100 )
193 #endif
194 set_category( CAT_INPUT )
195 set_subcategory( SUBCAT_INPUT_ACODEC )
196 set_callbacks( OpenDecoder, CloseDecoder )
198 add_submodule ()
199 set_description( N_("Vorbis audio packetizer") )
200 set_capability( "packetizer", 100 )
201 set_callbacks( OpenPacketizer, CloseDecoder )
203 #ifdef HAVE_VORBIS_ENCODER
204 # define ENC_CFG_PREFIX "sout-vorbis-"
205 add_submodule ()
206 set_description( N_("Vorbis audio encoder") )
207 set_capability( "encoder", 130 )
208 set_callbacks( OpenEncoder, CloseEncoder )
210 add_integer( ENC_CFG_PREFIX "quality", 0, ENC_QUALITY_TEXT,
211 ENC_QUALITY_LONGTEXT, false )
212 change_integer_range( 0, 10 )
213 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, ENC_MAXBR_TEXT,
214 ENC_MAXBR_LONGTEXT, false )
215 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, ENC_MINBR_TEXT,
216 ENC_MINBR_LONGTEXT, false )
217 add_bool( ENC_CFG_PREFIX "cbr", false, ENC_CBR_TEXT,
218 ENC_CBR_LONGTEXT, false )
219 #endif
221 vlc_module_end ()
223 #ifdef HAVE_VORBIS_ENCODER
224 static const char *const ppsz_enc_options[] = {
225 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
227 #endif
229 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
231 decoder_t *p_dec = (decoder_t*)p_this;
232 decoder_sys_t *p_sys;
234 if( p_dec->fmt_in.i_codec != VLC_CODEC_VORBIS )
235 return VLC_EGENERIC;
237 /* Allocate the memory needed to store the decoder's structure */
238 p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) );
239 if( unlikely( !p_sys ) )
240 return VLC_ENOMEM;
242 /* Misc init */
243 date_Set( &p_sys->end_date, VLC_TICK_INVALID );
244 p_sys->i_last_block_size = 0;
245 p_sys->b_packetizer = b_packetizer;
246 p_sys->b_has_headers = false;
248 /* Take care of vorbis init */
249 vorbis_info_init( &p_sys->vi );
250 vorbis_comment_init( &p_sys->vc );
252 if( b_packetizer )
254 p_dec->fmt_out.i_codec = VLC_CODEC_VORBIS;
255 p_dec->pf_packetize = Packetize;
257 else
259 #ifdef MODULE_NAME_IS_tremor
260 p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
261 #else
262 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
263 #endif
264 p_dec->pf_decode = DecodeAudio;
267 p_dec->pf_flush = Flush;
269 return VLC_SUCCESS;
272 static int OpenDecoder( vlc_object_t *p_this )
274 return OpenCommon( p_this, false );
277 static int OpenPacketizer( vlc_object_t *p_this )
279 return OpenCommon( p_this, true );
282 /****************************************************************************
283 * DecodeBlock: the whole thing
284 ****************************************************************************
285 * This function must be fed with ogg packets.
286 ****************************************************************************/
287 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
289 decoder_sys_t *p_sys = p_dec->p_sys;
290 ogg_packet oggpacket;
292 if( *pp_block )
294 /* Block to Ogg packet */
295 oggpacket.packet = (*pp_block)->p_buffer;
296 oggpacket.bytes = (*pp_block)->i_buffer;
298 else
300 if( p_sys->b_packetizer ) return NULL;
302 /* Block to Ogg packet */
303 oggpacket.packet = NULL;
304 oggpacket.bytes = 0;
307 oggpacket.granulepos = -1;
308 oggpacket.b_o_s = 0;
309 oggpacket.e_o_s = 0;
310 oggpacket.packetno = 0;
312 /* Check for headers */
313 if( !p_sys->b_has_headers )
315 if( ProcessHeaders( p_dec ) )
317 if( *pp_block )
318 block_Release( *pp_block );
319 return NULL;
321 p_sys->b_has_headers = true;
324 return ProcessPacket( p_dec, &oggpacket, pp_block );
327 static int DecodeAudio( decoder_t *p_dec, block_t *p_block )
329 if( p_block == NULL ) /* No Drain */
330 return VLCDEC_SUCCESS;
332 block_t **pp_block = &p_block, *p_out;
333 while( ( p_out = DecodeBlock( p_dec, pp_block ) ) != NULL )
334 decoder_QueueAudio( p_dec, p_out );
335 return VLCDEC_SUCCESS;
338 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
340 if( pp_block == NULL ) /* No Drain */
341 return NULL;
342 return DecodeBlock( p_dec, pp_block );
345 /*****************************************************************************
346 * ProcessHeaders: process Vorbis headers.
347 *****************************************************************************/
348 static int ProcessHeaders( decoder_t *p_dec )
350 decoder_sys_t *p_sys = p_dec->p_sys;
351 ogg_packet oggpacket;
353 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
354 const void *pp_data[XIPH_MAX_HEADER_COUNT];
355 unsigned i_count;
356 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
357 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
358 return VLC_EGENERIC;
359 if( i_count < 3 )
360 return VLC_EGENERIC;
362 oggpacket.granulepos = -1;
363 oggpacket.e_o_s = 0;
364 oggpacket.packetno = 0;
366 /* Take care of the initial Vorbis header */
367 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
368 oggpacket.bytes = pi_size[0];
369 oggpacket.packet = (void *)pp_data[0];
370 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
372 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
373 return VLC_EGENERIC;
376 /* Setup the format */
377 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
378 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
380 if( p_dec->fmt_out.audio.i_channels >= ARRAY_SIZE(pi_channels_maps) )
382 msg_Err( p_dec, "invalid number of channels (1-%zu): %i",
383 ARRAY_SIZE(pi_channels_maps),
384 p_dec->fmt_out.audio.i_channels );
385 return VLC_EGENERIC;
388 p_dec->fmt_out.audio.i_physical_channels =
389 pi_channels_maps[p_sys->vi.channels];
390 p_dec->fmt_out.i_bitrate = __MAX( 0, (int32_t) p_sys->vi.bitrate_nominal );
392 date_Init( &p_sys->end_date, p_sys->vi.rate, 1 );
394 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ud",
395 p_sys->vi.channels, p_sys->vi.rate, p_dec->fmt_out.i_bitrate );
397 /* The next packet in order is the comments header */
398 oggpacket.b_o_s = 0;
399 oggpacket.bytes = pi_size[1];
400 oggpacket.packet = (void *)pp_data[1];
401 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
403 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
404 return VLC_EGENERIC;
406 ParseVorbisComments( p_dec );
408 /* The next packet in order is the codebooks header
409 * We need to watch out that this packet is not missing as a
410 * missing or corrupted header is fatal. */
411 oggpacket.b_o_s = 0;
412 oggpacket.bytes = pi_size[2];
413 oggpacket.packet = (void *)pp_data[2];
414 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
416 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
417 return VLC_EGENERIC;
420 if( !p_sys->b_packetizer )
422 /* Initialize the Vorbis packet->PCM decoder */
423 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
424 vorbis_block_init( &p_sys->vd, &p_sys->vb );
426 else
428 void* p_extra = realloc( p_dec->fmt_out.p_extra,
429 p_dec->fmt_in.i_extra );
430 if( unlikely( p_extra == NULL ) )
432 return VLC_ENOMEM;
434 p_dec->fmt_out.p_extra = p_extra;
435 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
436 memcpy( p_dec->fmt_out.p_extra,
437 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
440 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
441 p_dec->fmt_out.audio.i_physical_channels);
443 return VLC_SUCCESS;
446 /*****************************************************************************
447 * Flush:
448 *****************************************************************************/
449 static void Flush( decoder_t *p_dec )
451 decoder_sys_t *p_sys = p_dec->p_sys;
453 date_Set( &p_sys->end_date, VLC_TICK_INVALID );
456 /*****************************************************************************
457 * ProcessPacket: processes a Vorbis packet.
458 *****************************************************************************/
459 static block_t *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
460 block_t **pp_block )
462 decoder_sys_t *p_sys = p_dec->p_sys;
463 block_t *p_block = *pp_block;
465 *pp_block = NULL; /* To avoid being fed the same packet again */
466 if( !p_block )
467 return NULL;
469 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
471 Flush( p_dec );
472 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
474 block_Release(p_block);
475 return NULL;
479 /* Date management */
480 if( p_block->i_pts != VLC_TICK_INVALID &&
481 p_block->i_pts != date_Get( &p_sys->end_date ) )
483 date_Set( &p_sys->end_date, p_block->i_pts );
486 if( date_Get( &p_sys->end_date ) == VLC_TICK_INVALID )
488 /* We've just started the stream, wait for the first PTS. */
489 if( p_block ) block_Release( p_block );
490 return NULL;
494 if( p_sys->b_packetizer )
496 return SendPacket( p_dec, p_oggpacket, p_block );
498 else
500 block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
501 if( p_block )
502 block_Release( p_block );
503 return p_aout_buffer;
507 /*****************************************************************************
508 * Interleave: helper function to interleave channels
509 *****************************************************************************/
510 static void Interleave( INTERLEAVE_TYPE *p_out, const INTERLEAVE_TYPE **pp_in,
511 int i_nb_channels, int i_samples, uint8_t *pi_chan_table)
513 for( int j = 0; j < i_samples; j++ )
514 for( int i = 0; i < i_nb_channels; i++ )
516 #ifdef MODULE_NAME_IS_tremor
517 union { int32_t i; uint32_t u;} spl;
519 spl.u = ((uint32_t)pp_in[i][j]) << 8;
520 p_out[j * i_nb_channels + pi_chan_table[i]] = spl.i;
521 #else
522 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
523 #endif
527 /*****************************************************************************
528 * DecodePacket: decodes a Vorbis packet.
529 *****************************************************************************/
530 static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
532 decoder_sys_t *p_sys = p_dec->p_sys;
533 int i_samples;
535 INTERLEAVE_TYPE **pp_pcm;
537 if( p_oggpacket->bytes &&
538 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
539 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
541 /* **pp_pcm is a multichannel float vector. In stereo, for
542 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
543 * the size of each channel. Convert the float values
544 * (-1.<=range<=1.) to whatever PCM format and write it out */
546 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
549 block_t *p_aout_buffer;
551 if( decoder_UpdateAudioFormat( p_dec ) ) return NULL;
552 p_aout_buffer =
553 decoder_NewAudioBuffer( p_dec, i_samples );
555 if( p_aout_buffer == NULL ) return NULL;
557 /* Interleave the samples */
558 Interleave( (INTERLEAVE_TYPE*)p_aout_buffer->p_buffer,
559 (const INTERLEAVE_TYPE**)pp_pcm, p_sys->vi.channels, i_samples,
560 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;
571 else
573 return NULL;
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,
581 block_t *p_block )
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 p_block->i_length = date_Increment( &p_sys->end_date, i_samples ) - p_block->i_pts;
596 return p_block;
599 /*****************************************************************************
600 * ParseVorbisComments
601 *****************************************************************************/
602 static void ParseVorbisComments( decoder_t *p_dec )
604 char *psz_name, *psz_value, *psz_comment;
605 int i = 0;
607 decoder_sys_t *p_sys = p_dec->p_sys;
609 while( i < p_sys->vc.comments )
611 psz_comment = strdup( p_sys->vc.user_comments[i] );
612 if( !psz_comment )
613 break;
614 psz_name = psz_comment;
615 psz_value = strchr( psz_comment, '=' );
616 /* Don't add empty values */
617 if( psz_value && psz_value[1] != '\0')
619 *psz_value = '\0';
620 psz_value++;
622 if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
623 !strcasecmp( psz_name, "RG_RADIO" ) )
625 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
627 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
628 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = us_atof( psz_value );
630 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
631 !strcasecmp( psz_name, "RG_PEAK" ) )
633 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
635 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
636 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = us_atof( psz_value );
638 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
639 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
641 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
643 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
644 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( psz_value );
646 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
648 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
650 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
651 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( psz_value );
653 else if( !strcasecmp( psz_name, "METADATA_BLOCK_PICTURE" ) )
654 { /* Do nothing, for now */ }
655 else
657 if( !p_dec->p_description )
658 p_dec->p_description = vlc_meta_New();
659 if( p_dec->p_description )
660 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
664 free( psz_comment );
665 i++;
669 /*****************************************************************************
671 *****************************************************************************/
672 static void ConfigureChannelOrder(uint8_t *pi_chan_table, int i_channels,
673 uint32_t i_channel_mask)
675 const uint32_t *pi_channels_in;
676 switch( i_channels )
678 case 8:
679 pi_channels_in = pi_8channels_in;
680 break;
681 case 7:
682 pi_channels_in = pi_7channels_in;
683 break;
684 case 6:
685 case 5:
686 pi_channels_in = pi_6channels_in;
687 break;
688 case 4:
689 pi_channels_in = pi_4channels_in;
690 break;
691 case 3:
692 pi_channels_in = pi_3channels_in;
693 break;
694 default:
695 for( int i = 0; i< i_channels; ++i )
696 pi_chan_table[i] = i;
698 return;
701 aout_CheckChannelReorder( pi_channels_in, NULL,
702 i_channel_mask, pi_chan_table );
705 /*****************************************************************************
706 * CloseDecoder: vorbis decoder destruction
707 *****************************************************************************/
708 static void CloseDecoder( vlc_object_t *p_this )
710 decoder_t *p_dec = (decoder_t *)p_this;
711 decoder_sys_t *p_sys = p_dec->p_sys;
713 if( !p_sys->b_packetizer && p_sys->b_has_headers )
715 vorbis_block_clear( &p_sys->vb );
716 vorbis_dsp_clear( &p_sys->vd );
719 vorbis_comment_clear( &p_sys->vc );
720 vorbis_info_clear( &p_sys->vi ); /* must be called last */
722 free( p_sys );
725 #ifdef HAVE_VORBIS_ENCODER
726 /*****************************************************************************
727 * encoder_sys_t : vorbis encoder descriptor
728 *****************************************************************************/
729 typedef struct
732 * Vorbis properties
734 vorbis_info vi; /* struct that stores all the static vorbis bitstream
735 settings */
736 vorbis_comment vc; /* struct that stores all the bitstream user
737 * comments */
738 vorbis_dsp_state vd; /* central working state for the packet->PCM
739 * decoder */
740 vorbis_block vb; /* local working space for packet->PCM decode */
742 int i_last_block_size;
743 int i_samples_delay;
746 ** Channel reordering
748 uint8_t pi_chan_table[AOUT_CHAN_MAX];
750 } encoder_sys_t;
752 /*****************************************************************************
753 * OpenEncoder: probe the encoder and return score
754 *****************************************************************************/
755 static int OpenEncoder( vlc_object_t *p_this )
757 encoder_t *p_enc = (encoder_t *)p_this;
758 encoder_sys_t *p_sys;
759 int i_quality, i_min_bitrate, i_max_bitrate;
760 ogg_packet header[3];
762 if( p_enc->fmt_out.i_codec != VLC_CODEC_VORBIS &&
763 !p_enc->obj.force )
765 return VLC_EGENERIC;
768 /* Allocate the memory needed to store the decoder's structure */
769 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
770 return VLC_ENOMEM;
771 p_enc->p_sys = p_sys;
773 p_enc->pf_encode_audio = Encode;
774 p_enc->fmt_in.i_codec = VLC_CODEC_FL32;
775 p_enc->fmt_out.i_codec = VLC_CODEC_VORBIS;
777 if( p_enc->fmt_in.audio.i_channels >= ARRAY_SIZE(pi_channels_maps) )
779 p_enc->fmt_in.audio.i_channels = ARRAY_SIZE(pi_channels_maps) - 1;
780 msg_Warn( p_enc, "lowering channel count to %u", p_enc->fmt_in.audio.i_channels );
782 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
784 i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
785 if( i_quality > 10 ) i_quality = 10;
786 if( i_quality < 0 ) i_quality = 0;
788 if( var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ) i_quality = 0;
789 i_max_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
790 i_min_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "min-bitrate" );
792 /* Initialize vorbis encoder */
793 vorbis_info_init( &p_sys->vi );
795 if( i_quality > 0 )
797 /* VBR mode */
798 if( vorbis_encode_setup_vbr( &p_sys->vi,
799 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
800 i_quality * 0.1 ) )
802 vorbis_info_clear( &p_sys->vi );
803 free( p_enc->p_sys );
804 msg_Err( p_enc, "VBR mode initialisation failed %"PRIu8"x(%uHz,q=%d)",
805 p_enc->fmt_in.audio.i_channels,
806 p_enc->fmt_in.audio.i_rate, i_quality );
807 return VLC_EGENERIC;
810 /* Do we have optional hard quality restrictions? */
811 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
813 struct ovectl_ratemanage_arg ai;
814 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
816 ai.bitrate_hard_min = i_min_bitrate;
817 ai.bitrate_hard_max = i_max_bitrate;
818 ai.management_active = 1;
820 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
823 else
825 /* Turn off management entirely */
826 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
829 else
831 if( vorbis_encode_setup_managed( &p_sys->vi,
832 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
833 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
834 p_enc->fmt_out.i_bitrate,
835 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
837 vorbis_info_clear( &p_sys->vi );
838 msg_Err( p_enc, "CBR mode initialisation failed %"PRIu8"x(%uHz,r=%u)",
839 p_enc->fmt_in.audio.i_channels,
840 p_enc->fmt_in.audio.i_rate,
841 p_enc->fmt_out.i_bitrate);
842 free( p_enc->p_sys );
843 return VLC_EGENERIC;
847 vorbis_encode_setup_init( &p_sys->vi );
849 /* Add a comment */
850 vorbis_comment_init( &p_sys->vc);
851 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
853 /* Set up the analysis state and auxiliary encoding storage */
854 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
855 vorbis_block_init( &p_sys->vd, &p_sys->vb );
857 /* Create and store headers */
858 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
859 &header[0], &header[1], &header[2]);
860 for( int i = 0; i < 3; i++ )
862 if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
863 header[i].bytes, header[i].packet ) )
865 p_enc->fmt_out.i_extra = 0;
866 p_enc->fmt_out.p_extra = NULL;
870 assert(p_sys->vi.channels > 0 && (size_t) p_sys->vi.channels < ARRAY_SIZE(pi_channels_maps) );
872 p_enc->fmt_out.audio.i_channels = p_enc->fmt_in.audio.i_channels =
873 p_sys->vi.channels;
875 p_enc->fmt_out.audio.i_physical_channels =
876 p_enc->fmt_in.audio.i_physical_channels =
877 pi_channels_maps[p_sys->vi.channels];
879 p_sys->i_last_block_size = 0;
880 p_sys->i_samples_delay = 0;
882 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
883 p_enc->fmt_in.audio.i_physical_channels);
885 return VLC_SUCCESS;
888 /****************************************************************************
889 * Encode: the whole thing
890 ****************************************************************************
891 * This function spits out ogg packets.
892 ****************************************************************************/
893 static block_t *Encode( encoder_t *p_enc, block_t *p_aout_buf )
895 encoder_sys_t *p_sys = p_enc->p_sys;
896 ogg_packet oggpacket;
897 block_t *p_block, *p_chain = NULL;
898 float **buffer;
900 /* Packets are already flushed, see bellow. */
901 if( unlikely( !p_aout_buf ) ) return NULL;
903 vlc_tick_t i_pts = p_aout_buf->i_pts -
904 vlc_tick_from_samples( p_sys->i_samples_delay,
905 p_enc->fmt_in.audio.i_rate );
907 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
909 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
911 /* convert samples to float and uninterleave */
912 const unsigned i_channels = p_enc->fmt_in.audio.i_channels;
913 for( unsigned int i = 0; i < i_channels; i++ )
915 for( unsigned int j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
917 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
918 [j * i_channels + p_sys->pi_chan_table[i]];
922 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
924 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
926 int i_samples;
928 vorbis_analysis( &p_sys->vb, NULL );
929 vorbis_bitrate_addblock( &p_sys->vb );
931 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
933 int i_block_size;
934 p_block = block_Alloc( oggpacket.bytes );
935 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
937 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
939 if( i_block_size < 0 ) i_block_size = 0;
940 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
941 p_sys->i_last_block_size = i_block_size;
943 p_block->i_length = vlc_tick_from_samples(i_samples,
944 p_enc->fmt_in.audio.i_rate);
946 p_block->i_dts = p_block->i_pts = i_pts;
948 p_sys->i_samples_delay -= i_samples;
950 /* Update pts */
951 i_pts += p_block->i_length;
952 block_ChainAppend( &p_chain, p_block );
956 return p_chain;
959 /*****************************************************************************
960 * CloseEncoder: vorbis encoder destruction
961 *****************************************************************************/
962 static void CloseEncoder( vlc_object_t *p_this )
964 encoder_t *p_enc = (encoder_t *)p_this;
965 encoder_sys_t *p_sys = p_enc->p_sys;
967 vorbis_block_clear( &p_sys->vb );
968 vorbis_dsp_clear( &p_sys->vd );
969 vorbis_comment_clear( &p_sys->vc );
970 vorbis_info_clear( &p_sys->vi ); /* must be called last */
972 free( p_sys );
975 #endif /* HAVE_VORBIS_ENCODER */