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