00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00031 #include "avcodec.h"
00032 #define ALT_BITSTREAM_READER_LE
00033 #include "get_bits.h"
00034 #include "dsputil.h"
00035 #include "fft.h"
00036 #include "fmtconvert.h"
00037 #include "libavutil/intfloat_readwrite.h"
00038
00039 extern const uint16_t ff_wma_critical_freqs[25];
00040
00041 #define MAX_CHANNELS 2
00042 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
00043
00044 typedef struct {
00045 GetBitContext gb;
00046 DSPContext dsp;
00047 FmtConvertContext fmt_conv;
00048 int version_b;
00049 int first;
00050 int channels;
00051 int frame_len;
00052 int overlap_len;
00053 int block_size;
00054 int num_bands;
00055 unsigned int *bands;
00056 float root;
00057 DECLARE_ALIGNED(16, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
00058 DECLARE_ALIGNED(16, short, previous)[BINK_BLOCK_MAX_SIZE / 16];
00059 float *coeffs_ptr[MAX_CHANNELS];
00060 union {
00061 RDFTContext rdft;
00062 DCTContext dct;
00063 } trans;
00064 } BinkAudioContext;
00065
00066
00067 static av_cold int decode_init(AVCodecContext *avctx)
00068 {
00069 BinkAudioContext *s = avctx->priv_data;
00070 int sample_rate = avctx->sample_rate;
00071 int sample_rate_half;
00072 int i;
00073 int frame_len_bits;
00074
00075 dsputil_init(&s->dsp, avctx);
00076 ff_fmt_convert_init(&s->fmt_conv, avctx);
00077
00078
00079 if (avctx->sample_rate < 22050) {
00080 frame_len_bits = 9;
00081 } else if (avctx->sample_rate < 44100) {
00082 frame_len_bits = 10;
00083 } else {
00084 frame_len_bits = 11;
00085 }
00086
00087 if (avctx->channels > MAX_CHANNELS) {
00088 av_log(avctx, AV_LOG_ERROR, "too many channels: %d\n", avctx->channels);
00089 return -1;
00090 }
00091
00092 s->version_b = avctx->codec_tag == MKTAG('B','I','K','b');
00093
00094 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
00095
00096 sample_rate *= avctx->channels;
00097 s->channels = 1;
00098 if (!s->version_b)
00099 frame_len_bits += av_log2(avctx->channels);
00100 } else {
00101 s->channels = avctx->channels;
00102 }
00103
00104 s->frame_len = 1 << frame_len_bits;
00105 s->overlap_len = s->frame_len / 16;
00106 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
00107 sample_rate_half = (sample_rate + 1) / 2;
00108 s->root = 2.0 / sqrt(s->frame_len);
00109
00110
00111 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
00112 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
00113 break;
00114
00115 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
00116 if (!s->bands)
00117 return AVERROR(ENOMEM);
00118
00119
00120 s->bands[0] = 2;
00121 for (i = 1; i < s->num_bands; i++)
00122 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
00123 s->bands[s->num_bands] = s->frame_len;
00124
00125 s->first = 1;
00126 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00127
00128 for (i = 0; i < s->channels; i++)
00129 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
00130
00131 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
00132 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
00133 else if (CONFIG_BINKAUDIO_DCT_DECODER)
00134 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
00135 else
00136 return -1;
00137
00138 return 0;
00139 }
00140
00141 static float get_float(GetBitContext *gb)
00142 {
00143 int power = get_bits(gb, 5);
00144 float f = ldexpf(get_bits_long(gb, 23), power - 23);
00145 if (get_bits1(gb))
00146 f = -f;
00147 return f;
00148 }
00149
00150 static const uint8_t rle_length_tab[16] = {
00151 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
00152 };
00153
00158 static void decode_block(BinkAudioContext *s, short *out, int use_dct)
00159 {
00160 int ch, i, j, k;
00161 float q, quant[25];
00162 int width, coeff;
00163 GetBitContext *gb = &s->gb;
00164
00165 if (use_dct)
00166 skip_bits(gb, 2);
00167
00168 for (ch = 0; ch < s->channels; ch++) {
00169 FFTSample *coeffs = s->coeffs_ptr[ch];
00170 if (s->version_b) {
00171 coeffs[0] = av_int2flt(get_bits(gb, 32)) * s->root;
00172 coeffs[1] = av_int2flt(get_bits(gb, 32)) * s->root;
00173 } else {
00174 coeffs[0] = get_float(gb) * s->root;
00175 coeffs[1] = get_float(gb) * s->root;
00176 }
00177
00178 for (i = 0; i < s->num_bands; i++) {
00179
00180 int value = get_bits(gb, 8);
00181 quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
00182 }
00183
00184 k = 0;
00185 q = quant[0];
00186
00187
00188 i = 2;
00189 while (i < s->frame_len) {
00190 if (s->version_b) {
00191 j = i + 16;
00192 } else if (get_bits1(gb)) {
00193 j = i + rle_length_tab[get_bits(gb, 4)] * 8;
00194 } else {
00195 j = i + 8;
00196 }
00197
00198 j = FFMIN(j, s->frame_len);
00199
00200 width = get_bits(gb, 4);
00201 if (width == 0) {
00202 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
00203 i = j;
00204 while (s->bands[k] < i)
00205 q = quant[k++];
00206 } else {
00207 while (i < j) {
00208 if (s->bands[k] == i)
00209 q = quant[k++];
00210 coeff = get_bits(gb, width);
00211 if (coeff) {
00212 if (get_bits1(gb))
00213 coeffs[i] = -q * coeff;
00214 else
00215 coeffs[i] = q * coeff;
00216 } else {
00217 coeffs[i] = 0.0f;
00218 }
00219 i++;
00220 }
00221 }
00222 }
00223
00224 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
00225 coeffs[0] /= 0.5;
00226 ff_dct_calc (&s->trans.dct, coeffs);
00227 s->dsp.vector_fmul_scalar(coeffs, coeffs, s->frame_len / 2, s->frame_len);
00228 }
00229 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
00230 ff_rdft_calc(&s->trans.rdft, coeffs);
00231 }
00232
00233 s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
00234 s->frame_len, s->channels);
00235
00236 if (!s->first) {
00237 int count = s->overlap_len * s->channels;
00238 int shift = av_log2(count);
00239 for (i = 0; i < count; i++) {
00240 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
00241 }
00242 }
00243
00244 memcpy(s->previous, out + s->block_size,
00245 s->overlap_len * s->channels * sizeof(*out));
00246
00247 s->first = 0;
00248 }
00249
00250 static av_cold int decode_end(AVCodecContext *avctx)
00251 {
00252 BinkAudioContext * s = avctx->priv_data;
00253 av_freep(&s->bands);
00254 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
00255 ff_rdft_end(&s->trans.rdft);
00256 else if (CONFIG_BINKAUDIO_DCT_DECODER)
00257 ff_dct_end(&s->trans.dct);
00258 return 0;
00259 }
00260
00261 static void get_bits_align32(GetBitContext *s)
00262 {
00263 int n = (-get_bits_count(s)) & 31;
00264 if (n) skip_bits(s, n);
00265 }
00266
00267 static int decode_frame(AVCodecContext *avctx,
00268 void *data, int *data_size,
00269 AVPacket *avpkt)
00270 {
00271 BinkAudioContext *s = avctx->priv_data;
00272 const uint8_t *buf = avpkt->data;
00273 int buf_size = avpkt->size;
00274 short *samples = data;
00275 short *samples_end = (short*)((uint8_t*)data + *data_size);
00276 int reported_size;
00277 GetBitContext *gb = &s->gb;
00278
00279 init_get_bits(gb, buf, buf_size * 8);
00280
00281 reported_size = get_bits_long(gb, 32);
00282 while (get_bits_count(gb) / 8 < buf_size &&
00283 samples + s->block_size <= samples_end) {
00284 decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
00285 samples += s->block_size;
00286 get_bits_align32(gb);
00287 }
00288
00289 *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
00290 return buf_size;
00291 }
00292
00293 AVCodec ff_binkaudio_rdft_decoder = {
00294 "binkaudio_rdft",
00295 AVMEDIA_TYPE_AUDIO,
00296 CODEC_ID_BINKAUDIO_RDFT,
00297 sizeof(BinkAudioContext),
00298 decode_init,
00299 NULL,
00300 decode_end,
00301 decode_frame,
00302 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
00303 };
00304
00305 AVCodec ff_binkaudio_dct_decoder = {
00306 "binkaudio_dct",
00307 AVMEDIA_TYPE_AUDIO,
00308 CODEC_ID_BINKAUDIO_DCT,
00309 sizeof(BinkAudioContext),
00310 decode_init,
00311 NULL,
00312 decode_end,
00313 decode_frame,
00314 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
00315 };