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 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_charset.h>
38 #include <vlc_input_item.h>
40 #include "../demux/xiph.h"
44 #ifdef MODULE_NAME_IS_tremor
45 # include <tremor/ivorbiscodec.h>
46 # define INTERLEAVE_TYPE int32_t
49 # include <vorbis/codec.h>
50 # define INTERLEAVE_TYPE float
53 # define HAVE_VORBIS_ENCODER
54 # include <vorbis/vorbisenc.h>
55 # ifndef OV_ECTL_RATEMANAGE_AVG
56 # define OV_ECTL_RATEMANAGE_AVG 0x0
61 /*****************************************************************************
62 * decoder_sys_t : vorbis decoder descriptor
63 *****************************************************************************/
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 uint8_t 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_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
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 /****************************************************************************
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
* );
169 /*****************************************************************************
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)." )
187 set_shortname( "Vorbis" )
188 set_description( N_("Vorbis audio decoder") )
189 #ifdef MODULE_NAME_IS_tremor
190 set_capability( "audio decoder", 90 )
192 set_capability( "audio decoder", 100 )
194 set_category( CAT_INPUT
)
195 set_subcategory( SUBCAT_INPUT_ACODEC
)
196 set_callbacks( OpenDecoder
, CloseDecoder
)
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-"
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 )
223 #ifdef HAVE_VORBIS_ENCODER
224 static const char *const ppsz_enc_options
[] = {
225 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
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
)
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
) )
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
);
254 p_dec
->fmt_out
.i_codec
= VLC_CODEC_VORBIS
;
255 p_dec
->pf_packetize
= Packetize
;
259 #ifdef MODULE_NAME_IS_tremor
260 p_dec
->fmt_out
.i_codec
= VLC_CODEC_S32N
;
262 p_dec
->fmt_out
.i_codec
= VLC_CODEC_FL32
;
264 p_dec
->pf_decode
= DecodeAudio
;
267 p_dec
->pf_flush
= Flush
;
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
;
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
->b_has_headers
)
315 if( ProcessHeaders( p_dec
) )
318 block_Release( *pp_block
);
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 */
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
];
356 if( xiph_SplitHeaders( pi_size
, pp_data
, &i_count
,
357 p_dec
->fmt_in
.i_extra
, p_dec
->fmt_in
.p_extra
) )
362 oggpacket
.granulepos
= -1;
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");
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
);
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 */
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" );
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. */
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" );
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
);
428 void* p_extra
= realloc( p_dec
->fmt_out
.p_extra
,
429 p_dec
->fmt_in
.i_extra
);
430 if( unlikely( p_extra
== NULL
) )
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
);
446 /*****************************************************************************
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
,
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 */
469 if( p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
) )
472 if( p_block
->i_flags
& BLOCK_FLAG_CORRUPTED
)
474 block_Release(p_block
);
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
);
494 if( p_sys
->b_packetizer
)
496 return SendPacket( p_dec
, p_oggpacket
, p_block
);
500 block_t
*p_aout_buffer
= DecodePacket( p_dec
, p_oggpacket
);
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
;
522 p_out
[j
* i_nb_channels
+ pi_chan_table
[i
]] = pp_in
[i
][j
];
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
;
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
;
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
;
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 p_block
->i_length
= date_Increment( &p_sys
->end_date
, i_samples
) - p_block
->i_pts
;
599 /*****************************************************************************
600 * ParseVorbisComments
601 *****************************************************************************/
602 static void ParseVorbisComments( decoder_t
*p_dec
)
604 char *psz_name
, *psz_value
, *psz_comment
;
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
] );
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')
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 */ }
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
);
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
;
679 pi_channels_in
= pi_8channels_in
;
682 pi_channels_in
= pi_7channels_in
;
686 pi_channels_in
= pi_6channels_in
;
689 pi_channels_in
= pi_4channels_in
;
692 pi_channels_in
= pi_3channels_in
;
695 for( int i
= 0; i
< i_channels
; ++i
)
696 pi_chan_table
[i
] = i
;
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 */
725 #ifdef HAVE_VORBIS_ENCODER
726 /*****************************************************************************
727 * encoder_sys_t : vorbis encoder descriptor
728 *****************************************************************************/
734 vorbis_info vi
; /* struct that stores all the static vorbis bitstream
736 vorbis_comment vc
; /* struct that stores all the bitstream user
738 vorbis_dsp_state vd
; /* central working state for the packet->PCM
740 vorbis_block vb
; /* local working space for packet->PCM decode */
742 int i_last_block_size
;
746 ** Channel reordering
748 uint8_t pi_chan_table
[AOUT_CHAN_MAX
];
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
&&
768 /* Allocate the memory needed to store the decoder's structure */
769 if( ( p_sys
= (encoder_sys_t
*)malloc(sizeof(encoder_sys_t
)) ) == NULL
)
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
);
798 if( vorbis_encode_setup_vbr( &p_sys
->vi
,
799 p_enc
->fmt_in
.audio
.i_channels
, p_enc
->fmt_in
.audio
.i_rate
,
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
);
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
);
825 /* Turn off management entirely */
826 vorbis_encode_ctl( &p_sys
->vi
, OV_ECTL_RATEMANAGE_SET
, NULL
);
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
);
847 vorbis_encode_setup_init( &p_sys
->vi
);
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
=
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
);
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
;
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 )
928 vorbis_analysis( &p_sys
->vb
, NULL
);
929 vorbis_bitrate_addblock( &p_sys
->vb
);
931 while( vorbis_bitrate_flushpacket( &p_sys
->vd
, &oggpacket
) )
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
;
951 i_pts
+= p_block
->i_length
;
952 block_ChainAppend( &p_chain
, p_block
);
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 */
975 #endif /* HAVE_VORBIS_ENCODER */