]> git.sesse.net Git - vlc/blob - modules/codec/vorbis.c
* ALL: moved i_bitrate out of audio_format_t into es_format_t
[vlc] / modules / codec / vorbis.c
1 /*****************************************************************************
2  * vorbis.c: vorbis decoder/encoder/packetizer module making use of libvorbis.
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: vorbis.c,v 1.23 2003/11/16 22:54:12 gbazin 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 <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>                                    /* memcpy(), memset() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/decoder.h>
32 #include <vlc/input.h>
33
34 #include <ogg/ogg.h>
35
36 #ifdef MODULE_NAME_IS_tremor
37 #include <tremor/ivorbiscodec.h>
38
39 #else
40 #include <vorbis/codec.h>
41
42 /* vorbis header */
43 #ifdef HAVE_VORBIS_VORBISENC_H
44 #   include <vorbis/vorbisenc.h>
45 #   ifndef OV_ECTL_RATEMANAGE_AVG
46 #       define OV_ECTL_RATEMANAGE_AVG 0x0
47 #   endif
48 #endif
49
50 #endif
51
52 /*****************************************************************************
53  * decoder_sys_t : vorbis decoder descriptor
54  *****************************************************************************/
55 struct decoder_sys_t
56 {
57     /* Module mode */
58     vlc_bool_t b_packetizer;
59
60     /*
61      * Input properties
62      */
63     int i_headers;
64
65     /*
66      * Vorbis properties
67      */
68     vorbis_info      vi; /* struct that stores all the static vorbis bitstream
69                             settings */
70     vorbis_comment   vc; /* struct that stores all the bitstream user
71                           * comments */
72     vorbis_dsp_state vd; /* central working state for the packet->PCM
73                           * decoder */
74     vorbis_block     vb; /* local working space for packet->PCM decode */
75
76     /*
77      * Common properties
78      */
79     audio_date_t end_date;
80     int          i_last_block_size;
81
82 };
83
84 static int pi_channels_maps[6] =
85 {
86     0,
87     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
88     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
89     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
90      | AOUT_CHAN_REARRIGHT,
91     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
92      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
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     input_info_category_t *p_cat =
456         input_InfoCategory( p_input, _("Vorbis Comment") );
457     int i = 0;
458     char *psz_name, *psz_value, *psz_comment;
459     while ( i < p_dec->p_sys->vc.comments )
460     {
461         psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
462         if( !psz_comment )
463         {
464             msg_Warn( p_dec, "Out of memory" );
465             break;
466         }
467         psz_name = psz_comment;
468         psz_value = strchr( psz_comment, '=' );
469         if( psz_value )
470         {
471             *psz_value = '\0';
472             psz_value++;
473             input_AddInfo( p_cat, psz_name, psz_value );
474         }
475         free( psz_comment );
476         i++;
477     }
478 }
479
480 /*****************************************************************************
481  * Interleave: helper function to interleave channels
482  *****************************************************************************/
483 #ifdef MODULE_NAME_IS_tremor
484 static void Interleave( int32_t *p_out, const int32_t **pp_in,
485 #else
486 static void Interleave( 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     {
564         return VLC_EGENERIC;
565     }
566
567     /* Allocate the memory needed to store the decoder's structure */
568     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
569     {
570         msg_Err( p_enc, "out of memory" );
571         return VLC_EGENERIC;
572     }
573     p_enc->p_sys = p_sys;
574
575     p_enc->pf_header = Headers;
576     p_enc->pf_encode_audio = Encode;
577     p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
578
579     /* Initialize vorbis encoder */
580     vorbis_info_init( &p_sys->vi );
581
582     if( vorbis_encode_setup_managed( &p_sys->vi,
583             p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
584             -1, p_enc->fmt_out.i_bitrate, -1 ) ||
585         vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_AVG, NULL ) ||
586         vorbis_encode_setup_init( &p_sys->vi ) ){}
587
588     /* add a comment */
589     vorbis_comment_init( &p_sys->vc);
590     vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
591
592     /* set up the analysis state and auxiliary encoding storage */
593     vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
594     vorbis_block_init( &p_sys->vd, &p_sys->vb );
595
596     p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
597     p_sys->i_last_block_size = 0;
598     p_sys->i_samples_delay = 0;
599     p_sys->i_headers = 0;
600     p_sys->i_pts = 0;
601
602     return VLC_SUCCESS;
603 }
604
605 /****************************************************************************
606  * Encode: the whole thing
607  ****************************************************************************
608  * This function spits out ogg packets.
609  ****************************************************************************/
610 static block_t *Headers( encoder_t *p_enc )
611 {
612     encoder_sys_t *p_sys = p_enc->p_sys;
613     block_t *p_block, *p_chain = NULL;
614
615     /* Create theora headers */
616     if( !p_sys->i_headers )
617     {
618         ogg_packet header[3];
619         int i;
620
621         vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
622                                    &header[0], &header[1], &header[2]);
623         for( i = 0; i < 3; i++ )
624         {
625             p_block = block_New( p_enc, header[i].bytes );
626             memcpy( p_block->p_buffer, header[i].packet, header[i].bytes );
627
628             p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
629
630             block_ChainAppend( &p_chain, p_block );
631         }
632         p_sys->i_headers = 3;
633     }
634
635     return p_chain;
636 }
637
638 /****************************************************************************
639  * Encode: the whole thing
640  ****************************************************************************
641  * This function spits out ogg packets.
642  ****************************************************************************/
643 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
644 {
645     encoder_sys_t *p_sys = p_enc->p_sys;
646     ogg_packet oggpacket;
647     block_t *p_block, *p_chain = NULL;
648     float **buffer;
649     int i, j;
650
651     p_sys->i_pts = p_aout_buf->start_date -
652                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
653                 (mtime_t)p_enc->fmt_in.audio.i_rate;
654
655     p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
656
657     buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
658
659 #if 0
660     if( id->ff_dec_c->channels != id->ff_enc_c->channels )
661     {
662         int i, j;
663
664         /* dumb downmixing */
665         for( i = 0; i < id->ff_enc_c->frame_size; i++ )
666         {
667             for( j = 0 ; j < id->f_dst.i_channels; j++ )
668             {
669                 p_buffer[i*id->f_dst.i_channels+j] =
670                     p_buffer[i*id->f_src.i_channels+j];
671             }
672         }
673     }
674 #endif
675
676     /* convert samples to float and uninterleave */
677     for( i = 0; i < p_sys->i_channels; i++ )
678     {
679         for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
680         {
681             buffer[i][j]= ((float)( ((int16_t *)p_aout_buf->p_buffer )
682                                     [j * p_sys->i_channels + i ] )) / 32768.f;
683         }
684     }
685
686     vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
687
688     while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
689     {
690         int i_samples;
691
692         vorbis_analysis( &p_sys->vb, NULL );
693         vorbis_bitrate_addblock( &p_sys->vb );
694
695         while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
696         {
697             int i_block_size;
698             p_block = block_New( p_enc, oggpacket.bytes );
699             memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
700
701             i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
702
703             if( i_block_size < 0 ) i_block_size = 0;
704             i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
705             p_sys->i_last_block_size = i_block_size;
706
707             p_block->i_length = (mtime_t)1000000 *
708                 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
709
710             p_block->i_dts = p_block->i_pts = p_sys->i_pts;
711
712             p_sys->i_samples_delay -= i_samples;
713
714             /* Update pts */
715             p_sys->i_pts += p_block->i_length;
716             block_ChainAppend( &p_chain, p_block );
717         }
718     }
719
720     return p_chain;
721 }
722
723 /*****************************************************************************
724  * CloseEncoder: theora encoder destruction
725  *****************************************************************************/
726 static void CloseEncoder( vlc_object_t *p_this )
727 {
728     encoder_t *p_enc = (encoder_t *)p_this;
729     encoder_sys_t *p_sys = p_enc->p_sys;
730
731     vorbis_block_clear( &p_sys->vb );
732     vorbis_dsp_clear( &p_sys->vd );
733     vorbis_comment_clear( &p_sys->vc );
734     vorbis_info_clear( &p_sys->vi );  /* must be called last */
735
736     free( p_sys );
737 }
738
739 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */