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.2 2004/02/29 13:05:22 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 #include <dts.h> /* libdts header file */
36 #include "audio_output.h"
37 #include "aout_internal.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Create ( vlc_object_t * );
43 static void Destroy ( vlc_object_t * );
44 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
47 /*****************************************************************************
49 *****************************************************************************/
50 struct aout_filter_sys_t
52 dts_state_t * p_libdts; /* libdts internal structure */
53 vlc_bool_t b_dynrng; /* see below */
54 int i_flags; /* libdts flags, see dtsdec/doc/libdts.txt */
55 vlc_bool_t b_dontwarn;
56 int i_nb_channels; /* number of float32 per sample */
59 /*****************************************************************************
61 *****************************************************************************/
62 #define DYNRNG_TEXT N_("DTS dynamic range compression")
63 #define DYNRNG_LONGTEXT N_( \
64 "Dynamic range compression makes the loud sounds softer, and the soft " \
65 "sounds louder, so you can more easily listen to the stream in a noisy " \
66 "environment without disturbing anyone. If you disable the dynamic range "\
67 "compression the playback will be more adapted to a movie theater or a " \
71 set_description( _("DTS Coherent Acoustics audio decoder") );
72 add_bool( "dts-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, VLC_FALSE );
73 set_capability( "audio filter", 100 );
74 set_callbacks( Create, Destroy );
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Create( vlc_object_t * _p_filter )
82 aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
83 struct aout_filter_sys_t * p_sys;
85 if ( p_filter->input.i_format != VLC_FOURCC('d','t','s',' ')
86 || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
91 if ( p_filter->input.i_rate != p_filter->output.i_rate )
96 /* Allocate the memory needed to store the module's structure */
97 p_sys = p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
100 msg_Err( p_filter, "out of memory" );
104 p_sys->b_dynrng = config_GetInt( p_filter, "dts-dynrng" );
105 p_sys->b_dontwarn = 0;
107 /* We'll do our own downmixing, thanks. */
108 p_sys->i_nb_channels = aout_FormatNbChannels( &p_filter->output );
109 switch ( (p_filter->output.i_physical_channels & AOUT_CHAN_PHYSMASK)
112 case AOUT_CHAN_CENTER:
113 if ( (p_filter->output.i_original_channels & AOUT_CHAN_CENTER)
114 || (p_filter->output.i_original_channels
115 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
117 p_sys->i_flags = DTS_MONO;
121 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
122 if ( p_filter->output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
124 p_sys->i_flags = DTS_DOLBY;
126 else if ( p_filter->input.i_original_channels == AOUT_CHAN_CENTER )
128 p_sys->i_flags = DTS_MONO;
130 else if ( p_filter->input.i_original_channels & AOUT_CHAN_DUALMONO )
132 p_sys->i_flags = DTS_CHANNEL;
136 p_sys->i_flags = DTS_STEREO;
140 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
141 p_sys->i_flags = DTS_3F;
144 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
145 p_sys->i_flags = DTS_2F1R;
148 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
149 | AOUT_CHAN_REARCENTER:
150 p_sys->i_flags = DTS_3F1R;
153 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
154 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
155 p_sys->i_flags = DTS_2F2R;
158 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
159 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
160 p_sys->i_flags = DTS_3F2R;
164 msg_Warn( p_filter, "unknown sample format!" );
168 if ( p_filter->output.i_physical_channels & AOUT_CHAN_LFE )
170 p_sys->i_flags |= DTS_LFE;
172 //p_sys->i_flags |= DTS_ADJUST_LEVEL;
174 /* Initialize libdts */
175 p_sys->p_libdts = dts_init( 0 );
176 if( p_sys->p_libdts == NULL )
178 msg_Err( p_filter, "unable to initialize libdts" );
182 p_filter->pf_do_work = DoWork;
183 p_filter->b_in_place = 0;
188 /*****************************************************************************
189 * Interleave: helper function to interleave channels
190 *****************************************************************************/
191 static void Interleave( float * p_out, const float * p_in, int i_nb_channels )
193 /* We do not only have to interleave, but also reorder the channels
194 * Channel reordering according to number of output channels of libdts
195 * The reordering needs to be different for different channel configurations
196 * (3F2R, 1F2R etc), so this is only temporary.
197 * The WG-4 order is appropriate for stereo, quadrophonia, and 5.1 surround.
200 * channel libdts order WG-4 order
208 * The libdts moves channels to the front if there are unused spaces, so
209 * there is no gap between channels. The translation table says which
210 * channel of the new stream is taken from which original channel [use
211 * the new channel as the array index, use the number you get from the
212 * array to address the original channel].
215 static const int translation[7][6] =
216 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
217 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
218 { 0, 1, 0, 0, 0, 0 }, /* 2 */
219 { 1, 2, 0, 0, 0, 0 }, /* 3 */
220 { 0, 1, 2, 3, 0, 0 }, /* 4 */
221 { 1, 2, 3, 4, 0, 0 }, /* 5 */
222 { 1, 2, 3, 4, 0, 5 }}; /* 6 */
225 for ( j = 0; j < i_nb_channels; j++ )
227 for ( i = 0; i < 256; i++ )
229 p_out[i * i_nb_channels + j] = p_in[translation[i_nb_channels][j]
235 /*****************************************************************************
236 * Duplicate: helper function to duplicate a unique channel
237 *****************************************************************************/
238 static void Duplicate( float * p_out, const float * p_in )
242 for ( i = 256; i--; )
250 /*****************************************************************************
251 * Exchange: helper function to exchange left & right channels
252 *****************************************************************************/
253 static void Exchange( float * p_out, const float * p_in )
256 const float * p_first = p_in + 256;
257 const float * p_second = p_in;
259 for ( i = 0; i < 256; i++ )
261 *p_out++ = *p_first++;
262 *p_out++ = *p_second++;
266 /*****************************************************************************
267 * DoWork: decode a DTS frame.
268 *****************************************************************************/
269 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
270 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
272 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
273 sample_t i_sample_level = 1;
274 int i_flags = p_sys->i_flags;
275 int i_bytes_per_block = 256 * p_sys->i_nb_channels
280 * Do the actual decoding now.
283 /* Needs to be called so the decoder knows which type of bitstream it is
285 int i_sample_rate, i_bit_rate, i_frame_length;
286 if( !dts_syncinfo( p_sys->p_libdts, p_in_buf->p_buffer, &i_flags,
287 &i_sample_rate, &i_bit_rate, &i_frame_length ) )
289 msg_Warn( p_filter, "libdts couldn't sync on frame" );
290 p_out_buf->i_nb_samples = p_out_buf->i_nb_bytes = 0;
294 i_flags = p_sys->i_flags;
295 dts_frame( p_sys->p_libdts, p_in_buf->p_buffer,
296 &i_flags, &i_sample_level, 0 );
298 if ( (i_flags & DTS_CHANNEL_MASK) != (p_sys->i_flags & DTS_CHANNEL_MASK)
299 && !p_sys->b_dontwarn )
302 "libdts couldn't do the requested downmix 0x%x->0x%x",
303 p_sys->i_flags & DTS_CHANNEL_MASK,
304 i_flags & DTS_CHANNEL_MASK );
306 p_sys->b_dontwarn = 1;
309 if( 0)//!p_sys->b_dynrng )
311 dts_dynrng( p_filter->p_sys->p_libdts, NULL, NULL );
314 for ( i = 0; i < dts_blocks_num(p_filter->p_sys->p_libdts); i++ )
316 sample_t * p_samples;
318 if( dts_block( p_sys->p_libdts ) )
320 msg_Warn( p_filter, "dts_block failed for block %d", i );
324 p_samples = dts_samples( p_sys->p_libdts );
326 if ( (p_sys->i_flags & DTS_CHANNEL_MASK) == DTS_MONO
327 && (p_filter->output.i_physical_channels
328 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
330 Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
333 else if ( p_filter->output.i_original_channels
334 & AOUT_CHAN_REVERSESTEREO )
336 Exchange( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
341 /* Interleave the *$£%ù samples. */
342 Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
343 p_samples, p_sys->i_nb_channels );
347 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
348 p_out_buf->i_nb_bytes = i_bytes_per_block * i;
351 /*****************************************************************************
352 * Destroy : deallocate data structures
353 *****************************************************************************/
354 static void Destroy( vlc_object_t * _p_filter )
356 aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
357 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
359 dts_free( p_sys->p_libdts );