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 if ( p_mad_adec->p_fifo->b_die == 1 ) {
54 intf_ErrMsg( "mad_adec error: libmad_input stopping libmad decoder" );
58 if ( p_mad_adec->p_fifo->b_error == 1 ) {
59 intf_ErrMsg( "mad_adec error: libmad_input ignoring current audio frame" );
60 return MAD_FLOW_IGNORE;
63 /* libmad_stream_buffer does not consume the total buffer, it consumes only data
64 * for one frame only. So all data left in the buffer should be put back in front.
66 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 if( p_mad_adec->buffer != p_libmad_stream->next_frame )
83 FAST_MEMCPY( p_mad_adec->buffer, p_libmad_stream->next_frame,
86 ReadStart=p_mad_adec->buffer+Remaining;
87 ReadSize=(MAD_BUFFER_SIZE)-Remaining;
89 /* Store time stamp of next frame */
90 p_mad_adec->i_current_pts = p_mad_adec->i_next_pts;
91 p_mad_adec->i_next_pts = p_mad_adec->p_fifo->p_first->i_pts;
95 ReadSize=(MAD_BUFFER_SIZE);
96 ReadStart=p_mad_adec->buffer;
98 p_mad_adec->i_next_pts = 0;
99 p_mad_adec->i_current_pts = p_mad_adec->p_fifo->p_first->i_pts;
102 /* Fill-in the buffer. If an error occurs print a message
103 * and leave the decoding loop. If the end of stream is
104 * reached we also leave the loop but the return status is
107 if( ReadSize > p_mad_adec->p_data->p_payload_end
108 - p_mad_adec->p_data->p_payload_start )
110 ReadSize = p_mad_adec->p_data->p_payload_end
111 - p_mad_adec->p_data->p_payload_start;
112 FAST_MEMCPY( ReadStart, p_mad_adec->p_data->p_payload_start,
114 NextDataPacket( p_mad_adec->p_fifo, &p_mad_adec->p_data );
118 FAST_MEMCPY( ReadStart, p_mad_adec->p_data->p_payload_start,
120 p_mad_adec->p_data->p_payload_start += ReadSize;
123 if ( p_mad_adec->p_fifo->b_die == 1 )
125 intf_ErrMsg( "mad_adec error: libmad_input stopping libmad decoder" );
126 return MAD_FLOW_STOP;
129 if ( p_mad_adec->p_fifo->b_error == 1 )
131 intf_ErrMsg( "mad_adec error: libmad_input ignoring current audio frame" );
132 return MAD_FLOW_IGNORE;
135 /* Pipe the new buffer content to libmad's stream decoder facility.
136 * Libmad never copies the buffer, but just references it. So keep it in
137 * mad_adec_thread_t structure.
139 mad_stream_buffer(p_libmad_stream,(unsigned char*) &p_mad_adec->buffer,
140 Remaining + ReadSize);
141 p_libmad_stream->error=0;
144 return MAD_FLOW_CONTINUE;
147 /*****************************************************************************
148 * libmad_header: this function is called just after the header of a frame is
150 *****************************************************************************/
152 *enum mad_flow libmad_header(void *data, struct mad_header const *p_libmad_header)
154 * mad_adec_thread_t *p_mad_adec = (mad_adec_thread_t *) data;
156 * intf_ErrMsg( "mad_adec: libmad_header samplerate %d", p_libmad_header->samplerate);
158 * p_mad_adec->p_aout_fifo->i_rate = p_libmad_header->samplerate;
159 * mad_timer_add(&p_mad_adec->libmad_timer,p_libmad_header->duration);
161 * return MAD_FLOW_CONTINUE;
165 /*****************************************************************************
166 * lib_mad_filter: this function is called to filter data of a frame
167 *****************************************************************************/
168 /* enum mad_flow libmad_filter(void *data, struct mad_stream const *p_libmad_stream, struct mad_frame *p_libmad_frame)
170 * return MAD_FLOW_CONTINUE;
174 //#define MPG321_ROUTINES 1
175 #ifdef MPG321_ROUTINES
176 /*****************************************************************************
177 * support routines borrowed from mpg321 (file: mad.c), which is distributed
180 * mpg321 was written by Joe Drew <drew@debian.org>, and based upon 'plaympeg'
181 * from the smpeg sources, which was written by various people from Loki Software
182 * (http://www.lokigames.com).
184 * It also incorporates some source from mad, written by Robert Leslie
185 *****************************************************************************/
187 /* The following two routines and data structure are from the ever-brilliant
191 struct audio_dither {
192 mad_fixed_t error[3];
198 * DESCRIPTION: 32-bit pseudo-random number generator
200 static __inline__ unsigned long prng(unsigned long state)
202 return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
206 * NAME: audio_linear_dither()
207 * DESCRIPTION: generic linear sample quantize and dither routine
209 static __inline__ signed int audio_linear_dither(unsigned int bits, mad_fixed_t sample,
210 struct audio_dither *dither)
212 unsigned int scalebits;
213 mad_fixed_t output, mask, random;
221 sample += dither->error[0] - dither->error[1] + dither->error[2];
223 dither->error[2] = dither->error[1];
224 dither->error[1] = dither->error[0] / 2;
227 output = sample + (1L << (MAD_F_FRACBITS + 1 - bits - 1));
229 scalebits = MAD_F_FRACBITS + 1 - bits;
230 mask = (1L << scalebits) - 1;
233 random = prng(dither->random);
234 output += (random & mask) - (dither->random & mask);
236 dither->random = random;
245 else if (output < MIN) {
256 dither->error[0] = sample - output;
259 return output >> scalebits;
263 /*****************************************************************************
264 * s24_to_s16_pcm: Scale a 24 bit pcm sample to a 16 bit pcm sample.
265 *****************************************************************************/
266 static __inline__ mad_fixed_t s24_to_s16_pcm(mad_fixed_t sample)
269 sample += (1L << (MAD_F_FRACBITS - 16));
272 if (sample >= MAD_F_ONE)
273 sample = MAD_F_ONE - 1;
274 else if (sample < -MAD_F_ONE)
278 return sample >> (MAD_F_FRACBITS + 1 - 16);
282 /*****************************************************************************
283 * libmad_ouput: this function is called just after the frame is decoded
284 *****************************************************************************/
285 enum mad_flow libmad_output(void *data, struct mad_header const *p_libmad_header, struct mad_pcm *p_libmad_pcm)
287 mad_adec_thread_t *p_mad_adec= (mad_adec_thread_t *) data;
290 mad_fixed_t const *left_ch = p_libmad_pcm->samples[0], *right_ch = p_libmad_pcm->samples[1];
291 register int nsamples = p_libmad_pcm->length;
293 #ifdef MPG321_ROUTINES
294 static struct audio_dither dither;
297 /* Creating the audio output fifo.
298 * Assume the samplerate and nr of channels from the first decoded frame is right for the entire audio track.
300 if (p_mad_adec->p_aout_fifo==NULL)
302 p_mad_adec->p_aout_fifo = aout_CreateFifo(
303 AOUT_FIFO_PCM, /* fifo type */
304 p_libmad_pcm->channels, /* nr. of channels */
305 p_libmad_pcm->samplerate, /* frame rate in Hz ?*/
306 ADEC_FRAME_SIZE, /* frame size */
309 if ( p_mad_adec->p_aout_fifo == NULL )
314 intf_ErrMsg("mad_adec debug: in libmad_output aout fifo created");
317 if (p_mad_adec->p_aout_fifo->i_rate != p_libmad_pcm->samplerate)
319 intf_ErrMsg( "mad_adec: libmad_output samplerate is changing from [%d] Hz to [%d] Hz, sample size [%d], error_code [%0x]",
320 p_mad_adec->p_aout_fifo->i_rate, p_libmad_pcm->samplerate,
321 p_libmad_pcm->length, p_mad_adec->libmad_decoder->sync->stream.error);
322 p_mad_adec->p_aout_fifo->i_rate = p_libmad_pcm->samplerate;
325 if( p_mad_adec->i_current_pts )
327 p_mad_adec->p_aout_fifo->date[p_mad_adec->p_aout_fifo->i_end_frame]
328 = p_mad_adec->i_current_pts;
332 p_mad_adec->p_aout_fifo->date[p_mad_adec->p_aout_fifo->i_end_frame]
335 mad_timer_add(&p_mad_adec->libmad_timer,p_libmad_header->duration);
337 buffer = ((byte_t *)p_mad_adec->p_aout_fifo->buffer) + (p_mad_adec->p_aout_fifo->i_end_frame * MAD_OUTPUT_SIZE);
341 #ifdef MPG321_ROUTINES
342 sample = audio_linear_dither(16, *left_ch++, &dither);
344 sample = s24_to_s16_pcm(*left_ch++);
347 #ifndef WORDS_BIGENDIAN
348 *buffer++ = (byte_t) (sample >> 0);
349 *buffer++ = (byte_t) (sample >> 8);
351 *buffer++ = (byte_t) (sample >> 8);
352 *buffer++ = (byte_t) (sample >> 0);
354 if (p_libmad_pcm->channels == 2) {
356 /* right audio channel */
357 #ifdef MPG321_ROUTINES
358 sample = audio_linear_dither(16, *right_ch++, &dither);
360 sample = s24_to_s16_pcm(*right_ch++);
363 #ifndef WORDS_BIGENDIAN
364 *buffer++ = (byte_t) (sample >> 0);
365 *buffer++ = (byte_t) (sample >> 8);
367 *buffer++ = (byte_t) (sample >> 8);
368 *buffer++ = (byte_t) (sample >> 0);
372 /* Somethimes a single channel frame is found, while the rest of the movie are
373 * stereo channel frames. How to deal with this ??
374 * One solution is to silence the second channel.
376 *buffer++ = (byte_t) (0);
377 *buffer++ = (byte_t) (0);
381 if (p_libmad_pcm->channels == 1) {
382 intf_ErrMsg( "mad debug: libmad_output channels [%d]", p_libmad_pcm->channels);
385 vlc_mutex_lock (&p_mad_adec->p_aout_fifo->data_lock);
386 p_mad_adec->p_aout_fifo->i_end_frame = (p_mad_adec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
387 vlc_cond_signal (&p_mad_adec->p_aout_fifo->data_wait);
388 vlc_mutex_unlock (&p_mad_adec->p_aout_fifo->data_lock);
390 return MAD_FLOW_CONTINUE;
393 /*****************************************************************************
394 * libmad_error: this function is called when an error occurs during decoding
395 *****************************************************************************/
396 enum mad_flow libmad_error(void *data, struct mad_stream *p_libmad_stream, struct mad_frame *p_libmad_frame)
398 enum mad_flow result = MAD_FLOW_CONTINUE;
400 switch (p_libmad_stream->error)
402 case MAD_ERROR_BUFLEN: /* input buffer too small (or EOF) */
403 intf_ErrMsg("libmad error: input buffer too small (or EOF)");
404 result = MAD_FLOW_CONTINUE;
406 case MAD_ERROR_BUFPTR: /* invalid (null) buffer pointer */
407 intf_ErrMsg("libmad error: invalid (null) buffer pointer");
408 result = MAD_FLOW_STOP;
410 case MAD_ERROR_NOMEM: /* not enough memory */
411 intf_ErrMsg("libmad error: invalid (null) buffer pointer");
412 result = MAD_FLOW_STOP;
414 case MAD_ERROR_LOSTSYNC: /* lost synchronization */
415 intf_ErrMsg("libmad error: lost synchronization");
416 mad_stream_sync(p_libmad_stream);
417 result = MAD_FLOW_CONTINUE;
419 case MAD_ERROR_BADLAYER: /* reserved header layer value */
420 intf_ErrMsg("libmad error: reserved header layer value");
421 result = MAD_FLOW_CONTINUE;
423 case MAD_ERROR_BADBITRATE: /* forbidden bitrate value */
424 intf_ErrMsg("libmad error: forbidden bitrate value");
425 result = MAD_FLOW_CONTINUE;
427 case MAD_ERROR_BADSAMPLERATE: /* reserved sample frequency value */
428 intf_ErrMsg("libmad error: reserved sample frequency value");
429 result = MAD_FLOW_CONTINUE;
431 case MAD_ERROR_BADEMPHASIS: /* reserved emphasis value */
432 intf_ErrMsg("libmad error: reserverd emphasis value");
433 result = MAD_FLOW_CONTINUE;
435 case MAD_ERROR_BADCRC: /* CRC check failed */
436 intf_ErrMsg("libmad error: CRC check failed");
437 result = MAD_FLOW_CONTINUE;
439 case MAD_ERROR_BADBITALLOC: /* forbidden bit allocation value */
440 intf_ErrMsg("libmad error: forbidden bit allocation value");
441 result = MAD_FLOW_IGNORE;
443 case MAD_ERROR_BADSCALEFACTOR:/* bad scalefactor index */
444 intf_ErrMsg("libmad error: bad scalefactor index");
445 result = MAD_FLOW_CONTINUE;
447 case MAD_ERROR_BADFRAMELEN: /* bad frame length */
448 intf_ErrMsg("libmad error: bad frame length");
449 result = MAD_FLOW_CONTINUE;
451 case MAD_ERROR_BADBIGVALUES: /* bad big_values count */
452 intf_ErrMsg("libmad error: bad big values count");
453 result = MAD_FLOW_IGNORE;
455 case MAD_ERROR_BADBLOCKTYPE: /* reserved block_type */
456 intf_ErrMsg("libmad error: reserverd block_type");
457 result = MAD_FLOW_IGNORE;
459 case MAD_ERROR_BADSCFSI: /* bad scalefactor selection info */
460 intf_ErrMsg("libmad error: bad scalefactor selection info");
461 result = MAD_FLOW_CONTINUE;
463 case MAD_ERROR_BADDATAPTR: /* bad main_data_begin pointer */
464 intf_ErrMsg("libmad error: bad main_data_begin pointer");
465 result = MAD_FLOW_STOP;
467 case MAD_ERROR_BADPART3LEN: /* bad audio data length */
468 intf_ErrMsg("libmad error: bad audio data length");
469 result = MAD_FLOW_IGNORE;
471 case MAD_ERROR_BADHUFFTABLE: /* bad Huffman table select */
472 intf_ErrMsg("libmad error: bad Huffman table select");
473 result = MAD_FLOW_IGNORE;
475 case MAD_ERROR_BADHUFFDATA: /* Huffman data overrun */
476 intf_ErrMsg("libmad error: Huffman data overrun");
477 result = MAD_FLOW_IGNORE;
479 case MAD_ERROR_BADSTEREO: /* incompatible block_type for JS */
480 intf_ErrMsg("libmad error: incompatible block_type for JS");
481 result = MAD_FLOW_IGNORE;
484 intf_ErrMsg("libmad error: unknown error occured stopping decoder");
485 result = MAD_FLOW_STOP;
489 return (MAD_RECOVERABLE(p_libmad_stream->error)? result: MAD_FLOW_STOP);
490 //return (MAD_FLOW_CONTINUE);
493 /*****************************************************************************
494 * libmad_message: this function is called to send a message
495 *****************************************************************************/
496 /* enum mad_flow libmad_message(void *, void*, unsigned int*)
498 * return MAD_FLOW_CONTINUE;