1 /***************************************************************************
2 mad_libmad.c - description
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() */
27 #include <videolan/vlc.h>
29 #include "audio_output.h"
31 #include "stream_control.h"
32 #include "input_ext-dec.h"
36 /*****************************************************************************
37 * Libmad includes files
38 *****************************************************************************/
41 #include "mad_libmad.h"
43 /*****************************************************************************
44 * libmad_input: this function is called by libmad when the input buffer needs
46 *****************************************************************************/
47 enum mad_flow libmad_input(void *data, struct mad_stream *p_libmad_stream)
49 mad_adec_thread_t *p_mad_adec = (mad_adec_thread_t *) data;
50 size_t ReadSize, Remaining;
51 unsigned char *ReadStart;
53 /* Store time stamp of current frame */
54 if ( p_mad_adec->p_fifo->p_first->i_pts ) {
55 p_mad_adec->i_pts_save = p_mad_adec->p_fifo->p_first->i_pts;
56 p_mad_adec->p_fifo->p_first->i_pts = 0;
59 p_mad_adec->i_pts_save = LAST_MDATE;
62 /* libmad_stream_buffer does not consume the total buffer, it consumes only data
63 * for one frame only. So all data left in the buffer should be put back in front.
65 if ((p_libmad_stream->buffer==NULL) || (p_libmad_stream->error==MAD_ERROR_BUFLEN))
68 /* libmad does not consume all the buffer it's given. Some
69 * datas, part of a truncated frame, is left unused at the
70 * end of the buffer. Those datas must be put back at the
71 * beginning of the buffer and taken in account for
72 * refilling the buffer. This means that the input buffer
73 * must be large enough to hold a complete frame at the
74 * highest observable bit-rate (currently 448 kb/s). XXX=XXX
75 * Is 2016 bytes the size of the largest frame?
76 * (448000*(1152/32000))/8
78 if(p_libmad_stream->next_frame!=NULL)
80 Remaining=p_libmad_stream->bufend-p_libmad_stream->next_frame;
81 memmove(p_mad_adec->buffer,p_libmad_stream->next_frame,Remaining);
82 ReadStart=p_mad_adec->buffer+Remaining;
83 ReadSize=(MAD_BUFFER_SIZE)-Remaining;
87 ReadSize=(MAD_BUFFER_SIZE);
88 ReadStart=p_mad_adec->buffer;
91 //intf_ErrMsg( "mad_adec debug: buffer size remaining [%d] and readsize [%d] total [%d]",
92 // Remaining, ReadSize, ReadSize+Remaining);
94 /* Fill-in the buffer. If an error occurs print a message
95 * and leave the decoding loop. If the end of stream is
96 * reached we also leave the loop but the return status is
99 GetChunk( &p_mad_adec->bit_stream, ReadStart, ReadSize );
101 if ( p_mad_adec->p_fifo->b_die == 1 ) {
102 intf_ErrMsg( "mad_adec error: libmad_input stopping libmad decoder" );
103 return MAD_FLOW_STOP;
106 if ( p_mad_adec->p_fifo->b_error == 1 ) {
107 intf_ErrMsg( "mad_adec error: libmad_input ignoring current audio frame" );
108 return MAD_FLOW_IGNORE;
111 /* Pipe the new buffer content to libmad's stream decoder facility.
112 * Libmad never copies the buffer, but just references it. So keep it in
113 * mad_adec_thread_t structure.
115 mad_stream_buffer(p_libmad_stream,(unsigned char*) &p_mad_adec->buffer,ReadSize+Remaining);
116 p_libmad_stream->error=0;
119 return MAD_FLOW_CONTINUE;
122 /*****************************************************************************
123 * libmad_header: this function is called just after the header of a frame is
125 *****************************************************************************/
127 *enum mad_flow libmad_header(void *data, struct mad_header const *p_libmad_header)
129 * mad_adec_thread_t *p_mad_adec = (mad_adec_thread_t *) data;
131 * intf_ErrMsg( "mad_adec: libmad_header samplerate %d", p_libmad_header->samplerate);
132 * intf_DbgMsg( "mad_adec: libmad_header bitrate %d", p_libmad_header->bitrate);
134 * p_mad_adec->p_aout_fifo->l_rate = p_libmad_header->samplerate;
135 * mad_timer_add(&p_mad_adec->libmad_timer,p_libmad_header->duration);
137 * return MAD_FLOW_CONTINUE;
141 /*****************************************************************************
142 * lib_mad_filter: this function is called to filter data of a frame
143 *****************************************************************************/
144 /* enum mad_flow libmad_filter(void *data, struct mad_stream const *p_libmad_stream, struct mad_frame *p_libmad_frame)
146 * return MAD_FLOW_CONTINUE;
150 //#define MPG321_ROUTINES 1
151 #ifdef MPG321_ROUTINES
152 /*****************************************************************************
153 * support routines borrowed from mpg321 (file: mad.c), which is distributed
156 * mpg321 was written by Joe Drew <drew@debian.org>, and based upon 'plaympeg'
157 * from the smpeg sources, which was written by various people from Loki Software
158 * (http://www.lokigames.com).
160 * It also incorporates some source from mad, written by Robert Leslie
161 *****************************************************************************/
163 /* The following two routines and data structure are from the ever-brilliant
167 struct audio_dither {
168 mad_fixed_t error[3];
174 * DESCRIPTION: 32-bit pseudo-random number generator
176 static __inline__ unsigned long prng(unsigned long state)
178 return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
182 * NAME: audio_linear_dither()
183 * DESCRIPTION: generic linear sample quantize and dither routine
185 static __inline__ signed int audio_linear_dither(unsigned int bits, mad_fixed_t sample,
186 struct audio_dither *dither)
188 unsigned int scalebits;
189 mad_fixed_t output, mask, random;
197 sample += dither->error[0] - dither->error[1] + dither->error[2];
199 dither->error[2] = dither->error[1];
200 dither->error[1] = dither->error[0] / 2;
203 output = sample + (1L << (MAD_F_FRACBITS + 1 - bits - 1));
205 scalebits = MAD_F_FRACBITS + 1 - bits;
206 mask = (1L << scalebits) - 1;
209 random = prng(dither->random);
210 output += (random & mask) - (dither->random & mask);
212 dither->random = random;
221 else if (output < MIN) {
232 dither->error[0] = sample - output;
235 return output >> scalebits;
239 /*****************************************************************************
240 * s24_to_s16_pcm: Scale a 24 bit pcm sample to a 16 bit pcm sample.
241 *****************************************************************************/
242 static __inline__ mad_fixed_t s24_to_s16_pcm(mad_fixed_t sample)
245 sample += (1L << (MAD_F_FRACBITS - 16));
248 if (sample >= MAD_F_ONE)
249 sample = MAD_F_ONE - 1;
250 else if (sample < -MAD_F_ONE)
254 return sample >> (MAD_F_FRACBITS + 1 - 16);
257 /*****************************************************************************
258 * libmad_ouput: this function is called just after the frame is decoded
259 *****************************************************************************/
260 enum mad_flow libmad_output(void *data, struct mad_header const *p_libmad_header, struct mad_pcm *p_libmad_pcm)
262 mad_adec_thread_t *p_mad_adec= (mad_adec_thread_t *) data;
265 mad_fixed_t const *left_ch = p_libmad_pcm->samples[0], *right_ch = p_libmad_pcm->samples[1];
266 register int nsamples = p_libmad_pcm->length;
268 #ifdef MPG321_ROUTINES
269 static struct audio_dither dither;
272 /* Creating the audio output fifo */
273 if (p_mad_adec->p_aout_fifo==NULL) {
274 p_mad_adec->p_aout_fifo = aout_CreateFifo(
275 AOUT_ADEC_STEREO_FIFO, /* fifo type */
276 p_libmad_pcm->channels, /* nr. of channels */
277 p_libmad_pcm->samplerate, /* frame rate in Hz ?*/
279 ADEC_FRAME_SIZE, /* frame size */
282 if ( p_mad_adec->p_aout_fifo == NULL )
287 intf_ErrMsg("mad_adec debug: in libmad_output aout fifo created");
290 p_mad_adec->p_aout_fifo->l_rate = p_libmad_pcm->samplerate;
293 /* Some frames are nog quite right. Why ??? I do not know. Probably syncing and CRC errors ??
294 * Leaving those frames out futher removes the jitter in the sound and makes it more fluent.
295 * Still I am missing something, because it is not completely fluent.
297 if ((p_mad_adec->libmad_decoder->sync->stream.error==MAD_ERROR_BADCRC) ||
298 (p_mad_adec->libmad_decoder->sync->stream.error==MAD_ERROR_BADBITRATE) ||
299 (p_mad_adec->libmad_decoder->sync->stream.error==MAD_ERROR_BADSCALEFACTOR)
301 // intf_ErrMsg( "LIBMAD_OUTPUT: nr of channels [%d], samplerate in Hz [%d,%d], sample size [%d], error_code [%0x]",
302 // p_libmad_pcm->channels, p_libmad_pcm->samplerate, p_libmad_header->samplerate,
303 // p_libmad_pcm->length, p_mad_adec->libmad_decoder->sync->stream.error);
304 // PrintFrameInfo(&p_libmad_header);
305 return MAD_FLOW_IGNORE;
308 /* Set timestamp to synchronize audio and video decoder fifo's */
309 p_mad_adec->p_aout_fifo->date[p_mad_adec->p_aout_fifo->l_end_frame] = p_mad_adec->i_pts_save;
310 mad_timer_add(&p_mad_adec->libmad_timer,p_libmad_header->duration);
312 buffer = ((byte_t *)p_mad_adec->p_aout_fifo->buffer) + (p_mad_adec->p_aout_fifo->l_end_frame * MAD_OUTPUT_SIZE);
316 #ifdef MPG321_ROUTINES
317 sample = audio_linear_dither(16, *left_ch++, &dither);
319 sample = s24_to_s16_pcm(*left_ch++);
322 #ifndef WORDS_BIGENDIAN
323 *buffer++ = (byte_t) (sample >> 0);
324 *buffer++ = (byte_t) (sample >> 8);
326 *buffer++ = (byte_t) (sample >> 8);
327 *buffer++ = (byte_t) (sample >> 0);
329 if (p_libmad_pcm->channels == 2) {
331 /* right audio channel */
332 #ifdef MPG321_ROUTINES
333 sample = audio_linear_dither(16, *right_ch++, &dither);
335 sample = s24_to_s16_pcm(*right_ch++);
338 #ifndef WORDS_BIGENDIAN
339 *buffer++ = (byte_t) (sample >> 0);
340 *buffer++ = (byte_t) (sample >> 8);
342 *buffer++ = (byte_t) (sample >> 8);
343 *buffer++ = (byte_t) (sample >> 0);
347 /* Somethimes a single channel frame is found, while the rest of the movie are
348 * stereo channel frames. How to deal with this ??
349 * One solution is to silence the second channel.
351 *buffer++ = (byte_t) (0);
352 *buffer++ = (byte_t) (0);
356 if (p_libmad_pcm->channels == 1) {
357 intf_ErrMsg( "mad debug: libmad_output channels [%d]", p_libmad_pcm->channels);
360 vlc_mutex_lock (&p_mad_adec->p_aout_fifo->data_lock);
361 p_mad_adec->p_aout_fifo->l_end_frame = (p_mad_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
362 vlc_cond_signal (&p_mad_adec->p_aout_fifo->data_wait);
363 vlc_mutex_unlock (&p_mad_adec->p_aout_fifo->data_lock);
365 return MAD_FLOW_CONTINUE;
368 /*****************************************************************************
369 * libmad_error: this function is called when an error occurs during decoding
370 *****************************************************************************/
371 enum mad_flow libmad_error(void *data, struct mad_stream *p_libmad_stream, struct mad_frame *p_libmad_frame)
373 enum mad_flow result = MAD_FLOW_CONTINUE;
375 switch (p_libmad_stream->error)
377 case MAD_ERROR_BUFLEN: /* input buffer too small (or EOF) */
378 intf_ErrMsg("libmad error: input buffer too small (or EOF)");
379 result = MAD_FLOW_CONTINUE;
381 case MAD_ERROR_BUFPTR: /* invalid (null) buffer pointer */
382 intf_ErrMsg("libmad error: invalid (null) buffer pointer");
383 result = MAD_FLOW_STOP;
385 case MAD_ERROR_NOMEM: /* not enough memory */
386 intf_ErrMsg("libmad error: invalid (null) buffer pointer");
387 result = MAD_FLOW_STOP;
389 case MAD_ERROR_LOSTSYNC: /* lost synchronization */
390 intf_ErrMsg("libmad error: lost synchronization");
391 mad_stream_sync(p_libmad_stream);
392 result = MAD_FLOW_CONTINUE;
394 case MAD_ERROR_BADLAYER: /* reserved header layer value */
395 intf_ErrMsg("libmad error: reserved header layer value");
396 result = MAD_FLOW_CONTINUE;
398 case MAD_ERROR_BADBITRATE: /* forbidden bitrate value */
399 intf_ErrMsg("libmad error: forbidden bitrate value");
400 result = MAD_FLOW_CONTINUE;
402 case MAD_ERROR_BADSAMPLERATE: /* reserved sample frequency value */
403 intf_ErrMsg("libmad error: reserved sample frequency value");
404 result = MAD_FLOW_CONTINUE;
406 case MAD_ERROR_BADEMPHASIS: /* reserved emphasis value */
407 intf_ErrMsg("libmad error: reserverd emphasis value");
408 result = MAD_FLOW_CONTINUE;
410 case MAD_ERROR_BADCRC: /* CRC check failed */
411 intf_ErrMsg("libmad error: CRC check failed");
412 result = MAD_FLOW_CONTINUE;
414 case MAD_ERROR_BADBITALLOC: /* forbidden bit allocation value */
415 intf_ErrMsg("libmad error: forbidden bit allocation value");
416 result = MAD_FLOW_IGNORE;
418 case MAD_ERROR_BADSCALEFACTOR:/* bad scalefactor index */
419 intf_ErrMsg("libmad error: bad scalefactor index");
420 result = MAD_FLOW_CONTINUE;
422 case MAD_ERROR_BADFRAMELEN: /* bad frame length */
423 intf_ErrMsg("libmad error: bad frame length");
424 result = MAD_FLOW_CONTINUE;
426 case MAD_ERROR_BADBIGVALUES: /* bad big_values count */
427 intf_ErrMsg("libmad error: bad big values count");
428 result = MAD_FLOW_IGNORE;
430 case MAD_ERROR_BADBLOCKTYPE: /* reserved block_type */
431 intf_ErrMsg("libmad error: reserverd block_type");
432 result = MAD_FLOW_IGNORE;
434 case MAD_ERROR_BADSCFSI: /* bad scalefactor selection info */
435 intf_ErrMsg("libmad error: bad scalefactor selection info");
436 result = MAD_FLOW_CONTINUE;
438 case MAD_ERROR_BADDATAPTR: /* bad main_data_begin pointer */
439 intf_ErrMsg("libmad error: bad main_data_begin pointer");
440 result = MAD_FLOW_STOP;
442 case MAD_ERROR_BADPART3LEN: /* bad audio data length */
443 intf_ErrMsg("libmad error: bad audio data length");
444 result = MAD_FLOW_IGNORE;
446 case MAD_ERROR_BADHUFFTABLE: /* bad Huffman table select */
447 intf_ErrMsg("libmad error: bad Huffman table select");
448 result = MAD_FLOW_IGNORE;
450 case MAD_ERROR_BADHUFFDATA: /* Huffman data overrun */
451 intf_ErrMsg("libmad error: Huffman data overrun");
452 result = MAD_FLOW_IGNORE;
454 case MAD_ERROR_BADSTEREO: /* incompatible block_type for JS */
455 intf_ErrMsg("libmad error: incompatible block_type for JS");
456 result = MAD_FLOW_IGNORE;
459 intf_ErrMsg("libmad error: unknown error occured stopping decoder");
460 result = MAD_FLOW_STOP;
464 //return (MAD_RECOVERABLE(p_libmad_stream->error)? result: MAD_FLOW_STOP);
465 return (MAD_FLOW_CONTINUE);
468 /*****************************************************************************
469 * libmad_message: this function is called to send a message
470 *****************************************************************************/
471 /* enum mad_flow libmad_message(void *, void*, unsigned int*)
473 * return MAD_FLOW_CONTINUE;