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