sout: remove fmt storage from sout packetizers
[vlc.git] / modules / codec / mad.c
blob7fed81037073b12e44ac84cb9cd39c441a5daa15
1 /*****************************************************************************
2 * mad.c: MPEG-1 & 2 audio layer I, II, III + MPEG 2.5 decoder,
3 * using MAD (MPEG Audio Decoder)
4 *****************************************************************************
5 * Copyright (C) 2001-2016 VLC authors and VideoLAN
6 * $Id$
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * NOTA BENE: this module requires the linking against a library which is
28 * known to require licensing under the GNU General Public License version 2
29 * (or later). Therefore, the result of compiling this module will normally
30 * be subject to the terms of that later license.
31 *****************************************************************************/
34 /*****************************************************************************
35 * Preamble
36 *****************************************************************************/
38 #include <mad.h>
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43 #include <assert.h>
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47 #include <vlc_block.h>
48 #include <vlc_aout.h>
49 #include <vlc_codec.h>
51 #define MAD_BUFFER_GUARD 8
53 /*****************************************************************************
54 * Local prototypes
55 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
59 /*****************************************************************************
60 * Local structures
61 *****************************************************************************/
62 struct decoder_sys_t
64 struct mad_stream mad_stream;
65 struct mad_frame mad_frame;
66 struct mad_synth mad_synth;
68 int i_reject_count;
69 block_t *p_last_buf;
72 /*****************************************************************************
73 * Module descriptor
74 *****************************************************************************/
75 vlc_module_begin ()
76 set_category( CAT_INPUT )
77 set_subcategory( SUBCAT_INPUT_ACODEC )
78 set_description( N_("MPEG audio layer I/II/III decoder") )
79 set_capability( "audio decoder", 99 )
80 set_callbacks( Open, Close )
81 vlc_module_end ()
83 /*****************************************************************************
84 * DecodeBlock: decode an MPEG audio frame.
85 *****************************************************************************/
86 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
88 decoder_sys_t *p_sys = p_dec->p_sys;
89 block_t *p_out_buf = NULL, *p_last_buf = NULL;
91 if( !pp_block )
93 /* Drain */
94 p_last_buf = p_sys->p_last_buf;
95 p_sys->p_last_buf = NULL;
96 if( !p_last_buf )
97 return NULL;
99 else
101 if( !*pp_block )
102 return NULL;
103 block_t *p_in_buf = *pp_block;
104 *pp_block = NULL;
106 if( p_in_buf->i_buffer < MAD_BUFFER_GUARD )
108 block_Release( p_in_buf );
109 return NULL;
112 /* Buffers passed to the mad_stream_buffer() function need to ends with
113 * the header (MAD_BUFFER_GUARD) of the following block. Therefore,
114 * this DecodeBlock() function will always return the output buffer
115 * corresponding to the last input buffer. */
116 if( !p_sys->p_last_buf )
118 /* Wait for the next block */
119 p_sys->p_last_buf = p_in_buf;
120 return NULL;
122 p_last_buf = p_sys->p_last_buf;
123 p_sys->p_last_buf = p_in_buf;
125 /* Put the header of the current buffer at the end of the last one.
126 * Normally, this won't do a real realloc() since VLC blocks are
127 * allocated with pre and post padding */
128 p_last_buf = block_Realloc( p_last_buf, 0,
129 p_last_buf->i_buffer + MAD_BUFFER_GUARD );
130 if( !p_last_buf )
131 return NULL;
132 memcpy( &p_last_buf->p_buffer[p_last_buf->i_buffer - MAD_BUFFER_GUARD],
133 p_in_buf->p_buffer, MAD_BUFFER_GUARD);
136 mad_stream_buffer( &p_sys->mad_stream, p_last_buf->p_buffer,
137 p_last_buf->i_buffer );
138 /* Do the actual decoding now (ignore EOF error when draining). */
139 if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1
140 && ( pp_block != NULL || p_sys->mad_stream.error != MAD_ERROR_BUFLEN ) )
142 msg_Err( p_dec, "libmad error: %s",
143 mad_stream_errorstr( &p_sys->mad_stream ) );
144 if( !MAD_RECOVERABLE( p_sys->mad_stream.error ) )
145 p_sys->i_reject_count = 3;
147 else if( p_last_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
149 p_sys->i_reject_count = 3;
152 if( p_sys->i_reject_count > 0 )
153 goto reject;
155 mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
157 struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
158 unsigned int i_samples = p_pcm->length;
159 mad_fixed_t const * p_left = p_pcm->samples[0];
160 mad_fixed_t const * p_right = p_pcm->samples[1];
161 p_out_buf = decoder_NewAudioBuffer( p_dec, i_samples );
162 if( !p_out_buf )
163 goto end;
164 p_out_buf->i_dts = p_last_buf->i_dts;
165 p_out_buf->i_pts = p_last_buf->i_pts;
166 p_out_buf->i_length = p_last_buf->i_length;
167 float *p_samples = (float *)p_out_buf->p_buffer;
169 if (p_pcm->channels > p_dec->fmt_out.audio.i_channels)
171 msg_Err( p_dec, "wrong channels count (corrupt stream?): %u > %u",
172 p_pcm->channels, p_dec->fmt_out.audio.i_channels);
173 p_sys->i_reject_count = 3;
174 goto reject;
177 if( i_samples != p_out_buf->i_nb_samples )
179 msg_Err( p_dec, "unexpected samples count (corrupt stream?): "
180 "%u / %u", i_samples, p_out_buf->i_nb_samples );
181 p_sys->i_reject_count = 3;
182 goto reject;
185 /* Interleave and keep buffers in mad_fixed_t format */
186 if ( p_pcm->channels == 2 )
188 while ( i_samples-- )
190 //assert( *p_left < MAD_F_ONE );
191 //assert( *p_left >= -MAD_F_ONE );
192 //assert( *p_right < MAD_F_ONE );
193 //assert( *p_right >= -MAD_F_ONE );
194 *p_samples++ = (float)*p_left++ / (float)MAD_F_ONE;
195 *p_samples++ = (float)*p_right++ / (float)MAD_F_ONE;
198 else
200 assert( p_pcm->channels == 1 );
201 while ( i_samples-- )
203 //assert( *p_left < MAD_F_ONE );
204 //assert( *p_left >= -MAD_F_ONE );
205 *p_samples++ = (float)*p_left++ / (float)MAD_F_ONE;
209 end:
210 block_Release( p_last_buf );
211 return p_out_buf;
212 reject:
213 p_sys->i_reject_count--;
214 if( p_out_buf )
216 block_Release( p_out_buf );
217 p_out_buf = NULL;
219 goto end;
222 static int DecodeAudio( decoder_t *p_dec, block_t *p_block )
224 block_t **pp_block = p_block ? &p_block : NULL, *p_out;
225 while( ( p_out = DecodeBlock( p_dec, pp_block ) ) != NULL )
226 decoder_QueueAudio( p_dec, p_out );
227 return VLCDEC_SUCCESS;
230 static void DecodeFlush( decoder_t *p_dec )
232 decoder_sys_t *p_sys = p_dec->p_sys;
234 if( p_sys->p_last_buf )
235 block_Release( p_sys->p_last_buf );
236 p_sys->p_last_buf = NULL;
239 /*****************************************************************************
240 * Open:
241 *****************************************************************************/
242 static int Open( vlc_object_t *p_this )
244 decoder_t *p_dec = (decoder_t *)p_this;
245 decoder_sys_t *p_sys;
247 if( ( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA
248 && p_dec->fmt_in.i_codec != VLC_CODEC_MP3
249 && p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','3') )
250 || p_dec->fmt_in.audio.i_rate == 0
251 || p_dec->fmt_in.audio.i_physical_channels == 0
252 || p_dec->fmt_in.audio.i_original_channels == 0
253 || p_dec->fmt_in.audio.i_bytes_per_frame == 0
254 || p_dec->fmt_in.audio.i_frame_length == 0 )
255 return VLC_EGENERIC;
257 /* Allocate the memory needed to store the module's structure */
258 p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
259 if( p_sys == NULL )
260 return VLC_ENOMEM;
261 p_sys->i_reject_count = 0;
262 p_sys->p_last_buf = NULL;
264 /* Initialize libmad */
265 mad_stream_init( &p_sys->mad_stream );
266 mad_frame_init( &p_sys->mad_frame );
267 mad_synth_init( &p_sys->mad_synth );
268 mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
270 p_dec->fmt_out.i_cat = AUDIO_ES;
271 p_dec->fmt_out.audio = p_dec->fmt_in.audio;
272 p_dec->fmt_out.audio.i_format = VLC_CODEC_FL32;
273 p_dec->fmt_out.i_codec = p_dec->fmt_out.audio.i_format;
275 aout_FormatPrepare( &p_dec->fmt_out.audio );
277 if( decoder_UpdateAudioFormat( p_dec ) )
279 es_format_Init( &p_dec->fmt_out, UNKNOWN_ES, 0 );
280 Close( p_this );
281 return VLC_EGENERIC;
284 p_dec->pf_decode = DecodeAudio;
285 p_dec->pf_flush = DecodeFlush;
287 return VLC_SUCCESS;
290 /*****************************************************************************
291 * Close : deallocate data structures
292 *****************************************************************************/
293 static void Close( vlc_object_t *p_this )
295 decoder_t *p_dec = (decoder_t *)p_this;
296 decoder_sys_t *p_sys = p_dec->p_sys;
298 mad_synth_finish( &p_sys->mad_synth );
299 mad_frame_finish( &p_sys->mad_frame );
300 mad_stream_finish( &p_sys->mad_stream );
301 if( p_sys->p_last_buf )
302 block_Release( p_sys->p_last_buf );
303 free( p_sys );