]> git.sesse.net Git - vlc/blob - modules/codec/vorbis.c
* all: compilation warning fixes (mainly missings headers).
[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: vorbis.c,v 1.32 2004/02/22 15:57:41 fenrir Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
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_playlist.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 void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
105
106 static aout_buffer_t *DecodePacket  ( decoder_t *, ogg_packet * );
107 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
108
109 static void ParseVorbisComments( decoder_t * );
110
111 #ifdef MODULE_NAME_IS_tremor
112 static void Interleave   ( int32_t *, const int32_t **, int, int );
113 #else
114 static void Interleave   ( float *, const float **, int, int );
115 #endif
116
117 #ifndef MODULE_NAME_IS_tremor
118 static int OpenEncoder   ( vlc_object_t * );
119 static void CloseEncoder ( vlc_object_t * );
120 static block_t *Headers  ( encoder_t * );
121 static block_t *Encode   ( encoder_t *, aout_buffer_t * );
122 #endif
123
124 /*****************************************************************************
125  * Module descriptor
126  *****************************************************************************/
127 vlc_module_begin();
128
129     set_description( _("Vorbis audio decoder") );
130 #ifdef MODULE_NAME_IS_tremor
131     set_capability( "decoder", 90 );
132 #else
133     set_capability( "decoder", 100 );
134 #endif
135     set_callbacks( OpenDecoder, CloseDecoder );
136
137     add_submodule();
138     set_description( _("Vorbis audio packetizer") );
139     set_capability( "packetizer", 100 );
140     set_callbacks( OpenPacketizer, CloseDecoder );
141
142 #ifndef MODULE_NAME_IS_tremor
143     add_submodule();
144     set_description( _("Vorbis audio encoder") );
145     set_capability( "encoder", 100 );
146     set_callbacks( OpenEncoder, CloseEncoder );
147 #endif
148
149 vlc_module_end();
150
151 /*****************************************************************************
152  * OpenDecoder: probe the decoder and return score
153  *****************************************************************************/
154 static int OpenDecoder( vlc_object_t *p_this )
155 {
156     decoder_t *p_dec = (decoder_t*)p_this;
157     decoder_sys_t *p_sys;
158
159     if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
160     {
161         return VLC_EGENERIC;
162     }
163
164     /* Allocate the memory needed to store the decoder's structure */
165     if( ( p_dec->p_sys = p_sys =
166           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
167     {
168         msg_Err( p_dec, "out of memory" );
169         return VLC_EGENERIC;
170     }
171
172     /* Misc init */
173     aout_DateSet( &p_sys->end_date, 0 );
174     p_sys->b_packetizer = VLC_FALSE;
175     p_sys->i_headers = 0;
176
177     /* Take care of vorbis init */
178     vorbis_info_init( &p_sys->vi );
179     vorbis_comment_init( &p_sys->vc );
180
181     /* Set output properties */
182     p_dec->fmt_out.i_cat = AUDIO_ES;
183 #ifdef MODULE_NAME_IS_tremor
184     p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
185 #else
186     p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
187 #endif
188
189     /* Set callbacks */
190     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
191         DecodeBlock;
192     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
193         DecodeBlock;
194
195     return VLC_SUCCESS;
196 }
197
198 static int OpenPacketizer( vlc_object_t *p_this )
199 {
200     decoder_t *p_dec = (decoder_t*)p_this;
201
202     int i_ret = OpenDecoder( p_this );
203
204     if( i_ret == VLC_SUCCESS )
205     {
206         p_dec->p_sys->b_packetizer = VLC_TRUE;
207         p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
208     }
209
210     return i_ret;
211 }
212
213 /****************************************************************************
214  * DecodeBlock: the whole thing
215  ****************************************************************************
216  * This function must be fed with ogg packets.
217  ****************************************************************************/
218 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
219 {
220     decoder_sys_t *p_sys = p_dec->p_sys;
221     ogg_packet oggpacket;
222
223     if( !pp_block ) return NULL;
224
225     if( *pp_block )
226     {
227         /* Block to Ogg packet */
228         oggpacket.packet = (*pp_block)->p_buffer;
229         oggpacket.bytes = (*pp_block)->i_buffer;
230     }
231     else
232     {
233         if( p_sys->b_packetizer ) return NULL;
234
235         /* Block to Ogg packet */
236         oggpacket.packet = NULL;
237         oggpacket.bytes = 0;
238     }
239
240     oggpacket.granulepos = -1;
241     oggpacket.b_o_s = 0;
242     oggpacket.e_o_s = 0;
243     oggpacket.packetno = 0;
244
245     if( p_sys->i_headers == 0 )
246     {
247         /* Take care of the initial Vorbis header */
248
249         oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
250         if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc,
251                                        &oggpacket ) < 0 )
252         {
253             msg_Err( p_dec, "this bitstream does not contain Vorbis "
254                      "audio data.");
255             block_Release( *pp_block );
256             return NULL;
257         }
258         p_sys->i_headers++;
259
260         /* Setup the format */
261         p_dec->fmt_out.audio.i_rate     = p_sys->vi.rate;
262         p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
263         p_dec->fmt_out.audio.i_physical_channels =
264             p_dec->fmt_out.audio.i_original_channels =
265                 pi_channels_maps[p_sys->vi.channels];
266         p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
267
268         aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
269
270         msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
271                  p_sys->vi.channels, p_sys->vi.rate,
272                  p_sys->vi.bitrate_nominal );
273
274         return ProcessPacket( p_dec, &oggpacket, pp_block );
275     }
276
277     if( p_sys->i_headers == 1 )
278     {
279         /* The next packet in order is the comments header */
280         if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
281             < 0 )
282         {
283             msg_Err( p_dec, "2nd Vorbis header is corrupted" );
284             block_Release( *pp_block );
285             return NULL;
286         }
287         p_sys->i_headers++;
288
289         ParseVorbisComments( p_dec );
290
291         return ProcessPacket( p_dec, &oggpacket, pp_block );
292     }
293
294     if( p_sys->i_headers == 2 )
295     {
296         /* The next packet in order is the codebooks header
297            We need to watch out that this packet is not missing as a
298            missing or corrupted header is fatal. */
299         if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
300             < 0 )
301         {
302             msg_Err( p_dec, "3rd Vorbis header is corrupted" );
303             block_Release( *pp_block );
304             return NULL;
305         }
306         p_sys->i_headers++;
307
308         if( !p_sys->b_packetizer )
309         {
310             /* Initialize the Vorbis packet->PCM decoder */
311             vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
312             vorbis_block_init( &p_sys->vd, &p_sys->vb );
313         }
314
315         return ProcessPacket( p_dec, &oggpacket, pp_block );
316     }
317
318     return ProcessPacket( p_dec, &oggpacket, pp_block );
319 }
320
321 /*****************************************************************************
322  * ProcessPacket: processes a Vorbis packet.
323  *****************************************************************************/
324 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
325                             block_t **pp_block )
326 {
327     decoder_sys_t *p_sys = p_dec->p_sys;
328     block_t *p_block = *pp_block;
329
330     /* Date management */
331     if( p_block && p_block->i_pts > 0 &&
332         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
333     {
334         aout_DateSet( &p_sys->end_date, p_block->i_pts );
335     }
336
337     if( !aout_DateGet( &p_sys->end_date ) )
338     {
339         /* We've just started the stream, wait for the first PTS. */
340         if( p_block ) block_Release( p_block );
341         return NULL;
342     }
343
344     *pp_block = NULL; /* To avoid being fed the same packet again */
345
346     if( p_sys->b_packetizer )
347     {
348         return SendPacket( p_dec, p_oggpacket, p_block );
349     }
350     else
351     {
352         aout_buffer_t *p_aout_buffer;
353
354         if( p_sys->i_headers >= 3 )
355             p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
356         else
357             p_aout_buffer = NULL;
358
359         if( p_block )
360         {
361             block_Release( p_block );
362         }
363         return p_aout_buffer;
364     }
365 }
366
367 /*****************************************************************************
368  * DecodePacket: decodes a Vorbis packet.
369  *****************************************************************************/
370 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
371 {
372     decoder_sys_t *p_sys = p_dec->p_sys;
373     int           i_samples;
374
375 #ifdef MODULE_NAME_IS_tremor
376     int32_t       **pp_pcm;
377 #else
378     float         **pp_pcm;
379 #endif
380
381     if( p_oggpacket->bytes &&
382         vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
383         vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
384
385     /* **pp_pcm is a multichannel float vector. In stereo, for
386      * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
387      * the size of each channel. Convert the float values
388      * (-1.<=range<=1.) to whatever PCM format and write it out */
389
390     if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
391     {
392
393         aout_buffer_t *p_aout_buffer;
394
395         p_aout_buffer =
396             p_dec->pf_aout_buffer_new( p_dec, i_samples );
397
398         if( p_aout_buffer == NULL ) return NULL;
399
400         /* Interleave the samples */
401 #ifdef MODULE_NAME_IS_tremor
402         Interleave( (int32_t *)p_aout_buffer->p_buffer,
403                     (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples );
404 #else
405         Interleave( (float *)p_aout_buffer->p_buffer,
406                     (const float **)pp_pcm, p_sys->vi.channels, i_samples );
407 #endif
408
409         /* Tell libvorbis how many samples we actually consumed */
410         vorbis_synthesis_read( &p_sys->vd, i_samples );
411
412         /* Date management */
413         p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
414         p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
415                                                       i_samples );
416         return p_aout_buffer;
417     }
418     else
419     {
420         return NULL;
421     }
422 }
423
424 /*****************************************************************************
425  * SendPacket: send an ogg dated packet to the stream output.
426  *****************************************************************************/
427 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
428                             block_t *p_block )
429 {
430     decoder_sys_t *p_sys = p_dec->p_sys;
431     int i_block_size, i_samples;
432
433     i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
434     if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
435     i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
436     p_sys->i_last_block_size = i_block_size;
437
438     /* Date management */
439     p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
440
441     if( p_sys->i_headers >= 3 )
442         p_block->i_length = aout_DateIncrement( &p_sys->end_date, i_samples ) -
443             p_block->i_pts;
444     else
445         p_block->i_length = 0;
446
447     return p_block;
448 }
449
450 /*****************************************************************************
451  * ParseVorbisComments: FIXME should be done in demuxer
452  *****************************************************************************/
453 static void ParseVorbisComments( decoder_t *p_dec )
454 {
455     input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
456     input_info_category_t *p_cat =
457         input_InfoCategory( p_input, _("Vorbis comment") );
458     playlist_t *p_playlist = vlc_object_find( p_dec, VLC_OBJECT_PLAYLIST,
459                                               FIND_ANYWHERE );
460     playlist_item_t *p_item;
461     int i = 0;
462     char *psz_name, *psz_value, *psz_comment;
463     while ( i < p_dec->p_sys->vc.comments )
464     {
465         psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
466         if( !psz_comment )
467         {
468             msg_Warn( p_dec, "out of memory" );
469             break;
470         }
471         psz_name = psz_comment;
472         psz_value = strchr( psz_comment, '=' );
473         if( psz_value )
474         {
475             *psz_value = '\0';
476             psz_value++;
477             input_AddInfo( p_cat, psz_name, psz_value );
478             vlc_mutex_lock( &p_playlist->object_lock );
479             p_item = playlist_ItemGetByPos( p_playlist, -1 );
480             vlc_mutex_unlock( &p_playlist->object_lock );
481             if( !p_item)
482             {
483                     msg_Err(p_dec, "unable to find item" );
484                     return;
485             }
486             vlc_mutex_lock( &p_item->lock );
487             playlist_ItemAddInfo( p_item, _("Vorbis comment") ,
488                             psz_name, psz_value );
489             vlc_mutex_unlock( &p_item->lock );
490         }
491         free( psz_comment );
492         i++;
493     }
494     if( p_playlist ) vlc_object_release( p_playlist );
495 }
496
497 /*****************************************************************************
498  * Interleave: helper function to interleave channels
499  *****************************************************************************/
500 static void Interleave(
501 #ifdef MODULE_NAME_IS_tremor
502                         int32_t *p_out, const int32_t **pp_in,
503 #else
504                         float *p_out, const float **pp_in,
505 #endif
506                         int i_nb_channels, int i_samples )
507 {
508     int i, j;
509
510     for ( j = 0; j < i_samples; j++ )
511     {
512         for ( i = 0; i < i_nb_channels; i++ )
513         {
514             p_out[j * i_nb_channels + i] = pp_in[i][j];
515         }
516     }
517 }
518
519 /*****************************************************************************
520  * CloseDecoder: vorbis decoder destruction
521  *****************************************************************************/
522 static void CloseDecoder( vlc_object_t *p_this )
523 {
524     decoder_t *p_dec = (decoder_t *)p_this;
525     decoder_sys_t *p_sys = p_dec->p_sys;
526
527     if( !p_sys->b_packetizer && p_sys->i_headers >= 3 )
528     {
529         vorbis_block_clear( &p_sys->vb );
530         vorbis_dsp_clear( &p_sys->vd );
531     }
532
533     vorbis_comment_clear( &p_sys->vc );
534     vorbis_info_clear( &p_sys->vi );  /* must be called last */
535
536     free( p_sys );
537 }
538
539 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
540
541 /*****************************************************************************
542  * encoder_sys_t : theora encoder descriptor
543  *****************************************************************************/
544 struct encoder_sys_t
545 {
546     /*
547      * Input properties
548      */
549     int i_headers;
550
551     /*
552      * Vorbis properties
553      */
554     vorbis_info      vi; /* struct that stores all the static vorbis bitstream
555                             settings */
556     vorbis_comment   vc; /* struct that stores all the bitstream user
557                           * comments */
558     vorbis_dsp_state vd; /* central working state for the packet->PCM
559                           * decoder */
560     vorbis_block     vb; /* local working space for packet->PCM decode */
561
562     int i_last_block_size;
563     int i_samples_delay;
564     int i_channels;
565
566     /*
567      * Common properties
568      */
569     mtime_t i_pts;
570 };
571
572 /*****************************************************************************
573  * OpenEncoder: probe the encoder and return score
574  *****************************************************************************/
575 static int OpenEncoder( vlc_object_t *p_this )
576 {
577     encoder_t *p_enc = (encoder_t *)p_this;
578     encoder_sys_t *p_sys;
579
580     if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') )
581     {
582         return VLC_EGENERIC;
583     }
584
585     /* Allocate the memory needed to store the decoder's structure */
586     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
587     {
588         msg_Err( p_enc, "out of memory" );
589         return VLC_EGENERIC;
590     }
591     p_enc->p_sys = p_sys;
592
593     p_enc->pf_header = Headers;
594     p_enc->pf_encode_audio = Encode;
595     p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
596
597     /* Initialize vorbis encoder */
598     vorbis_info_init( &p_sys->vi );
599
600     if( vorbis_encode_setup_managed( &p_sys->vi,
601             p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
602             -1, p_enc->fmt_out.i_bitrate, -1 ) ||
603         vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_AVG, NULL ) ||
604         vorbis_encode_setup_init( &p_sys->vi ) )
605     {
606         ;
607     }
608
609     /* add a comment */
610     vorbis_comment_init( &p_sys->vc);
611     vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
612
613     /* set up the analysis state and auxiliary encoding storage */
614     vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
615     vorbis_block_init( &p_sys->vd, &p_sys->vb );
616
617     p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
618     p_sys->i_last_block_size = 0;
619     p_sys->i_samples_delay = 0;
620     p_sys->i_headers = 0;
621     p_sys->i_pts = 0;
622
623     return VLC_SUCCESS;
624 }
625
626 /****************************************************************************
627  * Encode: the whole thing
628  ****************************************************************************
629  * This function spits out ogg packets.
630  ****************************************************************************/
631 static block_t *Headers( encoder_t *p_enc )
632 {
633     encoder_sys_t *p_sys = p_enc->p_sys;
634     block_t *p_block, *p_chain = NULL;
635
636     /* Create theora headers */
637     if( !p_sys->i_headers )
638     {
639         ogg_packet header[3];
640         int i;
641
642         vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
643                                    &header[0], &header[1], &header[2]);
644         for( i = 0; i < 3; i++ )
645         {
646             p_block = block_New( p_enc, header[i].bytes );
647             memcpy( p_block->p_buffer, header[i].packet, header[i].bytes );
648
649             p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
650
651             block_ChainAppend( &p_chain, p_block );
652         }
653         p_sys->i_headers = 3;
654     }
655
656     return p_chain;
657 }
658
659 /****************************************************************************
660  * Encode: the whole thing
661  ****************************************************************************
662  * This function spits out ogg packets.
663  ****************************************************************************/
664 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
665 {
666     encoder_sys_t *p_sys = p_enc->p_sys;
667     ogg_packet oggpacket;
668     block_t *p_block, *p_chain = NULL;
669     float **buffer;
670     int i;
671     unsigned int j;
672
673     p_sys->i_pts = p_aout_buf->start_date -
674                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
675                 (mtime_t)p_enc->fmt_in.audio.i_rate;
676
677     p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
678
679     buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
680
681     /* convert samples to float and uninterleave */
682     for( i = 0; i < p_sys->i_channels; i++ )
683     {
684         for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
685         {
686             buffer[i][j]= ((float)( ((int16_t *)p_aout_buf->p_buffer )
687                                     [j * p_sys->i_channels + i ] )) / 32768.f;
688         }
689     }
690
691     vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
692
693     while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
694     {
695         int i_samples;
696
697         vorbis_analysis( &p_sys->vb, NULL );
698         vorbis_bitrate_addblock( &p_sys->vb );
699
700         while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
701         {
702             int i_block_size;
703             p_block = block_New( p_enc, oggpacket.bytes );
704             memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
705
706             i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
707
708             if( i_block_size < 0 ) i_block_size = 0;
709             i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
710             p_sys->i_last_block_size = i_block_size;
711
712             p_block->i_length = (mtime_t)1000000 *
713                 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
714
715             p_block->i_dts = p_block->i_pts = p_sys->i_pts;
716
717             p_sys->i_samples_delay -= i_samples;
718
719             /* Update pts */
720             p_sys->i_pts += p_block->i_length;
721             block_ChainAppend( &p_chain, p_block );
722         }
723     }
724
725     return p_chain;
726 }
727
728 /*****************************************************************************
729  * CloseEncoder: theora encoder destruction
730  *****************************************************************************/
731 static void CloseEncoder( vlc_object_t *p_this )
732 {
733     encoder_t *p_enc = (encoder_t *)p_this;
734     encoder_sys_t *p_sys = p_enc->p_sys;
735
736     vorbis_block_clear( &p_sys->vb );
737     vorbis_dsp_clear( &p_sys->vd );
738     vorbis_comment_clear( &p_sys->vc );
739     vorbis_info_clear( &p_sys->vi );  /* must be called last */
740
741     free( p_sys );
742 }
743
744 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */