]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/dtstofloat32.c
Improvements to preferences
[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 VideoLAN
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_AUDIO );
91     set_subcategory( SUBCAT_AUDIO_MISC );
92     set_description( _("DTS Coherent Acoustics audio decoder") );
93     add_bool( "dts-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, VLC_FALSE );
94     set_capability( "audio filter", 100 );
95     set_callbacks( Create, Destroy );
96
97     add_submodule();
98     set_description( _("DTS Coherent Acoustics audio decoder") );
99     set_capability( "audio filter2", 100 );
100     set_callbacks( OpenFilter, CloseFilter );
101 vlc_module_end();
102
103 /*****************************************************************************
104  * Create: 
105  *****************************************************************************/
106 static int Create( vlc_object_t *p_this )
107 {
108     aout_filter_t *p_filter = (aout_filter_t *)p_this;
109     filter_sys_t *p_sys;
110     int i_ret;
111
112     if ( p_filter->input.i_format != VLC_FOURCC('d','t','s',' ')
113           || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
114     {
115         return -1;
116     }
117
118     if ( p_filter->input.i_rate != p_filter->output.i_rate )
119     {
120         return -1;
121     }
122
123     /* Allocate the memory needed to store the module's structure */
124     p_sys = malloc( sizeof(filter_sys_t) );
125     p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
126     if( p_sys == NULL )
127     {
128         msg_Err( p_filter, "out of memory" );
129         return -1;
130     }
131
132     i_ret = Open( VLC_OBJECT(p_filter), p_sys,
133                   p_filter->input, p_filter->output );
134
135     p_filter->pf_do_work = DoWork;
136     p_filter->b_in_place = 0;
137
138     return i_ret;
139 }
140
141 /*****************************************************************************
142  * Open: 
143  *****************************************************************************/
144 static int Open( vlc_object_t *p_this, filter_sys_t *p_sys,
145                  audio_format_t input, audio_format_t output )
146 {
147     p_sys->b_dynrng = config_GetInt( p_this, "dts-dynrng" );
148     p_sys->b_dontwarn = 0;
149
150     /* We'll do our own downmixing, thanks. */
151     p_sys->i_nb_channels = aout_FormatNbChannels( &output );
152     switch ( (output.i_physical_channels & AOUT_CHAN_PHYSMASK)
153               & ~AOUT_CHAN_LFE )
154     {
155     case AOUT_CHAN_CENTER:
156         if ( (output.i_original_channels & AOUT_CHAN_CENTER)
157               || (output.i_original_channels
158                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
159         {
160             p_sys->i_flags = DTS_MONO;
161         }
162         break;
163
164     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
165         if ( output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
166         {
167             p_sys->i_flags = DTS_DOLBY;
168         }
169         else if ( input.i_original_channels == AOUT_CHAN_CENTER )
170         {
171             p_sys->i_flags = DTS_MONO;
172         }
173         else if ( input.i_original_channels & AOUT_CHAN_DUALMONO )
174         {
175             p_sys->i_flags = DTS_CHANNEL;
176         }
177         else
178         {
179             p_sys->i_flags = DTS_STEREO;
180         }
181         break;
182
183     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
184         p_sys->i_flags = DTS_3F;
185         break;
186
187     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
188         p_sys->i_flags = DTS_2F1R;
189         break;
190
191     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
192           | AOUT_CHAN_REARCENTER:
193         p_sys->i_flags = DTS_3F1R;
194         break;
195
196     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
197           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
198         p_sys->i_flags = DTS_2F2R;
199         break;
200
201     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
202           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
203         p_sys->i_flags = DTS_3F2R;
204         break;
205
206     default:
207         msg_Warn( p_this, "unknown sample format!" );
208         free( p_sys );
209         return -1;
210     }
211     if ( output.i_physical_channels & AOUT_CHAN_LFE )
212     {
213         p_sys->i_flags |= DTS_LFE;
214     }
215     //p_sys->i_flags |= DTS_ADJUST_LEVEL;
216
217     /* Initialize libdts */
218     p_sys->p_libdts = dts_init( 0 );
219     if( p_sys->p_libdts == NULL )
220     {
221         msg_Err( p_this, "unable to initialize libdts" );
222         return VLC_EGENERIC;
223     }
224
225     aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
226                               output.i_physical_channels & AOUT_CHAN_PHYSMASK,
227                               p_sys->i_nb_channels,
228                               p_sys->pi_chan_table );
229
230     return VLC_SUCCESS;
231 }
232
233 /*****************************************************************************
234  * Interleave: helper function to interleave channels
235  *****************************************************************************/
236 static void Interleave( float * p_out, const float * p_in, int i_nb_channels,
237                         int *pi_chan_table )
238 {
239     /* We do not only have to interleave, but also reorder the channels. */
240
241     int i, j;
242     for ( j = 0; j < i_nb_channels; j++ )
243     {
244         for ( i = 0; i < 256; i++ )
245         {
246             p_out[i * i_nb_channels + pi_chan_table[j]] = p_in[j * 256 + i];
247         }
248     }
249 }
250
251 /*****************************************************************************
252  * Duplicate: helper function to duplicate a unique channel
253  *****************************************************************************/
254 static void Duplicate( float * p_out, const float * p_in )
255 {
256     int i;
257
258     for ( i = 256; i--; )
259     {
260         *p_out++ = *p_in;
261         *p_out++ = *p_in;
262         p_in++;
263     }
264 }
265
266 /*****************************************************************************
267  * Exchange: helper function to exchange left & right channels
268  *****************************************************************************/
269 static void Exchange( float * p_out, const float * p_in )
270 {
271     int i;
272     const float * p_first = p_in + 256;
273     const float * p_second = p_in;
274
275     for ( i = 0; i < 256; i++ )
276     {
277         *p_out++ = *p_first++;
278         *p_out++ = *p_second++;
279     }
280 }
281
282 /*****************************************************************************
283  * DoWork: decode a DTS frame.
284  *****************************************************************************/
285 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
286                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
287 {
288     filter_sys_t    *p_sys = (filter_sys_t *)p_filter->p_sys;
289     sample_t        i_sample_level = 1;
290     int             i_flags = p_sys->i_flags;
291     int             i_bytes_per_block = 256 * p_sys->i_nb_channels
292                       * sizeof(float);
293     int             i;
294
295     /*
296      * Do the actual decoding now.
297      */
298
299     /* Needs to be called so the decoder knows which type of bitstream it is
300      * dealing with. */
301     int i_sample_rate, i_bit_rate, i_frame_length;
302     if( !dts_syncinfo( p_sys->p_libdts, p_in_buf->p_buffer, &i_flags,
303                        &i_sample_rate, &i_bit_rate, &i_frame_length ) )
304     {
305         msg_Warn( p_filter, "libdts couldn't sync on frame" );
306         p_out_buf->i_nb_samples = p_out_buf->i_nb_bytes = 0;
307         return;
308     }
309
310     i_flags = p_sys->i_flags;
311     dts_frame( p_sys->p_libdts, p_in_buf->p_buffer,
312                &i_flags, &i_sample_level, 0 );
313
314     if ( (i_flags & DTS_CHANNEL_MASK) != (p_sys->i_flags & DTS_CHANNEL_MASK)
315           && !p_sys->b_dontwarn )
316     {
317         msg_Warn( p_filter,
318                   "libdts couldn't do the requested downmix 0x%x->0x%x",
319                   p_sys->i_flags  & DTS_CHANNEL_MASK,
320                   i_flags & DTS_CHANNEL_MASK );
321
322         p_sys->b_dontwarn = 1;
323     }
324
325     if( 0)//!p_sys->b_dynrng )
326     {
327         dts_dynrng( p_sys->p_libdts, NULL, NULL );
328     }
329
330     for ( i = 0; i < dts_blocks_num(p_sys->p_libdts); i++ )
331     {
332         sample_t * p_samples;
333
334         if( dts_block( p_sys->p_libdts ) )
335         {
336             msg_Warn( p_filter, "dts_block failed for block %d", i );
337             break;
338         }
339
340         p_samples = dts_samples( p_sys->p_libdts );
341
342         if ( (p_sys->i_flags & DTS_CHANNEL_MASK) == DTS_MONO
343               && (p_filter->output.i_physical_channels 
344                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
345         {
346             Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
347                        p_samples );
348         }
349         else if ( p_filter->output.i_original_channels
350                     & AOUT_CHAN_REVERSESTEREO )
351         {
352             Exchange( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
353                       p_samples );
354         }
355         else
356         {
357             /* Interleave the *$£%ù samples. */
358             Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
359                         p_samples, p_sys->i_nb_channels, p_sys->pi_chan_table);
360         }
361     }
362
363     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
364     p_out_buf->i_nb_bytes = i_bytes_per_block * i;
365 }
366
367 /*****************************************************************************
368  * Destroy : deallocate data structures
369  *****************************************************************************/
370 static void Destroy( vlc_object_t *p_this )
371 {
372     aout_filter_t *p_filter = (aout_filter_t *)p_this;
373     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
374
375     dts_free( p_sys->p_libdts );
376     free( p_sys );
377 }
378
379 /*****************************************************************************
380  * OpenFilter: 
381  *****************************************************************************/
382 static int OpenFilter( vlc_object_t *p_this )
383 {
384     filter_t *p_filter = (filter_t *)p_this;
385     filter_sys_t *p_sys;
386     int i_ret;
387
388     if( p_filter->fmt_in.i_codec != VLC_FOURCC('d','t','s',' ')  )
389     {
390         return VLC_EGENERIC;
391     }
392
393     p_filter->fmt_out.audio.i_format =
394         p_filter->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
395
396     /* Allocate the memory needed to store the module's structure */
397     p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
398     if( p_sys == NULL )
399     {
400         msg_Err( p_filter, "out of memory" );
401         return VLC_EGENERIC;
402     }
403
404     /* Allocate the memory needed to store the module's structure */
405     p_filter->p_sys = p_sys = malloc( sizeof(filter_sys_t) );
406     if( p_sys == NULL )
407     {
408         msg_Err( p_filter, "out of memory" );
409         return VLC_EGENERIC;
410     }
411
412     i_ret = Open( VLC_OBJECT(p_filter), p_sys,
413                   p_filter->fmt_in.audio, p_filter->fmt_out.audio );
414
415     p_filter->pf_audio_filter = Convert;
416
417     return i_ret;
418 }
419
420 /*****************************************************************************
421  * CloseFilter : deallocate data structures
422  *****************************************************************************/
423 static void CloseFilter( vlc_object_t *p_this )
424 {
425     filter_t *p_filter = (filter_t *)p_this;
426     filter_sys_t *p_sys = p_filter->p_sys;
427
428     dts_free( p_sys->p_libdts );
429     free( p_sys );
430 }
431
432 static block_t *Convert( filter_t *p_filter, block_t *p_block )
433 {
434     aout_filter_t aout_filter;
435     aout_buffer_t in_buf, out_buf;
436     block_t *p_out;
437     int i_out_size;
438
439     if( !p_block || !p_block->i_samples )
440     {
441         if( p_block ) p_block->pf_release( p_block );
442         return NULL;
443     }
444
445     i_out_size = p_block->i_samples *
446       p_filter->fmt_out.audio.i_bitspersample *
447         p_filter->fmt_out.audio.i_channels / 8;
448
449     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
450     if( !p_out )
451     {
452         msg_Warn( p_filter, "can't get output buffer" );
453         p_block->pf_release( p_block );
454         return NULL;
455     }
456
457     p_out->i_samples = p_block->i_samples;
458     p_out->i_dts = p_block->i_dts;
459     p_out->i_pts = p_block->i_pts;
460     p_out->i_length = p_block->i_length;
461
462     aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
463     aout_filter.input = p_filter->fmt_in.audio;
464     aout_filter.input.i_format = p_filter->fmt_in.i_codec;
465     aout_filter.output = p_filter->fmt_out.audio;
466     aout_filter.output.i_format = p_filter->fmt_out.i_codec;
467
468     in_buf.p_buffer = p_block->p_buffer;
469     in_buf.i_nb_bytes = p_block->i_buffer;
470     in_buf.i_nb_samples = p_block->i_samples;
471     out_buf.p_buffer = p_out->p_buffer;
472     out_buf.i_nb_bytes = p_out->i_buffer;
473     out_buf.i_nb_samples = p_out->i_samples;
474
475     DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
476
477     p_out->i_buffer = out_buf.i_nb_bytes;
478     p_out->i_samples = out_buf.i_nb_samples;
479
480     p_block->pf_release( p_block );
481
482     return p_out;
483 }