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