annotate binkaudio.c @ 11154:c11bcf7e3e6b libavcodec

Move CODEC_FLAG_GRAY check to outer loop
author conrad
date Sat, 13 Feb 2010 18:59:25 +0000
parents 28f00789adcd
children 359c8ba0698e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11067
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
1 /*
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
2 * Bink Audio decoder
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
3 * Copyright (c) 2007-2010 Peter Ross (pross@xvid.org)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
4 * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
5 *
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
6 * This file is part of FFmpeg.
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
7 *
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
12 *
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
16 * Lesser General Public License for more details.
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
17 *
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
21 */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
22
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
23 /**
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
24 * @file libavcodec/binkaudio.c
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
25 * Bink Audio decoder
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
26 *
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
27 * Technical details here:
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
28 * http://wiki.multimedia.cx/index.php?title=Bink_Audio
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
29 */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
30
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
31 #include "avcodec.h"
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
32 #define ALT_BITSTREAM_READER_LE
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
33 #include "get_bits.h"
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
34 #include "dsputil.h"
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
35 extern const uint16_t ff_wma_critical_freqs[25];
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
36
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
37 #define MAX_CHANNELS 2
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
38 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
39
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
40 typedef struct {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
41 AVCodecContext *avctx;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
42 GetBitContext gb;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
43 DSPContext dsp;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
44 int first;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
45 int channels;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
46 int frame_len; ///< transform size (samples)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
47 int overlap_len; ///< overlap size (samples)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
48 int block_size;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
49 int num_bands;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
50 unsigned int *bands;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
51 float root;
11069
28f00789adcd Update binkaudio to new DECLARE_ALIGNED syntax
daniel
parents: 11067
diff changeset
52 DECLARE_ALIGNED_16(FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
28f00789adcd Update binkaudio to new DECLARE_ALIGNED syntax
daniel
parents: 11067
diff changeset
53 DECLARE_ALIGNED_16(short, previous)[BINK_BLOCK_MAX_SIZE / 16]; ///< coeffs from previous audio block
11067
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
54 float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
55 union {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
56 RDFTContext rdft;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
57 DCTContext dct;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
58 } trans;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
59 } BinkAudioContext;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
60
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
61
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
62 static av_cold int decode_init(AVCodecContext *avctx)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
63 {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
64 BinkAudioContext *s = avctx->priv_data;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
65 int sample_rate = avctx->sample_rate;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
66 int sample_rate_half;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
67 int i;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
68 int frame_len_bits;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
69
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
70 s->avctx = avctx;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
71 dsputil_init(&s->dsp, avctx);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
72
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
73 /* determine frame length */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
74 if (avctx->sample_rate < 22050) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
75 frame_len_bits = 9;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
76 } else if (avctx->sample_rate < 44100) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
77 frame_len_bits = 10;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
78 } else {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
79 frame_len_bits = 11;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
80 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
81 s->frame_len = 1 << frame_len_bits;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
82
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
83 if (s->channels > MAX_CHANNELS) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
84 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
85 return -1;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
86 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
87
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
88 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
89 // audio is already interleaved for the RDFT format variant
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
90 sample_rate *= avctx->channels;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
91 s->frame_len *= avctx->channels;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
92 s->channels = 1;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
93 if (avctx->channels == 2)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
94 frame_len_bits++;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
95 } else {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
96 s->channels = avctx->channels;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
97 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
98
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
99 s->overlap_len = s->frame_len / 16;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
100 s->block_size = (s->frame_len - s->overlap_len) * s->channels;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
101 sample_rate_half = (sample_rate + 1) / 2;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
102 s->root = 2.0 / sqrt(s->frame_len);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
103
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
104 /* calculate number of bands */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
105 for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
106 if (sample_rate_half <= ff_wma_critical_freqs[s->num_bands - 1])
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
107 break;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
108
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
109 s->bands = av_malloc((s->num_bands + 1) * sizeof(*s->bands));
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
110 if (!s->bands)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
111 return AVERROR(ENOMEM);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
112
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
113 /* populate bands data */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
114 s->bands[0] = 1;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
115 for (i = 1; i < s->num_bands; i++)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
116 s->bands[i] = ff_wma_critical_freqs[i - 1] * (s->frame_len / 2) / sample_rate_half;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
117 s->bands[s->num_bands] = s->frame_len / 2;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
118
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
119 s->first = 1;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
120 avctx->sample_fmt = SAMPLE_FMT_S16;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
121
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
122 for (i = 0; i < s->channels; i++)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
123 s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
124
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
125 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
126 ff_rdft_init(&s->trans.rdft, frame_len_bits, IRIDFT);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
127 else
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
128 ff_dct_init(&s->trans.dct, frame_len_bits, 0);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
129
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
130 return 0;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
131 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
132
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
133 static float get_float(GetBitContext *gb)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
134 {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
135 int power = get_bits(gb, 5);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
136 float f = ldexpf(get_bits_long(gb, 23), power - 23);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
137 if (get_bits1(gb))
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
138 f = -f;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
139 return f;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
140 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
141
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
142 static const uint8_t rle_length_tab[16] = {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
143 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
144 };
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
145
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
146 /**
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
147 * Decode Bink Audio block
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
148 * @param[out] out Output buffer (must contain s->block_size elements)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
149 */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
150 static void decode_block(BinkAudioContext *s, short *out, int use_dct)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
151 {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
152 int ch, i, j, k;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
153 float q, quant[25];
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
154 int width, coeff;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
155 GetBitContext *gb = &s->gb;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
156
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
157 if (use_dct)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
158 skip_bits(gb, 2);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
159
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
160 for (ch = 0; ch < s->channels; ch++) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
161 FFTSample *coeffs = s->coeffs_ptr[ch];
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
162 q = 0.0f;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
163 coeffs[0] = get_float(gb) * s->root;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
164 coeffs[1] = get_float(gb) * s->root;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
165
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
166 for (i = 0; i < s->num_bands; i++) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
167 /* constant is result of 0.066399999/log10(M_E) */
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
168 int value = get_bits(gb, 8);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
169 quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
170 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
171
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
172 // find band (k)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
173 for (k = 0; s->bands[k] < 1; k++) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
174 q = quant[k];
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
175 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
176
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
177 // parse coefficients
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
178 i = 2;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
179 while (i < s->frame_len) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
180 if (get_bits1(gb)) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
181 j = i + rle_length_tab[get_bits(gb, 4)] * 8;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
182 } else {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
183 j = i + 8;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
184 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
185
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
186 j = FFMIN(j, s->frame_len);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
187
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
188 width = get_bits(gb, 4);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
189 if (width == 0) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
190 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
191 i = j;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
192 while (s->bands[k] * 2 < i)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
193 q = quant[k++];
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
194 } else {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
195 while (i < j) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
196 if (s->bands[k] * 2 == i)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
197 q = quant[k++];
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
198 coeff = get_bits(gb, width);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
199 if (coeff) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
200 if (get_bits1(gb))
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
201 coeffs[i] = -q * coeff;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
202 else
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
203 coeffs[i] = q * coeff;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
204 } else {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
205 coeffs[i] = 0.0f;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
206 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
207 i++;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
208 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
209 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
210 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
211
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
212 if (use_dct)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
213 ff_dct_calc (&s->trans.dct, coeffs);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
214 else
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
215 ff_rdft_calc(&s->trans.rdft, coeffs);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
216 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
217
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
218 s->dsp.float_to_int16_interleave(out, (const float **)s->coeffs_ptr, s->frame_len, s->channels);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
219
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
220 if (!s->first) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
221 int count = s->overlap_len * s->channels;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
222 int shift = av_log2(count);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
223 for (i = 0; i < count; i++) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
224 out[i] = (s->previous[i] * (count - i) + out[i] * i) >> shift;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
225 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
226 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
227
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
228 memcpy(s->previous, out + s->block_size,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
229 s->overlap_len * s->channels * sizeof(*out));
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
230
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
231 s->first = 0;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
232 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
233
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
234 static av_cold int decode_end(AVCodecContext *avctx)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
235 {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
236 BinkAudioContext * s = avctx->priv_data;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
237 av_freep(&s->bands);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
238 if (avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
239 ff_rdft_end(&s->trans.rdft);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
240 else
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
241 ff_dct_end(&s->trans.dct);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
242 return 0;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
243 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
244
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
245 static void get_bits_align32(GetBitContext *s)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
246 {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
247 int n = (-get_bits_count(s)) & 31;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
248 if (n) skip_bits(s, n);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
249 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
250
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
251 static int decode_frame(AVCodecContext *avctx,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
252 void *data, int *data_size,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
253 AVPacket *avpkt)
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
254 {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
255 BinkAudioContext *s = avctx->priv_data;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
256 const uint8_t *buf = avpkt->data;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
257 int buf_size = avpkt->size;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
258 short *samples = data;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
259 short *samples_end = (short*)((uint8_t*)data + *data_size);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
260 int reported_size;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
261 GetBitContext *gb = &s->gb;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
262
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
263 init_get_bits(gb, buf, buf_size * 8);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
264
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
265 reported_size = get_bits_long(gb, 32);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
266 while (get_bits_count(gb) / 8 < buf_size &&
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
267 samples + s->block_size <= samples_end) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
268 decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
269 samples += s->block_size;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
270 get_bits_align32(gb);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
271 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
272
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
273 *data_size = (uint8_t*)samples - (uint8_t*)data;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
274 if (reported_size != *data_size) {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
275 av_log(avctx, AV_LOG_WARNING, "reported data size (%d) does not match output data size (%d)\n",
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
276 reported_size, *data_size);
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
277 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
278 return buf_size;
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
279 }
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
280
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
281 AVCodec binkaudio_rdft_decoder = {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
282 "binkaudio_rdft",
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
283 CODEC_TYPE_AUDIO,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
284 CODEC_ID_BINKAUDIO_RDFT,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
285 sizeof(BinkAudioContext),
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
286 decode_init,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
287 NULL,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
288 decode_end,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
289 decode_frame,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
290 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
291 };
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
292
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
293 AVCodec binkaudio_dct_decoder = {
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
294 "binkaudio_dct",
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
295 CODEC_TYPE_AUDIO,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
296 CODEC_ID_BINKAUDIO_DCT,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
297 sizeof(BinkAudioContext),
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
298 decode_init,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
299 NULL,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
300 decode_end,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
301 decode_frame,
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
302 .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
91b1e4327340 Bink Audio decoder
pross
parents:
diff changeset
303 };