1 /*****************************************************************************
2 * a52tofloat32.c: ATSC A/52 aka AC-3 decoder plugin for VLC.
3 * This plugin makes use of liba52 to decode A/52 audio
4 * (http://liba52.sf.net/).
5 *****************************************************************************
6 * Copyright (C) 2001, 2002 VideoLAN
9 * Authors: Gildas Bazin <gbazin@videolan.org>
10 * Christophe Massiot <massiot@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <stdlib.h> /* malloc(), free() */
33 #include <string.h> /* strdup() */
35 # include <stdint.h> /* int16_t .. */
37 # include <inttypes.h> /* int16_t .. */
44 #ifdef USE_A52DEC_TREE /* liba52 header file */
45 # include "include/a52.h"
47 # include "a52dec/a52.h"
50 #include <vlc/decoder.h>
51 #include "aout_internal.h"
52 #include "vlc_filter.h"
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Create ( vlc_object_t * );
58 static void Destroy ( vlc_object_t * );
59 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
61 static int Open ( vlc_object_t *, filter_sys_t *,
62 audio_format_t, audio_format_t );
64 static int OpenFilter ( vlc_object_t * );
65 static void CloseFilter( vlc_object_t * );
66 static block_t *Convert( filter_t *, block_t * );
68 /* liba52 channel order */
69 static const uint32_t pi_channels_in[] =
70 { AOUT_CHAN_LFE, AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
71 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
72 /* our internal channel order (WG-4 order) */
73 static const uint32_t pi_channels_out[] =
74 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
75 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
77 /*****************************************************************************
79 *****************************************************************************/
82 a52_state_t * p_liba52; /* liba52 internal structure */
83 vlc_bool_t b_dynrng; /* see below */
84 int i_flags; /* liba52 flags, see a52dec/doc/liba52.txt */
85 vlc_bool_t b_dontwarn;
86 int i_nb_channels; /* number of float32 per sample */
88 int pi_chan_table[AOUT_CHAN_MAX]; /* channel reordering */
91 /*****************************************************************************
93 *****************************************************************************/
94 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
95 #define DYNRNG_LONGTEXT N_( \
96 "Dynamic range compression makes the loud sounds softer, and the soft " \
97 "sounds louder, so you can more easily listen to the stream in a noisy " \
98 "environment without disturbing anyone. If you disable the dynamic range "\
99 "compression the playback will be more adapted to a movie theater or a " \
103 set_shortname( _("A/52") );
104 set_description( _("ATSC A/52 (AC-3) audio decoder") );
105 set_category( CAT_INPUT );
106 set_subcategory( SUBCAT_INPUT_ACODEC );
107 add_bool( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, VLC_FALSE );
108 set_capability( "audio filter", 100 );
109 set_callbacks( Create, Destroy );
112 set_description( _("ATSC A/52 (AC-3) audio decoder") );
113 set_capability( "audio filter2", 100 );
114 set_callbacks( OpenFilter, CloseFilter );
117 /*****************************************************************************
119 *****************************************************************************/
120 static int Create( vlc_object_t *p_this )
122 aout_filter_t *p_filter = (aout_filter_t *)p_this;
123 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
126 if ( p_filter->input.i_format != VLC_FOURCC('a','5','2',' ')
127 || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
132 if ( p_filter->input.i_rate != p_filter->output.i_rate )
137 /* Allocate the memory needed to store the module's structure */
138 p_sys = malloc( sizeof(filter_sys_t) );
139 p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
142 msg_Err( p_filter, "out of memory" );
146 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
147 p_filter->input, p_filter->output );
149 p_filter->pf_do_work = DoWork;
150 p_filter->b_in_place = 0;
155 /*****************************************************************************
157 *****************************************************************************/
158 static int Open( vlc_object_t *p_this, filter_sys_t *p_sys,
159 audio_format_t input, audio_format_t output )
161 p_sys->b_dynrng = config_GetInt( p_this, "a52-dynrng" );
162 p_sys->b_dontwarn = 0;
164 /* We'll do our own downmixing, thanks. */
165 p_sys->i_nb_channels = aout_FormatNbChannels( &output );
166 switch ( (output.i_physical_channels & AOUT_CHAN_PHYSMASK)
169 case AOUT_CHAN_CENTER:
170 if ( (output.i_original_channels & AOUT_CHAN_CENTER)
171 || (output.i_original_channels
172 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
174 p_sys->i_flags = A52_MONO;
176 else if ( output.i_original_channels & AOUT_CHAN_LEFT )
178 p_sys->i_flags = A52_CHANNEL1;
182 p_sys->i_flags = A52_CHANNEL2;
186 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
187 if ( output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
189 p_sys->i_flags = A52_DOLBY;
191 else if ( input.i_original_channels == AOUT_CHAN_CENTER )
193 p_sys->i_flags = A52_MONO;
195 else if ( input.i_original_channels & AOUT_CHAN_DUALMONO )
197 p_sys->i_flags = A52_CHANNEL;
199 else if ( !(output.i_original_channels & AOUT_CHAN_RIGHT) )
201 p_sys->i_flags = A52_CHANNEL1;
203 else if ( !(output.i_original_channels & AOUT_CHAN_LEFT) )
205 p_sys->i_flags = A52_CHANNEL2;
209 p_sys->i_flags = A52_STEREO;
213 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
214 p_sys->i_flags = A52_3F;
217 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
218 p_sys->i_flags = A52_2F1R;
221 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
222 | AOUT_CHAN_REARCENTER:
223 p_sys->i_flags = A52_3F1R;
226 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
227 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
228 p_sys->i_flags = A52_2F2R;
231 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
232 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
233 p_sys->i_flags = A52_3F2R;
237 msg_Warn( p_this, "unknown sample format!" );
241 if ( output.i_physical_channels & AOUT_CHAN_LFE )
243 p_sys->i_flags |= A52_LFE;
245 p_sys->i_flags |= A52_ADJUST_LEVEL;
247 /* Initialize liba52 */
248 p_sys->p_liba52 = a52_init( 0 );
249 if( p_sys->p_liba52 == NULL )
251 msg_Err( p_this, "unable to initialize liba52" );
256 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
257 output.i_physical_channels & AOUT_CHAN_PHYSMASK,
258 p_sys->i_nb_channels,
259 p_sys->pi_chan_table );
264 /*****************************************************************************
265 * Interleave: helper function to interleave channels
266 *****************************************************************************/
267 static void Interleave( float * p_out, const float * p_in, int i_nb_channels,
270 /* We do not only have to interleave, but also reorder the channels */
273 for ( j = 0; j < i_nb_channels; j++ )
275 for ( i = 0; i < 256; i++ )
277 p_out[i * i_nb_channels + pi_chan_table[j]] = p_in[j * 256 + i];
282 /*****************************************************************************
283 * Duplicate: helper function to duplicate a unique channel
284 *****************************************************************************/
285 static void Duplicate( float * p_out, const float * p_in )
289 for ( i = 256; i--; )
297 /*****************************************************************************
298 * Exchange: helper function to exchange left & right channels
299 *****************************************************************************/
300 static void Exchange( float * p_out, const float * p_in )
303 const float * p_first = p_in + 256;
304 const float * p_second = p_in;
306 for ( i = 0; i < 256; i++ )
308 *p_out++ = *p_first++;
309 *p_out++ = *p_second++;
313 /*****************************************************************************
314 * DoWork: decode an ATSC A/52 frame.
315 *****************************************************************************/
316 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
317 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
319 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
320 sample_t i_sample_level = 1;
321 int i_flags = p_sys->i_flags;
322 int i_bytes_per_block = 256 * p_sys->i_nb_channels
326 /* Do the actual decoding now. */
327 a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
328 &i_flags, &i_sample_level, 0 );
330 if ( (i_flags & A52_CHANNEL_MASK) != (p_sys->i_flags & A52_CHANNEL_MASK)
331 && !p_sys->b_dontwarn )
333 msg_Warn( VLC_OBJECT(p_aout),
334 "liba52 couldn't do the requested downmix 0x%x->0x%x",
335 p_sys->i_flags & A52_CHANNEL_MASK,
336 i_flags & A52_CHANNEL_MASK );
338 p_sys->b_dontwarn = 1;
341 if( !p_sys->b_dynrng )
343 a52_dynrng( p_sys->p_liba52, NULL, NULL );
346 for ( i = 0; i < 6; i++ )
348 sample_t * p_samples;
350 if( a52_block( p_sys->p_liba52 ) )
352 msg_Warn( VLC_OBJECT(p_aout), "a52_block failed for block %d", i );
355 p_samples = a52_samples( p_sys->p_liba52 );
357 if ( ((p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL1
358 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL2
359 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_MONO)
360 && (p_filter->output.i_physical_channels
361 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
363 Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
366 else if ( p_filter->output.i_original_channels
367 & AOUT_CHAN_REVERSESTEREO )
369 Exchange( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
374 /* Interleave the *$£%ù samples. */
375 Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
376 p_samples, p_sys->i_nb_channels, p_sys->pi_chan_table);
380 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
381 p_out_buf->i_nb_bytes = i_bytes_per_block * 6;
384 /*****************************************************************************
385 * Destroy : deallocate data structures
386 *****************************************************************************/
387 static void Destroy( vlc_object_t *p_this )
389 aout_filter_t *p_filter = (aout_filter_t *)p_this;
390 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
392 a52_free( p_sys->p_liba52 );
396 /*****************************************************************************
398 *****************************************************************************/
399 static int OpenFilter( vlc_object_t *p_this )
401 filter_t *p_filter = (filter_t *)p_this;
405 if( p_filter->fmt_in.i_codec != VLC_FOURCC('a','5','2',' ') )
410 p_filter->fmt_out.audio.i_format =
411 p_filter->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
413 /* Allocate the memory needed to store the module's structure */
414 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
417 msg_Err( p_filter, "out of memory" );
421 /* Allocate the memory needed to store the module's structure */
422 p_filter->p_sys = p_sys = malloc( sizeof(filter_sys_t) );
425 msg_Err( p_filter, "out of memory" );
429 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
430 p_filter->fmt_in.audio, p_filter->fmt_out.audio );
432 p_filter->pf_audio_filter = Convert;
437 /*****************************************************************************
438 * CloseFilter : deallocate data structures
439 *****************************************************************************/
440 static void CloseFilter( vlc_object_t *p_this )
442 filter_t *p_filter = (filter_t *)p_this;
443 filter_sys_t *p_sys = p_filter->p_sys;
445 a52_free( p_sys->p_liba52 );
449 static block_t *Convert( filter_t *p_filter, block_t *p_block )
451 aout_filter_t aout_filter;
452 aout_buffer_t in_buf, out_buf;
456 if( !p_block || !p_block->i_samples )
458 if( p_block ) p_block->pf_release( p_block );
462 i_out_size = p_block->i_samples *
463 p_filter->fmt_out.audio.i_bitspersample *
464 p_filter->fmt_out.audio.i_channels / 8;
466 p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
469 msg_Warn( p_filter, "can't get output buffer" );
470 p_block->pf_release( p_block );
474 p_out->i_samples = p_block->i_samples;
475 p_out->i_dts = p_block->i_dts;
476 p_out->i_pts = p_block->i_pts;
477 p_out->i_length = p_block->i_length;
479 aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
480 aout_filter.input = p_filter->fmt_in.audio;
481 aout_filter.input.i_format = p_filter->fmt_in.i_codec;
482 aout_filter.output = p_filter->fmt_out.audio;
483 aout_filter.output.i_format = p_filter->fmt_out.i_codec;
485 in_buf.p_buffer = p_block->p_buffer;
486 in_buf.i_nb_bytes = p_block->i_buffer;
487 in_buf.i_nb_samples = p_block->i_samples;
488 out_buf.p_buffer = p_out->p_buffer;
489 out_buf.i_nb_bytes = p_out->i_buffer;
490 out_buf.i_nb_samples = p_out->i_samples;
492 DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
494 p_out->i_buffer = out_buf.i_nb_bytes;
495 p_out->i_samples = out_buf.i_nb_samples;
497 p_block->pf_release( p_block );