4 * This file is responsible for decoding Microsoft ADPCM data.
5 * Details about the data format can be found here:
6 * http://www.pcisys.net/~melanson/codecs/
8 * Copyright (c) 2002 Mike Melanson
10 * This file is part of MPlayer.
12 * MPlayer is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * MPlayer is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include "libavutil/common.h"
33 #include "ffmpeg_files/intreadwrite.h"
35 #include "ad_internal.h"
37 static const ad_info_t info
=
39 "MS ADPCM audio decoder",
48 static const int ms_adapt_table
[] =
50 230, 230, 230, 230, 307, 409, 512, 614,
51 768, 614, 512, 409, 307, 230, 230, 230
54 static const uint8_t ms_adapt_coeff1
[] =
56 64, 128, 0, 48, 60, 115, 98
59 static const int8_t ms_adapt_coeff2
[] =
61 0, -64, 0, 16, 0, -52, -58
64 #define MS_ADPCM_PREAMBLE_SIZE 6
66 #define LE_16(x) ((int16_t)AV_RL16(x))
68 // clamp a number between 0 and 88
69 #define CLAMP_0_TO_88(x) x = av_clip(x, 0, 88);
70 // clamp a number within a signed 16-bit range
71 #define CLAMP_S16(x) x = av_clip_int16(x);
72 // clamp a number above 16
73 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16;
74 // sign extend a 4-bit value
75 #define SE_4BIT(x) if (x & 0x8) x -= 0x10;
77 static int preinit(sh_audio_t
*sh_audio
)
79 sh_audio
->audio_out_minsize
= sh_audio
->wf
->nBlockAlign
* 4;
80 sh_audio
->ds
->ss_div
=
81 (sh_audio
->wf
->nBlockAlign
- MS_ADPCM_PREAMBLE_SIZE
) * 2;
82 sh_audio
->audio_in_minsize
=
83 sh_audio
->ds
->ss_mul
= sh_audio
->wf
->nBlockAlign
;
87 static int init(sh_audio_t
*sh_audio
)
89 sh_audio
->channels
=sh_audio
->wf
->nChannels
;
90 sh_audio
->samplerate
=sh_audio
->wf
->nSamplesPerSec
;
91 sh_audio
->i_bps
= sh_audio
->wf
->nBlockAlign
*
92 (sh_audio
->channels
*sh_audio
->samplerate
) / sh_audio
->ds
->ss_div
;
93 sh_audio
->samplesize
=2;
98 static void uninit(sh_audio_t
*sh_audio
)
102 static int control(sh_audio_t
*sh_audio
,int cmd
,void* arg
, ...)
104 if(cmd
==ADCTRL_SKIP_FRAME
){
105 demux_read_data(sh_audio
->ds
, sh_audio
->a_in_buffer
,sh_audio
->ds
->ss_mul
);
108 return CONTROL_UNKNOWN
;
111 static inline int check_coeff(uint8_t c
) {
113 mp_msg(MSGT_DECAUDIO
, MSGL_WARN
,
114 "MS ADPCM: coefficient (%d) out of range (should be [0..6])\n",
121 static int ms_adpcm_decode_block(unsigned short *output
, unsigned char *input
,
122 int channels
, int block_size
)
124 int current_channel
= 0;
133 int upper_nibble
= 1;
135 int snibble
; // signed nibble
138 if (channels
!= 1) channels
= 2;
139 if (block_size
< 7 * channels
)
142 // fetch the header information, in stereo if both channels are present
143 coeff_idx
= check_coeff(input
[stream_ptr
]);
144 coeff1
[0] = ms_adapt_coeff1
[coeff_idx
];
145 coeff2
[0] = ms_adapt_coeff2
[coeff_idx
];
149 coeff_idx
= check_coeff(input
[stream_ptr
]);
150 coeff1
[1] = ms_adapt_coeff1
[coeff_idx
];
151 coeff2
[1] = ms_adapt_coeff2
[coeff_idx
];
155 idelta
[0] = LE_16(&input
[stream_ptr
]);
159 idelta
[1] = LE_16(&input
[stream_ptr
]);
163 sample1
[0] = LE_16(&input
[stream_ptr
]);
167 sample1
[1] = LE_16(&input
[stream_ptr
]);
171 sample2
[0] = LE_16(&input
[stream_ptr
]);
175 sample2
[1] = LE_16(&input
[stream_ptr
]);
181 output
[out_ptr
++] = sample2
[0];
182 output
[out_ptr
++] = sample1
[0];
184 output
[out_ptr
++] = sample2
[0];
185 output
[out_ptr
++] = sample2
[1];
186 output
[out_ptr
++] = sample1
[0];
187 output
[out_ptr
++] = sample1
[1];
190 while (stream_ptr
< block_size
)
192 // get the next nibble
194 nibble
= snibble
= input
[stream_ptr
] >> 4;
196 nibble
= snibble
= input
[stream_ptr
++] & 0x0F;
200 // should this really be a division and not a shift?
201 // coefficients were originally scaled by for, which might have
202 // been an optimization for 8-bit CPUs _if_ a shift is correct
204 ((sample1
[current_channel
] * coeff1
[current_channel
]) +
205 (sample2
[current_channel
] * coeff2
[current_channel
])) / 64) +
206 (snibble
* idelta
[current_channel
]);
207 CLAMP_S16(predictor
);
208 sample2
[current_channel
] = sample1
[current_channel
];
209 sample1
[current_channel
] = predictor
;
210 output
[out_ptr
++] = predictor
;
212 // compute the next adaptive scale factor (a.k.a. the variable idelta)
213 idelta
[current_channel
] =
214 (ms_adapt_table
[nibble
] * idelta
[current_channel
]) / 256;
215 CLAMP_ABOVE_16(idelta
[current_channel
]);
217 // toggle the channel
218 current_channel
^= channels
- 1;
221 return (block_size
- (MS_ADPCM_PREAMBLE_SIZE
* channels
)) * 2;
224 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
227 if (demux_read_data(sh_audio
->ds
, sh_audio
->a_in_buffer
,
228 sh_audio
->ds
->ss_mul
) !=
229 sh_audio
->ds
->ss_mul
)
232 res
= ms_adpcm_decode_block(
233 (unsigned short*)buf
, sh_audio
->a_in_buffer
,
234 sh_audio
->wf
->nChannels
, sh_audio
->wf
->nBlockAlign
);
235 return res
< 0 ? res
: 2 * res
;