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