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 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.h"
00039
00040 extern const uint16_t ff_wma_critical_freqs[25];
00041
00042 static float quant_table[96];
00043
00044 #define MAX_CHANNELS 2
00045 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
00046
00047 typedef struct {
00048 AVFrame frame;
00049 GetBitContext gb;
00050 int version_b;
00051 int first;
00052 int channels;
00053 int frame_len;
00054 int overlap_len;
00055 int block_size;
00056 int num_bands;
00057 unsigned int *bands;
00058 float root;
00059 DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
00060 float previous[MAX_CHANNELS][BINK_BLOCK_MAX_SIZE / 16];
00061 uint8_t *packet_buffer;
00062 union {
00063 RDFTContext rdft;
00064 DCTContext dct;
00065 } trans;
00066 } BinkAudioContext;
00067
00068
00069 static av_cold int decode_init(AVCodecContext *avctx)
00070 {
00071 BinkAudioContext *s = avctx->priv_data;
00072 int sample_rate = avctx->sample_rate;
00073 int sample_rate_half;
00074 int i;
00075 int frame_len_bits;
00076
00077
00078 if (avctx->sample_rate < 22050) {
00079 frame_len_bits = 9;
00080 } else if (avctx->sample_rate < 44100) {
00081 frame_len_bits = 10;
00082 } else {
00083 frame_len_bits = 11;
00084 }
00085
00086 if (avctx->channels < 1 || avctx->channels > MAX_CHANNELS) {
00087 av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", avctx->channels);
00088 return AVERROR_INVALIDDATA;
00089 }
00090
00091 s->version_b = avctx->extradata_size >= 4 && avctx->extradata[3] == 'b';
00092
00093 if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT) {
00094
00095 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
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 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
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 if (avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
00110 s->root = 2.0 / (sqrt(s->frame_len) * 32768.0);
00111 else
00112 s->root = s->frame_len / (sqrt(s->frame_len) * 32768.0);
00113 for (i = 0; i < 96; i++) {
00114
00115 quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
00116 }
00117
00118
00119 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
00120 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
00121 break;
00122
00123 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
00124 if (!s->bands)
00125 return AVERROR(ENOMEM);
00126
00127
00128 s->bands[0] = 2;
00129 for (i = 1; i < s->num_bands; i++)
00130 s->bands[i] = (ff_wma_critical_freqs[i - 1] * s->frame_len / sample_rate_half) & ~1;
00131 s->bands[s->num_bands] = s->frame_len;
00132
00133 s->first = 1;
00134
00135 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
00136 ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
00137 else if (CONFIG_BINKAUDIO_DCT_DECODER)
00138 ff_dct_init(&s->trans.dct, frame_len_bits, DCT_III);
00139 else
00140 return -1;
00141
00142 avcodec_get_frame_defaults(&s->frame);
00143 avctx->coded_frame = &s->frame;
00144
00145 return 0;
00146 }
00147
00148 static float get_float(GetBitContext *gb)
00149 {
00150 int power = get_bits(gb, 5);
00151 float f = ldexpf(get_bits_long(gb, 23), power - 23);
00152 if (get_bits1(gb))
00153 f = -f;
00154 return f;
00155 }
00156
00157 static const uint8_t rle_length_tab[16] = {
00158 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
00159 };
00160
00166 static int decode_block(BinkAudioContext *s, float **out, int use_dct)
00167 {
00168 int ch, i, j, k;
00169 float q, quant[25];
00170 int width, coeff;
00171 GetBitContext *gb = &s->gb;
00172
00173 if (use_dct)
00174 skip_bits(gb, 2);
00175
00176 for (ch = 0; ch < s->channels; ch++) {
00177 FFTSample *coeffs = out[ch];
00178
00179 if (s->version_b) {
00180 if (get_bits_left(gb) < 64)
00181 return AVERROR_INVALIDDATA;
00182 coeffs[0] = av_int2float(get_bits_long(gb, 32)) * s->root;
00183 coeffs[1] = av_int2float(get_bits_long(gb, 32)) * s->root;
00184 } else {
00185 if (get_bits_left(gb) < 58)
00186 return AVERROR_INVALIDDATA;
00187 coeffs[0] = get_float(gb) * s->root;
00188 coeffs[1] = get_float(gb) * s->root;
00189 }
00190
00191 if (get_bits_left(gb) < s->num_bands * 8)
00192 return AVERROR_INVALIDDATA;
00193 for (i = 0; i < s->num_bands; i++) {
00194 int value = get_bits(gb, 8);
00195 quant[i] = quant_table[FFMIN(value, 95)];
00196 }
00197
00198 k = 0;
00199 q = quant[0];
00200
00201
00202 i = 2;
00203 while (i < s->frame_len) {
00204 if (s->version_b) {
00205 j = i + 16;
00206 } else {
00207 int v = get_bits1(gb);
00208 if (v) {
00209 v = get_bits(gb, 4);
00210 j = i + rle_length_tab[v] * 8;
00211 } else {
00212 j = i + 8;
00213 }
00214 }
00215
00216 j = FFMIN(j, s->frame_len);
00217
00218 width = get_bits(gb, 4);
00219 if (width == 0) {
00220 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
00221 i = j;
00222 while (s->bands[k] < i)
00223 q = quant[k++];
00224 } else {
00225 while (i < j) {
00226 if (s->bands[k] == i)
00227 q = quant[k++];
00228 coeff = get_bits(gb, width);
00229 if (coeff) {
00230 int v;
00231 v = get_bits1(gb);
00232 if (v)
00233 coeffs[i] = -q * coeff;
00234 else
00235 coeffs[i] = q * coeff;
00236 } else {
00237 coeffs[i] = 0.0f;
00238 }
00239 i++;
00240 }
00241 }
00242 }
00243
00244 if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {
00245 coeffs[0] /= 0.5;
00246 s->trans.dct.dct_calc(&s->trans.dct, coeffs);
00247 }
00248 else if (CONFIG_BINKAUDIO_RDFT_DECODER)
00249 s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
00250 }
00251
00252 for (ch = 0; ch < s->channels; ch++) {
00253 int j;
00254 int count = s->overlap_len * s->channels;
00255 if (!s->first) {
00256 j = ch;
00257 for (i = 0; i < s->overlap_len; i++, j += s->channels)
00258 out[ch][i] = (s->previous[ch][i] * (count - j) +
00259 out[ch][i] * j) / count;
00260 }
00261 memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],
00262 s->overlap_len * sizeof(*s->previous[ch]));
00263 }
00264
00265 s->first = 0;
00266
00267 return 0;
00268 }
00269
00270 static av_cold int decode_end(AVCodecContext *avctx)
00271 {
00272 BinkAudioContext * s = avctx->priv_data;
00273 av_freep(&s->bands);
00274 av_freep(&s->packet_buffer);
00275 if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == AV_CODEC_ID_BINKAUDIO_RDFT)
00276 ff_rdft_end(&s->trans.rdft);
00277 else if (CONFIG_BINKAUDIO_DCT_DECODER)
00278 ff_dct_end(&s->trans.dct);
00279
00280 return 0;
00281 }
00282
00283 static void get_bits_align32(GetBitContext *s)
00284 {
00285 int n = (-get_bits_count(s)) & 31;
00286 if (n) skip_bits(s, n);
00287 }
00288
00289 static int decode_frame(AVCodecContext *avctx, void *data,
00290 int *got_frame_ptr, AVPacket *avpkt)
00291 {
00292 BinkAudioContext *s = avctx->priv_data;
00293 GetBitContext *gb = &s->gb;
00294 int ret, consumed = 0;
00295
00296 if (!get_bits_left(gb)) {
00297 uint8_t *buf;
00298
00299 if (!avpkt->size) {
00300 *got_frame_ptr = 0;
00301 return 0;
00302 }
00303 if (avpkt->size < 4) {
00304 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
00305 return AVERROR_INVALIDDATA;
00306 }
00307 buf = av_realloc(s->packet_buffer, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
00308 if (!buf)
00309 return AVERROR(ENOMEM);
00310 s->packet_buffer = buf;
00311 memcpy(s->packet_buffer, avpkt->data, avpkt->size);
00312 init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
00313 consumed = avpkt->size;
00314
00315
00316 skip_bits_long(gb, 32);
00317 }
00318
00319
00320 s->frame.nb_samples = s->frame_len;
00321 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00322 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00323 return ret;
00324 }
00325
00326 if (decode_block(s, (float **)s->frame.extended_data,
00327 avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
00328 av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
00329 return AVERROR_INVALIDDATA;
00330 }
00331 get_bits_align32(gb);
00332
00333 s->frame.nb_samples = s->block_size / avctx->channels;
00334 *got_frame_ptr = 1;
00335 *(AVFrame *)data = s->frame;
00336
00337 return consumed;
00338 }
00339
00340 AVCodec ff_binkaudio_rdft_decoder = {
00341 .name = "binkaudio_rdft",
00342 .type = AVMEDIA_TYPE_AUDIO,
00343 .id = AV_CODEC_ID_BINKAUDIO_RDFT,
00344 .priv_data_size = sizeof(BinkAudioContext),
00345 .init = decode_init,
00346 .close = decode_end,
00347 .decode = decode_frame,
00348 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00349 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
00350 };
00351
00352 AVCodec ff_binkaudio_dct_decoder = {
00353 .name = "binkaudio_dct",
00354 .type = AVMEDIA_TYPE_AUDIO,
00355 .id = AV_CODEC_ID_BINKAUDIO_DCT,
00356 .priv_data_size = sizeof(BinkAudioContext),
00357 .init = decode_init,
00358 .close = decode_end,
00359 .decode = decode_frame,
00360 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00361 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
00362 };