]> git.sesse.net Git - vlc/blob - modules/codec/mad/libmad.c
* all: access plugins should not modify
[vlc] / modules / codec / mad / libmad.c
1 /***************************************************************************
2              libmad.c  -  description
3                -------------------
4     Functions that are called by libmad to communicate with vlc decoder
5     infrastructure.
6
7     begin                : Mon Nov 5 2001
8     copyright            : (C) 2001 by Jean-Paul Saman
9     email                : jpsaman@wxs.nl
10  ***************************************************************************/
11
12 /***************************************************************************
13  *                                                                         *
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.                                   *
18  *                                                                         *
19  ***************************************************************************/
20
21 /*****************************************************************************
22  * Preamble
23  *****************************************************************************/
24 #include <stdlib.h>                                      /* malloc(), free() */
25 #include <string.h>                                              /* strdup() */
26
27 #include <vlc/vlc.h>
28 #include <vlc/aout.h>
29 #include <vlc/decoder.h>
30
31 /*****************************************************************************
32  * Libmad includes files
33  *****************************************************************************/
34 #include <mad.h>
35 #include "decoder.h"
36 #include "libmad.h"
37
38 static void PrintFrameInfo(struct mad_header *Header);
39
40 /*****************************************************************************
41  * libmad_input: this function is called by libmad when the input buffer needs
42  * to be filled.
43  *****************************************************************************/
44 enum mad_flow libmad_input( void *p_data, struct mad_stream *p_stream )
45 {
46     mad_adec_thread_t * p_dec = (mad_adec_thread_t *) p_data;
47     size_t   i_wanted, i_left;
48
49     if ( p_dec->p_fifo->b_die )
50     {
51         msg_Dbg( p_dec->p_fifo, "stopping libmad decoder" );
52         return MAD_FLOW_STOP;
53     }
54
55     if ( p_dec->p_fifo->b_error )
56     {
57         msg_Warn( p_dec->p_fifo, "ignoring current audio frame" );
58         return MAD_FLOW_IGNORE;
59     }
60
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 )
65     {
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 )
75         {
76             i_left = p_stream->bufend - p_stream->next_frame;
77             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 */
84             p_dec->i_next_pts = p_dec->bit_stream.p_pes->i_pts;
85         }
86         else
87         {
88             i_wanted = MAD_BUFFER_MDLEN;
89             i_left = 0;
90
91             /* Store timestamp for this frame */
92             p_dec->i_current_pts = p_dec->bit_stream.p_pes->i_pts;
93         }
94
95         /* Fill-in the buffer. If an error occurs print a message and leave
96          * the decoding loop. If the end of stream is reached we also leave
97          * the loop but the return status is left untouched. */
98         if( i_wanted > (size_t)(p_dec->bit_stream.p_data->p_payload_end
99                                  - p_dec->bit_stream.p_data->p_payload_start) )
100         {
101             i_wanted = p_dec->bit_stream.p_data->p_payload_end
102                         - p_dec->bit_stream.p_data->p_payload_start;
103             memcpy( p_dec->buffer + i_left,
104                     p_dec->bit_stream.p_data->p_payload_start, i_wanted );
105             NextDataPacket( p_dec->p_fifo, &p_dec->bit_stream );
106             /* No need to check that p_dec->bit_stream->p_data is valid
107              * since we check later on for b_die and b_error */
108         }
109         else
110         {
111             memcpy( p_dec->buffer + i_left,
112                     p_dec->bit_stream.p_data->p_payload_start, i_wanted );
113             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             return MAD_FLOW_STOP;
120         }
121
122         if ( p_dec->p_fifo->b_error )
123         {
124             msg_Warn( p_dec->p_fifo, "ignoring current audio frame" );    
125             return MAD_FLOW_IGNORE;
126         }
127
128         /* Pipe the new buffer content to libmad's stream decoder facility.
129          * Libmad never copies the buffer, but just references it. So keep
130          * it in mad_adec_thread_t structure. */
131         mad_stream_buffer( p_stream, (unsigned char*) &p_dec->buffer,
132                            i_left + i_wanted );
133         p_stream->error = 0;
134     }
135
136     return MAD_FLOW_CONTINUE;
137 }
138
139 /*****************************************************************************
140  * libmad_output: this function is called just after the frame is decoded
141  *****************************************************************************/
142 enum mad_flow libmad_output( void *p_data, struct mad_header const *p_header,
143                              struct mad_pcm *p_pcm )
144 {
145     mad_adec_thread_t * p_dec = (mad_adec_thread_t *) p_data;
146     aout_buffer_t *     p_buffer;
147     mad_fixed_t const * p_left = p_pcm->samples[0];
148     mad_fixed_t const * p_right = p_pcm->samples[1];
149     unsigned int        i_samples = p_pcm->length;
150     mad_fixed_t *       p_samples;
151     unsigned int        i_channels = (p_pcm->channels == 2) ?
152                                      AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT :
153                                      AOUT_CHAN_CENTER;
154
155     /* Creating the audio output fifo. Assume the samplerate and nr of channels
156      * from the first decoded frame is right for the entire audio track. */
157     if( (p_dec->p_aout_input != NULL) &&
158         (p_dec->output_format.i_rate != p_pcm->samplerate
159            || p_dec->output_format.i_physical_channels != i_channels) )
160     {
161         /* Parameters changed - this should not happen. */
162         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
163         p_dec->p_aout_input = NULL;
164     }
165
166     /* Creating the audio input if not created yet. */
167     if( p_dec->p_aout_input == NULL )
168     {
169         p_dec->output_format.i_rate = p_pcm->samplerate;
170         p_dec->output_format.i_physical_channels = i_channels;
171         p_dec->output_format.i_original_channels = i_channels;
172         aout_DateInit( &p_dec->end_date, p_pcm->samplerate );
173         p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
174                                            &p_dec->p_aout,
175                                            &p_dec->output_format );
176
177         if ( p_dec->p_aout_input == NULL )
178         {
179             p_dec->p_fifo->b_error = VLC_TRUE;
180             return MAD_FLOW_BREAK;
181         }
182     }
183
184     if( p_dec->i_current_pts )
185     {
186         /* Set the Presentation Time Stamp */
187         if( p_dec->i_current_pts != aout_DateGet( &p_dec->end_date ) )
188         {
189             aout_DateSet( &p_dec->end_date, p_dec->i_current_pts );
190         }
191
192         p_dec->i_current_pts = 0;
193     }
194     else if( p_dec->i_next_pts )
195     {
196         /* No PTS this time, but it'll be for next frame */
197         p_dec->i_current_pts = p_dec->i_next_pts;
198         p_dec->i_next_pts = 0;
199     }
200
201     if( !aout_DateGet( &p_dec->end_date ) )
202     {
203         /* No date available yet, wait for the first PTS. */
204         return MAD_FLOW_CONTINUE;
205     }
206
207     p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input, i_samples );
208
209     if ( p_buffer == NULL )
210     {
211         msg_Err( p_dec->p_fifo, "allocating new buffer failed" );
212         return MAD_FLOW_BREAK;
213     }
214
215     p_buffer->start_date = aout_DateGet( &p_dec->end_date );
216     p_buffer->end_date = aout_DateIncrement( &p_dec->end_date, i_samples );
217
218     /* Interleave and keep buffers in mad_fixed_t format */
219     p_samples = (mad_fixed_t *)p_buffer->p_buffer;
220
221     switch( p_pcm->channels )
222     {
223     case 2:
224         while( i_samples-- )
225         {
226             *p_samples++ = *p_left++;
227             *p_samples++ = *p_right++;
228         }
229         break;
230
231     case 1:
232         p_dec->p_fifo->p_vlc->pf_memcpy( p_samples, p_left,
233                                          i_samples * sizeof(mad_fixed_t) );
234         break;
235
236     default:
237         msg_Err( p_dec->p_fifo, "cannot interleave %i channels",
238                                 p_pcm->channels );
239     }
240
241     aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
242
243     return MAD_FLOW_CONTINUE;
244 }
245
246 /*****************************************************************************
247  * libmad_error: this function is called when an error occurs during decoding
248  *****************************************************************************/
249 enum mad_flow libmad_error( void *data, struct mad_stream *p_libmad_stream,
250                             struct mad_frame *p_libmad_frame )
251 {
252     mad_adec_thread_t *p_dec = (mad_adec_thread_t *) data;
253     enum mad_flow result = MAD_FLOW_CONTINUE;
254
255     switch (p_libmad_stream->error)
256     {             
257     case MAD_ERROR_BUFLEN:                /* input buffer too small (or EOF) */
258         msg_Err( p_dec->p_fifo, "input buffer too small (or EOF)" );
259         result = MAD_FLOW_CONTINUE;
260         break;
261     case MAD_ERROR_BUFPTR:                /* invalid (null) buffer pointer */
262         msg_Err( p_dec->p_fifo, "invalid (null) buffer pointer" );
263         result = MAD_FLOW_STOP;
264         break;
265     case MAD_ERROR_NOMEM:                 /* not enough memory */
266         msg_Err( p_dec->p_fifo, "invalid (null) buffer pointer" );
267         result = MAD_FLOW_STOP;
268         break;
269     case MAD_ERROR_LOSTSYNC:            /* lost synchronization */
270         msg_Err( p_dec->p_fifo, "lost synchronization" );
271         mad_stream_sync(p_libmad_stream);
272         result = MAD_FLOW_CONTINUE;
273         break;
274     case MAD_ERROR_BADLAYER:            /* reserved header layer value */
275         msg_Err( p_dec->p_fifo, "reserved header layer value" );
276         result = MAD_FLOW_CONTINUE;
277         break;
278     case MAD_ERROR_BADBITRATE:        /* forbidden bitrate value */
279         msg_Err( p_dec->p_fifo, "forbidden bitrate value" );
280         result = MAD_FLOW_CONTINUE;
281         break;
282     case MAD_ERROR_BADSAMPLERATE: /* reserved sample frequency value */
283         msg_Err( p_dec->p_fifo, "reserved sample frequency value" );
284         result = MAD_FLOW_CONTINUE;
285         break;
286     case MAD_ERROR_BADEMPHASIS:     /* reserved emphasis value */
287         msg_Err( p_dec->p_fifo, "reserverd emphasis value" );
288         result = MAD_FLOW_CONTINUE;
289         break;
290     case MAD_ERROR_BADCRC:                /* CRC check failed */
291         msg_Err( p_dec->p_fifo, "CRC check failed" );
292         result = MAD_FLOW_CONTINUE;
293         break;
294     case MAD_ERROR_BADBITALLOC:     /* forbidden bit allocation value */
295         msg_Err( p_dec->p_fifo, "forbidden bit allocation value" );
296         result = MAD_FLOW_IGNORE;
297         break;
298     case MAD_ERROR_BADSCALEFACTOR:/* bad scalefactor index */
299         msg_Err( p_dec->p_fifo, "bad scalefactor index" );
300         result = MAD_FLOW_CONTINUE;
301         break;
302     case MAD_ERROR_BADFRAMELEN:     /* bad frame length */
303         msg_Err( p_dec->p_fifo, "bad frame length" );
304         result = MAD_FLOW_CONTINUE;
305         break;
306     case MAD_ERROR_BADBIGVALUES:    /* bad big_values count */
307         msg_Err( p_dec->p_fifo, "bad big values count" );
308         result = MAD_FLOW_IGNORE;
309         break;
310     case MAD_ERROR_BADBLOCKTYPE:    /* reserved block_type */
311         msg_Err( p_dec->p_fifo, "reserverd block_type" );
312         result = MAD_FLOW_IGNORE;
313         break;
314     case MAD_ERROR_BADSCFSI:            /* bad scalefactor selection info */
315         msg_Err( p_dec->p_fifo, "bad scalefactor selection info" );
316         result = MAD_FLOW_CONTINUE;
317         break;
318     case MAD_ERROR_BADDATAPTR:        /* bad main_data_begin pointer */
319         msg_Err( p_dec->p_fifo, "bad main_data_begin pointer" );
320         result = MAD_FLOW_STOP;
321         break;
322     case MAD_ERROR_BADPART3LEN:     /* bad audio data length */
323         msg_Err( p_dec->p_fifo, "bad audio data length" );
324         result = MAD_FLOW_IGNORE;
325         break;
326     case MAD_ERROR_BADHUFFTABLE:    /* bad Huffman table select */
327         msg_Err( p_dec->p_fifo, "bad Huffman table select" );
328         result = MAD_FLOW_IGNORE;
329         break;
330     case MAD_ERROR_BADHUFFDATA:     /* Huffman data overrun */
331         msg_Err( p_dec->p_fifo, "Huffman data overrun" );
332         result = MAD_FLOW_IGNORE;
333         break;
334     case MAD_ERROR_BADSTEREO:         /* incompatible block_type for JS */
335         msg_Err( p_dec->p_fifo, "incompatible block_type for JS" );
336         result = MAD_FLOW_IGNORE;
337         break;
338     default:
339         msg_Err( p_dec->p_fifo, "unknown error occured stopping decoder" );
340         result = MAD_FLOW_STOP;
341         break;
342     }
343     
344     return (MAD_RECOVERABLE(p_libmad_stream->error)? result: MAD_FLOW_STOP);
345     //return (MAD_FLOW_CONTINUE);
346 }
347
348 /*****************************************************************************
349  * libmad_message: this function is called to send a message
350  *****************************************************************************/
351 /* enum mad_flow libmad_message(void *, void*, unsigned int*)
352  * {
353  *     return MAD_FLOW_CONTINUE;
354  * }
355  */
356
357
358
359 /****************************************************************************
360  * Print human readable informations about an audio MPEG frame.
361  ****************************************************************************/
362 static void PrintFrameInfo(struct mad_header *Header)
363 {
364         const char      *Layer,
365                         *Mode,
366                         *Emphasis;
367
368         /* Convert the layer number to its printed representation. */
369         switch(Header->layer)
370         {
371                 case MAD_LAYER_I:
372                         Layer="I";
373                         break;
374                 case MAD_LAYER_II:
375                         Layer="II";
376                         break;
377                 case MAD_LAYER_III:
378                         Layer="III";
379                         break;
380                 default:
381                         Layer="(unexpected layer value)";
382                         break;
383         }
384
385         /* Convert the audio mode to its printed representation. */
386         switch(Header->mode)
387         {
388                 case MAD_MODE_SINGLE_CHANNEL:
389                         Mode="single channel";
390                         break;
391                 case MAD_MODE_DUAL_CHANNEL:
392                         Mode="dual channel";
393                         break;
394                 case MAD_MODE_JOINT_STEREO:
395                         Mode="joint (MS/intensity) stereo";
396                         break;
397                 case MAD_MODE_STEREO:
398                         Mode="normal LR stereo";
399                         break;
400                 default:
401                         Mode="(unexpected mode value)";
402                         break;
403         }
404
405         /* Convert the emphasis to its printed representation. */
406         switch(Header->emphasis)
407         {
408                 case MAD_EMPHASIS_NONE:
409                         Emphasis="no";
410                         break;
411                 case MAD_EMPHASIS_50_15_US:
412                         Emphasis="50/15 us";
413                         break;
414                 case MAD_EMPHASIS_CCITT_J_17:
415                         Emphasis="CCITT J.17";
416                         break;
417                 default:
418                         Emphasis="(unexpected emphasis value)";
419                         break;
420         }
421
422 //X     msg_Err("statistics: %lu kb/s audio mpeg layer %s stream %s crc, "
423 //X                     "%s with %s emphasis at %d Hz sample rate\n",
424 //X                     Header->bitrate,Layer,
425 //X                     Header->flags&MAD_FLAG_PROTECTION?"with":"without",
426 //X                     Mode,Emphasis,Header->samplerate);
427 }