input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / codec / vorbis.c
blob6a2740086f88299b6a67d093dcb3964a6e555e9c
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
8 * $Id$
10 * Authors: Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * 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_charset.h>
38 #include <vlc_aout.h>
39 #include <vlc_input.h>
40 #include <vlc_sout.h>
41 #include "../demux/xiph.h"
43 #include <ogg/ogg.h>
45 #ifdef MODULE_NAME_IS_tremor
46 # include <tremor/ivorbiscodec.h>
47 # define INTERLEAVE_TYPE int32_t
49 #else
50 # include <vorbis/codec.h>
51 # define INTERLEAVE_TYPE float
53 # ifdef ENABLE_SOUT
54 # define HAVE_VORBIS_ENCODER
55 # include <vorbis/vorbisenc.h>
56 # ifndef OV_ECTL_RATEMANAGE_AVG
57 # define OV_ECTL_RATEMANAGE_AVG 0x0
58 # endif
59 # endif
60 #endif
62 /*****************************************************************************
63 * decoder_sys_t : vorbis decoder descriptor
64 *****************************************************************************/
65 typedef struct
67 /* Module mode */
68 bool b_packetizer;
70 bool b_has_headers;
73 * Vorbis properties
75 vorbis_info vi; /* struct that stores all the static vorbis bitstream
76 settings */
77 vorbis_comment vc; /* struct that stores all the bitstream user
78 * comments */
79 vorbis_dsp_state vd; /* central working state for the packet->PCM
80 * decoder */
81 vorbis_block vb; /* local working space for packet->PCM decode */
84 * Common properties
86 date_t end_date;
87 int i_last_block_size;
90 ** Channel reordering
92 uint8_t pi_chan_table[AOUT_CHAN_MAX];
93 } decoder_sys_t;
95 static const int pi_channels_maps[9] =
98 AOUT_CHAN_CENTER,
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_REARCENTER | AOUT_CHAN_MIDDLELEFT
109 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
110 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
111 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
112 | AOUT_CHAN_LFE,
116 ** channel order as defined in http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
119 /* recommended vorbis channel order for 8 channels */
120 static const uint32_t pi_8channels_in[] =
121 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
122 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
123 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE, 0 };
125 /* recommended vorbis channel order for 7 channels */
126 static const uint32_t pi_7channels_in[] =
127 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
128 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
129 AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE, 0 };
131 /* recommended vorbis channel order for 6 channels */
132 static const uint32_t pi_6channels_in[] =
133 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
134 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_LFE, 0 };
136 /* recommended vorbis channel order for 4 channels */
137 static const uint32_t pi_4channels_in[] =
138 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
140 /* recommended vorbis channel order for 3 channels */
141 static const uint32_t pi_3channels_in[] =
142 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
144 /****************************************************************************
145 * Local prototypes
146 ****************************************************************************/
147 static int OpenDecoder ( vlc_object_t * );
148 static int OpenPacketizer( vlc_object_t * );
149 static void CloseDecoder ( vlc_object_t * );
150 static int DecodeAudio ( decoder_t *, block_t * );
151 static block_t *Packetize ( decoder_t *, block_t ** );
152 static void Flush( decoder_t * );
154 static int ProcessHeaders( decoder_t * );
155 static block_t *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
157 static block_t *DecodePacket( decoder_t *, ogg_packet * );
158 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
160 static void ParseVorbisComments( decoder_t * );
162 static void ConfigureChannelOrder(uint8_t *, int, uint32_t );
164 #ifdef HAVE_VORBIS_ENCODER
165 static int OpenEncoder ( vlc_object_t * );
166 static void CloseEncoder ( vlc_object_t * );
167 static block_t *Encode ( encoder_t *, block_t * );
168 #endif
170 /*****************************************************************************
171 * Module descriptor
172 *****************************************************************************/
173 #define ENC_QUALITY_TEXT N_("Encoding quality")
174 #define ENC_QUALITY_LONGTEXT N_( \
175 "Enforce a quality between 1 (low) and 10 (high), instead " \
176 "of specifying a particular bitrate. This will produce a VBR stream." )
177 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
178 #define ENC_MAXBR_LONGTEXT N_( \
179 "Maximum bitrate in kbps. This is useful for streaming applications." )
180 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
181 #define ENC_MINBR_LONGTEXT N_( \
182 "Minimum bitrate in kbps. This is useful for encoding for a fixed-size channel." )
183 #define ENC_CBR_TEXT N_("CBR encoding")
184 #define ENC_CBR_LONGTEXT N_( \
185 "Force a constant bitrate encoding (CBR)." )
187 vlc_module_begin ()
188 set_shortname( "Vorbis" )
189 set_description( N_("Vorbis audio decoder") )
190 #ifdef MODULE_NAME_IS_tremor
191 set_capability( "audio decoder", 90 )
192 #else
193 set_capability( "audio decoder", 100 )
194 #endif
195 set_category( CAT_INPUT )
196 set_subcategory( SUBCAT_INPUT_ACODEC )
197 set_callbacks( OpenDecoder, CloseDecoder )
199 add_submodule ()
200 set_description( N_("Vorbis audio packetizer") )
201 set_capability( "packetizer", 100 )
202 set_callbacks( OpenPacketizer, CloseDecoder )
204 #ifdef HAVE_VORBIS_ENCODER
205 # define ENC_CFG_PREFIX "sout-vorbis-"
206 add_submodule ()
207 set_description( N_("Vorbis audio encoder") )
208 set_capability( "encoder", 130 )
209 set_callbacks( OpenEncoder, CloseEncoder )
211 add_integer( ENC_CFG_PREFIX "quality", 0, ENC_QUALITY_TEXT,
212 ENC_QUALITY_LONGTEXT, false )
213 change_integer_range( 0, 10 )
214 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, ENC_MAXBR_TEXT,
215 ENC_MAXBR_LONGTEXT, false )
216 add_integer( ENC_CFG_PREFIX "min-bitrate", 0, ENC_MINBR_TEXT,
217 ENC_MINBR_LONGTEXT, false )
218 add_bool( ENC_CFG_PREFIX "cbr", false, ENC_CBR_TEXT,
219 ENC_CBR_LONGTEXT, false )
220 #endif
222 vlc_module_end ()
224 #ifdef HAVE_VORBIS_ENCODER
225 static const char *const ppsz_enc_options[] = {
226 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
228 #endif
230 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
232 decoder_t *p_dec = (decoder_t*)p_this;
233 decoder_sys_t *p_sys;
235 if( p_dec->fmt_in.i_codec != VLC_CODEC_VORBIS )
236 return VLC_EGENERIC;
238 /* Allocate the memory needed to store the decoder's structure */
239 p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) );
240 if( unlikely( !p_sys ) )
241 return VLC_ENOMEM;
243 /* Misc init */
244 date_Set( &p_sys->end_date, VLC_TICK_INVALID );
245 p_sys->i_last_block_size = 0;
246 p_sys->b_packetizer = b_packetizer;
247 p_sys->b_has_headers = false;
249 /* Take care of vorbis init */
250 vorbis_info_init( &p_sys->vi );
251 vorbis_comment_init( &p_sys->vc );
253 if( b_packetizer )
255 p_dec->fmt_out.i_codec = VLC_CODEC_VORBIS;
256 p_dec->pf_packetize = Packetize;
258 else
260 #ifdef MODULE_NAME_IS_tremor
261 p_dec->fmt_out.i_codec = VLC_CODEC_S32N;
262 #else
263 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
264 #endif
265 p_dec->pf_decode = DecodeAudio;
268 p_dec->pf_flush = Flush;
270 return VLC_SUCCESS;
273 static int OpenDecoder( vlc_object_t *p_this )
275 return OpenCommon( p_this, false );
278 static int OpenPacketizer( vlc_object_t *p_this )
280 return OpenCommon( p_this, true );
283 /****************************************************************************
284 * DecodeBlock: the whole thing
285 ****************************************************************************
286 * This function must be fed with ogg packets.
287 ****************************************************************************/
288 static block_t *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 )
295 /* Block to Ogg packet */
296 oggpacket.packet = (*pp_block)->p_buffer;
297 oggpacket.bytes = (*pp_block)->i_buffer;
299 else
301 if( p_sys->b_packetizer ) return NULL;
303 /* Block to Ogg packet */
304 oggpacket.packet = NULL;
305 oggpacket.bytes = 0;
308 oggpacket.granulepos = -1;
309 oggpacket.b_o_s = 0;
310 oggpacket.e_o_s = 0;
311 oggpacket.packetno = 0;
313 /* Check for headers */
314 if( !p_sys->b_has_headers )
316 if( ProcessHeaders( p_dec ) )
318 if( *pp_block )
319 block_Release( *pp_block );
320 return NULL;
322 p_sys->b_has_headers = true;
325 return ProcessPacket( p_dec, &oggpacket, pp_block );
328 static int DecodeAudio( decoder_t *p_dec, block_t *p_block )
330 if( p_block == NULL ) /* No Drain */
331 return VLCDEC_SUCCESS;
333 block_t **pp_block = &p_block, *p_out;
334 while( ( p_out = DecodeBlock( p_dec, pp_block ) ) != NULL )
335 decoder_QueueAudio( p_dec, p_out );
336 return VLCDEC_SUCCESS;
339 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
341 if( pp_block == NULL ) /* No Drain */
342 return NULL;
343 return DecodeBlock( p_dec, pp_block );
346 /*****************************************************************************
347 * ProcessHeaders: process Vorbis headers.
348 *****************************************************************************/
349 static int ProcessHeaders( decoder_t *p_dec )
351 decoder_sys_t *p_sys = p_dec->p_sys;
352 ogg_packet oggpacket;
354 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
355 void *pp_data[XIPH_MAX_HEADER_COUNT];
356 unsigned i_count;
357 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
358 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
359 return VLC_EGENERIC;
360 if( i_count < 3 )
361 return VLC_EGENERIC;
363 oggpacket.granulepos = -1;
364 oggpacket.e_o_s = 0;
365 oggpacket.packetno = 0;
367 /* Take care of the initial Vorbis header */
368 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
369 oggpacket.bytes = pi_size[0];
370 oggpacket.packet = pp_data[0];
371 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
373 msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
374 return VLC_EGENERIC;
377 /* Setup the format */
378 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
379 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
381 if( p_dec->fmt_out.audio.i_channels >= ARRAY_SIZE(pi_channels_maps) )
383 msg_Err( p_dec, "invalid number of channels (1-%zu): %i",
384 ARRAY_SIZE(pi_channels_maps),
385 p_dec->fmt_out.audio.i_channels );
386 return VLC_EGENERIC;
389 p_dec->fmt_out.audio.i_physical_channels =
390 pi_channels_maps[p_sys->vi.channels];
391 p_dec->fmt_out.i_bitrate = __MAX( 0, (int32_t) p_sys->vi.bitrate_nominal );
393 date_Init( &p_sys->end_date, p_sys->vi.rate, 1 );
395 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ud",
396 p_sys->vi.channels, p_sys->vi.rate, p_dec->fmt_out.i_bitrate );
398 /* The next packet in order is the comments header */
399 oggpacket.b_o_s = 0;
400 oggpacket.bytes = pi_size[1];
401 oggpacket.packet = pp_data[1];
402 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
404 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
405 return VLC_EGENERIC;
407 ParseVorbisComments( p_dec );
409 /* The next packet in order is the codebooks header
410 * We need to watch out that this packet is not missing as a
411 * missing or corrupted header is fatal. */
412 oggpacket.b_o_s = 0;
413 oggpacket.bytes = pi_size[2];
414 oggpacket.packet = pp_data[2];
415 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
417 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
418 return VLC_EGENERIC;
421 if( !p_sys->b_packetizer )
423 /* Initialize the Vorbis packet->PCM decoder */
424 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
425 vorbis_block_init( &p_sys->vd, &p_sys->vb );
427 else
429 void* p_extra = realloc( p_dec->fmt_out.p_extra,
430 p_dec->fmt_in.i_extra );
431 if( unlikely( p_extra == NULL ) )
433 return VLC_ENOMEM;
435 p_dec->fmt_out.p_extra = p_extra;
436 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
437 memcpy( p_dec->fmt_out.p_extra,
438 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
441 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
442 p_dec->fmt_out.audio.i_physical_channels);
444 return VLC_SUCCESS;
447 /*****************************************************************************
448 * Flush:
449 *****************************************************************************/
450 static void Flush( decoder_t *p_dec )
452 decoder_sys_t *p_sys = p_dec->p_sys;
454 date_Set( &p_sys->end_date, VLC_TICK_INVALID );
457 /*****************************************************************************
458 * ProcessPacket: processes a Vorbis packet.
459 *****************************************************************************/
460 static block_t *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
461 block_t **pp_block )
463 decoder_sys_t *p_sys = p_dec->p_sys;
464 block_t *p_block = *pp_block;
466 *pp_block = NULL; /* To avoid being fed the same packet again */
467 if( !p_block )
468 return NULL;
470 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
472 Flush( p_dec );
473 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
475 block_Release(p_block);
476 return NULL;
480 /* Date management */
481 if( p_block->i_pts != VLC_TICK_INVALID &&
482 p_block->i_pts != date_Get( &p_sys->end_date ) )
484 date_Set( &p_sys->end_date, p_block->i_pts );
487 if( date_Get( &p_sys->end_date ) == VLC_TICK_INVALID )
489 /* We've just started the stream, wait for the first PTS. */
490 if( p_block ) block_Release( p_block );
491 return NULL;
495 if( p_sys->b_packetizer )
497 return SendPacket( p_dec, p_oggpacket, p_block );
499 else
501 block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
502 if( p_block )
503 block_Release( p_block );
504 return p_aout_buffer;
508 /*****************************************************************************
509 * Interleave: helper function to interleave channels
510 *****************************************************************************/
511 static void Interleave( INTERLEAVE_TYPE *p_out, const INTERLEAVE_TYPE **pp_in,
512 int i_nb_channels, int i_samples, uint8_t *pi_chan_table)
514 for( int j = 0; j < i_samples; j++ )
515 for( int i = 0; i < i_nb_channels; i++ )
517 #ifdef MODULE_NAME_IS_tremor
518 union { int32_t i; uint32_t u;} spl;
520 spl.u = ((uint32_t)pp_in[i][j]) << 8;
521 p_out[j * i_nb_channels + pi_chan_table[i]] = spl.i;
522 #else
523 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
524 #endif
528 /*****************************************************************************
529 * DecodePacket: decodes a Vorbis packet.
530 *****************************************************************************/
531 static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
533 decoder_sys_t *p_sys = p_dec->p_sys;
534 int i_samples;
536 INTERLEAVE_TYPE **pp_pcm;
538 if( p_oggpacket->bytes &&
539 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
540 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
542 /* **pp_pcm is a multichannel float vector. In stereo, for
543 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
544 * the size of each channel. Convert the float values
545 * (-1.<=range<=1.) to whatever PCM format and write it out */
547 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
550 block_t *p_aout_buffer;
552 if( decoder_UpdateAudioFormat( p_dec ) ) return NULL;
553 p_aout_buffer =
554 decoder_NewAudioBuffer( p_dec, i_samples );
556 if( p_aout_buffer == NULL ) return NULL;
558 /* Interleave the samples */
559 Interleave( (INTERLEAVE_TYPE*)p_aout_buffer->p_buffer,
560 (const INTERLEAVE_TYPE**)pp_pcm, p_sys->vi.channels, i_samples,
561 p_sys->pi_chan_table);
563 /* Tell libvorbis how many samples we actually consumed */
564 vorbis_synthesis_read( &p_sys->vd, i_samples );
566 /* Date management */
567 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
568 p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
569 i_samples ) - p_aout_buffer->i_pts;
570 return p_aout_buffer;
572 else
574 return NULL;
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,
582 block_t *p_block )
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 = date_Get( &p_sys->end_date );
595 p_block->i_length = date_Increment( &p_sys->end_date, i_samples ) - p_block->i_pts;
597 return p_block;
600 /*****************************************************************************
601 * ParseVorbisComments
602 *****************************************************************************/
603 static void ParseVorbisComments( decoder_t *p_dec )
605 char *psz_name, *psz_value, *psz_comment;
606 int i = 0;
608 decoder_sys_t *p_sys = p_dec->p_sys;
610 while( i < p_sys->vc.comments )
612 psz_comment = strdup( p_sys->vc.user_comments[i] );
613 if( !psz_comment )
614 break;
615 psz_name = psz_comment;
616 psz_value = strchr( psz_comment, '=' );
617 /* Don't add empty values */
618 if( psz_value && psz_value[1] != '\0')
620 *psz_value = '\0';
621 psz_value++;
623 if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_GAIN" ) ||
624 !strcasecmp( psz_name, "RG_RADIO" ) )
626 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
628 r->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
629 r->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = us_atof( psz_value );
631 else if( !strcasecmp( psz_name, "REPLAYGAIN_TRACK_PEAK" ) ||
632 !strcasecmp( psz_name, "RG_PEAK" ) )
634 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
636 r->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
637 r->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = us_atof( psz_value );
639 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_GAIN" ) ||
640 !strcasecmp( psz_name, "RG_AUDIOPHILE" ) )
642 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
644 r->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
645 r->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( psz_value );
647 else if( !strcasecmp( psz_name, "REPLAYGAIN_ALBUM_PEAK" ) )
649 audio_replay_gain_t *r = &p_dec->fmt_out.audio_replay_gain;
651 r->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
652 r->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( psz_value );
654 else if( !strcasecmp( psz_name, "METADATA_BLOCK_PICTURE" ) )
655 { /* Do nothing, for now */ }
656 else
658 if( !p_dec->p_description )
659 p_dec->p_description = vlc_meta_New();
660 if( p_dec->p_description )
661 vlc_meta_AddExtra( p_dec->p_description, psz_name, psz_value );
665 free( psz_comment );
666 i++;
670 /*****************************************************************************
672 *****************************************************************************/
673 static void ConfigureChannelOrder(uint8_t *pi_chan_table, int i_channels,
674 uint32_t i_channel_mask)
676 const uint32_t *pi_channels_in;
677 switch( i_channels )
679 case 8:
680 pi_channels_in = pi_8channels_in;
681 break;
682 case 7:
683 pi_channels_in = pi_7channels_in;
684 break;
685 case 6:
686 case 5:
687 pi_channels_in = pi_6channels_in;
688 break;
689 case 4:
690 pi_channels_in = pi_4channels_in;
691 break;
692 case 3:
693 pi_channels_in = pi_3channels_in;
694 break;
695 default:
696 for( int i = 0; i< i_channels; ++i )
697 pi_chan_table[i] = i;
699 return;
702 aout_CheckChannelReorder( pi_channels_in, NULL,
703 i_channel_mask, pi_chan_table );
706 /*****************************************************************************
707 * CloseDecoder: vorbis decoder destruction
708 *****************************************************************************/
709 static void CloseDecoder( vlc_object_t *p_this )
711 decoder_t *p_dec = (decoder_t *)p_this;
712 decoder_sys_t *p_sys = p_dec->p_sys;
714 if( !p_sys->b_packetizer && p_sys->b_has_headers )
716 vorbis_block_clear( &p_sys->vb );
717 vorbis_dsp_clear( &p_sys->vd );
720 vorbis_comment_clear( &p_sys->vc );
721 vorbis_info_clear( &p_sys->vi ); /* must be called last */
723 free( p_sys );
726 #ifdef HAVE_VORBIS_ENCODER
727 /*****************************************************************************
728 * encoder_sys_t : vorbis encoder descriptor
729 *****************************************************************************/
730 typedef struct
733 * Vorbis properties
735 vorbis_info vi; /* struct that stores all the static vorbis bitstream
736 settings */
737 vorbis_comment vc; /* struct that stores all the bitstream user
738 * comments */
739 vorbis_dsp_state vd; /* central working state for the packet->PCM
740 * decoder */
741 vorbis_block vb; /* local working space for packet->PCM decode */
743 int i_last_block_size;
744 int i_samples_delay;
747 ** Channel reordering
749 uint8_t pi_chan_table[AOUT_CHAN_MAX];
751 } encoder_sys_t;
753 /*****************************************************************************
754 * OpenEncoder: probe the encoder and return score
755 *****************************************************************************/
756 static int OpenEncoder( vlc_object_t *p_this )
758 encoder_t *p_enc = (encoder_t *)p_this;
759 encoder_sys_t *p_sys;
760 int i_quality, i_min_bitrate, i_max_bitrate;
761 ogg_packet header[3];
763 if( p_enc->fmt_out.i_codec != VLC_CODEC_VORBIS &&
764 !p_enc->obj.force )
766 return VLC_EGENERIC;
769 /* Allocate the memory needed to store the decoder's structure */
770 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
771 return VLC_ENOMEM;
772 p_enc->p_sys = p_sys;
774 p_enc->pf_encode_audio = Encode;
775 p_enc->fmt_in.i_codec = VLC_CODEC_FL32;
776 p_enc->fmt_out.i_codec = VLC_CODEC_VORBIS;
778 if( p_enc->fmt_in.audio.i_channels >= ARRAY_SIZE(pi_channels_maps) )
780 p_enc->fmt_in.audio.i_channels = ARRAY_SIZE(pi_channels_maps) - 1;
781 msg_Warn( p_enc, "lowering channel count to %u", p_enc->fmt_in.audio.i_channels );
783 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
785 i_quality = var_GetInteger( p_enc, ENC_CFG_PREFIX "quality" );
786 if( i_quality > 10 ) i_quality = 10;
787 if( i_quality < 0 ) i_quality = 0;
789 if( var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ) i_quality = 0;
790 i_max_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
791 i_min_bitrate = var_GetInteger( p_enc, ENC_CFG_PREFIX "min-bitrate" );
793 /* Initialize vorbis encoder */
794 vorbis_info_init( &p_sys->vi );
796 if( i_quality > 0 )
798 /* VBR mode */
799 if( vorbis_encode_setup_vbr( &p_sys->vi,
800 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
801 i_quality * 0.1 ) )
803 vorbis_info_clear( &p_sys->vi );
804 free( p_enc->p_sys );
805 msg_Err( p_enc, "VBR mode initialisation failed %"PRIu8"x(%uHz,q=%d)",
806 p_enc->fmt_in.audio.i_channels,
807 p_enc->fmt_in.audio.i_rate, i_quality );
808 return VLC_EGENERIC;
811 /* Do we have optional hard quality restrictions? */
812 if( i_max_bitrate > 0 || i_min_bitrate > 0 )
814 struct ovectl_ratemanage_arg ai;
815 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
817 ai.bitrate_hard_min = i_min_bitrate;
818 ai.bitrate_hard_max = i_max_bitrate;
819 ai.management_active = 1;
821 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
824 else
826 /* Turn off management entirely */
827 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
830 else
832 if( vorbis_encode_setup_managed( &p_sys->vi,
833 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
834 i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
835 p_enc->fmt_out.i_bitrate,
836 i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
838 vorbis_info_clear( &p_sys->vi );
839 msg_Err( p_enc, "CBR mode initialisation failed %"PRIu8"x(%uHz,r=%u)",
840 p_enc->fmt_in.audio.i_channels,
841 p_enc->fmt_in.audio.i_rate,
842 p_enc->fmt_out.i_bitrate);
843 free( p_enc->p_sys );
844 return VLC_EGENERIC;
848 vorbis_encode_setup_init( &p_sys->vi );
850 /* Add a comment */
851 vorbis_comment_init( &p_sys->vc);
852 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
854 /* Set up the analysis state and auxiliary encoding storage */
855 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
856 vorbis_block_init( &p_sys->vd, &p_sys->vb );
858 /* Create and store headers */
859 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
860 &header[0], &header[1], &header[2]);
861 for( int i = 0; i < 3; i++ )
863 if( xiph_AppendHeaders( &p_enc->fmt_out.i_extra, &p_enc->fmt_out.p_extra,
864 header[i].bytes, header[i].packet ) )
866 p_enc->fmt_out.i_extra = 0;
867 p_enc->fmt_out.p_extra = NULL;
871 assert(p_sys->vi.channels > 0 && (size_t) p_sys->vi.channels < ARRAY_SIZE(pi_channels_maps) );
873 p_enc->fmt_out.audio.i_channels = p_enc->fmt_in.audio.i_channels =
874 p_sys->vi.channels;
876 p_enc->fmt_out.audio.i_physical_channels =
877 p_enc->fmt_in.audio.i_physical_channels =
878 pi_channels_maps[p_sys->vi.channels];
880 p_sys->i_last_block_size = 0;
881 p_sys->i_samples_delay = 0;
883 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
884 p_enc->fmt_in.audio.i_physical_channels);
886 return VLC_SUCCESS;
889 /****************************************************************************
890 * Encode: the whole thing
891 ****************************************************************************
892 * This function spits out ogg packets.
893 ****************************************************************************/
894 static block_t *Encode( encoder_t *p_enc, block_t *p_aout_buf )
896 encoder_sys_t *p_sys = p_enc->p_sys;
897 ogg_packet oggpacket;
898 block_t *p_block, *p_chain = NULL;
899 float **buffer;
901 /* Packets are already flushed, see bellow. */
902 if( unlikely( !p_aout_buf ) ) return NULL;
904 vlc_tick_t i_pts = p_aout_buf->i_pts -
905 CLOCK_FREQ * (vlc_tick_t)p_sys->i_samples_delay /
906 (vlc_tick_t)p_enc->fmt_in.audio.i_rate;
908 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
910 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
912 /* convert samples to float and uninterleave */
913 const unsigned i_channels = p_enc->fmt_in.audio.i_channels;
914 for( unsigned int i = 0; i < i_channels; i++ )
916 for( unsigned int j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
918 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
919 [j * i_channels + p_sys->pi_chan_table[i]];
923 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
925 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
927 int i_samples;
929 vorbis_analysis( &p_sys->vb, NULL );
930 vorbis_bitrate_addblock( &p_sys->vb );
932 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
934 int i_block_size;
935 p_block = block_Alloc( oggpacket.bytes );
936 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
938 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
940 if( i_block_size < 0 ) i_block_size = 0;
941 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
942 p_sys->i_last_block_size = i_block_size;
944 p_block->i_length = CLOCK_FREQ *
945 (vlc_tick_t)i_samples / (vlc_tick_t)p_enc->fmt_in.audio.i_rate;
947 p_block->i_dts = p_block->i_pts = i_pts;
949 p_sys->i_samples_delay -= i_samples;
951 /* Update pts */
952 i_pts += p_block->i_length;
953 block_ChainAppend( &p_chain, p_block );
957 return p_chain;
960 /*****************************************************************************
961 * CloseEncoder: vorbis encoder destruction
962 *****************************************************************************/
963 static void CloseEncoder( vlc_object_t *p_this )
965 encoder_t *p_enc = (encoder_t *)p_this;
966 encoder_sys_t *p_sys = p_enc->p_sys;
968 vorbis_block_clear( &p_sys->vb );
969 vorbis_dsp_clear( &p_sys->vd );
970 vorbis_comment_clear( &p_sys->vc );
971 vorbis_info_clear( &p_sys->vi ); /* must be called last */
973 free( p_sys );
976 #endif /* HAVE_VORBIS_ENCODER */