packetizer: hevc: add poc debug
[vlc.git] / modules / codec / vorbis.c
blob6c30af825c85a73b1c0f5fbd7283234ac3f45d10
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 struct decoder_sys_t
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];
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, bool );
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 /*****************************************************************************
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 )
239 return VLC_EGENERIC;
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 ) )
244 return VLC_ENOMEM;
246 /* Misc init */
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;
259 #else
260 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
261 #endif
263 /* Set callbacks */
264 p_dec->pf_decode = DecodeAudio;
265 p_dec->pf_packetize = Packetize;
266 p_dec->pf_flush = Flush;
268 return VLC_SUCCESS;
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;
283 return i_ret;
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;
296 if( *pp_block )
298 /* Block to Ogg packet */
299 oggpacket.packet = (*pp_block)->p_buffer;
300 oggpacket.bytes = (*pp_block)->i_buffer;
302 else
304 if( p_sys->b_packetizer ) return NULL;
306 /* Block to Ogg packet */
307 oggpacket.packet = NULL;
308 oggpacket.bytes = 0;
311 oggpacket.granulepos = -1;
312 oggpacket.b_o_s = 0;
313 oggpacket.e_o_s = 0;
314 oggpacket.packetno = 0;
316 /* Check for headers */
317 if( !p_sys->b_has_headers )
319 if( ProcessHeaders( p_dec ) )
321 if( *pp_block )
322 block_Release( *pp_block );
323 return NULL;
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 */
345 return NULL;
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];
359 unsigned i_count;
360 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
361 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
362 return VLC_EGENERIC;
363 if( i_count < 3 )
364 return VLC_EGENERIC;
366 oggpacket.granulepos = -1;
367 oggpacket.e_o_s = 0;
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");
377 return VLC_EGENERIC;
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 );
389 return VLC_EGENERIC;
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 */
402 oggpacket.b_o_s = 0;
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" );
408 return VLC_EGENERIC;
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. */
415 oggpacket.b_o_s = 0;
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" );
421 return VLC_EGENERIC;
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 );
430 else
432 void* p_extra = realloc( p_dec->fmt_out.p_extra,
433 p_dec->fmt_in.i_extra );
434 if( unlikely( p_extra == NULL ) )
436 return VLC_ENOMEM;
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, true);
447 return VLC_SUCCESS;
450 /*****************************************************************************
451 * Flush:
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,
464 block_t **pp_block )
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 */
470 if( !p_block )
471 return NULL;
473 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
475 Flush( p_dec );
476 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
478 block_Release(p_block);
479 return NULL;
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 );
494 return NULL;
498 if( p_sys->b_packetizer )
500 return SendPacket( p_dec, p_oggpacket, p_block );
502 else
504 block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
505 if( p_block )
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;
525 #else
526 p_out[j * i_nb_channels + pi_chan_table[i]] = pp_in[i][j];
527 #endif
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;
537 int i_samples;
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;
556 p_aout_buffer =
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;
575 else
577 return NULL;
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,
585 block_t *p_block )
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;
600 return p_block;
603 /*****************************************************************************
604 * ParseVorbisComments
605 *****************************************************************************/
606 static void ParseVorbisComments( decoder_t *p_dec )
608 char *psz_name, *psz_value, *psz_comment;
609 int i = 0;
611 while( i < p_dec->p_sys->vc.comments )
613 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
614 if( !psz_comment )
615 break;
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')
621 *psz_value = '\0';
622 psz_value++;
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 */ }
657 else
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 );
666 free( psz_comment );
667 i++;
671 /*****************************************************************************
673 *****************************************************************************/
674 static void ConfigureChannelOrder(uint8_t *pi_chan_table, int i_channels,
675 uint32_t i_channel_mask, bool b_decode)
677 const uint32_t *pi_channels_in;
678 switch( i_channels )
680 case 8:
681 pi_channels_in = pi_8channels_in;
682 break;
683 case 7:
684 pi_channels_in = pi_7channels_in;
685 break;
686 case 6:
687 case 5:
688 pi_channels_in = pi_6channels_in;
689 break;
690 case 4:
691 pi_channels_in = pi_4channels_in;
692 break;
693 case 3:
694 pi_channels_in = pi_3channels_in;
695 break;
696 default:
697 for( int i = 0; i< i_channels; ++i )
698 pi_chan_table[i] = i;
700 return;
703 if( b_decode )
704 aout_CheckChannelReorder( pi_channels_in, NULL,
705 i_channel_mask, pi_chan_table );
706 else
707 aout_CheckChannelReorder( NULL, pi_channels_in,
708 i_channel_mask, pi_chan_table );
711 /*****************************************************************************
712 * CloseDecoder: vorbis decoder destruction
713 *****************************************************************************/
714 static void CloseDecoder( vlc_object_t *p_this )
716 decoder_t *p_dec = (decoder_t *)p_this;
717 decoder_sys_t *p_sys = p_dec->p_sys;
719 if( !p_sys->b_packetizer && p_sys->b_has_headers )
721 vorbis_block_clear( &p_sys->vb );
722 vorbis_dsp_clear( &p_sys->vd );
725 vorbis_comment_clear( &p_sys->vc );
726 vorbis_info_clear( &p_sys->vi ); /* must be called last */
728 free( p_sys );
731 #ifdef HAVE_VORBIS_ENCODER
732 /*****************************************************************************
733 * encoder_sys_t : vorbis encoder descriptor
734 *****************************************************************************/
735 struct encoder_sys_t
738 * Vorbis properties
740 vorbis_info vi; /* struct that stores all the static vorbis bitstream
741 settings */
742 vorbis_comment vc; /* struct that stores all the bitstream user
743 * comments */
744 vorbis_dsp_state vd; /* central working state for the packet->PCM
745 * decoder */
746 vorbis_block vb; /* local working space for packet->PCM decode */
748 int i_last_block_size;
749 int i_samples_delay;
750 unsigned int i_channels;
753 ** Channel reordering
755 uint8_t pi_chan_table[AOUT_CHAN_MAX];
759 /*****************************************************************************
760 * OpenEncoder: probe the encoder and return score
761 *****************************************************************************/
762 static int OpenEncoder( vlc_object_t *p_this )
764 encoder_t *p_enc = (encoder_t *)p_this;
765 encoder_sys_t *p_sys;
766 int i_quality, i_min_bitrate, i_max_bitrate;
767 ogg_packet header[3];
769 if( p_enc->fmt_out.i_codec != VLC_CODEC_VORBIS &&
770 !p_enc->obj.force )
772 return VLC_EGENERIC;
775 /* Allocate the memory needed to store the decoder's structure */
776 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
777 return VLC_ENOMEM;
778 p_enc->p_sys = p_sys;
780 p_enc->pf_encode_audio = Encode;
781 p_enc->fmt_in.i_codec = VLC_CODEC_FL32;
782 p_enc->fmt_out.i_codec = VLC_CODEC_VORBIS;
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 );
797 if( i_quality > 0 )
799 /* VBR mode */
800 if( vorbis_encode_setup_vbr( &p_sys->vi,
801 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
802 i_quality * 0.1 ) )
804 vorbis_info_clear( &p_sys->vi );
805 free( p_enc->p_sys );
806 msg_Err( p_enc, "VBR mode initialisation failed" );
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" );
839 free( p_enc->p_sys );
840 return VLC_EGENERIC;
844 vorbis_encode_setup_init( &p_sys->vi );
846 /* Add a comment */
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 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
868 p_sys->i_last_block_size = 0;
869 p_sys->i_samples_delay = 0;
871 ConfigureChannelOrder(p_sys->pi_chan_table, p_sys->vi.channels,
872 p_enc->fmt_in.audio.i_physical_channels, true);
874 return VLC_SUCCESS;
877 /****************************************************************************
878 * Encode: the whole thing
879 ****************************************************************************
880 * This function spits out ogg packets.
881 ****************************************************************************/
882 static block_t *Encode( encoder_t *p_enc, block_t *p_aout_buf )
884 encoder_sys_t *p_sys = p_enc->p_sys;
885 ogg_packet oggpacket;
886 block_t *p_block, *p_chain = NULL;
887 float **buffer;
889 /* FIXME: flush buffers in here */
890 if( unlikely( !p_aout_buf ) ) return NULL;
892 mtime_t i_pts = p_aout_buf->i_pts -
893 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
894 (mtime_t)p_enc->fmt_in.audio.i_rate;
896 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
898 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
900 /* convert samples to float and uninterleave */
901 for( unsigned int i = 0; i < p_sys->i_channels; i++ )
903 for( unsigned int j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
905 buffer[i][j]= ((float *)p_aout_buf->p_buffer)
906 [j * p_sys->i_channels + p_sys->pi_chan_table[i]];
910 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
912 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
914 int i_samples;
916 vorbis_analysis( &p_sys->vb, NULL );
917 vorbis_bitrate_addblock( &p_sys->vb );
919 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
921 int i_block_size;
922 p_block = block_Alloc( oggpacket.bytes );
923 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
925 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
927 if( i_block_size < 0 ) i_block_size = 0;
928 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
929 p_sys->i_last_block_size = i_block_size;
931 p_block->i_length = (mtime_t)1000000 *
932 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
934 p_block->i_dts = p_block->i_pts = i_pts;
936 p_sys->i_samples_delay -= i_samples;
938 /* Update pts */
939 i_pts += p_block->i_length;
940 block_ChainAppend( &p_chain, p_block );
944 return p_chain;
947 /*****************************************************************************
948 * CloseEncoder: vorbis encoder destruction
949 *****************************************************************************/
950 static void CloseEncoder( vlc_object_t *p_this )
952 encoder_t *p_enc = (encoder_t *)p_this;
953 encoder_sys_t *p_sys = p_enc->p_sys;
955 vorbis_block_clear( &p_sys->vb );
956 vorbis_dsp_clear( &p_sys->vd );
957 vorbis_comment_clear( &p_sys->vc );
958 vorbis_info_clear( &p_sys->vi ); /* must be called last */
960 free( p_sys );
963 #endif /* HAVE_VORBIS_ENCODER */