qt: playlist: use item title if available
[vlc.git] / modules / codec / a52.c
blobfb6a1ea2a8421bad9beeab46dc150a8ec0f2c618
1 /*****************************************************************************
2 * a52.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-2009 VLC authors and VideoLAN
8 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * Christophe Massiot <massiot@via.ecp.fr>
10 * Thomas Guillem <thomas@gllm.fr>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * NOTA BENE: this module requires the linking against a library which is
29 * known to require licensing under the GNU General Public License version 2
30 * (or later). Therefore, the result of compiling this module will normally
31 * be subject to the terms of that later license.
32 *****************************************************************************/
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 #include <vlc_cpu.h>
42 #include <stdint.h> /* int16_t .. */
44 #ifdef USE_A52DEC_TREE /* liba52 header file */
45 # include "include/a52.h"
46 #else
47 # include "a52dec/a52.h"
48 #endif
50 #include <vlc_aout.h>
51 #include <vlc_block.h>
52 #include <vlc_codec.h>
54 static int Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
57 typedef struct
59 a52_state_t *p_liba52; /* liba52 internal structure */
60 bool b_dynrng; /* see below */
61 int i_flags; /* liba52 flags, see a52dec/doc/liba52.txt */
62 bool b_dontwarn;
63 int i_nb_channels; /* number of float32 per sample */
65 uint8_t pi_chan_table[AOUT_CHAN_MAX]; /* channel reordering */
66 bool b_synced;
67 } decoder_sys_t;
69 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
70 #define DYNRNG_LONGTEXT N_( \
71 "Dynamic range compression makes the loud sounds softer, and the soft " \
72 "sounds louder, so you can more easily listen to the stream in a noisy " \
73 "environment without disturbing anyone. If you disable the dynamic range "\
74 "compression the playback will be more adapted to a movie theater or a " \
75 "listening room.")
77 vlc_module_begin ()
78 set_shortname( "A/52" )
79 set_description( N_("ATSC A/52 (AC-3) audio decoder") )
80 set_category( CAT_INPUT )
81 set_subcategory( SUBCAT_INPUT_ACODEC )
82 add_bool( "a52-dynrng", true, DYNRNG_TEXT, DYNRNG_LONGTEXT, false )
83 set_capability( "audio decoder", 60 )
84 set_callbacks( Open, Close )
85 vlc_module_end ()
88 * helper function to interleave channels
90 static void Interleave( sample_t *restrict p_out, const sample_t *restrict p_in,
91 unsigned i_nb_channels, uint8_t *restrict pi_chan_table )
93 /* We do not only have to interleave, but also reorder the channels */
94 for( unsigned j = 0; j < i_nb_channels; j++ )
96 for( unsigned i = 0; i < 256; i++ )
98 #ifdef LIBA52_FIXED
99 union { uint32_t u; int32_t i; } spl;
101 spl.u = ((uint32_t)p_in[j * 256 + i]) << 4;
102 p_out[i * i_nb_channels + pi_chan_table[j]] = spl.i;
103 #else
104 p_out[i * i_nb_channels + pi_chan_table[j]] = p_in[j * 256 + i];
105 #endif
111 * helper function to duplicate a unique channel
113 static void Duplicate( sample_t *restrict p_out, const sample_t *restrict p_in )
115 for( unsigned i = 256; i--; )
117 #ifdef LIBA52_FIXED
118 union { uint32_t u; int32_t i; } spl;
120 spl.u = ((uint32_t)*(p_in++)) << 4;
121 *p_out++ = spl.i;
122 *p_out++ = spl.i;
123 #else
124 sample_t s = *(p_in++);
126 *p_out++ = s;
127 *p_out++ = s;
128 #endif
132 static int Decode( decoder_t *p_dec, block_t *p_in_buf )
134 decoder_sys_t *p_sys = p_dec->p_sys;
136 if (p_in_buf == NULL) /* No drain */
137 return VLCDEC_SUCCESS;
139 #ifdef LIBA52_FIXED
140 sample_t i_sample_level = (1 << 24);
141 #else
142 sample_t i_sample_level = 1;
143 #endif
144 int i_flags = p_sys->i_flags;
145 size_t i_bytes_per_block = 256 * p_sys->i_nb_channels * sizeof(sample_t);
148 /* Every A/52 frame is composed of 6 blocks, each with an output of 256
149 * samples for each channel. */
150 block_t *p_out_buf = block_Alloc( 6 * i_bytes_per_block );
151 if( unlikely(p_out_buf == NULL) )
153 block_Release( p_in_buf );
154 return VLCDEC_SUCCESS;
157 /* Do the actual decoding now. */
158 a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
159 &i_flags, &i_sample_level, 0 );
161 if ( (i_flags & A52_CHANNEL_MASK) != (p_sys->i_flags & A52_CHANNEL_MASK)
162 && !p_sys->b_dontwarn )
164 msg_Warn( p_dec,
165 "liba52 couldn't do the requested downmix 0x%x->0x%x",
166 p_sys->i_flags & A52_CHANNEL_MASK,
167 i_flags & A52_CHANNEL_MASK );
169 p_sys->b_dontwarn = 1;
172 if( !p_sys->b_dynrng )
174 a52_dynrng( p_sys->p_liba52, NULL, NULL );
177 for( unsigned i = 0; i < 6; i++ )
179 if( a52_block( p_sys->p_liba52 ) )
180 msg_Warn( p_dec, "a52_block failed for block %d", i );
182 sample_t *p_samples = a52_samples( p_sys->p_liba52 );
184 if ( ((p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL1
185 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL2
186 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_MONO)
187 && (p_dec->fmt_out.audio.i_physical_channels
188 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
190 Duplicate( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
191 p_samples );
193 else
195 /* Interleave the *$£%ù samples. */
196 Interleave( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
197 p_samples, p_sys->i_nb_channels, p_sys->pi_chan_table);
201 p_out_buf->i_nb_samples = A52_FRAME_NB; /* 6 * 256 */
202 p_out_buf->i_dts = p_in_buf->i_dts;
203 p_out_buf->i_pts = p_in_buf->i_pts;
204 p_out_buf->i_length = p_in_buf->i_length;
205 block_Release( p_in_buf );
206 decoder_QueueAudio( p_dec, p_out_buf );
207 return VLCDEC_SUCCESS;
210 static int channels_vlc2a52( const audio_format_t *p_audio, int *p_flags )
212 int i_flags = 0;
214 switch ( p_audio->i_physical_channels & ~AOUT_CHAN_LFE )
216 case AOUT_CHAN_CENTER:
217 if ( (p_audio->i_physical_channels & AOUT_CHAN_CENTER)
218 || (p_audio->i_physical_channels
219 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
220 i_flags = A52_MONO;
221 else if ( p_audio->i_physical_channels & AOUT_CHAN_LEFT )
222 i_flags = A52_CHANNEL1;
223 else
224 i_flags = A52_CHANNEL2;
225 break;
227 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
228 if ( p_audio->i_chan_mode & AOUT_CHANMODE_DOLBYSTEREO )
229 i_flags = A52_DOLBY;
230 else if ( p_audio->i_chan_mode & AOUT_CHANMODE_DUALMONO )
231 i_flags = A52_CHANNEL;
232 else if ( !(p_audio->i_physical_channels & AOUT_CHAN_RIGHT) )
233 i_flags = A52_CHANNEL1;
234 else if ( !(p_audio->i_physical_channels & AOUT_CHAN_LEFT) )
235 i_flags = A52_CHANNEL2;
236 else
237 i_flags = A52_STEREO;
238 break;
240 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
241 i_flags = A52_3F;
242 break;
244 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
245 i_flags = A52_2F1R;
246 break;
248 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
249 | AOUT_CHAN_REARCENTER:
250 i_flags = A52_3F1R;
251 break;
253 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
254 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
255 i_flags = A52_2F2R;
256 break;
258 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
259 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
260 i_flags = A52_3F2R;
261 break;
263 default:
264 return VLC_EGENERIC;
267 if ( p_audio->i_physical_channels & AOUT_CHAN_LFE )
268 i_flags |= A52_LFE;
269 *p_flags = i_flags;
270 return VLC_SUCCESS;
273 static int Open( vlc_object_t *p_this )
275 decoder_t *p_dec = (decoder_t *)p_this;
276 decoder_sys_t *p_sys;
278 if( p_dec->fmt_in.i_codec != VLC_CODEC_A52
279 || p_dec->fmt_in.audio.i_rate == 0
280 || p_dec->fmt_in.audio.i_physical_channels == 0
281 || p_dec->fmt_in.audio.i_bytes_per_frame == 0
282 || p_dec->fmt_in.audio.i_frame_length == 0 )
283 return VLC_EGENERIC;
285 /* Allocate the memory needed to store the module's structure */
286 p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) );
287 if( p_sys == NULL )
288 return VLC_ENOMEM;
290 p_sys->b_dynrng = var_InheritBool( p_this, "a52-dynrng" );
291 p_sys->b_dontwarn = 0;
293 p_sys->i_nb_channels = aout_FormatNbChannels( &p_dec->fmt_in.audio );
294 if( channels_vlc2a52( &p_dec->fmt_in.audio, &p_sys->i_flags )
295 != VLC_SUCCESS )
297 msg_Warn( p_this, "unknown sample format!" );
298 free( p_sys );
299 return VLC_EGENERIC;
301 p_sys->i_flags |= A52_ADJUST_LEVEL;
303 /* Initialize liba52 */
304 p_sys->p_liba52 = a52_init( 0 );
305 if( p_sys->p_liba52 == NULL )
307 msg_Err( p_this, "unable to initialize liba52" );
308 free( p_sys );
309 return VLC_EGENERIC;
312 static const uint32_t pi_channels_in[] = {
313 AOUT_CHAN_LFE, AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
314 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARCENTER, AOUT_CHAN_REARRIGHT, 0
317 aout_CheckChannelReorder( pi_channels_in, NULL,
318 p_dec->fmt_in.audio.i_physical_channels,
319 p_sys->pi_chan_table );
321 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
322 #ifdef LIBA52_FIXED
323 p_dec->fmt_out.audio.i_format = VLC_CODEC_S32N;
324 #else
325 p_dec->fmt_out.audio.i_format = VLC_CODEC_FL32;
326 #endif
327 p_dec->fmt_out.i_codec = p_dec->fmt_out.audio.i_format;
329 aout_FormatPrepare( &p_dec->fmt_out.audio );
331 if( decoder_UpdateAudioFormat( p_dec ) )
333 Close( p_this );
334 return VLC_EGENERIC;
337 p_dec->pf_decode = Decode;
338 p_dec->pf_flush = NULL;
339 return VLC_SUCCESS;
342 static void Close( vlc_object_t *p_this )
344 decoder_t *p_dec = (decoder_t *)p_this;
345 decoder_sys_t *p_sys = p_dec->p_sys;
347 a52_free( p_sys->p_liba52 );
348 free( p_sys );