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