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