Mercurial > libavcodec.hg
comparison gsmdec.c @ 12124:11b27985b3d0 libavcodec
Add native GSM 06.10 audio decoder.
author | reimar |
---|---|
date | Sat, 10 Jul 2010 07:55:06 +0000 |
parents | |
children | 81871058b27c |
comparison
equal
deleted
inserted
replaced
12123:750962dae4c9 | 12124:11b27985b3d0 |
---|---|
1 /* | |
2 * gsm 06.10 decoder | |
3 * Copyright (c) 2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de> | |
4 * | |
5 * This file is part of FFmpeg. | |
6 * | |
7 * FFmpeg is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2.1 of the License, or (at your option) any later version. | |
11 * | |
12 * FFmpeg is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with FFmpeg; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 */ | |
21 | |
22 /** | |
23 * @file | |
24 * GSM decoder | |
25 */ | |
26 | |
27 #define ALT_BITSTREAM_READER_LE | |
28 #include "avcodec.h" | |
29 #include "get_bits.h" | |
30 | |
31 // input and output sizes in byte | |
32 #define GSM_BLOCK_SIZE 33 | |
33 #define GSM_MS_BLOCK_SIZE 65 | |
34 #define GSM_FRAME_SIZE 160 | |
35 | |
36 typedef struct { | |
37 int16_t ref_buf[280]; | |
38 int v[9]; | |
39 int lar[2][8]; | |
40 int lar_idx; | |
41 int msr; | |
42 } GSMContext; | |
43 | |
44 static av_cold int gsm_init(AVCodecContext *avctx) | |
45 { | |
46 avctx->channels = 1; | |
47 if (!avctx->sample_rate) | |
48 avctx->sample_rate = 8000; | |
49 avctx->sample_fmt = SAMPLE_FMT_S16; | |
50 | |
51 switch (avctx->codec_id) { | |
52 case CODEC_ID_GSM: | |
53 avctx->frame_size = GSM_FRAME_SIZE; | |
54 avctx->block_align = GSM_BLOCK_SIZE; | |
55 break; | |
56 case CODEC_ID_GSM_MS: | |
57 avctx->frame_size = 2 * GSM_FRAME_SIZE; | |
58 avctx->block_align = GSM_MS_BLOCK_SIZE; | |
59 } | |
60 | |
61 return 0; | |
62 } | |
63 | |
64 static const int16_t dequant_tab[64][8] = { | |
65 { -28, -20, -12, -4, 4, 12, 20, 28}, | |
66 { -56, -40, -24, -8, 8, 24, 40, 56}, | |
67 { -84, -60, -36, -12, 12, 36, 60, 84}, | |
68 { -112, -80, -48, -16, 16, 48, 80, 112}, | |
69 { -140, -100, -60, -20, 20, 60, 100, 140}, | |
70 { -168, -120, -72, -24, 24, 72, 120, 168}, | |
71 { -196, -140, -84, -28, 28, 84, 140, 196}, | |
72 { -224, -160, -96, -32, 32, 96, 160, 224}, | |
73 { -252, -180, -108, -36, 36, 108, 180, 252}, | |
74 { -280, -200, -120, -40, 40, 120, 200, 280}, | |
75 { -308, -220, -132, -44, 44, 132, 220, 308}, | |
76 { -336, -240, -144, -48, 48, 144, 240, 336}, | |
77 { -364, -260, -156, -52, 52, 156, 260, 364}, | |
78 { -392, -280, -168, -56, 56, 168, 280, 392}, | |
79 { -420, -300, -180, -60, 60, 180, 300, 420}, | |
80 { -448, -320, -192, -64, 64, 192, 320, 448}, | |
81 { -504, -360, -216, -72, 72, 216, 360, 504}, | |
82 { -560, -400, -240, -80, 80, 240, 400, 560}, | |
83 { -616, -440, -264, -88, 88, 264, 440, 616}, | |
84 { -672, -480, -288, -96, 96, 288, 480, 672}, | |
85 { -728, -520, -312, -104, 104, 312, 520, 728}, | |
86 { -784, -560, -336, -112, 112, 336, 560, 784}, | |
87 { -840, -600, -360, -120, 120, 360, 600, 840}, | |
88 { -896, -640, -384, -128, 128, 384, 640, 896}, | |
89 { -1008, -720, -432, -144, 144, 432, 720, 1008}, | |
90 { -1120, -800, -480, -160, 160, 480, 800, 1120}, | |
91 { -1232, -880, -528, -176, 176, 528, 880, 1232}, | |
92 { -1344, -960, -576, -192, 192, 576, 960, 1344}, | |
93 { -1456, -1040, -624, -208, 208, 624, 1040, 1456}, | |
94 { -1568, -1120, -672, -224, 224, 672, 1120, 1568}, | |
95 { -1680, -1200, -720, -240, 240, 720, 1200, 1680}, | |
96 { -1792, -1280, -768, -256, 256, 768, 1280, 1792}, | |
97 { -2016, -1440, -864, -288, 288, 864, 1440, 2016}, | |
98 { -2240, -1600, -960, -320, 320, 960, 1600, 2240}, | |
99 { -2464, -1760, -1056, -352, 352, 1056, 1760, 2464}, | |
100 { -2688, -1920, -1152, -384, 384, 1152, 1920, 2688}, | |
101 { -2912, -2080, -1248, -416, 416, 1248, 2080, 2912}, | |
102 { -3136, -2240, -1344, -448, 448, 1344, 2240, 3136}, | |
103 { -3360, -2400, -1440, -480, 480, 1440, 2400, 3360}, | |
104 { -3584, -2560, -1536, -512, 512, 1536, 2560, 3584}, | |
105 { -4032, -2880, -1728, -576, 576, 1728, 2880, 4032}, | |
106 { -4480, -3200, -1920, -640, 640, 1920, 3200, 4480}, | |
107 { -4928, -3520, -2112, -704, 704, 2112, 3520, 4928}, | |
108 { -5376, -3840, -2304, -768, 768, 2304, 3840, 5376}, | |
109 { -5824, -4160, -2496, -832, 832, 2496, 4160, 5824}, | |
110 { -6272, -4480, -2688, -896, 896, 2688, 4480, 6272}, | |
111 { -6720, -4800, -2880, -960, 960, 2880, 4800, 6720}, | |
112 { -7168, -5120, -3072, -1024, 1024, 3072, 5120, 7168}, | |
113 { -8063, -5759, -3456, -1152, 1152, 3456, 5760, 8064}, | |
114 { -8959, -6399, -3840, -1280, 1280, 3840, 6400, 8960}, | |
115 { -9855, -7039, -4224, -1408, 1408, 4224, 7040, 9856}, | |
116 {-10751, -7679, -4608, -1536, 1536, 4608, 7680, 10752}, | |
117 {-11647, -8319, -4992, -1664, 1664, 4992, 8320, 11648}, | |
118 {-12543, -8959, -5376, -1792, 1792, 5376, 8960, 12544}, | |
119 {-13439, -9599, -5760, -1920, 1920, 5760, 9600, 13440}, | |
120 {-14335, -10239, -6144, -2048, 2048, 6144, 10240, 14336}, | |
121 {-16127, -11519, -6912, -2304, 2304, 6912, 11519, 16127}, | |
122 {-17919, -12799, -7680, -2560, 2560, 7680, 12799, 17919}, | |
123 {-19711, -14079, -8448, -2816, 2816, 8448, 14079, 19711}, | |
124 {-21503, -15359, -9216, -3072, 3072, 9216, 15359, 21503}, | |
125 {-23295, -16639, -9984, -3328, 3328, 9984, 16639, 23295}, | |
126 {-25087, -17919, -10752, -3584, 3584, 10752, 17919, 25087}, | |
127 {-26879, -19199, -11520, -3840, 3840, 11520, 19199, 26879}, | |
128 {-28671, -20479, -12288, -4096, 4096, 12288, 20479, 28671} | |
129 }; | |
130 | |
131 static void apcm_dequant_add(GetBitContext *gb, int16_t *dst) | |
132 { | |
133 int i; | |
134 int maxidx = get_bits(gb, 6); | |
135 const int16_t *tab = dequant_tab[maxidx]; | |
136 for (i = 0; i < 13; i++) | |
137 dst[3*i] += tab[get_bits(gb, 3)]; | |
138 } | |
139 | |
140 static inline int gsm_mult(int a, int b) | |
141 { | |
142 return (a * b + (1 << 14)) >> 15; | |
143 } | |
144 | |
145 static const uint16_t long_term_gain_tab[4] = { | |
146 3277, 11469, 21299, 32767 | |
147 }; | |
148 | |
149 static void long_term_synth(int16_t *dst, int lag, int gain_idx) | |
150 { | |
151 int i; | |
152 const int16_t *src = dst - lag; | |
153 uint16_t gain = long_term_gain_tab[gain_idx]; | |
154 for (i = 0; i < 40; i++) | |
155 dst[i] = gsm_mult(gain, src[i]); | |
156 } | |
157 | |
158 static inline int decode_log_area(int coded, int factor, int offset) | |
159 { | |
160 coded <<= 10; | |
161 coded -= offset; | |
162 return gsm_mult(coded, factor) << 1; | |
163 } | |
164 | |
165 static av_noinline int get_rrp(int filtered) | |
166 { | |
167 int abs = FFABS(filtered); | |
168 if (abs < 11059) abs <<= 1; | |
169 else if (abs < 20070) abs += 11059; | |
170 else abs = (abs >> 2) + 26112; | |
171 return filtered < 0 ? -abs : abs; | |
172 } | |
173 | |
174 static int filter_value(int in, int rrp[8], int v[9]) | |
175 { | |
176 int i; | |
177 for (i = 7; i >= 0; i--) { | |
178 in -= gsm_mult(rrp[i], v[i]); | |
179 v[i + 1] = v[i] + gsm_mult(rrp[i], in); | |
180 } | |
181 v[0] = in; | |
182 return in; | |
183 } | |
184 | |
185 static void short_term_synth(GSMContext *ctx, int16_t *dst, const int16_t *src) | |
186 { | |
187 int i; | |
188 int rrp[8]; | |
189 int *lar = ctx->lar[ctx->lar_idx]; | |
190 int *lar_prev = ctx->lar[ctx->lar_idx ^ 1]; | |
191 for (i = 0; i < 8; i++) | |
192 rrp[i] = get_rrp((lar_prev[i] >> 2) + (lar_prev[i] >> 1) + (lar[i] >> 2)); | |
193 for (i = 0; i < 13; i++) | |
194 dst[i] = filter_value(src[i], rrp, ctx->v); | |
195 | |
196 for (i = 0; i < 8; i++) | |
197 rrp[i] = get_rrp((lar_prev[i] >> 1) + (lar [i] >> 1)); | |
198 for (i = 13; i < 27; i++) | |
199 dst[i] = filter_value(src[i], rrp, ctx->v); | |
200 | |
201 for (i = 0; i < 8; i++) | |
202 rrp[i] = get_rrp((lar_prev[i] >> 2) + (lar [i] >> 1) + (lar[i] >> 2)); | |
203 for (i = 27; i < 40; i++) | |
204 dst[i] = filter_value(src[i], rrp, ctx->v); | |
205 | |
206 for (i = 0; i < 8; i++) | |
207 rrp[i] = get_rrp(lar[i]); | |
208 for (i = 40; i < 160; i++) | |
209 dst[i] = filter_value(src[i], rrp, ctx->v); | |
210 | |
211 ctx->lar_idx ^= 1; | |
212 } | |
213 | |
214 static int postprocess(int16_t *data, int msr) | |
215 { | |
216 int i; | |
217 for (i = 0; i < 160; i++) { | |
218 msr = av_clip_int16(data[i] + gsm_mult(msr, 28180)); | |
219 data[i] = av_clip_int16(msr << 1) & ~7; | |
220 } | |
221 return msr; | |
222 } | |
223 | |
224 static int gsm_decode_block(AVCodecContext *avctx, int16_t *samples, | |
225 GetBitContext *gb) | |
226 { | |
227 GSMContext *ctx = avctx->priv_data; | |
228 int i; | |
229 int16_t *ref_dst = ctx->ref_buf + 120; | |
230 int *lar = ctx->lar[ctx->lar_idx]; | |
231 lar[0] = decode_log_area(get_bits(gb, 6), 13107, 1 << 15); | |
232 lar[1] = decode_log_area(get_bits(gb, 6), 13107, 1 << 15); | |
233 lar[2] = decode_log_area(get_bits(gb, 5), 13107, (1 << 14) + 2048*2); | |
234 lar[3] = decode_log_area(get_bits(gb, 5), 13107, (1 << 14) - 2560*2); | |
235 lar[4] = decode_log_area(get_bits(gb, 4), 19223, (1 << 13) + 94*2); | |
236 lar[5] = decode_log_area(get_bits(gb, 4), 17476, (1 << 13) - 1792*2); | |
237 lar[6] = decode_log_area(get_bits(gb, 3), 31454, (1 << 12) - 341*2); | |
238 lar[7] = decode_log_area(get_bits(gb, 3), 29708, (1 << 12) - 1144*2); | |
239 | |
240 for (i = 0; i < 4; i++) { | |
241 int lag = get_bits(gb, 7); | |
242 int gain_idx = get_bits(gb, 2); | |
243 int offset = get_bits(gb, 2); | |
244 lag = av_clip(lag, 40, 120); | |
245 long_term_synth(ref_dst, lag, gain_idx); | |
246 apcm_dequant_add(gb, ref_dst + offset); | |
247 ref_dst += 40; | |
248 } | |
249 memcpy(ctx->ref_buf, ctx->ref_buf + 160, 120 * sizeof(*ctx->ref_buf)); | |
250 short_term_synth(ctx, samples, ctx->ref_buf + 120); | |
251 // for optimal speed this could be merged with short_term_synth, | |
252 // not done yet because it is a bit ugly | |
253 ctx->msr = postprocess(samples, ctx->msr); | |
254 return 0; | |
255 } | |
256 | |
257 static int gsm_decode_frame(AVCodecContext *avctx, void *data, | |
258 int *data_size, AVPacket *avpkt) | |
259 { | |
260 int res; | |
261 GetBitContext gb; | |
262 const uint8_t *buf = avpkt->data; | |
263 int buf_size = avpkt->size; | |
264 int16_t *samples = data; | |
265 int frame_bytes = 2 * avctx->frame_size; | |
266 | |
267 if (*data_size < frame_bytes) | |
268 return -1; | |
269 *data_size = 0; | |
270 if(buf_size < avctx->block_align) | |
271 return AVERROR_INVALIDDATA; | |
272 init_get_bits(&gb, buf, buf_size * 8); | |
273 | |
274 switch (avctx->codec_id) { | |
275 case CODEC_ID_GSM: | |
276 if (get_bits(&gb, 4) != 0xd) | |
277 av_log(avctx, AV_LOG_WARNING, "Missing GSM magic!\n"); | |
278 res = gsm_decode_block(avctx, samples, &gb); | |
279 if (res < 0) | |
280 return res; | |
281 break; | |
282 case CODEC_ID_GSM_MS: | |
283 res = gsm_decode_block(avctx, samples, &gb); | |
284 if (res < 0) | |
285 return res; | |
286 res = gsm_decode_block(avctx, samples + GSM_FRAME_SIZE, &gb); | |
287 if (res < 0) | |
288 return res; | |
289 } | |
290 *data_size = frame_bytes; | |
291 return avctx->block_align; | |
292 } | |
293 | |
294 AVCodec gsm_decoder = { | |
295 "gsm", | |
296 AVMEDIA_TYPE_AUDIO, | |
297 CODEC_ID_GSM, | |
298 sizeof(GSMContext), | |
299 gsm_init, | |
300 NULL, | |
301 NULL, | |
302 gsm_decode_frame, | |
303 .long_name = NULL_IF_CONFIG_SMALL("GSM"), | |
304 }; | |
305 | |
306 AVCodec gsm_ms_decoder = { | |
307 "gsm_ms", | |
308 AVMEDIA_TYPE_AUDIO, | |
309 CODEC_ID_GSM_MS, | |
310 sizeof(GSMContext), | |
311 gsm_init, | |
312 NULL, | |
313 NULL, | |
314 gsm_decode_frame, | |
315 .long_name = NULL_IF_CONFIG_SMALL("GSM Microsoft variant"), | |
316 }; |