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
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
37 #include <dca.h> /* libdca header file */
40 #include <vlc_block.h>
41 #include "vlc_filter.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 static int Open ( vlc_object_t *, filter_sys_t *,
52 audio_format_t, audio_format_t );
54 static int OpenFilter ( vlc_object_t * );
55 static void CloseFilter( vlc_object_t * );
56 static block_t *Convert( filter_t *, block_t * );
58 /* libdca channel order
59 * libdca currently only decodes 5.1, even if you have a DTS-ES source. */
60 static const uint32_t pi_channels_in[] =
61 { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
62 AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
66 /*****************************************************************************
68 *****************************************************************************/
71 dca_state_t * p_libdca; /* libdca internal structure */
72 bool b_dynrng; /* see below */
73 int i_flags; /* libdca flags, see dtsdec/doc/libdts.txt */
75 int i_nb_channels; /* number of float32 per sample */
77 int pi_chan_table[AOUT_CHAN_MAX]; /* channel reordering */
80 /*****************************************************************************
82 *****************************************************************************/
83 #define DYNRNG_TEXT N_("DTS dynamic range compression")
84 #define DYNRNG_LONGTEXT N_( \
85 "Dynamic range compression makes the loud sounds softer, and the soft " \
86 "sounds louder, so you can more easily listen to the stream in a noisy " \
87 "environment without disturbing anyone. If you disable the dynamic range "\
88 "compression the playback will be more adapted to a movie theater or a " \
92 set_category( CAT_INPUT )
93 set_subcategory( SUBCAT_INPUT_ACODEC )
94 set_shortname( "DCA" )
95 set_description( N_("DTS Coherent Acoustics audio decoder") )
96 add_bool( "dts-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, false )
97 set_capability( "audio filter", 100 )
98 set_callbacks( Create, Destroy )
101 set_description( N_("DTS Coherent Acoustics audio decoder") )
102 set_capability( "audio filter2", 100 )
103 set_callbacks( OpenFilter, CloseFilter )
106 /*****************************************************************************
108 *****************************************************************************/
109 static int Create( vlc_object_t *p_this )
111 aout_filter_t *p_filter = (aout_filter_t *)p_this;
115 if ( p_filter->input.i_format != VLC_CODEC_DTS
116 || p_filter->output.i_format != VLC_CODEC_FL32 )
121 if ( p_filter->input.i_rate != p_filter->output.i_rate )
126 /* Allocate the memory needed to store the module's structure */
127 p_sys = malloc( sizeof(filter_sys_t) );
128 p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
132 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
133 p_filter->input, p_filter->output );
135 p_filter->pf_do_work = DoWork;
136 p_filter->b_in_place = 0;
141 /*****************************************************************************
143 *****************************************************************************/
144 static int Open( vlc_object_t *p_this, filter_sys_t *p_sys,
145 audio_format_t input, audio_format_t output )
147 p_sys->b_dynrng = config_GetInt( p_this, "dts-dynrng" );
148 p_sys->b_dontwarn = 0;
150 /* We'll do our own downmixing, thanks. */
151 p_sys->i_nb_channels = aout_FormatNbChannels( &output );
152 switch ( (output.i_physical_channels & AOUT_CHAN_PHYSMASK)
155 case AOUT_CHAN_CENTER:
156 if ( (output.i_original_channels & AOUT_CHAN_CENTER)
157 || (output.i_original_channels
158 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
160 p_sys->i_flags = DCA_MONO;
164 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
165 if ( output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
167 p_sys->i_flags = DCA_DOLBY;
169 else if ( input.i_original_channels == AOUT_CHAN_CENTER )
171 p_sys->i_flags = DCA_MONO;
173 else if ( input.i_original_channels & AOUT_CHAN_DUALMONO )
175 p_sys->i_flags = DCA_CHANNEL;
179 p_sys->i_flags = DCA_STEREO;
183 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
184 p_sys->i_flags = DCA_3F;
187 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
188 p_sys->i_flags = DCA_2F1R;
191 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
192 | AOUT_CHAN_REARCENTER:
193 p_sys->i_flags = DCA_3F1R;
196 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
197 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
198 p_sys->i_flags = DCA_2F2R;
201 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
202 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
203 p_sys->i_flags = DCA_3F2R;
207 msg_Warn( p_this, "unknown sample format!" );
211 if ( output.i_physical_channels & AOUT_CHAN_LFE )
213 p_sys->i_flags |= DCA_LFE;
215 //p_sys->i_flags |= DCA_ADJUST_LEVEL;
217 /* Initialize libdca */
218 p_sys->p_libdca = dca_init( 0 );
219 if( p_sys->p_libdca == NULL )
221 msg_Err( p_this, "unable to initialize libdca" );
225 aout_CheckChannelReorder( pi_channels_in, NULL,
226 output.i_physical_channels & AOUT_CHAN_PHYSMASK,
227 p_sys->i_nb_channels,
228 p_sys->pi_chan_table );
233 /*****************************************************************************
234 * Interleave: helper function to interleave channels
235 *****************************************************************************/
236 static void Interleave( float * p_out, const float * p_in, int i_nb_channels,
239 /* We do not only have to interleave, but also reorder the channels. */
242 for ( j = 0; j < i_nb_channels; j++ )
244 for ( i = 0; i < 256; i++ )
246 p_out[i * i_nb_channels + pi_chan_table[j]] = p_in[j * 256 + i];
251 /*****************************************************************************
252 * Duplicate: helper function to duplicate a unique channel
253 *****************************************************************************/
254 static void Duplicate( float * p_out, const float * p_in )
258 for ( i = 256; i--; )
266 /*****************************************************************************
267 * Exchange: helper function to exchange left & right channels
268 *****************************************************************************/
269 static void Exchange( float * p_out, const float * p_in )
272 const float * p_first = p_in + 256;
273 const float * p_second = p_in;
275 for ( i = 0; i < 256; i++ )
277 *p_out++ = *p_first++;
278 *p_out++ = *p_second++;
282 /*****************************************************************************
283 * DoWork: decode a DTS frame.
284 *****************************************************************************/
285 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
286 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
288 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
289 sample_t i_sample_level = 1;
290 int i_flags = p_sys->i_flags;
291 int i_bytes_per_block = 256 * p_sys->i_nb_channels
296 * Do the actual decoding now.
299 /* Needs to be called so the decoder knows which type of bitstream it is
301 int i_sample_rate, i_bit_rate, i_frame_length;
302 if( !dca_syncinfo( p_sys->p_libdca, p_in_buf->p_buffer, &i_flags,
303 &i_sample_rate, &i_bit_rate, &i_frame_length ) )
305 msg_Warn( p_aout, "libdca couldn't sync on frame" );
306 p_out_buf->i_nb_samples = p_out_buf->i_nb_bytes = 0;
310 i_flags = p_sys->i_flags;
311 dca_frame( p_sys->p_libdca, p_in_buf->p_buffer,
312 &i_flags, &i_sample_level, 0 );
314 if ( (i_flags & DCA_CHANNEL_MASK) != (p_sys->i_flags & DCA_CHANNEL_MASK)
315 && !p_sys->b_dontwarn )
318 "libdca couldn't do the requested downmix 0x%x->0x%x",
319 p_sys->i_flags & DCA_CHANNEL_MASK,
320 i_flags & DCA_CHANNEL_MASK );
322 p_sys->b_dontwarn = 1;
325 if( 0)//!p_sys->b_dynrng )
327 dca_dynrng( p_sys->p_libdca, NULL, NULL );
330 for ( i = 0; i < dca_blocks_num(p_sys->p_libdca); i++ )
332 sample_t * p_samples;
334 if( dca_block( p_sys->p_libdca ) )
336 msg_Warn( p_aout, "dca_block failed for block %d", i );
340 p_samples = dca_samples( p_sys->p_libdca );
342 if ( (p_sys->i_flags & DCA_CHANNEL_MASK) == DCA_MONO
343 && (p_filter->output.i_physical_channels
344 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
346 Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
349 else if ( p_filter->output.i_original_channels
350 & AOUT_CHAN_REVERSESTEREO )
352 Exchange( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
357 /* Interleave the *$£%ù samples. */
358 Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
359 p_samples, p_sys->i_nb_channels, p_sys->pi_chan_table);
363 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
364 p_out_buf->i_nb_bytes = i_bytes_per_block * i;
367 /*****************************************************************************
368 * Destroy : deallocate data structures
369 *****************************************************************************/
370 static void Destroy( vlc_object_t *p_this )
372 aout_filter_t *p_filter = (aout_filter_t *)p_this;
373 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
375 dca_free( p_sys->p_libdca );
379 /*****************************************************************************
381 *****************************************************************************/
382 static int OpenFilter( vlc_object_t *p_this )
384 filter_t *p_filter = (filter_t *)p_this;
388 if( p_filter->fmt_in.i_codec != VLC_CODEC_DTS )
393 p_filter->fmt_out.audio.i_format =
394 p_filter->fmt_out.i_codec = VLC_CODEC_FL32;
395 p_filter->fmt_out.audio.i_bitspersample =
396 aout_BitsPerSample( p_filter->fmt_out.i_codec );
398 /* Allocate the memory needed to store the module's structure */
399 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
403 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
404 p_filter->fmt_in.audio, p_filter->fmt_out.audio );
406 p_filter->pf_audio_filter = Convert;
407 p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
412 /*****************************************************************************
413 * CloseFilter : deallocate data structures
414 *****************************************************************************/
415 static void CloseFilter( vlc_object_t *p_this )
417 filter_t *p_filter = (filter_t *)p_this;
418 filter_sys_t *p_sys = p_filter->p_sys;
420 dca_free( p_sys->p_libdca );
424 static block_t *Convert( filter_t *p_filter, block_t *p_block )
426 aout_filter_t aout_filter;
427 aout_buffer_t in_buf, out_buf;
431 if( !p_block || !p_block->i_samples )
434 block_Release( p_block );
438 i_out_size = p_block->i_samples *
439 p_filter->fmt_out.audio.i_bitspersample *
440 p_filter->fmt_out.audio.i_channels / 8;
442 p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
445 msg_Warn( p_filter, "can't get output buffer" );
446 block_Release( p_block );
450 p_out->i_samples = p_block->i_samples;
451 p_out->i_dts = p_block->i_dts;
452 p_out->i_pts = p_block->i_pts;
453 p_out->i_length = p_block->i_length;
455 aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
456 aout_filter.input = p_filter->fmt_in.audio;
457 aout_filter.input.i_format = p_filter->fmt_in.i_codec;
458 aout_filter.output = p_filter->fmt_out.audio;
459 aout_filter.output.i_format = p_filter->fmt_out.i_codec;
461 in_buf.p_buffer = p_block->p_buffer;
462 in_buf.i_nb_bytes = p_block->i_buffer;
463 in_buf.i_nb_samples = p_block->i_samples;
464 out_buf.p_buffer = p_out->p_buffer;
465 out_buf.i_nb_bytes = p_out->i_buffer;
466 out_buf.i_nb_samples = p_out->i_samples;
468 DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
470 p_out->i_buffer = out_buf.i_nb_bytes;
471 p_out->i_samples = out_buf.i_nb_samples;
473 block_Release( p_block );