1 /***************************************************************************
4 Functions that are called by libmad to communicate with vlc decoder
8 copyright : (C) 2001 by Jean-Paul Saman
10 ***************************************************************************/
12 /***************************************************************************
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 ***************************************************************************/
21 /*****************************************************************************
23 *****************************************************************************/
24 #include <stdlib.h> /* malloc(), free() */
25 #include <string.h> /* strdup() */
29 #include <vlc/decoder.h>
31 /*****************************************************************************
32 * Libmad includes files
33 *****************************************************************************/
38 static void PrintFrameInfo(struct mad_header *Header);
40 /*****************************************************************************
41 * libmad_input: this function is called by libmad when the input buffer needs
43 *****************************************************************************/
44 enum mad_flow libmad_input( void *p_data, struct mad_stream *p_stream )
46 mad_adec_thread_t * p_dec = (mad_adec_thread_t *) p_data;
47 size_t i_wanted, i_left;
49 if ( p_dec->p_fifo->b_die )
51 msg_Dbg( p_dec->p_fifo, "stopping libmad decoder" );
55 if ( p_dec->p_fifo->b_error )
57 msg_Warn( p_dec->p_fifo, "ignoring current audio frame" );
58 return MAD_FLOW_IGNORE;
61 /* libmad_stream_buffer does not consume the total buffer, it consumes
62 * only data for one frame only. So all data left in the buffer should
63 * be put back in front. */
64 if ( !p_stream->buffer || p_stream->error == MAD_ERROR_BUFLEN )
66 /* libmad does not consume all the buffer it's given. Some data,
67 * part of a truncated frame, is left unused at the end of the
68 * buffer. Those datas must be put back at the beginning of the
69 * buffer and taken in account for refilling the buffer. This
70 * means that the input buffer must be large enough to hold a
71 * complete frame at the highest observable bit-rate (currently
72 * 448 kb/s). XXX=XXX Is 2016 bytes the size of the largest frame?
73 * (448000*(1152/32000))/8 */
74 if( p_stream->next_frame )
76 i_left = p_stream->bufend - p_stream->next_frame;
77 if( p_dec->buffer != p_stream->next_frame )
79 memcpy( p_dec->buffer, p_stream->next_frame, i_left );
81 i_wanted = MAD_BUFFER_MDLEN - i_left;
83 /* Store timestamp for next frame */
84 p_dec->i_next_pts = p_dec->p_fifo->p_first->i_pts;
88 i_wanted = MAD_BUFFER_MDLEN;
91 /* Store timestamp for this frame */
92 p_dec->i_current_pts = p_dec->p_fifo->p_first->i_pts;
94 p_dec->p_fifo->p_first->i_pts = 0;
96 /* Fill-in the buffer. If an error occurs print a message and leave
97 * the decoding loop. If the end of stream is reached we also leave
98 * the loop but the return status is left untouched. */
99 if( i_wanted > p_dec->p_data->p_payload_end
100 - p_dec->p_data->p_payload_start )
102 i_wanted = p_dec->p_data->p_payload_end
103 - p_dec->p_data->p_payload_start;
104 memcpy( p_dec->buffer + i_left,
105 p_dec->p_data->p_payload_start, i_wanted );
106 NextDataPacket( p_dec->p_fifo, &p_dec->p_data );
110 memcpy( p_dec->buffer + i_left,
111 p_dec->p_data->p_payload_start, i_wanted );
112 p_dec->p_data->p_payload_start += i_wanted;
115 if ( p_dec->p_fifo->b_die )
117 msg_Dbg( p_dec->p_fifo, "stopping libmad decoder" );
118 return MAD_FLOW_STOP;
121 if ( p_dec->p_fifo->b_error )
123 msg_Warn( p_dec->p_fifo, "ignoring current audio frame" );
124 return MAD_FLOW_IGNORE;
127 /* Pipe the new buffer content to libmad's stream decoder facility.
128 * Libmad never copies the buffer, but just references it. So keep
129 * it in mad_adec_thread_t structure. */
130 mad_stream_buffer( p_stream, (unsigned char*) &p_dec->buffer,
135 return MAD_FLOW_CONTINUE;
138 /*****************************************************************************
139 * libmad_output: this function is called just after the frame is decoded
140 *****************************************************************************/
141 enum mad_flow libmad_output( void *p_data, struct mad_header const *p_header,
142 struct mad_pcm *p_pcm )
144 mad_adec_thread_t * p_dec = (mad_adec_thread_t *) p_data;
145 aout_buffer_t * p_buffer;
146 mad_fixed_t const * p_left = p_pcm->samples[0];
147 mad_fixed_t const * p_right = p_pcm->samples[1];
148 int i_samples = p_pcm->length;
149 mad_fixed_t * p_samples;
151 /* Creating the audio output fifo. Assume the samplerate and nr of channels
152 * from the first decoded frame is right for the entire audio track. */
153 if( (p_dec->p_aout_input != NULL) &&
154 (p_dec->output_format.i_rate != p_pcm->samplerate
155 || p_dec->output_format.i_channels != p_pcm->channels) )
157 /* Parameters changed - this should not happen. */
158 aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
159 p_dec->p_aout_input = NULL;
162 /* Creating the audio input if not created yet. */
163 if( p_dec->p_aout_input == NULL )
165 p_dec->output_format.i_rate = p_pcm->samplerate;
166 p_dec->output_format.i_channels = p_pcm->channels;
167 aout_DateInit( &p_dec->end_date, p_pcm->samplerate );
168 p_dec->p_aout_input = aout_InputNew( p_dec->p_fifo,
170 &p_dec->output_format );
172 if ( p_dec->p_aout_input == NULL )
174 p_dec->p_fifo->b_error = VLC_TRUE;
175 return MAD_FLOW_BREAK;
179 if( p_dec->i_current_pts )
181 /* Set the Presentation Time Stamp */
182 if( p_dec->i_current_pts != aout_DateGet( &p_dec->end_date ) )
184 aout_DateSet( &p_dec->end_date, p_dec->i_current_pts );
187 p_dec->i_current_pts = 0;
189 else if( p_dec->i_next_pts )
191 /* No PTS this time, but it'll be for next frame */
192 p_dec->i_current_pts = p_dec->i_next_pts;
193 p_dec->i_next_pts = 0;
196 if( !aout_DateGet( &p_dec->end_date ) )
198 /* No date available yet, wait for the first PTS. */
199 return MAD_FLOW_CONTINUE;
202 p_buffer = aout_BufferNew( p_dec->p_aout, p_dec->p_aout_input, i_samples );
204 if ( p_buffer == NULL )
206 msg_Err( p_dec->p_fifo, "allocating new buffer failed" );
207 return MAD_FLOW_BREAK;
210 p_buffer->start_date = aout_DateGet( &p_dec->end_date );
211 p_buffer->end_date = aout_DateIncrement( &p_dec->end_date, i_samples );
213 /* Interleave and keep buffers in mad_fixed_t format */
214 p_samples = (mad_fixed_t *)p_buffer->p_buffer;
216 switch( p_pcm->channels )
221 *p_samples++ = *p_left++;
222 *p_samples++ = *p_right++;
226 p_dec->p_fifo->p_vlc->pf_memcpy( p_samples, p_left,
227 i_samples * sizeof(mad_fixed_t) );
229 msg_Err( p_dec->p_fifo, "cannot interleave %i channels",
233 aout_BufferPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
235 return MAD_FLOW_CONTINUE;
238 /*****************************************************************************
239 * libmad_error: this function is called when an error occurs during decoding
240 *****************************************************************************/
241 enum mad_flow libmad_error( void *data, struct mad_stream *p_libmad_stream,
242 struct mad_frame *p_libmad_frame )
244 mad_adec_thread_t *p_dec = (mad_adec_thread_t *) data;
245 enum mad_flow result = MAD_FLOW_CONTINUE;
247 switch (p_libmad_stream->error)
249 case MAD_ERROR_BUFLEN: /* input buffer too small (or EOF) */
250 msg_Err( p_dec->p_fifo, "input buffer too small (or EOF)" );
251 result = MAD_FLOW_CONTINUE;
253 case MAD_ERROR_BUFPTR: /* invalid (null) buffer pointer */
254 msg_Err( p_dec->p_fifo, "invalid (null) buffer pointer" );
255 result = MAD_FLOW_STOP;
257 case MAD_ERROR_NOMEM: /* not enough memory */
258 msg_Err( p_dec->p_fifo, "invalid (null) buffer pointer" );
259 result = MAD_FLOW_STOP;
261 case MAD_ERROR_LOSTSYNC: /* lost synchronization */
262 msg_Err( p_dec->p_fifo, "lost synchronization" );
263 mad_stream_sync(p_libmad_stream);
264 result = MAD_FLOW_CONTINUE;
266 case MAD_ERROR_BADLAYER: /* reserved header layer value */
267 msg_Err( p_dec->p_fifo, "reserved header layer value" );
268 result = MAD_FLOW_CONTINUE;
270 case MAD_ERROR_BADBITRATE: /* forbidden bitrate value */
271 msg_Err( p_dec->p_fifo, "forbidden bitrate value" );
272 result = MAD_FLOW_CONTINUE;
274 case MAD_ERROR_BADSAMPLERATE: /* reserved sample frequency value */
275 msg_Err( p_dec->p_fifo, "reserved sample frequency value" );
276 result = MAD_FLOW_CONTINUE;
278 case MAD_ERROR_BADEMPHASIS: /* reserved emphasis value */
279 msg_Err( p_dec->p_fifo, "reserverd emphasis value" );
280 result = MAD_FLOW_CONTINUE;
282 case MAD_ERROR_BADCRC: /* CRC check failed */
283 msg_Err( p_dec->p_fifo, "CRC check failed" );
284 result = MAD_FLOW_CONTINUE;
286 case MAD_ERROR_BADBITALLOC: /* forbidden bit allocation value */
287 msg_Err( p_dec->p_fifo, "forbidden bit allocation value" );
288 result = MAD_FLOW_IGNORE;
290 case MAD_ERROR_BADSCALEFACTOR:/* bad scalefactor index */
291 msg_Err( p_dec->p_fifo, "bad scalefactor index" );
292 result = MAD_FLOW_CONTINUE;
294 case MAD_ERROR_BADFRAMELEN: /* bad frame length */
295 msg_Err( p_dec->p_fifo, "bad frame length" );
296 result = MAD_FLOW_CONTINUE;
298 case MAD_ERROR_BADBIGVALUES: /* bad big_values count */
299 msg_Err( p_dec->p_fifo, "bad big values count" );
300 result = MAD_FLOW_IGNORE;
302 case MAD_ERROR_BADBLOCKTYPE: /* reserved block_type */
303 msg_Err( p_dec->p_fifo, "reserverd block_type" );
304 result = MAD_FLOW_IGNORE;
306 case MAD_ERROR_BADSCFSI: /* bad scalefactor selection info */
307 msg_Err( p_dec->p_fifo, "bad scalefactor selection info" );
308 result = MAD_FLOW_CONTINUE;
310 case MAD_ERROR_BADDATAPTR: /* bad main_data_begin pointer */
311 msg_Err( p_dec->p_fifo, "bad main_data_begin pointer" );
312 result = MAD_FLOW_STOP;
314 case MAD_ERROR_BADPART3LEN: /* bad audio data length */
315 msg_Err( p_dec->p_fifo, "bad audio data length" );
316 result = MAD_FLOW_IGNORE;
318 case MAD_ERROR_BADHUFFTABLE: /* bad Huffman table select */
319 msg_Err( p_dec->p_fifo, "bad Huffman table select" );
320 result = MAD_FLOW_IGNORE;
322 case MAD_ERROR_BADHUFFDATA: /* Huffman data overrun */
323 msg_Err( p_dec->p_fifo, "Huffman data overrun" );
324 result = MAD_FLOW_IGNORE;
326 case MAD_ERROR_BADSTEREO: /* incompatible block_type for JS */
327 msg_Err( p_dec->p_fifo, "incompatible block_type for JS" );
328 result = MAD_FLOW_IGNORE;
331 msg_Err( p_dec->p_fifo, "unknown error occured stopping decoder" );
332 result = MAD_FLOW_STOP;
336 return (MAD_RECOVERABLE(p_libmad_stream->error)? result: MAD_FLOW_STOP);
337 //return (MAD_FLOW_CONTINUE);
340 /*****************************************************************************
341 * libmad_message: this function is called to send a message
342 *****************************************************************************/
343 /* enum mad_flow libmad_message(void *, void*, unsigned int*)
345 * return MAD_FLOW_CONTINUE;
351 /****************************************************************************
352 * Print human readable informations about an audio MPEG frame. *
353 ****************************************************************************/
354 static void PrintFrameInfo(struct mad_header *Header)
360 /* Convert the layer number to its printed representation. */
361 switch(Header->layer)
373 Layer="(unexpected layer value)";
377 /* Convert the audio mode to its printed representation. */
380 case MAD_MODE_SINGLE_CHANNEL:
381 Mode="single channel";
383 case MAD_MODE_DUAL_CHANNEL:
386 case MAD_MODE_JOINT_STEREO:
387 Mode="joint (MS/intensity) stereo";
389 case MAD_MODE_STEREO:
390 Mode="normal LR stereo";
393 Mode="(unexpected mode value)";
397 /* Convert the emphasis to its printed representation. */
398 switch(Header->emphasis)
400 case MAD_EMPHASIS_NONE:
403 case MAD_EMPHASIS_50_15_US:
406 case MAD_EMPHASIS_CCITT_J_17:
407 Emphasis="CCITT J.17";
410 Emphasis="(unexpected emphasis value)";
414 //X msg_Err("statistics: %lu kb/s audio mpeg layer %s stream %s crc, "
415 //X "%s with %s emphasis at %d Hz sample rate\n",
416 //X Header->bitrate,Layer,
417 //X Header->flags&MAD_FLAG_PROTECTION?"with":"without",
418 //X Mode,Emphasis,Header->samplerate);