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