00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <vlc/vlc.h>
00031
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #ifdef HAVE_STDINT_H
00035 # include <stdint.h>
00036 #elif HAVE_INTTYPES_H
00037 # include <inttypes.h>
00038 #endif
00039
00040 #ifdef HAVE_UNISTD_H
00041 # include <unistd.h>
00042 #endif
00043
00044 #ifdef USE_A52DEC_TREE
00045 # include "include/a52.h"
00046 #else
00047 # include "a52dec/a52.h"
00048 #endif
00049
00050 #include <vlc/decoder.h>
00051 #include "aout_internal.h"
00052 #include "vlc_filter.h"
00053
00054
00055
00056
00057 static int Create ( vlc_object_t * );
00058 static void Destroy ( vlc_object_t * );
00059 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
00060 aout_buffer_t * );
00061 static int Open ( vlc_object_t *, filter_sys_t *,
00062 audio_format_t, audio_format_t );
00063
00064 static int OpenFilter ( vlc_object_t * );
00065 static void CloseFilter( vlc_object_t * );
00066 static block_t *Convert( filter_t *, block_t * );
00067
00068
00069 static const uint32_t pi_channels_in[] =
00070 { AOUT_CHAN_LFE, AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
00071 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
00072
00073 static const uint32_t pi_channels_out[] =
00074 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
00075 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
00076
00077
00078
00079
00080 struct filter_sys_t
00081 {
00082 a52_state_t * p_liba52;
00083 vlc_bool_t b_dynrng;
00084 int i_flags;
00085 vlc_bool_t b_dontwarn;
00086 int i_nb_channels;
00087
00088 int pi_chan_table[AOUT_CHAN_MAX];
00089 };
00090
00091
00092
00093
00094 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
00095 #define DYNRNG_LONGTEXT N_( \
00096 "Dynamic range compression makes the loud sounds softer, and the soft " \
00097 "sounds louder, so you can more easily listen to the stream in a noisy " \
00098 "environment without disturbing anyone. If you disable the dynamic range "\
00099 "compression the playback will be more adapted to a movie theater or a " \
00100 "listening room.")
00101 #define UPMIX_TEXT N_("Enable internal upmixing")
00102 #define UPMIX_LONGTEXT N_( \
00103 "Enable the internal upmixing algorithm (not recommended).")
00104
00105 vlc_module_begin();
00106 set_shortname( "A/52" );
00107 set_description( _("ATSC A/52 (AC-3) audio decoder") );
00108 set_category( CAT_INPUT );
00109 set_subcategory( SUBCAT_INPUT_ACODEC );
00110 add_bool( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, VLC_FALSE );
00111 add_bool( "a52-upmix", 0, NULL, UPMIX_TEXT, UPMIX_LONGTEXT, VLC_TRUE );
00112 set_capability( "audio filter", 100 );
00113 set_callbacks( Create, Destroy );
00114
00115 add_submodule();
00116 set_description( _("ATSC A/52 (AC-3) audio decoder") );
00117 set_capability( "audio filter2", 100 );
00118 set_callbacks( OpenFilter, CloseFilter );
00119 vlc_module_end();
00120
00121
00122
00123
00124 static int Create( vlc_object_t *p_this )
00125 {
00126 aout_filter_t *p_filter = (aout_filter_t *)p_this;
00127 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
00128 int i_ret;
00129
00130 if ( p_filter->input.i_format != VLC_FOURCC('a','5','2',' ')
00131 #ifdef LIBA52_FIXED
00132 || p_filter->output.i_format != VLC_FOURCC('f','i','3','2') )
00133 #else
00134 || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
00135 #endif
00136 {
00137 return -1;
00138 }
00139
00140 if ( p_filter->input.i_rate != p_filter->output.i_rate )
00141 {
00142 return -1;
00143 }
00144
00145
00146 p_sys = malloc( sizeof(filter_sys_t) );
00147 p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
00148 if( p_sys == NULL )
00149 {
00150 msg_Err( p_filter, "out of memory" );
00151 return -1;
00152 }
00153
00154 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
00155 p_filter->input, p_filter->output );
00156
00157 p_filter->pf_do_work = DoWork;
00158 p_filter->b_in_place = 0;
00159
00160 return i_ret;
00161 }
00162
00163
00164
00165
00166 static int Open( vlc_object_t *p_this, filter_sys_t *p_sys,
00167 audio_format_t input, audio_format_t output )
00168 {
00169 p_sys->b_dynrng = config_GetInt( p_this, "a52-dynrng" );
00170 p_sys->b_dontwarn = 0;
00171
00172
00173
00174 if ( aout_FormatNbChannels( &output ) > aout_FormatNbChannels( &input ) )
00175 {
00176 if ( ! config_GetInt( p_this, "a52-upmix" ) )
00177 {
00178 return VLC_EGENERIC;
00179 }
00180 }
00181
00182
00183 p_sys->i_nb_channels = aout_FormatNbChannels( &output );
00184 switch ( (output.i_physical_channels & AOUT_CHAN_PHYSMASK)
00185 & ~AOUT_CHAN_LFE )
00186 {
00187 case AOUT_CHAN_CENTER:
00188 if ( (output.i_original_channels & AOUT_CHAN_CENTER)
00189 || (output.i_original_channels
00190 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
00191 {
00192 p_sys->i_flags = A52_MONO;
00193 }
00194 else if ( output.i_original_channels & AOUT_CHAN_LEFT )
00195 {
00196 p_sys->i_flags = A52_CHANNEL1;
00197 }
00198 else
00199 {
00200 p_sys->i_flags = A52_CHANNEL2;
00201 }
00202 break;
00203
00204 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
00205 if ( output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
00206 {
00207 p_sys->i_flags = A52_DOLBY;
00208 }
00209 else if ( input.i_original_channels == AOUT_CHAN_CENTER )
00210 {
00211 p_sys->i_flags = A52_MONO;
00212 }
00213 else if ( input.i_original_channels & AOUT_CHAN_DUALMONO )
00214 {
00215 p_sys->i_flags = A52_CHANNEL;
00216 }
00217 else if ( !(output.i_original_channels & AOUT_CHAN_RIGHT) )
00218 {
00219 p_sys->i_flags = A52_CHANNEL1;
00220 }
00221 else if ( !(output.i_original_channels & AOUT_CHAN_LEFT) )
00222 {
00223 p_sys->i_flags = A52_CHANNEL2;
00224 }
00225 else
00226 {
00227 p_sys->i_flags = A52_STEREO;
00228 }
00229 break;
00230
00231 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
00232 p_sys->i_flags = A52_3F;
00233 break;
00234
00235 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
00236 p_sys->i_flags = A52_2F1R;
00237 break;
00238
00239 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
00240 | AOUT_CHAN_REARCENTER:
00241 p_sys->i_flags = A52_3F1R;
00242 break;
00243
00244 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
00245 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
00246 p_sys->i_flags = A52_2F2R;
00247 break;
00248
00249 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
00250 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
00251 p_sys->i_flags = A52_3F2R;
00252 break;
00253
00254 default:
00255 msg_Warn( p_this, "unknown sample format!" );
00256 free( p_sys );
00257 return VLC_EGENERIC;
00258 }
00259 if ( output.i_physical_channels & AOUT_CHAN_LFE )
00260 {
00261 p_sys->i_flags |= A52_LFE;
00262 }
00263 p_sys->i_flags |= A52_ADJUST_LEVEL;
00264
00265
00266 p_sys->p_liba52 = a52_init( 0 );
00267 if( p_sys->p_liba52 == NULL )
00268 {
00269 msg_Err( p_this, "unable to initialize liba52" );
00270 free( p_sys );
00271 return VLC_EGENERIC;
00272 }
00273
00274 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
00275 output.i_physical_channels & AOUT_CHAN_PHYSMASK,
00276 p_sys->i_nb_channels,
00277 p_sys->pi_chan_table );
00278
00279 return VLC_SUCCESS;
00280 }
00281
00282
00283
00284
00285 static void Interleave( float * p_out, const float * p_in, int i_nb_channels,
00286 int *pi_chan_table )
00287 {
00288
00289
00290 int i, j;
00291 for ( j = 0; j < i_nb_channels; j++ )
00292 {
00293 for ( i = 0; i < 256; i++ )
00294 {
00295 p_out[i * i_nb_channels + pi_chan_table[j]] = p_in[j * 256 + i];
00296 }
00297 }
00298 }
00299
00300
00301
00302
00303 static void Duplicate( float * p_out, const float * p_in )
00304 {
00305 int i;
00306
00307 for ( i = 256; i--; )
00308 {
00309 *p_out++ = *p_in;
00310 *p_out++ = *p_in;
00311 p_in++;
00312 }
00313 }
00314
00315
00316
00317
00318 static void Exchange( float * p_out, const float * p_in )
00319 {
00320 int i;
00321 const float * p_first = p_in + 256;
00322 const float * p_second = p_in;
00323
00324 for ( i = 0; i < 256; i++ )
00325 {
00326 *p_out++ = *p_first++;
00327 *p_out++ = *p_second++;
00328 }
00329 }
00330
00331
00332
00333
00334 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
00335 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
00336 {
00337 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
00338 #ifdef LIBA52_FIXED
00339 sample_t i_sample_level = (1 << 24);
00340 #else
00341 sample_t i_sample_level = 1;
00342 #endif
00343 int i_flags = p_sys->i_flags;
00344 int i_bytes_per_block = 256 * p_sys->i_nb_channels
00345 * sizeof(float);
00346 int i;
00347
00348
00349 a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
00350 &i_flags, &i_sample_level, 0 );
00351
00352 if ( (i_flags & A52_CHANNEL_MASK) != (p_sys->i_flags & A52_CHANNEL_MASK)
00353 && !p_sys->b_dontwarn )
00354 {
00355 msg_Warn( p_aout,
00356 "liba52 couldn't do the requested downmix 0x%x->0x%x",
00357 p_sys->i_flags & A52_CHANNEL_MASK,
00358 i_flags & A52_CHANNEL_MASK );
00359
00360 p_sys->b_dontwarn = 1;
00361 }
00362
00363 if( !p_sys->b_dynrng )
00364 {
00365 a52_dynrng( p_sys->p_liba52, NULL, NULL );
00366 }
00367
00368 for ( i = 0; i < 6; i++ )
00369 {
00370 sample_t * p_samples;
00371
00372 if( a52_block( p_sys->p_liba52 ) )
00373 {
00374 msg_Warn( p_aout, "a52_block failed for block %d", i );
00375 }
00376
00377 p_samples = a52_samples( p_sys->p_liba52 );
00378
00379 if ( ((p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL1
00380 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL2
00381 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_MONO)
00382 && (p_filter->output.i_physical_channels
00383 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
00384 {
00385 Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
00386 p_samples );
00387 }
00388 else if ( p_filter->output.i_original_channels
00389 & AOUT_CHAN_REVERSESTEREO )
00390 {
00391 Exchange( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
00392 p_samples );
00393 }
00394 else
00395 {
00396
00397 Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
00398 p_samples, p_sys->i_nb_channels, p_sys->pi_chan_table);
00399 }
00400 }
00401
00402 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
00403 p_out_buf->i_nb_bytes = i_bytes_per_block * 6;
00404 }
00405
00406
00407
00408
00409 static void Destroy( vlc_object_t *p_this )
00410 {
00411 aout_filter_t *p_filter = (aout_filter_t *)p_this;
00412 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
00413
00414 a52_free( p_sys->p_liba52 );
00415 free( p_sys );
00416 }
00417
00418
00419
00420
00421 static int OpenFilter( vlc_object_t *p_this )
00422 {
00423 filter_t *p_filter = (filter_t *)p_this;
00424 filter_sys_t *p_sys;
00425 int i_ret;
00426
00427 if( p_filter->fmt_in.i_codec != VLC_FOURCC('a','5','2',' ') )
00428 {
00429 return VLC_EGENERIC;
00430 }
00431
00432 p_filter->fmt_out.audio.i_format =
00433 #ifdef LIBA52_FIXED
00434 p_filter->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
00435 #else
00436 p_filter->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
00437 #endif
00438
00439
00440 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
00441 if( p_sys == NULL )
00442 {
00443 msg_Err( p_filter, "out of memory" );
00444 return VLC_EGENERIC;
00445 }
00446
00447
00448 p_filter->p_sys = p_sys = malloc( sizeof(filter_sys_t) );
00449 if( p_sys == NULL )
00450 {
00451 msg_Err( p_filter, "out of memory" );
00452 return VLC_EGENERIC;
00453 }
00454
00455 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
00456 p_filter->fmt_in.audio, p_filter->fmt_out.audio );
00457
00458 p_filter->pf_audio_filter = Convert;
00459 p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
00460
00461 return i_ret;
00462 }
00463
00464
00465
00466
00467 static void CloseFilter( vlc_object_t *p_this )
00468 {
00469 filter_t *p_filter = (filter_t *)p_this;
00470 filter_sys_t *p_sys = p_filter->p_sys;
00471
00472 a52_free( p_sys->p_liba52 );
00473 free( p_sys );
00474 }
00475
00476 static block_t *Convert( filter_t *p_filter, block_t *p_block )
00477 {
00478 aout_filter_t aout_filter;
00479 aout_buffer_t in_buf, out_buf;
00480 block_t *p_out;
00481 int i_out_size;
00482
00483 if( !p_block || !p_block->i_samples )
00484 {
00485 if( p_block ) p_block->pf_release( p_block );
00486 return NULL;
00487 }
00488
00489 i_out_size = p_block->i_samples *
00490 p_filter->fmt_out.audio.i_bitspersample *
00491 p_filter->fmt_out.audio.i_channels / 8;
00492
00493 p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
00494 if( !p_out )
00495 {
00496 msg_Warn( p_filter, "can't get output buffer" );
00497 p_block->pf_release( p_block );
00498 return NULL;
00499 }
00500
00501 p_out->i_samples = p_block->i_samples;
00502 p_out->i_dts = p_block->i_dts;
00503 p_out->i_pts = p_block->i_pts;
00504 p_out->i_length = p_block->i_length;
00505
00506 aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
00507 aout_filter.input = p_filter->fmt_in.audio;
00508 aout_filter.input.i_format = p_filter->fmt_in.i_codec;
00509 aout_filter.output = p_filter->fmt_out.audio;
00510 aout_filter.output.i_format = p_filter->fmt_out.i_codec;
00511
00512 in_buf.p_buffer = p_block->p_buffer;
00513 in_buf.i_nb_bytes = p_block->i_buffer;
00514 in_buf.i_nb_samples = p_block->i_samples;
00515 out_buf.p_buffer = p_out->p_buffer;
00516 out_buf.i_nb_bytes = p_out->i_buffer;
00517 out_buf.i_nb_samples = p_out->i_samples;
00518
00519 DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
00520
00521 p_out->i_buffer = out_buf.i_nb_bytes;
00522 p_out->i_samples = out_buf.i_nb_samples;
00523
00524 p_block->pf_release( p_block );
00525
00526 return p_out;
00527 }