]> git.sesse.net Git - vlc/blob - modules/codec/adpcm.c
* adpcm.c: add adpcm decoding support (Now just ms and ima adpcm from
[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.1 2002/12/03 17:00:16 fenrir 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     }
382
383
384     /* **** Now we can output these samples **** */
385     aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
386 }
387
388
389 /*****************************************************************************
390  * EndThread : faad decoder thread destruction
391  *****************************************************************************/
392 static void EndThread (adec_thread_t *p_adec)
393 {
394     if( p_adec->p_aout_input )
395     {
396         aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
397     }
398
399     msg_Dbg( p_adec->p_fifo, "adpcm audio decoder closed" );
400         
401     free( p_adec->p_block );
402     free( p_adec );
403 }
404 #define CLAMP( v, min, max ) \
405     if( (v) < (min) ) (v) = (min); \
406     if( (v) > (max) ) (v) = (max)
407
408 #define GetByte( v ) \
409     (v) = *p_buffer; p_buffer++;
410
411 #define GetWord( v ) \
412     (v) = *p_buffer; p_buffer++; \
413     (v) |= ( *p_buffer ) << 8; p_buffer++; \
414     if( (v)&0x8000 ) (v) -= 0x010000;
415
416 typedef struct adpcm_ms_channel_s
417 {
418     int i_idelta; 
419     int i_sample1, i_sample2;
420     int i_coeff1, i_coeff2;
421
422 } adpcm_ms_channel_t;
423
424
425 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
426                                int i_nibble )
427 {
428     int i_predictor;
429     int i_snibble;
430     /* expand sign */
431
432     i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
433     
434     i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 + 
435                     p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
436                   i_snibble * p_channel->i_idelta;
437
438     CLAMP( i_predictor, -32768, 32767 );
439
440     p_channel->i_sample2 = p_channel->i_sample1;
441     p_channel->i_sample1 = i_predictor;
442
443     p_channel->i_idelta = ( i_adaptation_table[i_nibble] * 
444                             p_channel->i_idelta ) / 256;
445     if( p_channel->i_idelta < 16 )
446     {
447         p_channel->i_idelta = 16;
448     }
449     return( i_predictor );
450 }
451     
452 static void DecodeAdpcmMs( adec_thread_t *p_adec,  
453                            aout_buffer_t *p_aout_buffer)
454 {
455     uint8_t            *p_buffer;
456     adpcm_ms_channel_t channel[2];
457     int i_nibbles;
458     uint16_t           *p_sample;
459     int b_stereo;
460     int i_block_predictor;
461     
462     p_buffer = p_adec->p_block;
463     b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
464
465     GetByte( i_block_predictor );
466     CLAMP( i_block_predictor, 0, 6 );
467     channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
468     channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
469
470     if( b_stereo )
471     {
472         GetByte( i_block_predictor );
473         CLAMP( i_block_predictor, 0, 6 );
474         channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
475         channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
476     }
477     GetWord( channel[0].i_idelta );
478     if( b_stereo )
479     {
480         GetWord( channel[1].i_idelta );
481     }
482     
483     GetWord( channel[0].i_sample1 );
484     if( b_stereo )
485     {
486         GetWord( channel[1].i_sample1 );
487     }
488
489     GetWord( channel[0].i_sample2 );
490     if( b_stereo )
491     {
492         GetWord( channel[1].i_sample2 );
493     }
494
495     p_sample = (int16_t*)p_aout_buffer->p_buffer;
496
497     if( b_stereo )
498     {
499         *p_sample = channel[0].i_sample2; p_sample++;
500         *p_sample = channel[1].i_sample2; p_sample++;
501         *p_sample = channel[0].i_sample1; p_sample++;
502         *p_sample = channel[1].i_sample1; p_sample++;
503     }
504     else
505     {
506         *p_sample = channel[0].i_sample2; p_sample++;
507         *p_sample = channel[0].i_sample1; p_sample++;
508     }
509
510     for( i_nibbles =  2 *( p_adec->i_block - 7 * p_adec->p_wf->nChannels );
511          i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
512     {
513         *p_sample = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
514         p_sample++;
515         
516         *p_sample = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0], 
517                                          (*p_buffer)&0x0f);
518         p_sample++;
519     }
520
521     
522 }
523
524 typedef struct adpcm_ima_wav_channel_s
525 {
526     int i_predictor;
527     int i_step_index;
528
529 } adpcm_ima_wav_channel_t;
530
531 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
532                                    int i_nibble )
533 {
534     int i_diff;
535
536     i_diff = i_step_table[p_channel->i_step_index] >> 3;
537     if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
538     if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
539     if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
540     if( i_nibble&0x08 )
541         p_channel->i_predictor -= i_diff;
542     else
543         p_channel->i_predictor += i_diff;
544
545     CLAMP( p_channel->i_predictor, -32768, 32767 );
546
547     p_channel->i_step_index += i_index_table[i_nibble];
548
549     CLAMP( p_channel->i_step_index, 0, 88 );
550
551     return( p_channel->i_predictor );
552 }
553
554 static void DecodeAdpcmImaWav( adec_thread_t *p_adec,  
555                                aout_buffer_t *p_aout_buffer)
556 {
557     uint8_t                 *p_buffer;
558     adpcm_ima_wav_channel_t channel[2];
559     int                     i_nibbles;
560     uint16_t                *p_sample;
561     int                     b_stereo;
562     
563     p_buffer = p_adec->p_block;
564     b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
565
566     GetWord( channel[0].i_predictor );
567     GetByte( channel[0].i_step_index );
568     CLAMP( channel[0].i_step_index, 0, 88 );
569     p_buffer++;
570
571     if( b_stereo )
572     {
573         GetWord( channel[1].i_predictor );
574         GetByte( channel[1].i_step_index );
575         CLAMP( channel[1].i_step_index, 0, 88 );
576         p_buffer++;
577     }
578     
579     p_sample = (int16_t*)p_aout_buffer->p_buffer;
580     if( b_stereo )
581     {
582         for( i_nibbles = 2 * (p_adec->i_block - 8); 
583              i_nibbles > 0; 
584              i_nibbles -= 16 )
585         {
586             int i;
587
588             for( i = 0; i < 4; i++ )
589             {
590                 p_sample[i * 4] = 
591                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
592                 p_sample[i * 4 + 2] =
593                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
594             }
595             p_buffer += 4;
596             
597             for( i = 0; i < 4; i++ )
598             {
599                 p_sample[i * 4 + 1] = 
600                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
601                 p_sample[i * 4 + 3] =
602                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
603             }
604             p_buffer += 4;
605             p_sample += 16;
606
607         }
608
609
610     }
611     else
612     {
613         for( i_nibbles = 2 * (p_adec->i_block - 4); 
614              i_nibbles > 0; 
615              i_nibbles -= 2, p_buffer++ )
616         {
617             *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
618             p_sample++;
619             *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
620             p_sample++;
621         }
622     }
623 }
624
625
626
627