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
9 * Author: Gildas Bazin <gbazin@videolan.org>
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 <vlc/decoder.h>
37 #include "aout_internal.h"
38 #include "vlc_filter.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Create ( vlc_object_t * );
44 static void Destroy ( vlc_object_t * );
45 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
48 static int Open ( vlc_object_t *, filter_sys_t *,
49 audio_format_t, audio_format_t );
51 static int OpenFilter ( vlc_object_t * );
52 static void CloseFilter( vlc_object_t * );
53 static block_t *Convert( filter_t *, block_t * );
55 /*****************************************************************************
57 *****************************************************************************/
60 dts_state_t * p_libdts; /* libdts internal structure */
61 vlc_bool_t b_dynrng; /* see below */
62 int i_flags; /* libdts flags, see dtsdec/doc/libdts.txt */
63 vlc_bool_t b_dontwarn;
64 int i_nb_channels; /* number of float32 per sample */
67 /*****************************************************************************
69 *****************************************************************************/
70 #define DYNRNG_TEXT N_("DTS dynamic range compression")
71 #define DYNRNG_LONGTEXT N_( \
72 "Dynamic range compression makes the loud sounds softer, and the soft " \
73 "sounds louder, so you can more easily listen to the stream in a noisy " \
74 "environment without disturbing anyone. If you disable the dynamic range "\
75 "compression the playback will be more adapted to a movie theater or a " \
79 set_description( _("DTS Coherent Acoustics audio decoder") );
80 add_bool( "dts-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, VLC_FALSE );
81 set_capability( "audio filter", 100 );
82 set_callbacks( Create, Destroy );
85 set_description( _("DTS Coherent Acoustics audio decoder") );
86 set_capability( "audio filter2", 100 );
87 set_callbacks( OpenFilter, CloseFilter );
90 /*****************************************************************************
92 *****************************************************************************/
93 static int Create( vlc_object_t *p_this )
95 aout_filter_t *p_filter = (aout_filter_t *)p_this;
99 if ( p_filter->input.i_format != VLC_FOURCC('d','t','s',' ')
100 || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
105 if ( p_filter->input.i_rate != p_filter->output.i_rate )
110 /* Allocate the memory needed to store the module's structure */
111 p_sys = malloc( sizeof(filter_sys_t) );
112 p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
115 msg_Err( p_filter, "out of memory" );
119 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
120 p_filter->input, p_filter->output );
122 p_filter->pf_do_work = DoWork;
123 p_filter->b_in_place = 0;
128 /*****************************************************************************
130 *****************************************************************************/
131 static int Open( vlc_object_t *p_this, filter_sys_t *p_sys,
132 audio_format_t input, audio_format_t output )
134 p_sys->b_dynrng = config_GetInt( p_this, "dts-dynrng" );
135 p_sys->b_dontwarn = 0;
137 /* We'll do our own downmixing, thanks. */
138 p_sys->i_nb_channels = aout_FormatNbChannels( &output );
139 switch ( (output.i_physical_channels & AOUT_CHAN_PHYSMASK)
142 case AOUT_CHAN_CENTER:
143 if ( (output.i_original_channels & AOUT_CHAN_CENTER)
144 || (output.i_original_channels
145 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
147 p_sys->i_flags = DTS_MONO;
151 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
152 if ( output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
154 p_sys->i_flags = DTS_DOLBY;
156 else if ( input.i_original_channels == AOUT_CHAN_CENTER )
158 p_sys->i_flags = DTS_MONO;
160 else if ( input.i_original_channels & AOUT_CHAN_DUALMONO )
162 p_sys->i_flags = DTS_CHANNEL;
166 p_sys->i_flags = DTS_STEREO;
170 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
171 p_sys->i_flags = DTS_3F;
174 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
175 p_sys->i_flags = DTS_2F1R;
178 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
179 | AOUT_CHAN_REARCENTER:
180 p_sys->i_flags = DTS_3F1R;
183 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
184 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
185 p_sys->i_flags = DTS_2F2R;
188 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
189 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
190 p_sys->i_flags = DTS_3F2R;
194 msg_Warn( p_this, "unknown sample format!" );
198 if ( output.i_physical_channels & AOUT_CHAN_LFE )
200 p_sys->i_flags |= DTS_LFE;
202 //p_sys->i_flags |= DTS_ADJUST_LEVEL;
204 /* Initialize libdts */
205 p_sys->p_libdts = dts_init( 0 );
206 if( p_sys->p_libdts == NULL )
208 msg_Err( p_this, "unable to initialize libdts" );
215 /*****************************************************************************
216 * Interleave: helper function to interleave channels
217 *****************************************************************************/
218 static void Interleave( float * p_out, const float * p_in, int i_nb_channels )
220 /* We do not only have to interleave, but also reorder the channels
221 * Channel reordering according to number of output channels of libdts
222 * The reordering needs to be different for different channel configurations
223 * (3F2R, 1F2R etc), so this is only temporary.
224 * The WG-4 order is appropriate for stereo, quadrophonia, and 5.1 surround.
227 * channel libdts order WG-4 order
235 * The libdts moves channels to the front if there are unused spaces, so
236 * there is no gap between channels. The translation table says which
237 * channel of the new stream is taken from which original channel [use
238 * the new channel as the array index, use the number you get from the
239 * array to address the original channel].
242 static const int translation[7][6] =
243 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
244 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
245 { 0, 1, 0, 0, 0, 0 }, /* 2 */
246 { 1, 2, 0, 0, 0, 0 }, /* 3 */
247 { 0, 1, 2, 3, 0, 0 }, /* 4 */
248 { 1, 2, 3, 4, 0, 0 }, /* 5 */
249 { 1, 2, 3, 4, 0, 5 }}; /* 6 */
252 for ( j = 0; j < i_nb_channels; j++ )
254 for ( i = 0; i < 256; i++ )
256 p_out[i * i_nb_channels + j] = p_in[translation[i_nb_channels][j]
262 /*****************************************************************************
263 * Duplicate: helper function to duplicate a unique channel
264 *****************************************************************************/
265 static void Duplicate( float * p_out, const float * p_in )
269 for ( i = 256; i--; )
277 /*****************************************************************************
278 * Exchange: helper function to exchange left & right channels
279 *****************************************************************************/
280 static void Exchange( float * p_out, const float * p_in )
283 const float * p_first = p_in + 256;
284 const float * p_second = p_in;
286 for ( i = 0; i < 256; i++ )
288 *p_out++ = *p_first++;
289 *p_out++ = *p_second++;
293 /*****************************************************************************
294 * DoWork: decode a DTS frame.
295 *****************************************************************************/
296 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
297 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
299 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
300 sample_t i_sample_level = 1;
301 int i_flags = p_sys->i_flags;
302 int i_bytes_per_block = 256 * p_sys->i_nb_channels
307 * Do the actual decoding now.
310 /* Needs to be called so the decoder knows which type of bitstream it is
312 int i_sample_rate, i_bit_rate, i_frame_length;
313 if( !dts_syncinfo( p_sys->p_libdts, p_in_buf->p_buffer, &i_flags,
314 &i_sample_rate, &i_bit_rate, &i_frame_length ) )
316 msg_Warn( p_filter, "libdts couldn't sync on frame" );
317 p_out_buf->i_nb_samples = p_out_buf->i_nb_bytes = 0;
321 i_flags = p_sys->i_flags;
322 dts_frame( p_sys->p_libdts, p_in_buf->p_buffer,
323 &i_flags, &i_sample_level, 0 );
325 if ( (i_flags & DTS_CHANNEL_MASK) != (p_sys->i_flags & DTS_CHANNEL_MASK)
326 && !p_sys->b_dontwarn )
329 "libdts couldn't do the requested downmix 0x%x->0x%x",
330 p_sys->i_flags & DTS_CHANNEL_MASK,
331 i_flags & DTS_CHANNEL_MASK );
333 p_sys->b_dontwarn = 1;
336 if( 0)//!p_sys->b_dynrng )
338 dts_dynrng( p_sys->p_libdts, NULL, NULL );
341 for ( i = 0; i < dts_blocks_num(p_sys->p_libdts); i++ )
343 sample_t * p_samples;
345 if( dts_block( p_sys->p_libdts ) )
347 msg_Warn( p_filter, "dts_block failed for block %d", i );
351 p_samples = dts_samples( p_sys->p_libdts );
353 if ( (p_sys->i_flags & DTS_CHANNEL_MASK) == DTS_MONO
354 && (p_filter->output.i_physical_channels
355 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
357 Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
360 else if ( p_filter->output.i_original_channels
361 & AOUT_CHAN_REVERSESTEREO )
363 Exchange( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
368 /* Interleave the *$£%ù samples. */
369 Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
370 p_samples, p_sys->i_nb_channels );
374 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
375 p_out_buf->i_nb_bytes = i_bytes_per_block * i;
378 /*****************************************************************************
379 * Destroy : deallocate data structures
380 *****************************************************************************/
381 static void Destroy( vlc_object_t *p_this )
383 aout_filter_t *p_filter = (aout_filter_t *)p_this;
384 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
386 dts_free( p_sys->p_libdts );
390 /*****************************************************************************
392 *****************************************************************************/
393 static int OpenFilter( vlc_object_t *p_this )
395 filter_t *p_filter = (filter_t *)p_this;
399 if( p_filter->fmt_in.i_codec != VLC_FOURCC('d','t','s',' ') )
404 p_filter->fmt_out.audio.i_format =
405 p_filter->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
407 /* Allocate the memory needed to store the module's structure */
408 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
411 msg_Err( p_filter, "out of memory" );
415 /* Allocate the memory needed to store the module's structure */
416 p_filter->p_sys = p_sys = malloc( sizeof(filter_sys_t) );
419 msg_Err( p_filter, "out of memory" );
423 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
424 p_filter->fmt_in.audio, p_filter->fmt_out.audio );
426 p_filter->pf_audio_filter = Convert;
431 /*****************************************************************************
432 * CloseFilter : deallocate data structures
433 *****************************************************************************/
434 static void CloseFilter( vlc_object_t *p_this )
436 filter_t *p_filter = (filter_t *)p_this;
437 filter_sys_t *p_sys = p_filter->p_sys;
439 dts_free( p_sys->p_libdts );
443 static block_t *Convert( filter_t *p_filter, block_t *p_block )
445 aout_filter_t aout_filter;
446 aout_buffer_t in_buf, out_buf;
449 int i_out_size = p_block->i_samples *
450 p_filter->fmt_out.audio.i_bitspersample *
451 p_filter->fmt_out.audio.i_channels;
453 p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
456 msg_Warn( p_filter, "can't get output buffer" );
460 p_out->i_samples = p_block->i_samples;
461 p_out->i_dts = p_block->i_dts;
462 p_out->i_pts = p_block->i_pts;
463 p_out->i_length = p_block->i_length;
465 aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
466 aout_filter.input = p_filter->fmt_in.audio;
467 aout_filter.input.i_format = p_filter->fmt_in.i_codec;
468 aout_filter.output = p_filter->fmt_out.audio;
469 aout_filter.output.i_format = p_filter->fmt_out.i_codec;
471 in_buf.p_buffer = p_block->p_buffer;
472 in_buf.i_nb_bytes = p_block->i_buffer;
473 in_buf.i_nb_samples = p_block->i_samples;
474 out_buf.p_buffer = p_out->p_buffer;
475 out_buf.i_nb_bytes = p_out->i_buffer;
476 out_buf.i_nb_samples = p_out->i_samples;
478 DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
480 p_out->i_buffer = out_buf.i_nb_bytes;
481 p_out->i_samples = out_buf.i_nb_samples;