]> git.sesse.net Git - vlc/blob - modules/codec/vorbis.c
* ALL: Introduction of a new api for decoders.
[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.17 2003/09/02 20:19:25 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 #ifdef MODULE_NAME_IS_tremor
41 #include <tremor/ivorbiscodec.h>
42 #else
43 #include <vorbis/codec.h>
44 #endif
45
46 /*****************************************************************************
47  * decoder_sys_t : vorbis decoder descriptor
48  *****************************************************************************/
49 struct decoder_sys_t
50 {
51     /* Module mode */
52     vlc_bool_t b_packetizer;
53
54     /*
55      * Input properties
56      */
57     int i_headers;
58
59     /*
60      * Vorbis properties
61      */
62     vorbis_info      vi; /* struct that stores all the static vorbis bitstream
63                             settings */
64     vorbis_comment   vc; /* struct that stores all the bitstream user
65                           * comments */
66     vorbis_dsp_state vd; /* central working state for the packet->PCM
67                           * decoder */
68     vorbis_block     vb; /* local working space for packet->PCM decode */
69
70     /*
71      * Output properties
72      */
73     aout_instance_t        *p_aout;
74     aout_input_t           *p_aout_input;
75     audio_sample_format_t   aout_format;
76
77     /*
78      * Packetizer output properties
79      */
80     sout_packetizer_input_t *p_sout_input;
81     sout_format_t           sout_format;
82
83     /*
84      * Common properties
85      */
86     audio_date_t          end_date;
87     int                   i_last_block_size;
88
89 };
90
91 static int pi_channels_maps[6] =
92 {
93     0,
94     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
95     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
96     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
97      | AOUT_CHAN_REARRIGHT,
98     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
99      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
100 };
101
102 /****************************************************************************
103  * Local prototypes
104  ****************************************************************************/
105 static int OpenDecoder   ( vlc_object_t * );
106 static int OpenPacketizer( vlc_object_t * );
107
108 static int InitDecoder   ( decoder_t * );
109 static int RunDecoder    ( decoder_t *, block_t * );
110 static int EndDecoder    ( decoder_t * );
111
112 static int ProcessPacket ( decoder_t *, ogg_packet *, mtime_t );
113 static int DecodePacket  ( decoder_t *, ogg_packet * );
114 static int SendPacket    ( decoder_t *, ogg_packet * );
115
116 static void ParseVorbisComments( decoder_t * );
117
118 #ifdef MODULE_NAME_IS_tremor
119 static void Interleave   ( int32_t *, const int32_t **, int, int );
120 #else
121 static void Interleave   ( float *, const float **, int, int );
122 #endif
123
124 /*****************************************************************************
125  * Module descriptor
126  *****************************************************************************/
127 vlc_module_begin();
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, NULL );
135
136     add_submodule();
137     set_description( _("Vorbis audio packetizer") );
138     set_capability( "packetizer", 100 );
139     set_callbacks( OpenPacketizer, NULL );
140 vlc_module_end();
141
142 /*****************************************************************************
143  * OpenDecoder: probe the decoder and return score
144  *****************************************************************************/
145 static int OpenDecoder( vlc_object_t *p_this )
146 {
147     decoder_t *p_dec = (decoder_t*)p_this;
148
149     if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('v','o','r','b') )
150     {
151         return VLC_EGENERIC;
152     }
153
154     p_dec->pf_init = InitDecoder;
155     p_dec->pf_decode = RunDecoder;
156     p_dec->pf_end = EndDecoder;
157
158     /* Allocate the memory needed to store the decoder's structure */
159     if( ( p_dec->p_sys =
160           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
161     {
162         msg_Err( p_dec, "out of memory" );
163         return VLC_EGENERIC;
164     }
165     p_dec->p_sys->b_packetizer = VLC_FALSE;
166
167     return VLC_SUCCESS;
168 }
169
170 static int OpenPacketizer( vlc_object_t *p_this )
171 {
172     decoder_t *p_dec = (decoder_t*)p_this;
173
174     int i_ret = OpenDecoder( p_this );
175
176     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
177
178     return i_ret;
179 }
180
181 /*****************************************************************************
182  * InitDecoder: Initalize the decoder
183  *****************************************************************************/
184 static int InitDecoder( decoder_t *p_dec )
185 {
186     decoder_sys_t *p_sys = p_dec->p_sys;
187
188     aout_DateSet( &p_sys->end_date, 0 );
189
190     p_sys->p_aout = NULL;
191     p_sys->p_aout_input = NULL;
192     p_sys->aout_format.i_format = VLC_FOURCC('v','o','r','b');
193
194     p_sys->p_sout_input = NULL;
195     p_sys->sout_format.i_cat = AUDIO_ES;
196     p_sys->sout_format.i_fourcc = VLC_FOURCC( 'v', 'o', 'r', 'b' );
197     p_sys->sout_format.i_block_align = 0;
198     p_sys->sout_format.i_bitrate     = 0;
199     p_sys->sout_format.i_extra_data  = 0;
200     p_sys->sout_format.p_extra_data  = NULL;
201
202     /* Take care of vorbis init */
203     vorbis_info_init( &p_sys->vi );
204     vorbis_comment_init( &p_sys->vc );
205
206     p_sys->i_headers = 0;
207
208     return VLC_SUCCESS;
209 }
210
211 /****************************************************************************
212  * RunDecoder: the whole thing
213  ****************************************************************************
214  * This function must be fed with ogg packets.
215  ****************************************************************************/
216 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
217 {
218     decoder_sys_t *p_sys = p_dec->p_sys;
219     ogg_packet oggpacket;
220     int i_ret;
221
222     /* Block to Ogg packet */
223     oggpacket.packet = p_block->p_buffer;
224     oggpacket.bytes = p_block->i_buffer;
225     oggpacket.granulepos = p_block->i_dts;
226     oggpacket.b_o_s = 0;
227     oggpacket.e_o_s = 0;
228     oggpacket.packetno = 0;
229
230     if( p_sys->i_headers == 0 )
231     {
232         /* Take care of the initial Vorbis header */
233
234         oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
235         if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc,
236                                        &oggpacket ) < 0 )
237         {
238             msg_Err( p_dec->p_fifo, "This bitstream does not contain Vorbis "
239                      "audio data");
240             block_Release( p_block );
241             return VLC_EGENERIC;
242         }
243         p_sys->i_headers++;
244
245  
246         if( p_sys->b_packetizer )
247         {
248             /* add a input for the stream ouput */
249             p_sys->sout_format.i_sample_rate = p_sys->vi.rate;
250             p_sys->sout_format.i_channels    = p_sys->vi.channels;
251             p_sys->sout_format.i_block_align = 1;
252             p_sys->sout_format.i_bitrate     = p_sys->vi.bitrate_nominal;
253
254             p_sys->p_sout_input =
255                 sout_InputNew( p_dec, &p_sys->sout_format );
256
257             if( !p_sys->p_sout_input )
258             {
259                 msg_Err( p_dec, "cannot add a new stream" );
260                 block_Release( p_block );
261                 return VLC_EGENERIC;
262             }
263         }
264         else
265         {
266 #ifdef MODULE_NAME_IS_tremor
267             p_sys->aout_format.i_format = VLC_FOURCC('f','i','3','2');
268 #else
269             p_sys->aout_format.i_format = VLC_FOURCC('f','l','3','2');
270 #endif
271             p_sys->aout_format.i_physical_channels =
272                 p_sys->aout_format.i_original_channels =
273                     pi_channels_maps[p_sys->vi.channels];
274             p_sys->aout_format.i_rate = p_sys->vi.rate;
275
276             p_sys->p_aout = NULL;
277             p_sys->p_aout_input = aout_DecNew( p_dec, &p_sys->p_aout,
278                                                &p_sys->aout_format );
279
280             if( p_sys->p_aout_input == NULL )
281             {
282                 msg_Err( p_dec, "failed to create aout fifo" );
283                 block_Release( p_block );
284                 return VLC_EGENERIC;
285             }
286         }
287
288         aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
289
290         msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
291                  p_sys->vi.channels, p_sys->vi.rate,
292                  p_sys->vi.bitrate_nominal );
293
294         if( p_sys->b_packetizer )
295         {
296             i_ret = SendPacket( p_dec, &oggpacket );
297             block_Release( p_block );
298             return i_ret;
299         }
300         else
301         {
302             block_Release( p_block );
303             return VLC_SUCCESS;
304         }
305     }
306
307     if( p_sys->i_headers == 1 )
308     {
309         /* The next packet in order is the comments header */
310         if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
311             < 0 )
312         {
313             msg_Err( p_dec, "2nd Vorbis header is corrupted" );
314             return VLC_EGENERIC;
315         }
316         p_sys->i_headers++;
317     
318         ParseVorbisComments( p_dec );
319
320         if( p_sys->b_packetizer )
321         {
322             i_ret = SendPacket( p_dec, &oggpacket );
323             block_Release( p_block );
324             return i_ret;
325         }
326         else
327         {
328             block_Release( p_block );
329             return VLC_SUCCESS;
330         }
331     }
332
333     if( p_sys->i_headers == 2 )
334     {
335         /* The next packet in order is the codebooks header
336            We need to watch out that this packet is not missing as a
337            missing or corrupted header is fatal. */
338         if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
339             < 0 )
340         {
341             msg_Err( p_dec, "3rd Vorbis header is corrupted" );
342             return VLC_EGENERIC;
343         }
344         p_sys->i_headers++;
345     
346         if( !p_sys->b_packetizer )
347         {
348             /* Initialize the Vorbis packet->PCM decoder */
349             vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
350             vorbis_block_init( &p_sys->vd, &p_sys->vb );
351         }
352
353         if( p_sys->b_packetizer )
354         {
355             i_ret = SendPacket( p_dec, &oggpacket );
356             block_Release( p_block );
357             return i_ret;
358         }
359         else
360         {
361             block_Release( p_block );
362             return VLC_SUCCESS;
363         }
364     }
365
366     i_ret = ProcessPacket( p_dec, &oggpacket, p_block->i_pts );
367     block_Release( p_block );
368     return i_ret;
369 }
370
371 /*****************************************************************************
372  * ProcessPacket: processes a Vorbis packet.
373  *****************************************************************************/
374 static int ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
375                           mtime_t i_pts )
376 {
377     decoder_sys_t *p_sys = p_dec->p_sys;
378
379     /* Date management */
380     if( i_pts > 0 && i_pts != aout_DateGet( &p_sys->end_date ) )
381     {
382         aout_DateSet( &p_sys->end_date, i_pts );
383     }
384
385     if( p_sys->b_packetizer )
386     {
387         return SendPacket( p_dec, p_oggpacket );
388     }
389     else
390     {
391         return DecodePacket( p_dec, p_oggpacket );
392     }
393 }
394
395 /*****************************************************************************
396  * DecodePacket: decodes a Vorbis packet.
397  *****************************************************************************/
398 static int DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
399 {
400     decoder_sys_t *p_sys = p_dec->p_sys;
401     int           i_samples;
402
403 #ifdef MODULE_NAME_IS_tremor
404     int32_t       **pp_pcm;
405 #else
406     float         **pp_pcm;
407 #endif
408
409     if( vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
410         vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
411
412     /* **pp_pcm is a multichannel float vector. In stereo, for
413      * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
414      * the size of each channel. Convert the float values
415      * (-1.<=range<=1.) to whatever PCM format and write it out */
416
417     while( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
418     {
419
420         aout_buffer_t *p_aout_buffer;
421         p_aout_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
422                                            i_samples );
423         if( !p_aout_buffer )
424         {
425             msg_Err( p_dec, "cannot get aout buffer" );
426             return VLC_SUCCESS;
427         }
428
429         /* Interleave the samples */
430 #ifdef MODULE_NAME_IS_tremor
431         Interleave( (int32_t *)p_aout_buffer->p_buffer,
432                     (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples );
433 #else
434         Interleave( (float *)p_aout_buffer->p_buffer,
435                     (const float **)pp_pcm, p_sys->vi.channels, i_samples );
436 #endif
437
438         /* Tell libvorbis how many samples we actually consumed */
439         vorbis_synthesis_read( &p_sys->vd, i_samples );
440
441         /* Date management */
442         p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
443         p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
444                                                       i_samples );
445
446         aout_DecPlay( p_sys->p_aout, p_sys->p_aout_input, p_aout_buffer );
447     }
448
449 }
450
451 /*****************************************************************************
452  * SendPacket: send an ogg packet to the stream output.
453  *****************************************************************************/
454 static int SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
455 {
456     decoder_sys_t *p_sys = p_dec->p_sys;
457     int           i_block_size, i_samples;
458
459     sout_buffer_t *p_sout_buffer =
460         sout_BufferNew( p_sys->p_sout_input->p_sout, p_oggpacket->bytes );
461
462     if( !p_sout_buffer ) return VLC_EGENERIC;
463
464     i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
465     if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
466     i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
467     p_sys->i_last_block_size = i_block_size;
468
469     p_dec->p_vlc->pf_memcpy( p_sout_buffer->p_buffer,
470                              p_oggpacket->packet,
471                              p_oggpacket->bytes );
472
473     p_sout_buffer->i_bitrate = p_sys->vi.bitrate_nominal;
474
475     /* Date management */
476     p_sout_buffer->i_dts = p_sout_buffer->i_pts =
477         aout_DateGet( &p_sys->end_date );
478
479     if( p_sys->i_headers >= 3 )
480         p_sout_buffer->i_length =
481             aout_DateIncrement( &p_sys->end_date, i_samples ) -
482             p_sout_buffer->i_pts;
483     else
484         p_sout_buffer->i_length = 0;
485
486     sout_InputSendBuffer( p_sys->p_sout_input, p_sout_buffer );
487
488     return VLC_SUCCESS;
489 }
490
491 /*****************************************************************************
492  * ParseVorbisComments: FIXME should be done in demuxer
493  *****************************************************************************/
494 static void ParseVorbisComments( decoder_t *p_dec )
495 {
496     input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
497     input_info_category_t *p_cat =
498         input_InfoCategory( p_input, _("Vorbis Comment") );
499     int i = 0;
500     char *psz_name, *psz_value, *psz_comment;
501     while ( i < p_dec->p_sys->vc.comments )
502     {
503         psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
504         if( !psz_comment )
505         {
506             msg_Warn( p_dec, "Out of memory" );
507             break;
508         }
509         psz_name = psz_comment;
510         psz_value = strchr( psz_comment, '=' );
511         if( psz_value )
512         {
513             *psz_value = '\0';
514             psz_value++;
515             input_AddInfo( p_cat, psz_name, psz_value );
516         }
517         free( psz_comment );
518         i++;
519     }
520 }
521
522 /*****************************************************************************
523  * Interleave: helper function to interleave channels
524  *****************************************************************************/
525 #ifdef MODULE_NAME_IS_tremor
526 static void Interleave( int32_t *p_out, const int32_t **pp_in,
527 #else
528 static void Interleave( float *p_out, const float **pp_in,
529 #endif
530                         int i_nb_channels, int i_samples )
531 {
532     int i, j;
533
534     for ( j = 0; j < i_samples; j++ )
535     {
536         for ( i = 0; i < i_nb_channels; i++ )
537         {
538             p_out[j * i_nb_channels + i] = pp_in[i][j];
539         }
540     }
541 }
542
543 /*****************************************************************************
544  * EndDecoder: vorbis decoder destruction
545  *****************************************************************************/
546 static int EndDecoder( decoder_t * p_dec )
547 {
548     decoder_sys_t *p_sys = p_dec->p_sys;
549
550     if( p_sys->p_aout_input != NULL )
551     {
552         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
553     }
554
555     if( p_sys->p_sout_input != NULL )
556     {
557         sout_InputDelete( p_sys->p_sout_input );
558     }
559
560     if( !p_sys->b_packetizer && p_sys->i_headers >= 3 )
561     {
562         vorbis_block_clear( &p_sys->vb );
563         vorbis_dsp_clear( &p_sys->vd );
564     }
565
566     vorbis_comment_clear( &p_sys->vc );
567     vorbis_info_clear( &p_sys->vi );  /* must be called last */
568
569     free( p_sys );
570
571     return VLC_SUCCESS;
572 }