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