]> git.sesse.net Git - vlc/blob - modules/codec/adpcm.c
* fixed several format string inconsistencies and deprecated C constructions.
[vlc] / modules / codec / adpcm.c
1 /*****************************************************************************
2  * adpcm.c : adpcm variant audio decoder
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: adpcm.c,v 1.2 2002/12/18 14:17:10 sam Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *      
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *
27  * Documentation: http://www.pcisys.net/~melanson/codecs/adpcm.txt
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc/aout.h>
31 #include <vlc/decoder.h>
32 #include <vlc/input.h>
33
34 #include <stdlib.h>                                      /* malloc(), free() */
35 #include <string.h>                                              /* strdup() */
36 #include "codecs.h"
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40
41 #define ADPCM_IMA_QT    1
42 #define ADPCM_IMA_WAV   2
43 #define ADPCM_MS        3
44
45 typedef struct adec_thread_s
46 {
47     int i_codec;
48
49     WAVEFORMATEX    *p_wf;
50     
51     /* The bit stream structure handles the PES stream at the bit level */
52     bit_stream_t        bit_stream;
53     int                 i_block;
54     uint8_t             *p_block;
55     int                 i_samplesperblock;
56     
57     /* Input properties */
58     decoder_fifo_t *p_fifo;
59     
60     /* Output properties */
61     aout_instance_t *   p_aout;       /* opaque */
62     aout_input_t *      p_aout_input; /* opaque */
63     audio_sample_format_t output_format;
64
65     audio_date_t        date;
66     mtime_t             pts;
67
68 } adec_thread_t;
69
70 static int  OpenDecoder    ( vlc_object_t * );
71
72 static int  RunDecoder     ( decoder_fifo_t * );
73 static int  InitThread     ( adec_thread_t * );
74 static void DecodeThread   ( adec_thread_t * );
75 static void EndThread      ( adec_thread_t * );
76
77
78 static void DecodeAdpcmMs( adec_thread_t *, aout_buffer_t * );
79 static void DecodeAdpcmImaWav( adec_thread_t *, aout_buffer_t * );
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84
85 vlc_module_begin();
86     set_description( _("ADPCM audio deocder") );
87     set_capability( "decoder", 50 );
88     set_callbacks( OpenDecoder, NULL );
89 vlc_module_end();
90
91
92 static int pi_channels_maps[6] =
93 {
94     0,
95     AOUT_CHAN_CENTER,
96     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
97     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
98     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
99     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
100      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT 
101 };
102
103 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
104 static int i_index_table[16] =
105 {
106     -1, -1, -1, -1, 2, 4, 6, 8,
107     -1, -1, -1, -1, 2, 4, 6, 8
108 };
109
110 static int i_step_table[89] =
111 {
112     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
113     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
114     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
115     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
116     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
117     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
118     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
119     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
120     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
121 };
122
123 static int i_adaptation_table[16] =
124 {
125     230, 230, 230, 230, 307, 409, 512, 614,
126     768, 614, 512, 409, 307, 230, 230, 230
127 };
128
129 static int i_adaptation_coeff1[7] =
130 {
131     256, 512, 0, 192, 240, 460, 392
132 };
133
134 static int i_adaptation_coeff2[7] =
135 {
136     0, -256, 0, 64, 0, -208, -232
137 };
138
139
140 /*****************************************************************************
141  * OpenDecoder: probe the decoder and return score
142  *****************************************************************************
143  * Tries to launch a decoder and return score so that the interface is able
144  * to choose.
145  *****************************************************************************/
146 static int OpenDecoder( vlc_object_t *p_this )
147 {
148     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
149     
150     switch( p_fifo->i_fourcc )
151     {   
152 //        case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
153         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
154         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
155 //        case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
156 //        case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
157
158             p_fifo->pf_run = RunDecoder;
159             return VLC_SUCCESS;
160             
161         default:
162             return VLC_EGENERIC;
163     }
164
165 }
166
167 /*****************************************************************************
168  * RunDecoder: this function is called just after the thread is created
169  *****************************************************************************/
170 static int RunDecoder( decoder_fifo_t *p_fifo )
171 {
172     adec_thread_t *p_adec;
173     int b_error;
174
175     if( !( p_adec = malloc( sizeof( adec_thread_t ) ) ) )
176     {
177         msg_Err( p_fifo, "out of memory" );
178         DecoderError( p_fifo );
179         return( -1 );
180     }
181     memset( p_adec, 0, sizeof( adec_thread_t ) );
182     
183     p_adec->p_fifo = p_fifo;
184
185     if( InitThread( p_adec ) != 0 )
186     {
187         DecoderError( p_fifo );
188         return( -1 );
189     }
190
191     while( ( !p_adec->p_fifo->b_die )&&( !p_adec->p_fifo->b_error ) )
192     {
193         DecodeThread( p_adec );
194     }
195
196
197     if( ( b_error = p_adec->p_fifo->b_error ) )
198     {
199         DecoderError( p_adec->p_fifo );
200     }
201
202     EndThread( p_adec );
203     if( b_error )
204     {
205         return( -1 );
206     }
207
208     return( 0 );
209 }
210
211
212 #define FREE( p ) if( p ) free( p ); p = NULL
213 #define GetWLE( p ) \
214     ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
215
216 #define GetDWLE( p ) \
217     (  *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
218         ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
219
220 /*****************************************************************************
221  * InitThread: initialize data before entering main loop
222  *****************************************************************************/
223
224 static int InitThread( adec_thread_t * p_adec )
225 {
226     if( !( p_adec->p_wf = (WAVEFORMATEX*)p_adec->p_fifo->p_demux_data ) )
227     {
228         msg_Err( p_adec->p_fifo, "missing format" );
229         return( -1 );
230     }
231     /* fourcc to codec */
232     switch( p_adec->p_fifo->i_fourcc )
233     {
234         case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
235             p_adec->i_codec = ADPCM_IMA_QT;
236             break;
237         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
238             p_adec->i_codec = ADPCM_IMA_WAV;
239             break;
240         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
241             p_adec->i_codec = ADPCM_MS;
242             break;
243         case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
244         case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
245             p_adec->i_codec = 0;
246             break;
247     }
248
249     if( p_adec->p_wf->nChannels < 1 || 
250             p_adec->p_wf->nChannels > 2 )
251     {
252         msg_Err( p_adec->p_fifo, "bad channels count(1-2)" );
253         return( -1 );
254     }
255     if( !( p_adec->i_block = p_adec->p_wf->nBlockAlign ) )
256     {
257         if( p_adec->i_codec == ADPCM_IMA_QT )
258         {
259             p_adec->i_block = 34;
260         }
261         else
262         {
263             p_adec->i_block = 1024; // XXX FIXME
264         }
265         msg_Err( p_adec->p_fifo,
266                  "block size undefined, using %d default", 
267                  p_adec->i_block );
268     }
269     p_adec->p_block = malloc( p_adec->i_block );
270
271     /* calculate samples per block */
272     switch( p_adec->i_codec )
273     {
274         case ADPCM_IMA_QT:
275             p_adec->i_samplesperblock = 64;
276             break;
277         case ADPCM_IMA_WAV:
278             p_adec->i_samplesperblock = 
279                  2 * ( p_adec->i_block - 4 * p_adec->p_wf->nChannels )/
280                  p_adec->p_wf->nChannels;
281                  break;
282         case ADPCM_MS:
283             p_adec->i_samplesperblock = 
284                 2 * ( p_adec->i_block - 7 * p_adec->p_wf->nChannels ) / 
285                 p_adec->p_wf->nChannels + 2;
286             break;
287         default:
288             p_adec->i_samplesperblock = 0;
289     }
290    
291     msg_Dbg( p_adec->p_fifo,
292              "format: samplerate:%dHz channels:%d bits/sample:%d blockalign:%d samplesperblock %d",
293              p_adec->p_wf->nSamplesPerSec,
294              p_adec->p_wf->nChannels,
295              p_adec->p_wf->wBitsPerSample, 
296              p_adec->p_wf->nBlockAlign,
297              p_adec->i_samplesperblock );
298     
299     //p_adec->output_format.i_format = VLC_FOURCC('s','1','6','l');
300     /* FIXME good way ? */
301     p_adec->output_format.i_format = AOUT_FMT_S16_NE;
302     p_adec->output_format.i_rate = p_adec->p_wf->nSamplesPerSec;
303
304
305     p_adec->output_format.i_physical_channels = 
306         p_adec->output_format.i_original_channels =
307             pi_channels_maps[p_adec->p_wf->nChannels];
308     p_adec->p_aout = NULL;
309     p_adec->p_aout_input = NULL;
310
311     /* **** Create a new audio output **** */
312     aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
313     p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
314                                         &p_adec->p_aout,
315                                         &p_adec->output_format );
316     if( !p_adec->p_aout_input )
317     {
318         msg_Err( p_adec->p_fifo, "cannot create aout" );
319         return( -1 );
320     }
321
322     /* Init the BitStream */
323     InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
324                    NULL, NULL );
325
326     return( 0 );
327 }
328
329
330 /*****************************************************************************
331  * DecodeThread: decodes a frame
332  *****************************************************************************/
333 static void DecodeThread( adec_thread_t *p_adec )
334 {
335     aout_buffer_t   *p_aout_buffer;
336
337     /* get pts */
338     CurrentPTS( &p_adec->bit_stream, &p_adec->pts, NULL );
339     /* gather block */
340     GetChunk( &p_adec->bit_stream,
341               p_adec->p_block,
342               p_adec->i_block );
343
344     /* get output buffer */
345     if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
346     {
347         aout_DateSet( &p_adec->date, p_adec->pts );
348     }
349     else if( !aout_DateGet( &p_adec->date ) )
350     {
351         return;
352     }
353
354     p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout, 
355                                        p_adec->p_aout_input,
356                                        p_adec->i_samplesperblock );
357     if( !p_aout_buffer )
358     {
359         msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
360         p_adec->p_fifo->b_error = 1;
361         return;
362     }
363     
364     p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
365     p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
366                                                   p_adec->i_samplesperblock );
367
368     /* decode */
369     
370     switch( p_adec->i_codec )
371     {
372         case ADPCM_IMA_QT:
373             break;
374         case ADPCM_IMA_WAV:
375             DecodeAdpcmImaWav( p_adec, p_aout_buffer );
376             break;
377         case ADPCM_MS:
378             DecodeAdpcmMs( p_adec, p_aout_buffer );
379             break;
380         default:
381             break;
382     }
383
384
385     /* **** Now we can output these samples **** */
386     aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
387 }
388
389
390 /*****************************************************************************
391  * EndThread : faad decoder thread destruction
392  *****************************************************************************/
393 static void EndThread (adec_thread_t *p_adec)
394 {
395     if( p_adec->p_aout_input )
396     {
397         aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
398     }
399
400     msg_Dbg( p_adec->p_fifo, "adpcm audio decoder closed" );
401         
402     free( p_adec->p_block );
403     free( p_adec );
404 }
405 #define CLAMP( v, min, max ) \
406     if( (v) < (min) ) (v) = (min); \
407     if( (v) > (max) ) (v) = (max)
408
409 #define GetByte( v ) \
410     (v) = *p_buffer; p_buffer++;
411
412 #define GetWord( v ) \
413     (v) = *p_buffer; p_buffer++; \
414     (v) |= ( *p_buffer ) << 8; p_buffer++; \
415     if( (v)&0x8000 ) (v) -= 0x010000;
416
417 typedef struct adpcm_ms_channel_s
418 {
419     int i_idelta; 
420     int i_sample1, i_sample2;
421     int i_coeff1, i_coeff2;
422
423 } adpcm_ms_channel_t;
424
425
426 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
427                                int i_nibble )
428 {
429     int i_predictor;
430     int i_snibble;
431     /* expand sign */
432
433     i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
434     
435     i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 + 
436                     p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
437                   i_snibble * p_channel->i_idelta;
438
439     CLAMP( i_predictor, -32768, 32767 );
440
441     p_channel->i_sample2 = p_channel->i_sample1;
442     p_channel->i_sample1 = i_predictor;
443
444     p_channel->i_idelta = ( i_adaptation_table[i_nibble] * 
445                             p_channel->i_idelta ) / 256;
446     if( p_channel->i_idelta < 16 )
447     {
448         p_channel->i_idelta = 16;
449     }
450     return( i_predictor );
451 }
452     
453 static void DecodeAdpcmMs( adec_thread_t *p_adec,  
454                            aout_buffer_t *p_aout_buffer)
455 {
456     uint8_t            *p_buffer;
457     adpcm_ms_channel_t channel[2];
458     int i_nibbles;
459     uint16_t           *p_sample;
460     int b_stereo;
461     int i_block_predictor;
462     
463     p_buffer = p_adec->p_block;
464     b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
465
466     GetByte( i_block_predictor );
467     CLAMP( i_block_predictor, 0, 6 );
468     channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
469     channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
470
471     if( b_stereo )
472     {
473         GetByte( i_block_predictor );
474         CLAMP( i_block_predictor, 0, 6 );
475         channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
476         channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
477     }
478     GetWord( channel[0].i_idelta );
479     if( b_stereo )
480     {
481         GetWord( channel[1].i_idelta );
482     }
483     
484     GetWord( channel[0].i_sample1 );
485     if( b_stereo )
486     {
487         GetWord( channel[1].i_sample1 );
488     }
489
490     GetWord( channel[0].i_sample2 );
491     if( b_stereo )
492     {
493         GetWord( channel[1].i_sample2 );
494     }
495
496     p_sample = (int16_t*)p_aout_buffer->p_buffer;
497
498     if( b_stereo )
499     {
500         *p_sample = channel[0].i_sample2; p_sample++;
501         *p_sample = channel[1].i_sample2; p_sample++;
502         *p_sample = channel[0].i_sample1; p_sample++;
503         *p_sample = channel[1].i_sample1; p_sample++;
504     }
505     else
506     {
507         *p_sample = channel[0].i_sample2; p_sample++;
508         *p_sample = channel[0].i_sample1; p_sample++;
509     }
510
511     for( i_nibbles =  2 *( p_adec->i_block - 7 * p_adec->p_wf->nChannels );
512          i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
513     {
514         *p_sample = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
515         p_sample++;
516         
517         *p_sample = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0], 
518                                          (*p_buffer)&0x0f);
519         p_sample++;
520     }
521
522     
523 }
524
525 typedef struct adpcm_ima_wav_channel_s
526 {
527     int i_predictor;
528     int i_step_index;
529
530 } adpcm_ima_wav_channel_t;
531
532 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
533                                    int i_nibble )
534 {
535     int i_diff;
536
537     i_diff = i_step_table[p_channel->i_step_index] >> 3;
538     if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
539     if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
540     if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
541     if( i_nibble&0x08 )
542         p_channel->i_predictor -= i_diff;
543     else
544         p_channel->i_predictor += i_diff;
545
546     CLAMP( p_channel->i_predictor, -32768, 32767 );
547
548     p_channel->i_step_index += i_index_table[i_nibble];
549
550     CLAMP( p_channel->i_step_index, 0, 88 );
551
552     return( p_channel->i_predictor );
553 }
554
555 static void DecodeAdpcmImaWav( adec_thread_t *p_adec,  
556                                aout_buffer_t *p_aout_buffer)
557 {
558     uint8_t                 *p_buffer;
559     adpcm_ima_wav_channel_t channel[2];
560     int                     i_nibbles;
561     uint16_t                *p_sample;
562     int                     b_stereo;
563     
564     p_buffer = p_adec->p_block;
565     b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
566
567     GetWord( channel[0].i_predictor );
568     GetByte( channel[0].i_step_index );
569     CLAMP( channel[0].i_step_index, 0, 88 );
570     p_buffer++;
571
572     if( b_stereo )
573     {
574         GetWord( channel[1].i_predictor );
575         GetByte( channel[1].i_step_index );
576         CLAMP( channel[1].i_step_index, 0, 88 );
577         p_buffer++;
578     }
579     
580     p_sample = (int16_t*)p_aout_buffer->p_buffer;
581     if( b_stereo )
582     {
583         for( i_nibbles = 2 * (p_adec->i_block - 8); 
584              i_nibbles > 0; 
585              i_nibbles -= 16 )
586         {
587             int i;
588
589             for( i = 0; i < 4; i++ )
590             {
591                 p_sample[i * 4] = 
592                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
593                 p_sample[i * 4 + 2] =
594                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
595             }
596             p_buffer += 4;
597             
598             for( i = 0; i < 4; i++ )
599             {
600                 p_sample[i * 4 + 1] = 
601                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
602                 p_sample[i * 4 + 3] =
603                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
604             }
605             p_buffer += 4;
606             p_sample += 16;
607
608         }
609
610
611     }
612     else
613     {
614         for( i_nibbles = 2 * (p_adec->i_block - 4); 
615              i_nibbles > 0; 
616              i_nibbles -= 2, p_buffer++ )
617         {
618             *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
619             p_sample++;
620             *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
621             p_sample++;
622         }
623     }
624 }
625
626
627
628