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