libmad.c 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/***************************************************************************
             libmad.c  -  description
               -------------------
    Functions that are called by libmad to communicate with vlc decoder
    infrastructure.

    begin                : Mon Nov 5 2001
    copyright            : (C) 2001 by Jean-Paul Saman
    email                : jpsaman@wxs.nl
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>                                              /* strdup() */

#include <vlc/vlc.h>
#include <vlc/aout.h>
#include <vlc/decoder.h>

/*****************************************************************************
 * Libmad includes files
 *****************************************************************************/
#include <mad.h>
#include "decoder.h"
#include "libmad.h"

static void PrintFrameInfo(struct mad_header *Header);

/*****************************************************************************
 * libmad_input: this function is called by libmad when the input buffer needs
 * to be filled.
 *****************************************************************************/
44
enum mad_flow libmad_input( void *p_data, struct mad_stream *p_stream )
45
{
46 47
    mad_adec_thread_t * p_dec = (mad_adec_thread_t *) p_data;
    size_t   i_wanted, i_left;
48

49 50 51
    if ( p_dec->p_fifo->b_die )
    {
        msg_Dbg( p_dec->p_fifo, "stopping libmad decoder" );
52 53 54
        return MAD_FLOW_STOP;
    }

55 56 57
    if ( p_dec->p_fifo->b_error )
    {
        msg_Warn( p_dec->p_fifo, "ignoring current audio frame" );
58 59 60
        return MAD_FLOW_IGNORE;
    }

61 62 63 64
    /* libmad_stream_buffer does not consume the total buffer, it consumes
     * only data for one frame only. So all data left in the buffer should
     * be put back in front. */
    if ( !p_stream->buffer || p_stream->error == MAD_ERROR_BUFLEN )
65
    {
66 67 68 69 70 71 72 73 74
       /* libmad does not consume all the buffer it's given. Some data,
        * part of a truncated frame, is left unused at the end of the
        * buffer. Those datas must be put back at the beginning of the
        * buffer and taken in account for refilling the buffer. This
        * means that the input buffer must be large enough to hold a
        * complete frame at the highest observable bit-rate (currently
        * 448 kb/s). XXX=XXX Is 2016 bytes the size of the largest frame?
        * (448000*(1152/32000))/8 */
        if( p_stream->next_frame )
75
        {
76 77
            i_left = p_stream->bufend - p_stream->next_frame;
            if( p_dec->buffer != p_stream->next_frame )
78
            {
79
                memcpy( p_dec->buffer, p_stream->next_frame, i_left );
80
            }
81
            i_wanted = MAD_BUFFER_MDLEN - i_left;
82

83
            /* Store timestamp for next frame */
Gildas Bazin's avatar
 
Gildas Bazin committed
84
            p_dec->i_next_pts = p_dec->bit_stream.p_pes->i_pts;
85 86 87
        }
        else
        {
88 89 90 91
            i_wanted = MAD_BUFFER_MDLEN;
            i_left = 0;

            /* Store timestamp for this frame */
Gildas Bazin's avatar
 
Gildas Bazin committed
92
            p_dec->i_current_pts = p_dec->bit_stream.p_pes->i_pts;
93 94
        }

95 96 97
        /* Fill-in the buffer. If an error occurs print a message and leave
         * the decoding loop. If the end of stream is reached we also leave
         * the loop but the return status is left untouched. */
Gildas Bazin's avatar
 
Gildas Bazin committed
98 99
        if( i_wanted > p_dec->bit_stream.p_data->p_payload_end
                        - p_dec->bit_stream.p_data->p_payload_start )
100
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
101 102
            i_wanted = p_dec->bit_stream.p_data->p_payload_end
                        - p_dec->bit_stream.p_data->p_payload_start;
103
            memcpy( p_dec->buffer + i_left,
Gildas Bazin's avatar
 
Gildas Bazin committed
104 105 106 107
                    p_dec->bit_stream.p_data->p_payload_start, i_wanted );
            NextDataPacket( p_dec->p_fifo, &p_dec->bit_stream );
            /* No need to check that p_dec->bit_stream->p_data is valid
             * since we check later on for b_die and b_error */
108 109 110
        }
        else
        {
111
            memcpy( p_dec->buffer + i_left,
Gildas Bazin's avatar
 
Gildas Bazin committed
112 113
                    p_dec->bit_stream.p_data->p_payload_start, i_wanted );
            p_dec->bit_stream.p_data->p_payload_start += i_wanted;
114 115
        }

116
        if ( p_dec->p_fifo->b_die )
117
        {
118
            msg_Dbg( p_dec->p_fifo, "stopping libmad decoder" );
119 120 121
            return MAD_FLOW_STOP;
        }

122
        if ( p_dec->p_fifo->b_error )
123
        {
124
            msg_Warn( p_dec->p_fifo, "ignoring current audio frame" );    
125 126 127 128
            return MAD_FLOW_IGNORE;
        }

        /* Pipe the new buffer content to libmad's stream decoder facility.
129 130 131 132 133
         * Libmad never copies the buffer, but just references it. So keep
         * it in mad_adec_thread_t structure. */
        mad_stream_buffer( p_stream, (unsigned char*) &p_dec->buffer,
                           i_left + i_wanted );
        p_stream->error = 0;
134 135 136 137 138 139
    }

    return MAD_FLOW_CONTINUE;
}

/*****************************************************************************
140
 * libmad_output: this function is called just after the frame is decoded
141
 *****************************************************************************/
142 143
enum mad_flow libmad_output( void *p_data, struct mad_header const *p_header,
                             struct mad_pcm *p_pcm )
144
{
145 146 147 148
    mad_adec_thread_t * p_dec = (mad_adec_thread_t *) p_data;
    aout_buffer_t *     p_buffer;
    mad_fixed_t const * p_left = p_pcm->samples[0];
    mad_fixed_t const * p_right = p_pcm->samples[1];
149
    int                 i_samples = p_pcm->length;
150
    mad_fixed_t *       p_samples;
151 152
    int                 i_channels = (p_pcm->channels == 2) ? AOUT_CHAN_STEREO :
                                     AOUT_CHAN_MONO;
153 154 155 156

    /* Creating the audio output fifo. Assume the samplerate and nr of channels
     * from the first decoded frame is right for the entire audio track. */
    if( (p_dec->p_aout_input != NULL) &&
157
        (p_dec->output_format.i_rate != p_pcm->samplerate
158
           || p_dec->output_format.i_channels != i_channels) )
159 160
    {
        /* Parameters changed - this should not happen. */
161
        aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
162
        p_dec->p_aout_input = NULL;
163 164 165
    }

    /* Creating the audio input if not created yet. */
166
    if( p_dec->p_aout_input == NULL )
167
    {
168
        p_dec->output_format.i_rate = p_pcm->samplerate;
169
        p_dec->output_format.i_channels = i_channels;
170
        aout_DateInit( &p_dec->end_date, p_pcm->samplerate );
171 172 173
        p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
                                           &p_dec->p_aout,
                                           &p_dec->output_format );
174 175 176 177 178 179 180

        if ( p_dec->p_aout_input == NULL )
        {
            p_dec->p_fifo->b_error = VLC_TRUE;
            return MAD_FLOW_BREAK;
        }
    }
181

182 183 184 185
    if( p_dec->i_current_pts )
    {
        /* Set the Presentation Time Stamp */
        if( p_dec->i_current_pts != aout_DateGet( &p_dec->end_date ) )
186
        {
187
            aout_DateSet( &p_dec->end_date, p_dec->i_current_pts );
188
        }
189 190 191 192 193 194 195 196

        p_dec->i_current_pts = 0;
    }
    else if( p_dec->i_next_pts )
    {
        /* No PTS this time, but it'll be for next frame */
        p_dec->i_current_pts = p_dec->i_next_pts;
        p_dec->i_next_pts = 0;
197 198
    }

199
    if( !aout_DateGet( &p_dec->end_date ) )
200
    {
201 202
        /* No date available yet, wait for the first PTS. */
        return MAD_FLOW_CONTINUE;
203 204
    }

205
    p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input, i_samples );
206

207
    if ( p_buffer == NULL )
208
    {
209
        msg_Err( p_dec->p_fifo, "allocating new buffer failed" );
210 211
        return MAD_FLOW_BREAK;
    }
212 213 214

    p_buffer->start_date = aout_DateGet( &p_dec->end_date );
    p_buffer->end_date = aout_DateIncrement( &p_dec->end_date, i_samples );
215

216
    /* Interleave and keep buffers in mad_fixed_t format */
217 218 219
    p_samples = (mad_fixed_t *)p_buffer->p_buffer;

    switch( p_pcm->channels )
220
    {
221 222 223 224 225 226 227
    case 2:
        while( i_samples-- )
        {
            *p_samples++ = *p_left++;
            *p_samples++ = *p_right++;
        }
        break;
228

229
    case 1:
230 231
        p_dec->p_fifo->p_vlc->pf_memcpy( p_samples, p_left,
                                         i_samples * sizeof(mad_fixed_t) );
232 233
        break;

234 235 236
    default:
        msg_Err( p_dec->p_fifo, "cannot interleave %i channels",
                                p_pcm->channels );
237 238
    }

239
    aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
240 241 242 243 244 245 246

    return MAD_FLOW_CONTINUE;
}

/*****************************************************************************
 * libmad_error: this function is called when an error occurs during decoding
 *****************************************************************************/
247 248
enum mad_flow libmad_error( void *data, struct mad_stream *p_libmad_stream,
                            struct mad_frame *p_libmad_frame )
249
{
250
    mad_adec_thread_t *p_dec = (mad_adec_thread_t *) data;
251 252 253 254 255
    enum mad_flow result = MAD_FLOW_CONTINUE;

    switch (p_libmad_stream->error)
    {             
    case MAD_ERROR_BUFLEN:                /* input buffer too small (or EOF) */
256
        msg_Err( p_dec->p_fifo, "input buffer too small (or EOF)" );
257 258 259
        result = MAD_FLOW_CONTINUE;
        break;
    case MAD_ERROR_BUFPTR:                /* invalid (null) buffer pointer */
260
        msg_Err( p_dec->p_fifo, "invalid (null) buffer pointer" );
261 262 263
        result = MAD_FLOW_STOP;
        break;
    case MAD_ERROR_NOMEM:                 /* not enough memory */
264
        msg_Err( p_dec->p_fifo, "invalid (null) buffer pointer" );
265 266 267
        result = MAD_FLOW_STOP;
        break;
    case MAD_ERROR_LOSTSYNC:            /* lost synchronization */
268
        msg_Err( p_dec->p_fifo, "lost synchronization" );
269 270 271 272
        mad_stream_sync(p_libmad_stream);
        result = MAD_FLOW_CONTINUE;
        break;
    case MAD_ERROR_BADLAYER:            /* reserved header layer value */
273
        msg_Err( p_dec->p_fifo, "reserved header layer value" );
274 275 276
        result = MAD_FLOW_CONTINUE;
        break;
    case MAD_ERROR_BADBITRATE:        /* forbidden bitrate value */
277
        msg_Err( p_dec->p_fifo, "forbidden bitrate value" );
278 279 280
        result = MAD_FLOW_CONTINUE;
        break;
    case MAD_ERROR_BADSAMPLERATE: /* reserved sample frequency value */
281
        msg_Err( p_dec->p_fifo, "reserved sample frequency value" );
282 283 284
        result = MAD_FLOW_CONTINUE;
        break;
    case MAD_ERROR_BADEMPHASIS:     /* reserved emphasis value */
285
        msg_Err( p_dec->p_fifo, "reserverd emphasis value" );
286 287 288
        result = MAD_FLOW_CONTINUE;
        break;
    case MAD_ERROR_BADCRC:                /* CRC check failed */
289
        msg_Err( p_dec->p_fifo, "CRC check failed" );
290 291 292
        result = MAD_FLOW_CONTINUE;
        break;
    case MAD_ERROR_BADBITALLOC:     /* forbidden bit allocation value */
293
        msg_Err( p_dec->p_fifo, "forbidden bit allocation value" );
294 295 296
        result = MAD_FLOW_IGNORE;
        break;
    case MAD_ERROR_BADSCALEFACTOR:/* bad scalefactor index */
297
        msg_Err( p_dec->p_fifo, "bad scalefactor index" );
298 299 300
        result = MAD_FLOW_CONTINUE;
        break;
    case MAD_ERROR_BADFRAMELEN:     /* bad frame length */
301
        msg_Err( p_dec->p_fifo, "bad frame length" );
302 303 304
        result = MAD_FLOW_CONTINUE;
        break;
    case MAD_ERROR_BADBIGVALUES:    /* bad big_values count */
305
        msg_Err( p_dec->p_fifo, "bad big values count" );
306 307 308
        result = MAD_FLOW_IGNORE;
        break;
    case MAD_ERROR_BADBLOCKTYPE:    /* reserved block_type */
309
        msg_Err( p_dec->p_fifo, "reserverd block_type" );
310 311 312
        result = MAD_FLOW_IGNORE;
        break;
    case MAD_ERROR_BADSCFSI:            /* bad scalefactor selection info */
313
        msg_Err( p_dec->p_fifo, "bad scalefactor selection info" );
314 315 316
        result = MAD_FLOW_CONTINUE;
        break;
    case MAD_ERROR_BADDATAPTR:        /* bad main_data_begin pointer */
317
        msg_Err( p_dec->p_fifo, "bad main_data_begin pointer" );
318 319 320
        result = MAD_FLOW_STOP;
        break;
    case MAD_ERROR_BADPART3LEN:     /* bad audio data length */
321
        msg_Err( p_dec->p_fifo, "bad audio data length" );
322 323 324
        result = MAD_FLOW_IGNORE;
        break;
    case MAD_ERROR_BADHUFFTABLE:    /* bad Huffman table select */
325
        msg_Err( p_dec->p_fifo, "bad Huffman table select" );
326 327 328
        result = MAD_FLOW_IGNORE;
        break;
    case MAD_ERROR_BADHUFFDATA:     /* Huffman data overrun */
329
        msg_Err( p_dec->p_fifo, "Huffman data overrun" );
330 331 332
        result = MAD_FLOW_IGNORE;
        break;
    case MAD_ERROR_BADSTEREO:         /* incompatible block_type for JS */
333
        msg_Err( p_dec->p_fifo, "incompatible block_type for JS" );
334 335 336
        result = MAD_FLOW_IGNORE;
        break;
    default:
337
        msg_Err( p_dec->p_fifo, "unknown error occured stopping decoder" );
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
        result = MAD_FLOW_STOP;
        break;
    }
    
    return (MAD_RECOVERABLE(p_libmad_stream->error)? result: MAD_FLOW_STOP);
    //return (MAD_FLOW_CONTINUE);
}

/*****************************************************************************
 * libmad_message: this function is called to send a message
 *****************************************************************************/
/* enum mad_flow libmad_message(void *, void*, unsigned int*)
 * {
 *     return MAD_FLOW_CONTINUE;
 * }
 */



/****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
358
 * Print human readable informations about an audio MPEG frame.
359 360 361 362 363 364 365
 ****************************************************************************/
static void PrintFrameInfo(struct mad_header *Header)
{
	const char	*Layer,
			*Mode,
			*Emphasis;

366
	/* Convert the layer number to its printed representation. */
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	switch(Header->layer)
	{
		case MAD_LAYER_I:
			Layer="I";
			break;
		case MAD_LAYER_II:
			Layer="II";
			break;
		case MAD_LAYER_III:
			Layer="III";
			break;
		default:
			Layer="(unexpected layer value)";
			break;
	}

383
	/* Convert the audio mode to its printed representation. */
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	switch(Header->mode)
	{
		case MAD_MODE_SINGLE_CHANNEL:
			Mode="single channel";
			break;
		case MAD_MODE_DUAL_CHANNEL:
			Mode="dual channel";
			break;
		case MAD_MODE_JOINT_STEREO:
			Mode="joint (MS/intensity) stereo";
			break;
		case MAD_MODE_STEREO:
			Mode="normal LR stereo";
			break;
		default:
			Mode="(unexpected mode value)";
			break;
	}

403
	/* Convert the emphasis to its printed representation. */
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	switch(Header->emphasis)
	{
		case MAD_EMPHASIS_NONE:
			Emphasis="no";
			break;
		case MAD_EMPHASIS_50_15_US:
			Emphasis="50/15 us";
			break;
		case MAD_EMPHASIS_CCITT_J_17:
			Emphasis="CCITT J.17";
			break;
		default:
			Emphasis="(unexpected emphasis value)";
			break;
	}

420
//X	msg_Err("statistics: %lu kb/s audio mpeg layer %s stream %s crc, "
421 422 423 424 425
//X			"%s with %s emphasis at %d Hz sample rate\n",
//X			Header->bitrate,Layer,
//X			Header->flags&MAD_FLAG_PROTECTION?"with":"without",
//X			Mode,Emphasis,Header->samplerate);
}