]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/dtstofloat32.c
Make Zorglub less unhappy
[vlc] / modules / audio_filter / converter / dtstofloat32.c
1 /*****************************************************************************
2  * dtstofloat32.c: DTS Coherent Acoustics decoder plugin for VLC.
3  *   This plugin makes use of libdts to do the actual decoding
4  *   (http://www.videolan.org/dtsdec/).
5  *****************************************************************************
6  * Copyright (C) 2001, 2002 the VideoLAN team
7  * $Id$
8  *
9  * Author: Gildas Bazin <gbazin@videolan.org>
10  *      
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30
31 #include <stdlib.h>                                      /* malloc(), free() */
32 #include <string.h>                                              /* strdup() */
33
34 #include <dts.h>                                       /* libdts header file */
35
36 #include <vlc/decoder.h>
37 #include "aout_internal.h"
38 #include "vlc_filter.h"
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  Create    ( vlc_object_t * );
44 static void Destroy   ( vlc_object_t * );
45 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,  
46                         aout_buffer_t * );
47
48 static int  Open      ( vlc_object_t *, filter_sys_t *,
49                         audio_format_t, audio_format_t );
50
51 static int  OpenFilter ( vlc_object_t * );
52 static void CloseFilter( vlc_object_t * );
53 static block_t *Convert( filter_t *, block_t * );
54
55 /* libdts channel order */
56 static const uint32_t pi_channels_in[] =
57 { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
58   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_LFE, 0 };
59 /* our internal channel order (WG-4 order) */
60 static const uint32_t pi_channels_out[] =
61 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
62   AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
63
64 /*****************************************************************************
65  * Local structures
66  *****************************************************************************/
67 struct filter_sys_t
68 {
69     dts_state_t * p_libdts; /* libdts internal structure */
70     vlc_bool_t b_dynrng; /* see below */
71     int i_flags; /* libdts flags, see dtsdec/doc/libdts.txt */
72     vlc_bool_t b_dontwarn;
73     int i_nb_channels; /* number of float32 per sample */
74
75     int pi_chan_table[AOUT_CHAN_MAX]; /* channel reordering */
76 };
77
78 /*****************************************************************************
79  * Module descriptor
80  *****************************************************************************/
81 #define DYNRNG_TEXT N_("DTS dynamic range compression")
82 #define DYNRNG_LONGTEXT N_( \
83     "Dynamic range compression makes the loud sounds softer, and the soft " \
84     "sounds louder, so you can more easily listen to the stream in a noisy " \
85     "environment without disturbing anyone. If you disable the dynamic range "\
86     "compression the playback will be more adapted to a movie theater or a " \
87     "listening room.")
88
89 vlc_module_begin();
90     set_category( CAT_INPUT );
91     set_subcategory( SUBCAT_INPUT_ACODEC );
92     set_shortname( _("DTS" ) );
93     set_description( _("DTS Coherent Acoustics audio decoder") );
94     add_bool( "dts-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, VLC_FALSE );
95     set_capability( "audio filter", 100 );
96     set_callbacks( Create, Destroy );
97
98     add_submodule();
99     set_description( _("DTS Coherent Acoustics audio decoder") );
100     set_capability( "audio filter2", 100 );
101     set_callbacks( OpenFilter, CloseFilter );
102 vlc_module_end();
103
104 /*****************************************************************************
105  * Create:
106  *****************************************************************************/
107 static int Create( vlc_object_t *p_this )
108 {
109     aout_filter_t *p_filter = (aout_filter_t *)p_this;
110     filter_sys_t *p_sys;
111     int i_ret;
112
113     if ( p_filter->input.i_format != VLC_FOURCC('d','t','s',' ')
114           || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
115     {
116         return -1;
117     }
118
119     if ( p_filter->input.i_rate != p_filter->output.i_rate )
120     {
121         return -1;
122     }
123
124     /* Allocate the memory needed to store the module's structure */
125     p_sys = malloc( sizeof(filter_sys_t) );
126     p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
127     if( p_sys == NULL )
128     {
129         msg_Err( p_filter, "out of memory" );
130         return -1;
131     }
132
133     i_ret = Open( VLC_OBJECT(p_filter), p_sys,
134                   p_filter->input, p_filter->output );
135
136     p_filter->pf_do_work = DoWork;
137     p_filter->b_in_place = 0;
138
139     return i_ret;
140 }
141
142 /*****************************************************************************
143  * Open: 
144  *****************************************************************************/
145 static int Open( vlc_object_t *p_this, filter_sys_t *p_sys,
146                  audio_format_t input, audio_format_t output )
147 {
148     p_sys->b_dynrng = config_GetInt( p_this, "dts-dynrng" );
149     p_sys->b_dontwarn = 0;
150
151     /* We'll do our own downmixing, thanks. */
152     p_sys->i_nb_channels = aout_FormatNbChannels( &output );
153     switch ( (output.i_physical_channels & AOUT_CHAN_PHYSMASK)
154               & ~AOUT_CHAN_LFE )
155     {
156     case AOUT_CHAN_CENTER:
157         if ( (output.i_original_channels & AOUT_CHAN_CENTER)
158               || (output.i_original_channels
159                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
160         {
161             p_sys->i_flags = DTS_MONO;
162         }
163         break;
164
165     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
166         if ( output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
167         {
168             p_sys->i_flags = DTS_DOLBY;
169         }
170         else if ( input.i_original_channels == AOUT_CHAN_CENTER )
171         {
172             p_sys->i_flags = DTS_MONO;
173         }
174         else if ( input.i_original_channels & AOUT_CHAN_DUALMONO )
175         {
176             p_sys->i_flags = DTS_CHANNEL;
177         }
178         else
179         {
180             p_sys->i_flags = DTS_STEREO;
181         }
182         break;
183
184     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
185         p_sys->i_flags = DTS_3F;
186         break;
187
188     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
189         p_sys->i_flags = DTS_2F1R;
190         break;
191
192     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
193           | AOUT_CHAN_REARCENTER:
194         p_sys->i_flags = DTS_3F1R;
195         break;
196
197     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
198           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
199         p_sys->i_flags = DTS_2F2R;
200         break;
201
202     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
203           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
204         p_sys->i_flags = DTS_3F2R;
205         break;
206
207     default:
208         msg_Warn( p_this, "unknown sample format!" );
209         free( p_sys );
210         return -1;
211     }
212     if ( output.i_physical_channels & AOUT_CHAN_LFE )
213     {
214         p_sys->i_flags |= DTS_LFE;
215     }
216     //p_sys->i_flags |= DTS_ADJUST_LEVEL;
217
218     /* Initialize libdts */
219     p_sys->p_libdts = dts_init( 0 );
220     if( p_sys->p_libdts == NULL )
221     {
222         msg_Err( p_this, "unable to initialize libdts" );
223         return VLC_EGENERIC;
224     }
225
226     aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
227                               output.i_physical_channels & AOUT_CHAN_PHYSMASK,
228                               p_sys->i_nb_channels,
229                               p_sys->pi_chan_table );
230
231     return VLC_SUCCESS;
232 }
233
234 /*****************************************************************************
235  * Interleave: helper function to interleave channels
236  *****************************************************************************/
237 static void Interleave( float * p_out, const float * p_in, int i_nb_channels,
238                         int *pi_chan_table )
239 {
240     /* We do not only have to interleave, but also reorder the channels. */
241
242     int i, j;
243     for ( j = 0; j < i_nb_channels; j++ )
244     {
245         for ( i = 0; i < 256; i++ )
246         {
247             p_out[i * i_nb_channels + pi_chan_table[j]] = p_in[j * 256 + i];
248         }
249     }
250 }
251
252 /*****************************************************************************
253  * Duplicate: helper function to duplicate a unique channel
254  *****************************************************************************/
255 static void Duplicate( float * p_out, const float * p_in )
256 {
257     int i;
258
259     for ( i = 256; i--; )
260     {
261         *p_out++ = *p_in;
262         *p_out++ = *p_in;
263         p_in++;
264     }
265 }
266
267 /*****************************************************************************
268  * Exchange: helper function to exchange left & right channels
269  *****************************************************************************/
270 static void Exchange( float * p_out, const float * p_in )
271 {
272     int i;
273     const float * p_first = p_in + 256;
274     const float * p_second = p_in;
275
276     for ( i = 0; i < 256; i++ )
277     {
278         *p_out++ = *p_first++;
279         *p_out++ = *p_second++;
280     }
281 }
282
283 /*****************************************************************************
284  * DoWork: decode a DTS frame.
285  *****************************************************************************/
286 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
287                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
288 {
289     filter_sys_t    *p_sys = (filter_sys_t *)p_filter->p_sys;
290     sample_t        i_sample_level = 1;
291     int             i_flags = p_sys->i_flags;
292     int             i_bytes_per_block = 256 * p_sys->i_nb_channels
293                       * sizeof(float);
294     int             i;
295
296     /*
297      * Do the actual decoding now.
298      */
299
300     /* Needs to be called so the decoder knows which type of bitstream it is
301      * dealing with. */
302     int i_sample_rate, i_bit_rate, i_frame_length;
303     if( !dts_syncinfo( p_sys->p_libdts, p_in_buf->p_buffer, &i_flags,
304                        &i_sample_rate, &i_bit_rate, &i_frame_length ) )
305     {
306         msg_Warn( p_aout, "libdts couldn't sync on frame" );
307         p_out_buf->i_nb_samples = p_out_buf->i_nb_bytes = 0;
308         return;
309     }
310
311     i_flags = p_sys->i_flags;
312     dts_frame( p_sys->p_libdts, p_in_buf->p_buffer,
313                &i_flags, &i_sample_level, 0 );
314
315     if ( (i_flags & DTS_CHANNEL_MASK) != (p_sys->i_flags & DTS_CHANNEL_MASK)
316           && !p_sys->b_dontwarn )
317     {
318         msg_Warn( p_aout,
319                   "libdts couldn't do the requested downmix 0x%x->0x%x",
320                   p_sys->i_flags  & DTS_CHANNEL_MASK,
321                   i_flags & DTS_CHANNEL_MASK );
322
323         p_sys->b_dontwarn = 1;
324     }
325
326     if( 0)//!p_sys->b_dynrng )
327     {
328         dts_dynrng( p_sys->p_libdts, NULL, NULL );
329     }
330
331     for ( i = 0; i < dts_blocks_num(p_sys->p_libdts); i++ )
332     {
333         sample_t * p_samples;
334
335         if( dts_block( p_sys->p_libdts ) )
336         {
337             msg_Warn( p_aout, "dts_block failed for block %d", i );
338             break;
339         }
340
341         p_samples = dts_samples( p_sys->p_libdts );
342
343         if ( (p_sys->i_flags & DTS_CHANNEL_MASK) == DTS_MONO
344               && (p_filter->output.i_physical_channels 
345                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
346         {
347             Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
348                        p_samples );
349         }
350         else if ( p_filter->output.i_original_channels
351                     & AOUT_CHAN_REVERSESTEREO )
352         {
353             Exchange( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
354                       p_samples );
355         }
356         else
357         {
358             /* Interleave the *$£%ù samples. */
359             Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
360                         p_samples, p_sys->i_nb_channels, p_sys->pi_chan_table);
361         }
362     }
363
364     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
365     p_out_buf->i_nb_bytes = i_bytes_per_block * i;
366 }
367
368 /*****************************************************************************
369  * Destroy : deallocate data structures
370  *****************************************************************************/
371 static void Destroy( vlc_object_t *p_this )
372 {
373     aout_filter_t *p_filter = (aout_filter_t *)p_this;
374     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
375
376     dts_free( p_sys->p_libdts );
377     free( p_sys );
378 }
379
380 /*****************************************************************************
381  * OpenFilter: 
382  *****************************************************************************/
383 static int OpenFilter( vlc_object_t *p_this )
384 {
385     filter_t *p_filter = (filter_t *)p_this;
386     filter_sys_t *p_sys;
387     int i_ret;
388
389     if( p_filter->fmt_in.i_codec != VLC_FOURCC('d','t','s',' ')  )
390     {
391         return VLC_EGENERIC;
392     }
393
394     p_filter->fmt_out.audio.i_format =
395         p_filter->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
396
397     /* Allocate the memory needed to store the module's structure */
398     p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
399     if( p_sys == NULL )
400     {
401         msg_Err( p_filter, "out of memory" );
402         return VLC_EGENERIC;
403     }
404
405     /* Allocate the memory needed to store the module's structure */
406     p_filter->p_sys = p_sys = malloc( sizeof(filter_sys_t) );
407     if( p_sys == NULL )
408     {
409         msg_Err( p_filter, "out of memory" );
410         return VLC_EGENERIC;
411     }
412
413     i_ret = Open( VLC_OBJECT(p_filter), p_sys,
414                   p_filter->fmt_in.audio, p_filter->fmt_out.audio );
415
416     p_filter->pf_audio_filter = Convert;
417     p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
418
419     return i_ret;
420 }
421
422 /*****************************************************************************
423  * CloseFilter : deallocate data structures
424  *****************************************************************************/
425 static void CloseFilter( vlc_object_t *p_this )
426 {
427     filter_t *p_filter = (filter_t *)p_this;
428     filter_sys_t *p_sys = p_filter->p_sys;
429
430     dts_free( p_sys->p_libdts );
431     free( p_sys );
432 }
433
434 static block_t *Convert( filter_t *p_filter, block_t *p_block )
435 {
436     aout_filter_t aout_filter;
437     aout_buffer_t in_buf, out_buf;
438     block_t *p_out;
439     int i_out_size;
440
441     if( !p_block || !p_block->i_samples )
442     {
443         if( p_block ) p_block->pf_release( p_block );
444         return NULL;
445     }
446
447     i_out_size = p_block->i_samples *
448       p_filter->fmt_out.audio.i_bitspersample *
449         p_filter->fmt_out.audio.i_channels / 8;
450
451     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
452     if( !p_out )
453     {
454         msg_Warn( p_filter, "can't get output buffer" );
455         p_block->pf_release( p_block );
456         return NULL;
457     }
458
459     p_out->i_samples = p_block->i_samples;
460     p_out->i_dts = p_block->i_dts;
461     p_out->i_pts = p_block->i_pts;
462     p_out->i_length = p_block->i_length;
463
464     aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
465     aout_filter.input = p_filter->fmt_in.audio;
466     aout_filter.input.i_format = p_filter->fmt_in.i_codec;
467     aout_filter.output = p_filter->fmt_out.audio;
468     aout_filter.output.i_format = p_filter->fmt_out.i_codec;
469
470     in_buf.p_buffer = p_block->p_buffer;
471     in_buf.i_nb_bytes = p_block->i_buffer;
472     in_buf.i_nb_samples = p_block->i_samples;
473     out_buf.p_buffer = p_out->p_buffer;
474     out_buf.i_nb_bytes = p_out->i_buffer;
475     out_buf.i_nb_samples = p_out->i_samples;
476
477     DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
478
479     p_out->i_buffer = out_buf.i_nb_bytes;
480     p_out->i_samples = out_buf.i_nb_samples;
481
482     p_block->pf_release( p_block );
483
484     return p_out;
485 }