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