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.1 2004/02/05 22:56:12 gbazin Exp $
9 * Author: Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <stdlib.h> /* malloc(), free() */
32 #include <string.h> /* strdup() */
34 #ifdef USE_DTSDEC_TREE /* libdts header file */
35 # include "include/dts.h"
37 # include "dtsdec/dts.h"
40 #include "audio_output.h"
41 #include "aout_internal.h"
43 /*****************************************************************************
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 *,
51 /*****************************************************************************
53 *****************************************************************************/
54 struct aout_filter_sys_t
56 dts_state_t * p_libdts; /* libdts internal structure */
57 vlc_bool_t b_dynrng; /* see below */
58 int i_flags; /* libdts flags, see dtsdec/doc/libdts.txt */
59 vlc_bool_t b_dontwarn;
60 int i_nb_channels; /* number of float32 per sample */
63 /*****************************************************************************
65 *****************************************************************************/
66 #define DYNRNG_TEXT N_("DTS dynamic range compression")
67 #define DYNRNG_LONGTEXT N_( \
68 "Dynamic range compression makes the loud sounds softer, and the soft " \
69 "sounds louder, so you can more easily listen to the stream in a noisy " \
70 "environment without disturbing anyone. If you disable the dynamic range "\
71 "compression the playback will be more adapted to a movie theater or a " \
75 set_description( _("DTS Coherent Acoustics audio decoder") );
76 add_bool( "dts-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, VLC_FALSE );
77 set_capability( "audio filter", 100 );
78 set_callbacks( Create, Destroy );
81 /*****************************************************************************
83 *****************************************************************************/
84 static int Create( vlc_object_t * _p_filter )
86 aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
87 struct aout_filter_sys_t * p_sys;
89 if ( p_filter->input.i_format != VLC_FOURCC('d','t','s',' ')
90 || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
95 if ( p_filter->input.i_rate != p_filter->output.i_rate )
100 /* Allocate the memory needed to store the module's structure */
101 p_sys = p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
104 msg_Err( p_filter, "out of memory" );
108 p_sys->b_dynrng = config_GetInt( p_filter, "dts-dynrng" );
109 p_sys->b_dontwarn = 0;
111 /* We'll do our own downmixing, thanks. */
112 p_sys->i_nb_channels = aout_FormatNbChannels( &p_filter->output );
113 switch ( (p_filter->output.i_physical_channels & AOUT_CHAN_PHYSMASK)
116 case AOUT_CHAN_CENTER:
117 if ( (p_filter->output.i_original_channels & AOUT_CHAN_CENTER)
118 || (p_filter->output.i_original_channels
119 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
121 p_sys->i_flags = DTS_MONO;
125 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
126 if ( p_filter->output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
128 p_sys->i_flags = DTS_DOLBY;
130 else if ( p_filter->input.i_original_channels == AOUT_CHAN_CENTER )
132 p_sys->i_flags = DTS_MONO;
134 else if ( p_filter->input.i_original_channels & AOUT_CHAN_DUALMONO )
136 p_sys->i_flags = DTS_CHANNEL;
140 p_sys->i_flags = DTS_STEREO;
144 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
145 p_sys->i_flags = DTS_3F;
148 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
149 p_sys->i_flags = DTS_2F1R;
152 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
153 | AOUT_CHAN_REARCENTER:
154 p_sys->i_flags = DTS_3F1R;
157 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
158 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
159 p_sys->i_flags = DTS_2F2R;
162 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
163 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
164 p_sys->i_flags = DTS_3F2R;
168 msg_Warn( p_filter, "unknown sample format!" );
172 if ( p_filter->output.i_physical_channels & AOUT_CHAN_LFE )
174 p_sys->i_flags |= DTS_LFE;
176 //p_sys->i_flags |= DTS_ADJUST_LEVEL;
178 /* Initialize libdts */
179 p_sys->p_libdts = dts_init( 0 );
180 if( p_sys->p_libdts == NULL )
182 msg_Err( p_filter, "unable to initialize libdts" );
186 p_filter->pf_do_work = DoWork;
187 p_filter->b_in_place = 0;
192 /*****************************************************************************
193 * Interleave: helper function to interleave channels
194 *****************************************************************************/
195 static void Interleave( float * p_out, const float * p_in, int i_nb_channels )
197 /* We do not only have to interleave, but also reorder the channels
198 * Channel reordering according to number of output channels of libdts
199 * The reordering needs to be different for different channel configurations
200 * (3F2R, 1F2R etc), so this is only temporary.
201 * The WG-4 order is appropriate for stereo, quadrophonia, and 5.1 surround.
204 * channel libdts order WG-4 order
212 * The libdts moves channels to the front if there are unused spaces, so
213 * there is no gap between channels. The translation table says which
214 * channel of the new stream is taken from which original channel [use
215 * the new channel as the array index, use the number you get from the
216 * array to address the original channel].
219 static const int translation[7][6] =
220 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
221 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
222 { 0, 1, 0, 0, 0, 0 }, /* 2 */
223 { 1, 2, 0, 0, 0, 0 }, /* 3 */
224 { 0, 1, 2, 3, 0, 0 }, /* 4 */
225 { 1, 2, 3, 4, 0, 0 }, /* 5 */
226 { 1, 2, 3, 4, 0, 5 }}; /* 6 */
229 for ( j = 0; j < i_nb_channels; j++ )
231 for ( i = 0; i < 256; i++ )
233 p_out[i * i_nb_channels + j] = p_in[translation[i_nb_channels][j]
239 /*****************************************************************************
240 * Duplicate: helper function to duplicate a unique channel
241 *****************************************************************************/
242 static void Duplicate( float * p_out, const float * p_in )
246 for ( i = 256; i--; )
254 /*****************************************************************************
255 * Exchange: helper function to exchange left & right channels
256 *****************************************************************************/
257 static void Exchange( float * p_out, const float * p_in )
260 const float * p_first = p_in + 256;
261 const float * p_second = p_in;
263 for ( i = 0; i < 256; i++ )
265 *p_out++ = *p_first++;
266 *p_out++ = *p_second++;
270 /*****************************************************************************
271 * DoWork: decode a DTS frame.
272 *****************************************************************************/
273 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
274 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
276 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
277 sample_t i_sample_level = 1;
278 int i_flags = p_sys->i_flags;
279 int i_bytes_per_block = 256 * p_sys->i_nb_channels
284 * Do the actual decoding now.
287 /* Needs to be called so the decoder knows which type of bitstream it is
289 int i_sample_rate, i_bit_rate, i_frame_length;
290 if( !dts_syncinfo( p_sys->p_libdts, p_in_buf->p_buffer, &i_flags,
291 &i_sample_rate, &i_bit_rate, &i_frame_length ) )
293 msg_Warn( p_filter, "libdts couldn't sync on frame" );
294 p_out_buf->i_nb_samples = p_out_buf->i_nb_bytes = 0;
298 i_flags = p_sys->i_flags;
299 dts_frame( p_sys->p_libdts, p_in_buf->p_buffer,
300 &i_flags, &i_sample_level, 0 );
302 if ( (i_flags & DTS_CHANNEL_MASK) != (p_sys->i_flags & DTS_CHANNEL_MASK)
303 && !p_sys->b_dontwarn )
306 "libdts couldn't do the requested downmix 0x%x->0x%x",
307 p_sys->i_flags & DTS_CHANNEL_MASK,
308 i_flags & DTS_CHANNEL_MASK );
310 p_sys->b_dontwarn = 1;
313 if( 0)//!p_sys->b_dynrng )
315 dts_dynrng( p_filter->p_sys->p_libdts, NULL, NULL );
318 for ( i = 0; i < dts_blocks_num(p_filter->p_sys->p_libdts); i++ )
320 sample_t * p_samples;
322 if( dts_block( p_sys->p_libdts ) )
324 msg_Warn( p_filter, "dts_block failed for block %d", i );
328 p_samples = dts_samples( p_sys->p_libdts );
330 if ( (p_sys->i_flags & DTS_CHANNEL_MASK) == DTS_MONO
331 && (p_filter->output.i_physical_channels
332 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
334 Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
337 else if ( p_filter->output.i_original_channels
338 & AOUT_CHAN_REVERSESTEREO )
340 Exchange( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
345 /* Interleave the *$£%ù samples. */
346 Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
347 p_samples, p_sys->i_nb_channels );
351 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
352 p_out_buf->i_nb_bytes = i_bytes_per_block * i;
355 /*****************************************************************************
356 * Destroy : deallocate data structures
357 *****************************************************************************/
358 static void Destroy( vlc_object_t * _p_filter )
360 aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
361 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
363 dts_free( p_sys->p_libdts );