]> git.sesse.net Git - vlc/blob - modules/codec/adpcm.c
* modules/codec/*: make sure all audio decoders set
[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.3 2002/12/30 17:28:31 gbazin 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->output_format.i_bytes_per_frame =
309         p_adec->p_wf->nChannels * 2;
310     p_adec->output_format.i_frame_length = 1;
311     p_adec->p_aout = NULL;
312     p_adec->p_aout_input = NULL;
313
314     /* **** Create a new audio output **** */
315     aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
316     p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
317                                         &p_adec->p_aout,
318                                         &p_adec->output_format );
319     if( !p_adec->p_aout_input )
320     {
321         msg_Err( p_adec->p_fifo, "cannot create aout" );
322         return( -1 );
323     }
324
325     /* Init the BitStream */
326     InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
327                    NULL, NULL );
328
329     return( 0 );
330 }
331
332
333 /*****************************************************************************
334  * DecodeThread: decodes a frame
335  *****************************************************************************/
336 static void DecodeThread( adec_thread_t *p_adec )
337 {
338     aout_buffer_t   *p_aout_buffer;
339
340     /* get pts */
341     CurrentPTS( &p_adec->bit_stream, &p_adec->pts, NULL );
342     /* gather block */
343     GetChunk( &p_adec->bit_stream,
344               p_adec->p_block,
345               p_adec->i_block );
346
347     /* get output buffer */
348     if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
349     {
350         aout_DateSet( &p_adec->date, p_adec->pts );
351     }
352     else if( !aout_DateGet( &p_adec->date ) )
353     {
354         return;
355     }
356
357     p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout, 
358                                        p_adec->p_aout_input,
359                                        p_adec->i_samplesperblock );
360     if( !p_aout_buffer )
361     {
362         msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
363         p_adec->p_fifo->b_error = 1;
364         return;
365     }
366     
367     p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
368     p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
369                                                   p_adec->i_samplesperblock );
370
371     /* decode */
372     
373     switch( p_adec->i_codec )
374     {
375         case ADPCM_IMA_QT:
376             break;
377         case ADPCM_IMA_WAV:
378             DecodeAdpcmImaWav( p_adec, p_aout_buffer );
379             break;
380         case ADPCM_MS:
381             DecodeAdpcmMs( p_adec, p_aout_buffer );
382             break;
383         default:
384             break;
385     }
386
387
388     /* **** Now we can output these samples **** */
389     aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
390 }
391
392
393 /*****************************************************************************
394  * EndThread : faad decoder thread destruction
395  *****************************************************************************/
396 static void EndThread (adec_thread_t *p_adec)
397 {
398     if( p_adec->p_aout_input )
399     {
400         aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
401     }
402
403     msg_Dbg( p_adec->p_fifo, "adpcm audio decoder closed" );
404         
405     free( p_adec->p_block );
406     free( p_adec );
407 }
408 #define CLAMP( v, min, max ) \
409     if( (v) < (min) ) (v) = (min); \
410     if( (v) > (max) ) (v) = (max)
411
412 #define GetByte( v ) \
413     (v) = *p_buffer; p_buffer++;
414
415 #define GetWord( v ) \
416     (v) = *p_buffer; p_buffer++; \
417     (v) |= ( *p_buffer ) << 8; p_buffer++; \
418     if( (v)&0x8000 ) (v) -= 0x010000;
419
420 typedef struct adpcm_ms_channel_s
421 {
422     int i_idelta; 
423     int i_sample1, i_sample2;
424     int i_coeff1, i_coeff2;
425
426 } adpcm_ms_channel_t;
427
428
429 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
430                                int i_nibble )
431 {
432     int i_predictor;
433     int i_snibble;
434     /* expand sign */
435
436     i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
437     
438     i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 + 
439                     p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
440                   i_snibble * p_channel->i_idelta;
441
442     CLAMP( i_predictor, -32768, 32767 );
443
444     p_channel->i_sample2 = p_channel->i_sample1;
445     p_channel->i_sample1 = i_predictor;
446
447     p_channel->i_idelta = ( i_adaptation_table[i_nibble] * 
448                             p_channel->i_idelta ) / 256;
449     if( p_channel->i_idelta < 16 )
450     {
451         p_channel->i_idelta = 16;
452     }
453     return( i_predictor );
454 }
455     
456 static void DecodeAdpcmMs( adec_thread_t *p_adec,  
457                            aout_buffer_t *p_aout_buffer)
458 {
459     uint8_t            *p_buffer;
460     adpcm_ms_channel_t channel[2];
461     int i_nibbles;
462     uint16_t           *p_sample;
463     int b_stereo;
464     int i_block_predictor;
465     
466     p_buffer = p_adec->p_block;
467     b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
468
469     GetByte( i_block_predictor );
470     CLAMP( i_block_predictor, 0, 6 );
471     channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
472     channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
473
474     if( b_stereo )
475     {
476         GetByte( i_block_predictor );
477         CLAMP( i_block_predictor, 0, 6 );
478         channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
479         channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
480     }
481     GetWord( channel[0].i_idelta );
482     if( b_stereo )
483     {
484         GetWord( channel[1].i_idelta );
485     }
486     
487     GetWord( channel[0].i_sample1 );
488     if( b_stereo )
489     {
490         GetWord( channel[1].i_sample1 );
491     }
492
493     GetWord( channel[0].i_sample2 );
494     if( b_stereo )
495     {
496         GetWord( channel[1].i_sample2 );
497     }
498
499     p_sample = (int16_t*)p_aout_buffer->p_buffer;
500
501     if( b_stereo )
502     {
503         *p_sample = channel[0].i_sample2; p_sample++;
504         *p_sample = channel[1].i_sample2; p_sample++;
505         *p_sample = channel[0].i_sample1; p_sample++;
506         *p_sample = channel[1].i_sample1; p_sample++;
507     }
508     else
509     {
510         *p_sample = channel[0].i_sample2; p_sample++;
511         *p_sample = channel[0].i_sample1; p_sample++;
512     }
513
514     for( i_nibbles =  2 *( p_adec->i_block - 7 * p_adec->p_wf->nChannels );
515          i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
516     {
517         *p_sample = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
518         p_sample++;
519         
520         *p_sample = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0], 
521                                          (*p_buffer)&0x0f);
522         p_sample++;
523     }
524
525     
526 }
527
528 typedef struct adpcm_ima_wav_channel_s
529 {
530     int i_predictor;
531     int i_step_index;
532
533 } adpcm_ima_wav_channel_t;
534
535 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
536                                    int i_nibble )
537 {
538     int i_diff;
539
540     i_diff = i_step_table[p_channel->i_step_index] >> 3;
541     if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
542     if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
543     if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
544     if( i_nibble&0x08 )
545         p_channel->i_predictor -= i_diff;
546     else
547         p_channel->i_predictor += i_diff;
548
549     CLAMP( p_channel->i_predictor, -32768, 32767 );
550
551     p_channel->i_step_index += i_index_table[i_nibble];
552
553     CLAMP( p_channel->i_step_index, 0, 88 );
554
555     return( p_channel->i_predictor );
556 }
557
558 static void DecodeAdpcmImaWav( adec_thread_t *p_adec,  
559                                aout_buffer_t *p_aout_buffer)
560 {
561     uint8_t                 *p_buffer;
562     adpcm_ima_wav_channel_t channel[2];
563     int                     i_nibbles;
564     uint16_t                *p_sample;
565     int                     b_stereo;
566     
567     p_buffer = p_adec->p_block;
568     b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
569
570     GetWord( channel[0].i_predictor );
571     GetByte( channel[0].i_step_index );
572     CLAMP( channel[0].i_step_index, 0, 88 );
573     p_buffer++;
574
575     if( b_stereo )
576     {
577         GetWord( channel[1].i_predictor );
578         GetByte( channel[1].i_step_index );
579         CLAMP( channel[1].i_step_index, 0, 88 );
580         p_buffer++;
581     }
582     
583     p_sample = (int16_t*)p_aout_buffer->p_buffer;
584     if( b_stereo )
585     {
586         for( i_nibbles = 2 * (p_adec->i_block - 8); 
587              i_nibbles > 0; 
588              i_nibbles -= 16 )
589         {
590             int i;
591
592             for( i = 0; i < 4; i++ )
593             {
594                 p_sample[i * 4] = 
595                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
596                 p_sample[i * 4 + 2] =
597                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
598             }
599             p_buffer += 4;
600             
601             for( i = 0; i < 4; i++ )
602             {
603                 p_sample[i * 4 + 1] = 
604                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
605                 p_sample[i * 4 + 3] =
606                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
607             }
608             p_buffer += 4;
609             p_sample += 16;
610
611         }
612
613
614     }
615     else
616     {
617         for( i_nibbles = 2 * (p_adec->i_block - 4); 
618              i_nibbles > 0; 
619              i_nibbles -= 2, p_buffer++ )
620         {
621             *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
622             p_sample++;
623             *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
624             p_sample++;
625         }
626     }
627 }
628
629
630
631