]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/dtstofloat32.c
* ALL: bumped revision number to 0.7.1
[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: dtstofloat32.c,v 1.2 2004/02/29 13:05:22 gbazin Exp $
8  *
9  * Author: Gildas Bazin <gbazin@netcourrier.com>
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 "audio_output.h"
37 #include "aout_internal.h"
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  Create    ( vlc_object_t * );
43 static void Destroy   ( vlc_object_t * );
44 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,  
45                         aout_buffer_t * );
46
47 /*****************************************************************************
48  * Local structures
49  *****************************************************************************/
50 struct aout_filter_sys_t
51 {
52     dts_state_t * p_libdts; /* libdts internal structure */
53     vlc_bool_t b_dynrng; /* see below */
54     int i_flags; /* libdts flags, see dtsdec/doc/libdts.txt */
55     vlc_bool_t b_dontwarn;
56     int i_nb_channels; /* number of float32 per sample */
57 };
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 #define DYNRNG_TEXT N_("DTS dynamic range compression")
63 #define DYNRNG_LONGTEXT N_( \
64     "Dynamic range compression makes the loud sounds softer, and the soft " \
65     "sounds louder, so you can more easily listen to the stream in a noisy " \
66     "environment without disturbing anyone. If you disable the dynamic range "\
67     "compression the playback will be more adapted to a movie theater or a " \
68     "listening room.")
69
70 vlc_module_begin();
71     set_description( _("DTS Coherent Acoustics audio decoder") );
72     add_bool( "dts-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, VLC_FALSE );
73     set_capability( "audio filter", 100 );
74     set_callbacks( Create, Destroy );
75 vlc_module_end();
76
77 /*****************************************************************************
78  * Create: 
79  *****************************************************************************/
80 static int Create( vlc_object_t * _p_filter )
81 {
82     aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
83     struct aout_filter_sys_t * p_sys;
84
85     if ( p_filter->input.i_format != VLC_FOURCC('d','t','s',' ')
86           || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
87     {
88         return -1;
89     }
90
91     if ( p_filter->input.i_rate != p_filter->output.i_rate )
92     {
93         return -1;
94     }
95
96     /* Allocate the memory needed to store the module's structure */
97     p_sys = p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
98     if( p_sys == NULL )
99     {
100         msg_Err( p_filter, "out of memory" );
101         return -1;
102     }
103
104     p_sys->b_dynrng = config_GetInt( p_filter, "dts-dynrng" );
105     p_sys->b_dontwarn = 0;
106
107     /* We'll do our own downmixing, thanks. */
108     p_sys->i_nb_channels = aout_FormatNbChannels( &p_filter->output );
109     switch ( (p_filter->output.i_physical_channels & AOUT_CHAN_PHYSMASK)
110               & ~AOUT_CHAN_LFE )
111     {
112     case AOUT_CHAN_CENTER:
113         if ( (p_filter->output.i_original_channels & AOUT_CHAN_CENTER)
114               || (p_filter->output.i_original_channels
115                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
116         {
117             p_sys->i_flags = DTS_MONO;
118         }
119         break;
120
121     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
122         if ( p_filter->output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
123         {
124             p_sys->i_flags = DTS_DOLBY;
125         }
126         else if ( p_filter->input.i_original_channels == AOUT_CHAN_CENTER )
127         {
128             p_sys->i_flags = DTS_MONO;
129         }
130         else if ( p_filter->input.i_original_channels & AOUT_CHAN_DUALMONO )
131         {
132             p_sys->i_flags = DTS_CHANNEL;
133         }
134         else
135         {
136             p_sys->i_flags = DTS_STEREO;
137         }
138         break;
139
140     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
141         p_sys->i_flags = DTS_3F;
142         break;
143
144     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
145         p_sys->i_flags = DTS_2F1R;
146         break;
147
148     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
149           | AOUT_CHAN_REARCENTER:
150         p_sys->i_flags = DTS_3F1R;
151         break;
152
153     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
154           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
155         p_sys->i_flags = DTS_2F2R;
156         break;
157
158     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
159           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
160         p_sys->i_flags = DTS_3F2R;
161         break;
162
163     default:
164         msg_Warn( p_filter, "unknown sample format!" );
165         free( p_sys );
166         return -1;
167     }
168     if ( p_filter->output.i_physical_channels & AOUT_CHAN_LFE )
169     {
170         p_sys->i_flags |= DTS_LFE;
171     }
172     //p_sys->i_flags |= DTS_ADJUST_LEVEL;
173
174     /* Initialize libdts */
175     p_sys->p_libdts = dts_init( 0 );
176     if( p_sys->p_libdts == NULL )
177     {
178         msg_Err( p_filter, "unable to initialize libdts" );
179         return -1;
180     }
181
182     p_filter->pf_do_work = DoWork;
183     p_filter->b_in_place = 0;
184
185     return 0;
186 }
187
188 /*****************************************************************************
189  * Interleave: helper function to interleave channels
190  *****************************************************************************/
191 static void Interleave( float * p_out, const float * p_in, int i_nb_channels )
192 {
193     /* We do not only have to interleave, but also reorder the channels
194      * Channel reordering according to number of output channels of libdts
195      * The reordering needs to be different for different channel configurations
196      * (3F2R, 1F2R etc), so this is only temporary.
197      * The WG-4 order is appropriate for stereo, quadrophonia, and 5.1 surround.
198      *
199      * 6 channel mode
200      * channel  libdts order    WG-4 order
201      * 0        C               // L
202      * 1        L               // R
203      * 2        R               // LS
204      * 3        LS              // RS
205      * 4        RS              // C
206      * 5        LFE             // LFE
207      *
208      * The libdts moves channels to the front if there are unused spaces, so
209      * there is no gap between channels. The translation table says which
210      * channel of the new stream is taken from which original channel [use
211      * the new channel as the array index, use the number you get from the
212      * array to address the original channel].
213      */
214
215     static const int translation[7][6] =
216     {{ 0, 0, 0, 0, 0, 0 },      /* 0 channels (rarely used) */
217     { 0, 0, 0, 0, 0, 0 },       /* 1 ch */
218     { 0, 1, 0, 0, 0, 0 },       /* 2 */
219     { 1, 2, 0, 0, 0, 0 },       /* 3 */
220     { 0, 1, 2, 3, 0, 0 },       /* 4 */
221     { 1, 2, 3, 4, 0, 0 },       /* 5 */
222     { 1, 2, 3, 4, 0, 5 }};      /* 6 */
223
224     int i, j;
225     for ( j = 0; j < i_nb_channels; j++ )
226     {
227         for ( i = 0; i < 256; i++ )
228         {
229             p_out[i * i_nb_channels + j] = p_in[translation[i_nb_channels][j]
230                                                  * 256 + i];
231         }
232     }
233 }
234
235 /*****************************************************************************
236  * Duplicate: helper function to duplicate a unique channel
237  *****************************************************************************/
238 static void Duplicate( float * p_out, const float * p_in )
239 {
240     int i;
241
242     for ( i = 256; i--; )
243     {
244         *p_out++ = *p_in;
245         *p_out++ = *p_in;
246         p_in++;
247     }
248 }
249
250 /*****************************************************************************
251  * Exchange: helper function to exchange left & right channels
252  *****************************************************************************/
253 static void Exchange( float * p_out, const float * p_in )
254 {
255     int i;
256     const float * p_first = p_in + 256;
257     const float * p_second = p_in;
258
259     for ( i = 0; i < 256; i++ )
260     {
261         *p_out++ = *p_first++;
262         *p_out++ = *p_second++;
263     }
264 }
265
266 /*****************************************************************************
267  * DoWork: decode a DTS frame.
268  *****************************************************************************/
269 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
270                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
271 {
272     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
273     sample_t        i_sample_level = 1;
274     int             i_flags = p_sys->i_flags;
275     int             i_bytes_per_block = 256 * p_sys->i_nb_channels
276                       * sizeof(float);
277     int             i;
278
279     /*
280      * Do the actual decoding now.
281      */
282
283     /* Needs to be called so the decoder knows which type of bitstream it is
284      * dealing with. */
285     int i_sample_rate, i_bit_rate, i_frame_length;
286     if( !dts_syncinfo( p_sys->p_libdts, p_in_buf->p_buffer, &i_flags,
287                        &i_sample_rate, &i_bit_rate, &i_frame_length ) )
288     {
289         msg_Warn( p_filter, "libdts couldn't sync on frame" );
290         p_out_buf->i_nb_samples = p_out_buf->i_nb_bytes = 0;
291         return;
292     }
293
294     i_flags = p_sys->i_flags;
295     dts_frame( p_sys->p_libdts, p_in_buf->p_buffer,
296                &i_flags, &i_sample_level, 0 );
297
298     if ( (i_flags & DTS_CHANNEL_MASK) != (p_sys->i_flags & DTS_CHANNEL_MASK)
299           && !p_sys->b_dontwarn )
300     {
301         msg_Warn( p_filter,
302                   "libdts couldn't do the requested downmix 0x%x->0x%x",
303                   p_sys->i_flags  & DTS_CHANNEL_MASK,
304                   i_flags & DTS_CHANNEL_MASK );
305
306         p_sys->b_dontwarn = 1;
307     }
308
309     if( 0)//!p_sys->b_dynrng )
310     {
311         dts_dynrng( p_filter->p_sys->p_libdts, NULL, NULL );
312     }
313
314     for ( i = 0; i < dts_blocks_num(p_filter->p_sys->p_libdts); i++ )
315     {
316         sample_t * p_samples;
317
318         if( dts_block( p_sys->p_libdts ) )
319         {
320             msg_Warn( p_filter, "dts_block failed for block %d", i );
321             break;
322         }
323
324         p_samples = dts_samples( p_sys->p_libdts );
325
326         if ( (p_sys->i_flags & DTS_CHANNEL_MASK) == DTS_MONO
327               && (p_filter->output.i_physical_channels 
328                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
329         {
330             Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
331                        p_samples );
332         }
333         else if ( p_filter->output.i_original_channels
334                     & AOUT_CHAN_REVERSESTEREO )
335         {
336             Exchange( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
337                       p_samples );
338         }
339         else
340         {
341             /* Interleave the *$£%ù samples. */
342             Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
343                         p_samples, p_sys->i_nb_channels );
344         }
345     }
346
347     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
348     p_out_buf->i_nb_bytes = i_bytes_per_block * i;
349 }
350
351 /*****************************************************************************
352  * Destroy : deallocate data structures
353  *****************************************************************************/
354 static void Destroy( vlc_object_t * _p_filter )
355 {
356     aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
357     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
358
359     dts_free( p_sys->p_libdts );
360     free( p_sys );
361 }