]> git.sesse.net Git - vlc/blob - modules/codec/vorbis.c
Improvements to preferences
[vlc] / modules / codec / vorbis.c
1 /*****************************************************************************
2  * vorbis.c: vorbis decoder/encoder/packetizer module making use of libvorbis.
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/decoder.h>
29 #include <vlc/input.h>
30 #include <vlc/sout.h>
31
32 #include <ogg/ogg.h>
33
34 #ifdef MODULE_NAME_IS_tremor
35 #include <tremor/ivorbiscodec.h>
36
37 #else
38 #include <vorbis/codec.h>
39
40 /* vorbis header */
41 #ifdef HAVE_VORBIS_VORBISENC_H
42 #   include <vorbis/vorbisenc.h>
43 #   ifndef OV_ECTL_RATEMANAGE_AVG
44 #       define OV_ECTL_RATEMANAGE_AVG 0x0
45 #   endif
46 #endif
47
48 #endif
49
50 /*****************************************************************************
51  * decoder_sys_t : vorbis decoder descriptor
52  *****************************************************************************/
53 struct decoder_sys_t
54 {
55     /* Module mode */
56     vlc_bool_t b_packetizer;
57
58     /*
59      * Input properties
60      */
61     int i_headers;
62
63     /*
64      * Vorbis properties
65      */
66     vorbis_info      vi; /* struct that stores all the static vorbis bitstream
67                             settings */
68     vorbis_comment   vc; /* struct that stores all the bitstream user
69                           * comments */
70     vorbis_dsp_state vd; /* central working state for the packet->PCM
71                           * decoder */
72     vorbis_block     vb; /* local working space for packet->PCM decode */
73
74     /*
75      * Common properties
76      */
77     audio_date_t end_date;
78     int          i_last_block_size;
79
80 };
81
82 static int pi_channels_maps[7] =
83 {
84     0,
85     AOUT_CHAN_CENTER,
86     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
87     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
88     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
89      | AOUT_CHAN_REARRIGHT,
90     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
91      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
92     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
93      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
94 };
95
96 /****************************************************************************
97  * Local prototypes
98  ****************************************************************************/
99 static int  OpenDecoder   ( vlc_object_t * );
100 static int  OpenPacketizer( vlc_object_t * );
101 static void CloseDecoder  ( vlc_object_t * );
102 static void *DecodeBlock  ( decoder_t *, block_t ** );
103
104 static int  ProcessHeaders( decoder_t * );
105 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
106
107 static aout_buffer_t *DecodePacket  ( decoder_t *, ogg_packet * );
108 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
109
110 static void ParseVorbisComments( decoder_t * );
111
112 #ifdef MODULE_NAME_IS_tremor
113 static void Interleave   ( int32_t *, const int32_t **, int, int );
114 #else
115 static void Interleave   ( float *, const float **, int, int );
116 #endif
117
118 #ifndef MODULE_NAME_IS_tremor
119 static int OpenEncoder   ( vlc_object_t * );
120 static void CloseEncoder ( vlc_object_t * );
121 static block_t *Encode   ( encoder_t *, aout_buffer_t * );
122 #endif
123
124 /*****************************************************************************
125  * Module descriptor
126  *****************************************************************************/
127 #define ENC_QUALITY_TEXT N_("Encoding quality")
128 #define ENC_QUALITY_LONGTEXT N_( \
129   "Allows you to specify a quality between 1 (low) and 10 (high), instead " \
130   "of specifying a particular bitrate. This will produce a VBR stream." )
131 #define ENC_MAXBR_TEXT N_("Maximum encoding bitrate")
132 #define ENC_MAXBR_LONGTEXT N_( \
133   "Allows you to specify a maximum bitrate in kbps. " \
134   "Useful for streaming applications." )
135 #define ENC_MINBR_TEXT N_("Minimum encoding bitrate")
136 #define ENC_MINBR_LONGTEXT N_( \
137   "Allows you to specify a minimum bitrate in kbps. " \
138   "Useful for encoding for a fixed-size channel." )
139 #define ENC_CBR_TEXT N_("CBR encoding")
140 #define ENC_CBR_LONGTEXT N_( \
141   "Allows you to force a constant bitrate encoding (CBR)." )
142
143 vlc_module_begin();
144
145     set_description( _("Vorbis audio decoder") );
146 #ifdef MODULE_NAME_IS_tremor
147     set_capability( "decoder", 90 );
148 #else
149     set_capability( "decoder", 100 );
150 #endif
151     set_category( CAT_INPUT );
152     set_subcategory( SUBCAT_INPUT_ACODEC );
153     set_callbacks( OpenDecoder, CloseDecoder );
154
155     add_submodule();
156     set_description( _("Vorbis audio packetizer") );
157     set_capability( "packetizer", 100 );
158     set_callbacks( OpenPacketizer, CloseDecoder );
159
160 #ifndef MODULE_NAME_IS_tremor
161 #   define ENC_CFG_PREFIX "sout-vorbis-"
162     add_submodule();
163     set_description( _("Vorbis audio encoder") );
164     set_capability( "encoder", 100 );
165     set_callbacks( OpenEncoder, CloseEncoder );
166
167     add_integer( ENC_CFG_PREFIX "quality", 3, NULL, ENC_QUALITY_TEXT,
168                  ENC_QUALITY_LONGTEXT, VLC_FALSE );
169     add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBR_TEXT,
170                  ENC_MAXBR_LONGTEXT, VLC_FALSE );
171     add_integer( ENC_CFG_PREFIX "min-bitrate", 0, NULL, ENC_MINBR_TEXT,
172                  ENC_MINBR_LONGTEXT, VLC_FALSE );
173     add_bool( ENC_CFG_PREFIX "cbr", 0, NULL, ENC_CBR_TEXT,
174                  ENC_CBR_LONGTEXT, VLC_FALSE );
175 #endif
176
177 vlc_module_end();
178
179 #ifndef MODULE_NAME_IS_tremor
180 static const char *ppsz_enc_options[] = {
181     "quality", "max-bitrate", "min-bitrate", "cbr", NULL
182 };
183 #endif
184
185 /*****************************************************************************
186  * OpenDecoder: probe the decoder and return score
187  *****************************************************************************/
188 static int OpenDecoder( vlc_object_t *p_this )
189 {
190     decoder_t *p_dec = (decoder_t*)p_this;
191     decoder_sys_t *p_sys;
192
193     if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
194     {
195         return VLC_EGENERIC;
196     }
197
198     /* Allocate the memory needed to store the decoder's structure */
199     if( ( p_dec->p_sys = p_sys =
200           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
201     {
202         msg_Err( p_dec, "out of memory" );
203         return VLC_EGENERIC;
204     }
205
206     /* Misc init */
207     aout_DateSet( &p_sys->end_date, 0 );
208     p_sys->b_packetizer = VLC_FALSE;
209     p_sys->i_headers = 0;
210
211     /* Take care of vorbis init */
212     vorbis_info_init( &p_sys->vi );
213     vorbis_comment_init( &p_sys->vc );
214
215     /* Set output properties */
216     p_dec->fmt_out.i_cat = AUDIO_ES;
217 #ifdef MODULE_NAME_IS_tremor
218     p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
219 #else
220     p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
221 #endif
222
223     /* Set callbacks */
224     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
225         DecodeBlock;
226     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
227         DecodeBlock;
228
229     return VLC_SUCCESS;
230 }
231
232 static int OpenPacketizer( vlc_object_t *p_this )
233 {
234     decoder_t *p_dec = (decoder_t*)p_this;
235
236     int i_ret = OpenDecoder( p_this );
237
238     if( i_ret == VLC_SUCCESS )
239     {
240         p_dec->p_sys->b_packetizer = VLC_TRUE;
241         p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
242     }
243
244     return i_ret;
245 }
246
247 /****************************************************************************
248  * DecodeBlock: the whole thing
249  ****************************************************************************
250  * This function must be fed with ogg packets.
251  ****************************************************************************/
252 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
253 {
254     decoder_sys_t *p_sys = p_dec->p_sys;
255     ogg_packet oggpacket;
256
257     if( !pp_block ) return NULL;
258
259     if( *pp_block )
260     {
261         /* Block to Ogg packet */
262         oggpacket.packet = (*pp_block)->p_buffer;
263         oggpacket.bytes = (*pp_block)->i_buffer;
264     }
265     else
266     {
267         if( p_sys->b_packetizer ) return NULL;
268
269         /* Block to Ogg packet */
270         oggpacket.packet = NULL;
271         oggpacket.bytes = 0;
272     }
273
274     oggpacket.granulepos = -1;
275     oggpacket.b_o_s = 0;
276     oggpacket.e_o_s = 0;
277     oggpacket.packetno = 0;
278
279     /* Check for headers */
280     if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
281     {
282         /* Headers already available as extra data */
283         p_sys->i_headers = 3;
284     }
285     else if( oggpacket.bytes && p_sys->i_headers < 3 )
286     {
287         /* Backup headers as extra data */
288         uint8_t *p_extra;
289
290         p_dec->fmt_in.p_extra =
291             realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
292                      oggpacket.bytes + 2 );
293         p_extra = p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra;
294         *(p_extra++) = oggpacket.bytes >> 8;
295         *(p_extra++) = oggpacket.bytes & 0xFF;
296
297         memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
298         p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
299
300         block_Release( *pp_block );
301         p_sys->i_headers++;
302         return NULL;
303     }
304
305     if( p_sys->i_headers == 3 )
306     {
307         if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
308         {
309             p_sys->i_headers = 0;
310             p_dec->fmt_in.i_extra = 0;
311             block_Release( *pp_block );
312             return NULL;
313         }
314         else p_sys->i_headers++;
315     }
316
317     return ProcessPacket( p_dec, &oggpacket, pp_block );
318 }
319
320 /*****************************************************************************
321  * ProcessHeaders: process Vorbis headers.
322  *****************************************************************************/
323 static int ProcessHeaders( decoder_t *p_dec )
324 {
325     decoder_sys_t *p_sys = p_dec->p_sys;
326     ogg_packet oggpacket;
327     uint8_t *p_extra;
328     int i_extra;
329
330     if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
331
332     oggpacket.granulepos = -1;
333     oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
334     oggpacket.e_o_s = 0;
335     oggpacket.packetno = 0;
336     p_extra = p_dec->fmt_in.p_extra;
337     i_extra = p_dec->fmt_in.i_extra;
338
339     /* Take care of the initial Vorbis header */
340     oggpacket.bytes = *(p_extra++) << 8;
341     oggpacket.bytes |= (*(p_extra++) & 0xFF);
342     oggpacket.packet = p_extra;
343     p_extra += oggpacket.bytes;
344     i_extra -= (oggpacket.bytes + 2);
345     if( i_extra < 0 )
346     {
347         msg_Err( p_dec, "header data corrupted");
348         return VLC_EGENERIC;
349     }
350
351     if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
352     {
353         msg_Err( p_dec, "this bitstream does not contain Vorbis audio data");
354         return VLC_EGENERIC;
355     }
356
357     /* Setup the format */
358     p_dec->fmt_out.audio.i_rate     = p_sys->vi.rate;
359     p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
360     p_dec->fmt_out.audio.i_physical_channels =
361         p_dec->fmt_out.audio.i_original_channels =
362             pi_channels_maps[p_sys->vi.channels];
363     p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
364
365     aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
366     aout_DateSet( &p_sys->end_date, 0 );
367
368     msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
369              p_sys->vi.channels, p_sys->vi.rate, p_sys->vi.bitrate_nominal );
370
371     /* The next packet in order is the comments header */
372     oggpacket.b_o_s = 0;
373     oggpacket.bytes = *(p_extra++) << 8;
374     oggpacket.bytes |= (*(p_extra++) & 0xFF);
375     oggpacket.packet = p_extra;
376     p_extra += oggpacket.bytes;
377     i_extra -= (oggpacket.bytes + 2);
378     if( i_extra < 0 )
379     {
380         msg_Err( p_dec, "header data corrupted");
381         return VLC_EGENERIC;
382     }
383
384     if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
385     {
386         msg_Err( p_dec, "2nd Vorbis header is corrupted" );
387         return VLC_EGENERIC;
388     }
389     ParseVorbisComments( p_dec );
390
391     /* The next packet in order is the codebooks header
392      * We need to watch out that this packet is not missing as a
393      * missing or corrupted header is fatal. */
394     oggpacket.bytes = *(p_extra++) << 8;
395     oggpacket.bytes |= (*(p_extra++) & 0xFF);
396     oggpacket.packet = p_extra;
397     i_extra -= (oggpacket.bytes + 2);
398     if( i_extra < 0 )
399     {
400         msg_Err( p_dec, "header data corrupted");
401         return VLC_EGENERIC;
402     }
403
404     if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket ) < 0 )
405     {
406         msg_Err( p_dec, "3rd Vorbis header is corrupted" );
407         return VLC_EGENERIC;
408     }
409
410     if( !p_sys->b_packetizer )
411     {
412         /* Initialize the Vorbis packet->PCM decoder */
413         vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
414         vorbis_block_init( &p_sys->vd, &p_sys->vb );
415     }
416     else
417     {
418         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
419         p_dec->fmt_out.p_extra =
420             realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
421         memcpy( p_dec->fmt_out.p_extra,
422                 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
423     }
424
425     return VLC_SUCCESS;
426 }
427
428 /*****************************************************************************
429  * ProcessPacket: processes a Vorbis packet.
430  *****************************************************************************/
431 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
432                             block_t **pp_block )
433 {
434     decoder_sys_t *p_sys = p_dec->p_sys;
435     block_t *p_block = *pp_block;
436
437     /* Date management */
438     if( p_block && p_block->i_pts > 0 &&
439         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
440     {
441         aout_DateSet( &p_sys->end_date, p_block->i_pts );
442     }
443
444     if( !aout_DateGet( &p_sys->end_date ) )
445     {
446         /* We've just started the stream, wait for the first PTS. */
447         if( p_block ) block_Release( p_block );
448         return NULL;
449     }
450
451     *pp_block = NULL; /* To avoid being fed the same packet again */
452
453     if( p_sys->b_packetizer )
454     {
455         return SendPacket( p_dec, p_oggpacket, p_block );
456     }
457     else
458     {
459         aout_buffer_t *p_aout_buffer;
460
461         if( p_sys->i_headers >= 3 )
462             p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
463         else
464             p_aout_buffer = NULL;
465
466         if( p_block ) block_Release( p_block );
467         return p_aout_buffer;
468     }
469 }
470
471 /*****************************************************************************
472  * DecodePacket: decodes a Vorbis packet.
473  *****************************************************************************/
474 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
475 {
476     decoder_sys_t *p_sys = p_dec->p_sys;
477     int           i_samples;
478
479 #ifdef MODULE_NAME_IS_tremor
480     int32_t       **pp_pcm;
481 #else
482     float         **pp_pcm;
483 #endif
484
485     if( p_oggpacket->bytes &&
486         vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
487         vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
488
489     /* **pp_pcm is a multichannel float vector. In stereo, for
490      * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
491      * the size of each channel. Convert the float values
492      * (-1.<=range<=1.) to whatever PCM format and write it out */
493
494     if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
495     {
496
497         aout_buffer_t *p_aout_buffer;
498
499         p_aout_buffer =
500             p_dec->pf_aout_buffer_new( p_dec, i_samples );
501
502         if( p_aout_buffer == NULL ) return NULL;
503
504         /* Interleave the samples */
505 #ifdef MODULE_NAME_IS_tremor
506         Interleave( (int32_t *)p_aout_buffer->p_buffer,
507                     (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples );
508 #else
509         Interleave( (float *)p_aout_buffer->p_buffer,
510                     (const float **)pp_pcm, p_sys->vi.channels, i_samples );
511 #endif
512
513         /* Tell libvorbis how many samples we actually consumed */
514         vorbis_synthesis_read( &p_sys->vd, i_samples );
515
516         /* Date management */
517         p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
518         p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
519                                                       i_samples );
520         return p_aout_buffer;
521     }
522     else
523     {
524         return NULL;
525     }
526 }
527
528 /*****************************************************************************
529  * SendPacket: send an ogg dated packet to the stream output.
530  *****************************************************************************/
531 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
532                             block_t *p_block )
533 {
534     decoder_sys_t *p_sys = p_dec->p_sys;
535     int i_block_size, i_samples;
536
537     i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
538     if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
539     i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
540     p_sys->i_last_block_size = i_block_size;
541
542     /* Date management */
543     p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
544
545     if( p_sys->i_headers >= 3 )
546         p_block->i_length = aout_DateIncrement( &p_sys->end_date, i_samples ) -
547             p_block->i_pts;
548     else
549         p_block->i_length = 0;
550
551     return p_block;
552 }
553
554 /*****************************************************************************
555  * ParseVorbisComments: FIXME should be done in demuxer
556  *****************************************************************************/
557 static void ParseVorbisComments( decoder_t *p_dec )
558 {
559     input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
560     char *psz_name, *psz_value, *psz_comment;
561     int i = 0;
562
563     if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
564
565     while( i < p_dec->p_sys->vc.comments )
566     {
567         psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
568         if( !psz_comment )
569         {
570             msg_Warn( p_dec, "out of memory" );
571             break;
572         }
573         psz_name = psz_comment;
574         psz_value = strchr( psz_comment, '=' );
575         if( psz_value )
576         {
577             *psz_value = '\0';
578             psz_value++;
579             input_Control( p_input, INPUT_ADD_INFO, _("Vorbis comment"),
580                            psz_name, psz_value );
581             /* HACK, we should use meta */
582             if( strstr( psz_name, "artist" ) )
583             {
584                 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
585                                _("Artist"), psz_value );
586             }
587             else if( strstr( psz_name, "title" ) )
588             {
589                 p_input->input.p_item->psz_name = strdup( psz_value );
590             }
591         }
592         /* FIXME */
593         var_SetInteger( p_input, "item-change", p_input->input.p_item->i_id );
594         free( psz_comment );
595         i++;
596     }
597 }
598
599 /*****************************************************************************
600  * Interleave: helper function to interleave channels
601  *****************************************************************************/
602 static void Interleave(
603 #ifdef MODULE_NAME_IS_tremor
604                         int32_t *p_out, const int32_t **pp_in,
605 #else
606                         float *p_out, const float **pp_in,
607 #endif
608                         int i_nb_channels, int i_samples )
609 {
610     int i, j;
611
612     for ( j = 0; j < i_samples; j++ )
613     {
614         for ( i = 0; i < i_nb_channels; i++ )
615         {
616             p_out[j * i_nb_channels + i] = pp_in[i][j];
617         }
618     }
619 }
620
621 /*****************************************************************************
622  * CloseDecoder: vorbis decoder destruction
623  *****************************************************************************/
624 static void CloseDecoder( vlc_object_t *p_this )
625 {
626     decoder_t *p_dec = (decoder_t *)p_this;
627     decoder_sys_t *p_sys = p_dec->p_sys;
628
629     if( !p_sys->b_packetizer && p_sys->i_headers >= 3 )
630     {
631         vorbis_block_clear( &p_sys->vb );
632         vorbis_dsp_clear( &p_sys->vd );
633     }
634
635     vorbis_comment_clear( &p_sys->vc );
636     vorbis_info_clear( &p_sys->vi );  /* must be called last */
637
638     free( p_sys );
639 }
640
641 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
642
643 /*****************************************************************************
644  * encoder_sys_t : vorbis encoder descriptor
645  *****************************************************************************/
646 struct encoder_sys_t
647 {
648     /*
649      * Vorbis properties
650      */
651     vorbis_info      vi; /* struct that stores all the static vorbis bitstream
652                             settings */
653     vorbis_comment   vc; /* struct that stores all the bitstream user
654                           * comments */
655     vorbis_dsp_state vd; /* central working state for the packet->PCM
656                           * decoder */
657     vorbis_block     vb; /* local working space for packet->PCM decode */
658
659     int i_last_block_size;
660     int i_samples_delay;
661     int i_channels;
662
663     /*
664      * Common properties
665      */
666     mtime_t i_pts;
667 };
668
669 /*****************************************************************************
670  * OpenEncoder: probe the encoder and return score
671  *****************************************************************************/
672 static int OpenEncoder( vlc_object_t *p_this )
673 {
674     encoder_t *p_enc = (encoder_t *)p_this;
675     encoder_sys_t *p_sys;
676     int i_quality, i_min_bitrate, i_max_bitrate, i;
677     ogg_packet header[3];
678     vlc_value_t val;
679     uint8_t *p_extra;
680
681     if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') &&
682         !p_enc->b_force )
683     {
684         return VLC_EGENERIC;
685     }
686
687     /* Allocate the memory needed to store the decoder's structure */
688     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
689     {
690         msg_Err( p_enc, "out of memory" );
691         return VLC_EGENERIC;
692     }
693     p_enc->p_sys = p_sys;
694
695     p_enc->pf_encode_audio = Encode;
696     p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
697     p_enc->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
698
699     sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
700
701     var_Get( p_enc, ENC_CFG_PREFIX "quality", &val );
702     i_quality = val.i_int;
703     if( i_quality > 10 ) i_quality = 10;
704     if( i_quality < 0 ) i_quality = 0;
705     var_Get( p_enc, ENC_CFG_PREFIX "cbr", &val );
706     if( val.b_bool ) i_quality = 0;
707     var_Get( p_enc, ENC_CFG_PREFIX "max-bitrate", &val );
708     i_max_bitrate = val.i_int;
709     var_Get( p_enc, ENC_CFG_PREFIX "min-bitrate", &val );
710     i_min_bitrate = val.i_int;
711
712     /* Initialize vorbis encoder */
713     vorbis_info_init( &p_sys->vi );
714
715     if( i_quality > 0 )
716     {
717         /* VBR mode */
718         if( vorbis_encode_setup_vbr( &p_sys->vi,
719               p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
720               i_quality * 0.1 ) )
721         {
722             vorbis_info_clear( &p_sys->vi );
723             free( p_enc->p_sys );
724             msg_Err( p_enc, "VBR mode initialisation failed" );
725             return VLC_EGENERIC;
726         }
727
728         /* Do we have optional hard quality restrictions? */
729         if( i_max_bitrate > 0 || i_min_bitrate > 0 )
730         {
731             struct ovectl_ratemanage_arg ai;
732             vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_GET, &ai );
733
734             ai.bitrate_hard_min = i_min_bitrate;
735             ai.bitrate_hard_max = i_max_bitrate;
736             ai.management_active = 1;
737
738             vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, &ai );
739
740         }
741         else
742         {
743             /* Turn off management entirely */
744             vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_SET, NULL );
745         }
746     }
747     else
748     {
749         if( vorbis_encode_setup_managed( &p_sys->vi,
750               p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
751               i_min_bitrate > 0 ? i_min_bitrate * 1000: -1,
752               p_enc->fmt_out.i_bitrate,
753               i_max_bitrate > 0 ? i_max_bitrate * 1000: -1 ) )
754           {
755               vorbis_info_clear( &p_sys->vi );
756               msg_Err( p_enc, "CBR mode initialisation failed" );
757               free( p_enc->p_sys );
758               return VLC_EGENERIC;
759           }
760     }
761
762     vorbis_encode_setup_init( &p_sys->vi );
763
764     /* Add a comment */
765     vorbis_comment_init( &p_sys->vc);
766     vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
767
768     /* Set up the analysis state and auxiliary encoding storage */
769     vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
770     vorbis_block_init( &p_sys->vd, &p_sys->vb );
771
772     /* Create and store headers */
773     vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
774                                &header[0], &header[1], &header[2]);
775     p_enc->fmt_out.i_extra = 3 * 2 + header[0].bytes +
776        header[1].bytes + header[2].bytes;
777     p_extra = p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
778     for( i = 0; i < 3; i++ )
779     {
780         *(p_extra++) = header[i].bytes >> 8;
781         *(p_extra++) = header[i].bytes & 0xFF;
782         memcpy( p_extra, header[i].packet, header[i].bytes );
783         p_extra += header[i].bytes;
784     }
785
786     p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
787     p_sys->i_last_block_size = 0;
788     p_sys->i_samples_delay = 0;
789     p_sys->i_pts = 0;
790
791     return VLC_SUCCESS;
792 }
793
794 /****************************************************************************
795  * Encode: the whole thing
796  ****************************************************************************
797  * This function spits out ogg packets.
798  ****************************************************************************/
799 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
800 {
801     encoder_sys_t *p_sys = p_enc->p_sys;
802     ogg_packet oggpacket;
803     block_t *p_block, *p_chain = NULL;
804     float **buffer;
805     int i;
806     unsigned int j;
807
808     p_sys->i_pts = p_aout_buf->start_date -
809                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
810                 (mtime_t)p_enc->fmt_in.audio.i_rate;
811
812     p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
813
814     buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
815
816     /* convert samples to float and uninterleave */
817     for( i = 0; i < p_sys->i_channels; i++ )
818     {
819         for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
820         {
821             buffer[i][j]= ((float *)p_aout_buf->p_buffer)
822                                     [j * p_sys->i_channels + i ];
823         }
824     }
825
826     vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
827
828     while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
829     {
830         int i_samples;
831
832         vorbis_analysis( &p_sys->vb, NULL );
833         vorbis_bitrate_addblock( &p_sys->vb );
834
835         while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
836         {
837             int i_block_size;
838             p_block = block_New( p_enc, oggpacket.bytes );
839             memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
840
841             i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
842
843             if( i_block_size < 0 ) i_block_size = 0;
844             i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
845             p_sys->i_last_block_size = i_block_size;
846
847             p_block->i_length = (mtime_t)1000000 *
848                 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
849
850             p_block->i_dts = p_block->i_pts = p_sys->i_pts;
851
852             p_sys->i_samples_delay -= i_samples;
853
854             /* Update pts */
855             p_sys->i_pts += p_block->i_length;
856             block_ChainAppend( &p_chain, p_block );
857         }
858     }
859
860     return p_chain;
861 }
862
863 /*****************************************************************************
864  * CloseEncoder: vorbis encoder destruction
865  *****************************************************************************/
866 static void CloseEncoder( vlc_object_t *p_this )
867 {
868     encoder_t *p_enc = (encoder_t *)p_this;
869     encoder_sys_t *p_sys = p_enc->p_sys;
870
871     vorbis_block_clear( &p_sys->vb );
872     vorbis_dsp_clear( &p_sys->vd );
873     vorbis_comment_clear( &p_sys->vc );
874     vorbis_info_clear( &p_sys->vi );  /* must be called last */
875
876     free( p_sys );
877 }
878
879 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */