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
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 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_charset.h>
39 #include <vlc_input.h>
41 #include "../demux/xiph.h"
45 #ifdef MODULE_NAME_IS_tremor
46 # include <tremor/ivorbiscodec.h>
47 # define INTERLEAVE_TYPE int32_t
50 # include <vorbis/codec.h>
51 # define INTERLEAVE_TYPE float
54 # define HAVE_VORBIS_ENCODER
55 # include <vorbis/vorbisenc.h>
56 # ifndef OV_ECTL_RATEMANAGE_AVG
57 # define OV_ECTL_RATEMANAGE_AVG 0x0
62 /*****************************************************************************
63 * decoder_sys_t : vorbis decoder descriptor
64 *****************************************************************************/
75 vorbis_info vi
; /* struct that stores all the static vorbis bitstream
77 vorbis_comment vc
; /* struct that stores all the bitstream user
79 vorbis_dsp_state vd
; /* central working state for the packet->PCM
81 vorbis_block vb
; /* local working space for packet->PCM decode */
87 int i_last_block_size
;
92 uint8_t pi_chan_table
[AOUT_CHAN_MAX
];
95 static const int pi_channels_maps
[9] =
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
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 /****************************************************************************
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
* );
170 /*****************************************************************************
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)." )
188 set_shortname( "Vorbis" )
189 set_description( N_("Vorbis audio decoder") )
190 #ifdef MODULE_NAME_IS_tremor
191 set_capability( "audio decoder", 90 )
193 set_capability( "audio decoder", 100 )
195 set_category( CAT_INPUT
)
196 set_subcategory( SUBCAT_INPUT_ACODEC
)
197 set_callbacks( OpenDecoder
, CloseDecoder
)
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-"
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 )
224 #ifdef HAVE_VORBIS_ENCODER
225 static const char *const ppsz_enc_options
[] = {
226 "quality", "max-bitrate", "min-bitrate", "cbr", NULL
230 /*****************************************************************************
231 * OpenDecoder: probe the decoder and return score
232 *****************************************************************************/
233 static int OpenDecoder( vlc_object_t
*p_this
)
235 decoder_t
*p_dec
= (decoder_t
*)p_this
;
236 decoder_sys_t
*p_sys
;
238 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_VORBIS
)
241 /* Allocate the memory needed to store the decoder's structure */
242 p_dec
->p_sys
= p_sys
= malloc( sizeof(*p_sys
) );
243 if( unlikely( !p_sys
) )
247 date_Set( &p_sys
->end_date
, 0 );
248 p_sys
->i_last_block_size
= 0;
249 p_sys
->b_packetizer
= false;
250 p_sys
->b_has_headers
= false;
252 /* Take care of vorbis init */
253 vorbis_info_init( &p_sys
->vi
);
254 vorbis_comment_init( &p_sys
->vc
);
256 /* Set output properties */
257 #ifdef MODULE_NAME_IS_tremor
258 p_dec
->fmt_out
.i_codec
= VLC_CODEC_S32N
;
260 p_dec
->fmt_out
.i_codec
= VLC_CODEC_FL32
;
264 p_dec
->pf_decode
= DecodeAudio
;
265 p_dec
->pf_packetize
= Packetize
;
266 p_dec
->pf_flush
= Flush
;
271 static int OpenPacketizer( vlc_object_t
*p_this
)
273 decoder_t
*p_dec
= (decoder_t
*)p_this
;
275 int i_ret
= OpenDecoder( p_this
);
277 if( i_ret
== VLC_SUCCESS
)
279 p_dec
->p_sys
->b_packetizer
= true;
280 p_dec
->fmt_out
.i_codec
= VLC_CODEC_VORBIS
;
286 /****************************************************************************
287 * DecodeBlock: the whole thing
288 ****************************************************************************
289 * This function must be fed with ogg packets.
290 ****************************************************************************/
291 static block_t
*DecodeBlock( decoder_t
*p_dec
, block_t
**pp_block
)
293 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
294 ogg_packet oggpacket
;
298 /* Block to Ogg packet */
299 oggpacket
.packet
= (*pp_block
)->p_buffer
;
300 oggpacket
.bytes
= (*pp_block
)->i_buffer
;
304 if( p_sys
->b_packetizer
) return NULL
;
306 /* Block to Ogg packet */
307 oggpacket
.packet
= NULL
;
311 oggpacket
.granulepos
= -1;
314 oggpacket
.packetno
= 0;
316 /* Check for headers */
317 if( !p_sys
->b_has_headers
)
319 if( ProcessHeaders( p_dec
) )
322 block_Release( *pp_block
);
325 p_sys
->b_has_headers
= true;
328 return ProcessPacket( p_dec
, &oggpacket
, pp_block
);
331 static int DecodeAudio( decoder_t
*p_dec
, block_t
*p_block
)
333 if( p_block
== NULL
) /* No Drain */
334 return VLCDEC_SUCCESS
;
336 block_t
**pp_block
= &p_block
, *p_out
;
337 while( ( p_out
= DecodeBlock( p_dec
, pp_block
) ) != NULL
)
338 decoder_QueueAudio( p_dec
, p_out
);
339 return VLCDEC_SUCCESS
;
342 static block_t
*Packetize( decoder_t
*p_dec
, block_t
**pp_block
)
344 if( pp_block
== NULL
) /* No Drain */
346 return DecodeBlock( p_dec
, pp_block
);
349 /*****************************************************************************
350 * ProcessHeaders: process Vorbis headers.
351 *****************************************************************************/
352 static int ProcessHeaders( decoder_t
*p_dec
)
354 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
355 ogg_packet oggpacket
;
357 unsigned pi_size
[XIPH_MAX_HEADER_COUNT
];
358 void *pp_data
[XIPH_MAX_HEADER_COUNT
];
360 if( xiph_SplitHeaders( pi_size
, pp_data
, &i_count
,
361 p_dec
->fmt_in
.i_extra
, p_dec
->fmt_in
.p_extra
) )
366 oggpacket
.granulepos
= -1;
368 oggpacket
.packetno
= 0;
370 /* Take care of the initial Vorbis header */
371 oggpacket
.b_o_s
= 1; /* yes this actually is a b_o_s packet :) */
372 oggpacket
.bytes
= pi_size
[0];
373 oggpacket
.packet
= pp_data
[0];
374 if( vorbis_synthesis_headerin( &p_sys
->vi
, &p_sys
->vc
, &oggpacket
) < 0 )
376 msg_Err( p_dec
, "this bitstream does not contain Vorbis audio data");
380 /* Setup the format */
381 p_dec
->fmt_out
.audio
.i_rate
= p_sys
->vi
.rate
;
382 p_dec
->fmt_out
.audio
.i_channels
= p_sys
->vi
.channels
;
384 if( p_dec
->fmt_out
.audio
.i_channels
>= ARRAY_SIZE(pi_channels_maps
) )
386 msg_Err( p_dec
, "invalid number of channels (1-%zu): %i",
387 ARRAY_SIZE(pi_channels_maps
),
388 p_dec
->fmt_out
.audio
.i_channels
);
392 p_dec
->fmt_out
.audio
.i_physical_channels
=
393 pi_channels_maps
[p_sys
->vi
.channels
];
394 p_dec
->fmt_out
.i_bitrate
= __MAX( 0, (int32_t) p_sys
->vi
.bitrate_nominal
);
396 date_Init( &p_sys
->end_date
, p_sys
->vi
.rate
, 1 );
398 msg_Dbg( p_dec
, "channels:%d samplerate:%ld bitrate:%ud",
399 p_sys
->vi
.channels
, p_sys
->vi
.rate
, p_dec
->fmt_out
.i_bitrate
);
401 /* The next packet in order is the comments header */
403 oggpacket
.bytes
= pi_size
[1];
404 oggpacket
.packet
= pp_data
[1];
405 if( vorbis_synthesis_headerin( &p_sys
->vi
, &p_sys
->vc
, &oggpacket
) < 0 )
407 msg_Err( p_dec
, "2nd Vorbis header is corrupted" );
410 ParseVorbisComments( p_dec
);
412 /* The next packet in order is the codebooks header
413 * We need to watch out that this packet is not missing as a
414 * missing or corrupted header is fatal. */
416 oggpacket
.bytes
= pi_size
[2];
417 oggpacket
.packet
= pp_data
[2];
418 if( vorbis_synthesis_headerin( &p_sys
->vi
, &p_sys
->vc
, &oggpacket
) < 0 )
420 msg_Err( p_dec
, "3rd Vorbis header is corrupted" );
424 if( !p_sys
->b_packetizer
)
426 /* Initialize the Vorbis packet->PCM decoder */
427 vorbis_synthesis_init( &p_sys
->vd
, &p_sys
->vi
);
428 vorbis_block_init( &p_sys
->vd
, &p_sys
->vb
);
432 void* p_extra
= realloc( p_dec
->fmt_out
.p_extra
,
433 p_dec
->fmt_in
.i_extra
);
434 if( unlikely( p_extra
== NULL
) )
438 p_dec
->fmt_out
.p_extra
= p_extra
;
439 p_dec
->fmt_out
.i_extra
= p_dec
->fmt_in
.i_extra
;
440 memcpy( p_dec
->fmt_out
.p_extra
,
441 p_dec
->fmt_in
.p_extra
, p_dec
->fmt_out
.i_extra
);
444 ConfigureChannelOrder(p_sys
->pi_chan_table
, p_sys
->vi
.channels
,
445 p_dec
->fmt_out
.audio
.i_physical_channels
);
450 /*****************************************************************************
452 *****************************************************************************/
453 static void Flush( decoder_t
*p_dec
)
455 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
457 date_Set( &p_sys
->end_date
, 0 );
460 /*****************************************************************************
461 * ProcessPacket: processes a Vorbis packet.
462 *****************************************************************************/
463 static block_t
*ProcessPacket( decoder_t
*p_dec
, ogg_packet
*p_oggpacket
,
466 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
467 block_t
*p_block
= *pp_block
;
469 *pp_block
= NULL
; /* To avoid being fed the same packet again */
473 if( p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
) )
476 if( p_block
->i_flags
& BLOCK_FLAG_CORRUPTED
)
478 block_Release(p_block
);
483 /* Date management */
484 if( p_block
->i_pts
> VLC_TS_INVALID
&&
485 p_block
->i_pts
!= date_Get( &p_sys
->end_date
) )
487 date_Set( &p_sys
->end_date
, p_block
->i_pts
);
490 if( !date_Get( &p_sys
->end_date
) )
492 /* We've just started the stream, wait for the first PTS. */
493 if( p_block
) block_Release( p_block
);
498 if( p_sys
->b_packetizer
)
500 return SendPacket( p_dec
, p_oggpacket
, p_block
);
504 block_t
*p_aout_buffer
= DecodePacket( p_dec
, p_oggpacket
);
506 block_Release( p_block
);
507 return p_aout_buffer
;
511 /*****************************************************************************
512 * Interleave: helper function to interleave channels
513 *****************************************************************************/
514 static void Interleave( INTERLEAVE_TYPE
*p_out
, const INTERLEAVE_TYPE
**pp_in
,
515 int i_nb_channels
, int i_samples
, uint8_t *pi_chan_table
)
517 for( int j
= 0; j
< i_samples
; j
++ )
518 for( int i
= 0; i
< i_nb_channels
; i
++ )
520 #ifdef MODULE_NAME_IS_tremor
521 union { int32_t i
; uint32_t u
;} spl
;
523 spl
.u
= ((uint32_t)pp_in
[i
][j
]) << 8;
524 p_out
[j
* i_nb_channels
+ pi_chan_table
[i
]] = spl
.i
;
526 p_out
[j
* i_nb_channels
+ pi_chan_table
[i
]] = pp_in
[i
][j
];
531 /*****************************************************************************
532 * DecodePacket: decodes a Vorbis packet.
533 *****************************************************************************/
534 static block_t
*DecodePacket( decoder_t
*p_dec
, ogg_packet
*p_oggpacket
)
536 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
539 INTERLEAVE_TYPE
**pp_pcm
;
541 if( p_oggpacket
->bytes
&&
542 vorbis_synthesis( &p_sys
->vb
, p_oggpacket
) == 0 )
543 vorbis_synthesis_blockin( &p_sys
->vd
, &p_sys
->vb
);
545 /* **pp_pcm is a multichannel float vector. In stereo, for
546 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
547 * the size of each channel. Convert the float values
548 * (-1.<=range<=1.) to whatever PCM format and write it out */
550 if( ( i_samples
= vorbis_synthesis_pcmout( &p_sys
->vd
, &pp_pcm
) ) > 0 )
553 block_t
*p_aout_buffer
;
555 if( decoder_UpdateAudioFormat( p_dec
) ) return NULL
;
557 decoder_NewAudioBuffer( p_dec
, i_samples
);
559 if( p_aout_buffer
== NULL
) return NULL
;
561 /* Interleave the samples */
562 Interleave( (INTERLEAVE_TYPE
*)p_aout_buffer
->p_buffer
,
563 (const INTERLEAVE_TYPE
**)pp_pcm
, p_sys
->vi
.channels
, i_samples
,
564 p_sys
->pi_chan_table
);
566 /* Tell libvorbis how many samples we actually consumed */
567 vorbis_synthesis_read( &p_sys
->vd
, i_samples
);
569 /* Date management */
570 p_aout_buffer
->i_pts
= date_Get( &p_sys
->end_date
);
571 p_aout_buffer
->i_length
= date_Increment( &p_sys
->end_date
,
572 i_samples
) - p_aout_buffer
->i_pts
;
573 return p_aout_buffer
;
581 /*****************************************************************************
582 * SendPacket: send an ogg dated packet to the stream output.
583 *****************************************************************************/
584 static block_t
*SendPacket( decoder_t
*p_dec
, ogg_packet
*p_oggpacket
,
587 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
588 int i_block_size
, i_samples
;
590 i_block_size
= vorbis_packet_blocksize( &p_sys
->vi
, p_oggpacket
);
591 if( i_block_size
< 0 ) i_block_size
= 0; /* non audio packet */
592 i_samples
= ( p_sys
->i_last_block_size
+ i_block_size
) >> 2;
593 p_sys
->i_last_block_size
= i_block_size
;
595 /* Date management */
596 p_block
->i_dts
= p_block
->i_pts
= date_Get( &p_sys
->end_date
);
598 p_block
->i_length
= date_Increment( &p_sys
->end_date
, i_samples
) - p_block
->i_pts
;
603 /*****************************************************************************
604 * ParseVorbisComments
605 *****************************************************************************/
606 static void ParseVorbisComments( decoder_t
*p_dec
)
608 char *psz_name
, *psz_value
, *psz_comment
;
611 while( i
< p_dec
->p_sys
->vc
.comments
)
613 psz_comment
= strdup( p_dec
->p_sys
->vc
.user_comments
[i
] );
616 psz_name
= psz_comment
;
617 psz_value
= strchr( psz_comment
, '=' );
618 /* Don't add empty values */
619 if( psz_value
&& psz_value
[1] != '\0')
624 if( !strcasecmp( psz_name
, "REPLAYGAIN_TRACK_GAIN" ) ||
625 !strcasecmp( psz_name
, "RG_RADIO" ) )
627 audio_replay_gain_t
*r
= &p_dec
->fmt_out
.audio_replay_gain
;
629 r
->pb_gain
[AUDIO_REPLAY_GAIN_TRACK
] = true;
630 r
->pf_gain
[AUDIO_REPLAY_GAIN_TRACK
] = us_atof( psz_value
);
632 else if( !strcasecmp( psz_name
, "REPLAYGAIN_TRACK_PEAK" ) ||
633 !strcasecmp( psz_name
, "RG_PEAK" ) )
635 audio_replay_gain_t
*r
= &p_dec
->fmt_out
.audio_replay_gain
;
637 r
->pb_peak
[AUDIO_REPLAY_GAIN_TRACK
] = true;
638 r
->pf_peak
[AUDIO_REPLAY_GAIN_TRACK
] = us_atof( psz_value
);
640 else if( !strcasecmp( psz_name
, "REPLAYGAIN_ALBUM_GAIN" ) ||
641 !strcasecmp( psz_name
, "RG_AUDIOPHILE" ) )
643 audio_replay_gain_t
*r
= &p_dec
->fmt_out
.audio_replay_gain
;
645 r
->pb_gain
[AUDIO_REPLAY_GAIN_ALBUM
] = true;
646 r
->pf_gain
[AUDIO_REPLAY_GAIN_ALBUM
] = us_atof( psz_value
);
648 else if( !strcasecmp( psz_name
, "REPLAYGAIN_ALBUM_PEAK" ) )
650 audio_replay_gain_t
*r
= &p_dec
->fmt_out
.audio_replay_gain
;
652 r
->pb_peak
[AUDIO_REPLAY_GAIN_ALBUM
] = true;
653 r
->pf_peak
[AUDIO_REPLAY_GAIN_ALBUM
] = us_atof( psz_value
);
655 else if( !strcasecmp( psz_name
, "METADATA_BLOCK_PICTURE" ) )
656 { /* Do nothing, for now */ }
659 if( !p_dec
->p_description
)
660 p_dec
->p_description
= vlc_meta_New();
661 if( p_dec
->p_description
)
662 vlc_meta_AddExtra( p_dec
->p_description
, psz_name
, psz_value
);
671 /*****************************************************************************
673 *****************************************************************************/
674 static void ConfigureChannelOrder(uint8_t *pi_chan_table
, int i_channels
,
675 uint32_t i_channel_mask
)
677 const uint32_t *pi_channels_in
;
681 pi_channels_in
= pi_8channels_in
;
684 pi_channels_in
= pi_7channels_in
;
688 pi_channels_in
= pi_6channels_in
;
691 pi_channels_in
= pi_4channels_in
;
694 pi_channels_in
= pi_3channels_in
;
697 for( int i
= 0; i
< i_channels
; ++i
)
698 pi_chan_table
[i
] = i
;
703 aout_CheckChannelReorder( pi_channels_in
, NULL
,
704 i_channel_mask
, pi_chan_table
);
707 /*****************************************************************************
708 * CloseDecoder: vorbis decoder destruction
709 *****************************************************************************/
710 static void CloseDecoder( vlc_object_t
*p_this
)
712 decoder_t
*p_dec
= (decoder_t
*)p_this
;
713 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
715 if( !p_sys
->b_packetizer
&& p_sys
->b_has_headers
)
717 vorbis_block_clear( &p_sys
->vb
);
718 vorbis_dsp_clear( &p_sys
->vd
);
721 vorbis_comment_clear( &p_sys
->vc
);
722 vorbis_info_clear( &p_sys
->vi
); /* must be called last */
727 #ifdef HAVE_VORBIS_ENCODER
728 /*****************************************************************************
729 * encoder_sys_t : vorbis encoder descriptor
730 *****************************************************************************/
736 vorbis_info vi
; /* struct that stores all the static vorbis bitstream
738 vorbis_comment vc
; /* struct that stores all the bitstream user
740 vorbis_dsp_state vd
; /* central working state for the packet->PCM
742 vorbis_block vb
; /* local working space for packet->PCM decode */
744 int i_last_block_size
;
748 ** Channel reordering
750 uint8_t pi_chan_table
[AOUT_CHAN_MAX
];
754 /*****************************************************************************
755 * OpenEncoder: probe the encoder and return score
756 *****************************************************************************/
757 static int OpenEncoder( vlc_object_t
*p_this
)
759 encoder_t
*p_enc
= (encoder_t
*)p_this
;
760 encoder_sys_t
*p_sys
;
761 int i_quality
, i_min_bitrate
, i_max_bitrate
;
762 ogg_packet header
[3];
764 if( p_enc
->fmt_out
.i_codec
!= VLC_CODEC_VORBIS
&&
770 /* Allocate the memory needed to store the decoder's structure */
771 if( ( p_sys
= (encoder_sys_t
*)malloc(sizeof(encoder_sys_t
)) ) == NULL
)
773 p_enc
->p_sys
= p_sys
;
775 p_enc
->pf_encode_audio
= Encode
;
776 p_enc
->fmt_in
.i_codec
= VLC_CODEC_FL32
;
777 p_enc
->fmt_out
.i_codec
= VLC_CODEC_VORBIS
;
779 if( p_enc
->fmt_in
.audio
.i_channels
>= ARRAY_SIZE(pi_channels_maps
) )
781 p_enc
->fmt_in
.audio
.i_channels
= ARRAY_SIZE(pi_channels_maps
) - 1;
782 msg_Warn( p_enc
, "lowering channel count to %u", p_enc
->fmt_in
.audio
.i_channels
);
784 config_ChainParse( p_enc
, ENC_CFG_PREFIX
, ppsz_enc_options
, p_enc
->p_cfg
);
786 i_quality
= var_GetInteger( p_enc
, ENC_CFG_PREFIX
"quality" );
787 if( i_quality
> 10 ) i_quality
= 10;
788 if( i_quality
< 0 ) i_quality
= 0;
790 if( var_GetBool( p_enc
, ENC_CFG_PREFIX
"cbr" ) ) i_quality
= 0;
791 i_max_bitrate
= var_GetInteger( p_enc
, ENC_CFG_PREFIX
"max-bitrate" );
792 i_min_bitrate
= var_GetInteger( p_enc
, ENC_CFG_PREFIX
"min-bitrate" );
794 /* Initialize vorbis encoder */
795 vorbis_info_init( &p_sys
->vi
);
800 if( vorbis_encode_setup_vbr( &p_sys
->vi
,
801 p_enc
->fmt_in
.audio
.i_channels
, p_enc
->fmt_in
.audio
.i_rate
,
804 vorbis_info_clear( &p_sys
->vi
);
805 free( p_enc
->p_sys
);
806 msg_Err( p_enc
, "VBR mode initialisation failed" );
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" );
839 free( p_enc
->p_sys
);
844 vorbis_encode_setup_init( &p_sys
->vi
);
847 vorbis_comment_init( &p_sys
->vc
);
848 vorbis_comment_add_tag( &p_sys
->vc
, "ENCODER", "VLC media player");
850 /* Set up the analysis state and auxiliary encoding storage */
851 vorbis_analysis_init( &p_sys
->vd
, &p_sys
->vi
);
852 vorbis_block_init( &p_sys
->vd
, &p_sys
->vb
);
854 /* Create and store headers */
855 vorbis_analysis_headerout( &p_sys
->vd
, &p_sys
->vc
,
856 &header
[0], &header
[1], &header
[2]);
857 for( int i
= 0; i
< 3; i
++ )
859 if( xiph_AppendHeaders( &p_enc
->fmt_out
.i_extra
, &p_enc
->fmt_out
.p_extra
,
860 header
[i
].bytes
, header
[i
].packet
) )
862 p_enc
->fmt_out
.i_extra
= 0;
863 p_enc
->fmt_out
.p_extra
= NULL
;
867 assert(p_sys
->vi
.channels
> 0 && (size_t) p_sys
->vi
.channels
< ARRAY_SIZE(pi_channels_maps
) );
869 p_enc
->fmt_out
.audio
.i_channels
= p_enc
->fmt_in
.audio
.i_channels
=
872 p_enc
->fmt_out
.audio
.i_physical_channels
=
873 p_enc
->fmt_in
.audio
.i_physical_channels
=
874 pi_channels_maps
[p_sys
->vi
.channels
];
876 p_sys
->i_last_block_size
= 0;
877 p_sys
->i_samples_delay
= 0;
879 ConfigureChannelOrder(p_sys
->pi_chan_table
, p_sys
->vi
.channels
,
880 p_enc
->fmt_in
.audio
.i_physical_channels
);
885 /****************************************************************************
886 * Encode: the whole thing
887 ****************************************************************************
888 * This function spits out ogg packets.
889 ****************************************************************************/
890 static block_t
*Encode( encoder_t
*p_enc
, block_t
*p_aout_buf
)
892 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
893 ogg_packet oggpacket
;
894 block_t
*p_block
, *p_chain
= NULL
;
897 /* Packets are already flushed, see bellow. */
898 if( unlikely( !p_aout_buf
) ) return NULL
;
900 mtime_t i_pts
= p_aout_buf
->i_pts
-
901 (mtime_t
)1000000 * (mtime_t
)p_sys
->i_samples_delay
/
902 (mtime_t
)p_enc
->fmt_in
.audio
.i_rate
;
904 p_sys
->i_samples_delay
+= p_aout_buf
->i_nb_samples
;
906 buffer
= vorbis_analysis_buffer( &p_sys
->vd
, p_aout_buf
->i_nb_samples
);
908 /* convert samples to float and uninterleave */
909 const unsigned i_channels
= p_enc
->fmt_in
.audio
.i_channels
;
910 for( unsigned int i
= 0; i
< i_channels
; i
++ )
912 for( unsigned int j
= 0 ; j
< p_aout_buf
->i_nb_samples
; j
++ )
914 buffer
[i
][j
]= ((float *)p_aout_buf
->p_buffer
)
915 [j
* i_channels
+ p_sys
->pi_chan_table
[i
]];
919 vorbis_analysis_wrote( &p_sys
->vd
, p_aout_buf
->i_nb_samples
);
921 while( vorbis_analysis_blockout( &p_sys
->vd
, &p_sys
->vb
) == 1 )
925 vorbis_analysis( &p_sys
->vb
, NULL
);
926 vorbis_bitrate_addblock( &p_sys
->vb
);
928 while( vorbis_bitrate_flushpacket( &p_sys
->vd
, &oggpacket
) )
931 p_block
= block_Alloc( oggpacket
.bytes
);
932 memcpy( p_block
->p_buffer
, oggpacket
.packet
, oggpacket
.bytes
);
934 i_block_size
= vorbis_packet_blocksize( &p_sys
->vi
, &oggpacket
);
936 if( i_block_size
< 0 ) i_block_size
= 0;
937 i_samples
= ( p_sys
->i_last_block_size
+ i_block_size
) >> 2;
938 p_sys
->i_last_block_size
= i_block_size
;
940 p_block
->i_length
= (mtime_t
)1000000 *
941 (mtime_t
)i_samples
/ (mtime_t
)p_enc
->fmt_in
.audio
.i_rate
;
943 p_block
->i_dts
= p_block
->i_pts
= i_pts
;
945 p_sys
->i_samples_delay
-= i_samples
;
948 i_pts
+= p_block
->i_length
;
949 block_ChainAppend( &p_chain
, p_block
);
956 /*****************************************************************************
957 * CloseEncoder: vorbis encoder destruction
958 *****************************************************************************/
959 static void CloseEncoder( vlc_object_t
*p_this
)
961 encoder_t
*p_enc
= (encoder_t
*)p_this
;
962 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
964 vorbis_block_clear( &p_sys
->vb
);
965 vorbis_dsp_clear( &p_sys
->vd
);
966 vorbis_comment_clear( &p_sys
->vc
);
967 vorbis_info_clear( &p_sys
->vi
); /* must be called last */
972 #endif /* HAVE_VORBIS_ENCODER */