Browse code

Fix GSM decoding: regular GSM and MSGSM do not use the same bitstream endianness, so they cannot reuse the same code without a lot of function call overhead. Thus use templating to compile two different binaries.

Originally committed as revision 25312 to svn://svn.ffmpeg.org/ffmpeg/trunk

Reimar Döffinger authored on 2010/10/02 18:51:26
Showing 7 changed files
... ...
@@ -136,8 +136,8 @@ OBJS-$(CONFIG_FRAPS_DECODER)           += fraps.o
136 136
 OBJS-$(CONFIG_FRWU_DECODER)            += frwu.o
137 137
 OBJS-$(CONFIG_GIF_DECODER)             += gifdec.o lzw.o
138 138
 OBJS-$(CONFIG_GIF_ENCODER)             += gif.o lzwenc.o
139
-OBJS-$(CONFIG_GSM_DECODER)             += gsmdec.o
140
-OBJS-$(CONFIG_GSM_MS_DECODER)          += gsmdec.o
139
+OBJS-$(CONFIG_GSM_DECODER)             += gsmdec.o gsmdec_data.o msgsmdec.o
140
+OBJS-$(CONFIG_GSM_MS_DECODER)          += gsmdec.o gsmdec_data.o msgsmdec.o
141 141
 OBJS-$(CONFIG_H261_DECODER)            += h261dec.o h261.o \
142 142
                                           mpegvideo.o error_resilience.o
143 143
 OBJS-$(CONFIG_H261_ENCODER)            += h261enc.o h261.o             \
... ...
@@ -24,26 +24,11 @@
24 24
  * GSM decoder
25 25
  */
26 26
 
27
-#define ALT_BITSTREAM_READER_LE
28 27
 #include "avcodec.h"
29 28
 #include "get_bits.h"
29
+#include "msgsmdec.h"
30 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
-    // Contains first 120 elements from the previous frame
38
-    // (used by long_term_synth according to the "lag"),
39
-    // then in the following 160 elements the current
40
-    // frame is constructed.
41
-    int16_t ref_buf[280];
42
-    int v[9];
43
-    int lar[2][8];
44
-    int lar_idx;
45
-    int msr;
46
-} GSMContext;
31
+#include "gsmdec_template.c"
47 32
 
48 33
 static av_cold int gsm_init(AVCodecContext *avctx)
49 34
 {
... ...
@@ -65,199 +50,6 @@ static av_cold int gsm_init(AVCodecContext *avctx)
65 65
     return 0;
66 66
 }
67 67
 
68
-static const int16_t dequant_tab[64][8] = {
69
-    {   -28,    -20,    -12,     -4,      4,     12,     20,     28},
70
-    {   -56,    -40,    -24,     -8,      8,     24,     40,     56},
71
-    {   -84,    -60,    -36,    -12,     12,     36,     60,     84},
72
-    {  -112,    -80,    -48,    -16,     16,     48,     80,    112},
73
-    {  -140,   -100,    -60,    -20,     20,     60,    100,    140},
74
-    {  -168,   -120,    -72,    -24,     24,     72,    120,    168},
75
-    {  -196,   -140,    -84,    -28,     28,     84,    140,    196},
76
-    {  -224,   -160,    -96,    -32,     32,     96,    160,    224},
77
-    {  -252,   -180,   -108,    -36,     36,    108,    180,    252},
78
-    {  -280,   -200,   -120,    -40,     40,    120,    200,    280},
79
-    {  -308,   -220,   -132,    -44,     44,    132,    220,    308},
80
-    {  -336,   -240,   -144,    -48,     48,    144,    240,    336},
81
-    {  -364,   -260,   -156,    -52,     52,    156,    260,    364},
82
-    {  -392,   -280,   -168,    -56,     56,    168,    280,    392},
83
-    {  -420,   -300,   -180,    -60,     60,    180,    300,    420},
84
-    {  -448,   -320,   -192,    -64,     64,    192,    320,    448},
85
-    {  -504,   -360,   -216,    -72,     72,    216,    360,    504},
86
-    {  -560,   -400,   -240,    -80,     80,    240,    400,    560},
87
-    {  -616,   -440,   -264,    -88,     88,    264,    440,    616},
88
-    {  -672,   -480,   -288,    -96,     96,    288,    480,    672},
89
-    {  -728,   -520,   -312,   -104,    104,    312,    520,    728},
90
-    {  -784,   -560,   -336,   -112,    112,    336,    560,    784},
91
-    {  -840,   -600,   -360,   -120,    120,    360,    600,    840},
92
-    {  -896,   -640,   -384,   -128,    128,    384,    640,    896},
93
-    { -1008,   -720,   -432,   -144,    144,    432,    720,   1008},
94
-    { -1120,   -800,   -480,   -160,    160,    480,    800,   1120},
95
-    { -1232,   -880,   -528,   -176,    176,    528,    880,   1232},
96
-    { -1344,   -960,   -576,   -192,    192,    576,    960,   1344},
97
-    { -1456,  -1040,   -624,   -208,    208,    624,   1040,   1456},
98
-    { -1568,  -1120,   -672,   -224,    224,    672,   1120,   1568},
99
-    { -1680,  -1200,   -720,   -240,    240,    720,   1200,   1680},
100
-    { -1792,  -1280,   -768,   -256,    256,    768,   1280,   1792},
101
-    { -2016,  -1440,   -864,   -288,    288,    864,   1440,   2016},
102
-    { -2240,  -1600,   -960,   -320,    320,    960,   1600,   2240},
103
-    { -2464,  -1760,  -1056,   -352,    352,   1056,   1760,   2464},
104
-    { -2688,  -1920,  -1152,   -384,    384,   1152,   1920,   2688},
105
-    { -2912,  -2080,  -1248,   -416,    416,   1248,   2080,   2912},
106
-    { -3136,  -2240,  -1344,   -448,    448,   1344,   2240,   3136},
107
-    { -3360,  -2400,  -1440,   -480,    480,   1440,   2400,   3360},
108
-    { -3584,  -2560,  -1536,   -512,    512,   1536,   2560,   3584},
109
-    { -4032,  -2880,  -1728,   -576,    576,   1728,   2880,   4032},
110
-    { -4480,  -3200,  -1920,   -640,    640,   1920,   3200,   4480},
111
-    { -4928,  -3520,  -2112,   -704,    704,   2112,   3520,   4928},
112
-    { -5376,  -3840,  -2304,   -768,    768,   2304,   3840,   5376},
113
-    { -5824,  -4160,  -2496,   -832,    832,   2496,   4160,   5824},
114
-    { -6272,  -4480,  -2688,   -896,    896,   2688,   4480,   6272},
115
-    { -6720,  -4800,  -2880,   -960,    960,   2880,   4800,   6720},
116
-    { -7168,  -5120,  -3072,  -1024,   1024,   3072,   5120,   7168},
117
-    { -8063,  -5759,  -3456,  -1152,   1152,   3456,   5760,   8064},
118
-    { -8959,  -6399,  -3840,  -1280,   1280,   3840,   6400,   8960},
119
-    { -9855,  -7039,  -4224,  -1408,   1408,   4224,   7040,   9856},
120
-    {-10751,  -7679,  -4608,  -1536,   1536,   4608,   7680,  10752},
121
-    {-11647,  -8319,  -4992,  -1664,   1664,   4992,   8320,  11648},
122
-    {-12543,  -8959,  -5376,  -1792,   1792,   5376,   8960,  12544},
123
-    {-13439,  -9599,  -5760,  -1920,   1920,   5760,   9600,  13440},
124
-    {-14335, -10239,  -6144,  -2048,   2048,   6144,  10240,  14336},
125
-    {-16127, -11519,  -6912,  -2304,   2304,   6912,  11519,  16127},
126
-    {-17919, -12799,  -7680,  -2560,   2560,   7680,  12799,  17919},
127
-    {-19711, -14079,  -8448,  -2816,   2816,   8448,  14079,  19711},
128
-    {-21503, -15359,  -9216,  -3072,   3072,   9216,  15359,  21503},
129
-    {-23295, -16639,  -9984,  -3328,   3328,   9984,  16639,  23295},
130
-    {-25087, -17919, -10752,  -3584,   3584,  10752,  17919,  25087},
131
-    {-26879, -19199, -11520,  -3840,   3840,  11520,  19199,  26879},
132
-    {-28671, -20479, -12288,  -4096,   4096,  12288,  20479,  28671}
133
-};
134
-
135
-static void apcm_dequant_add(GetBitContext *gb, int16_t *dst)
136
-{
137
-    int i;
138
-    int maxidx = get_bits(gb, 6);
139
-    const int16_t *tab = dequant_tab[maxidx];
140
-    for (i = 0; i < 13; i++)
141
-        dst[3*i] += tab[get_bits(gb, 3)];
142
-}
143
-
144
-static inline int gsm_mult(int a, int b)
145
-{
146
-    return (a * b + (1 << 14)) >> 15;
147
-}
148
-
149
-static const uint16_t long_term_gain_tab[4] = {
150
-    3277, 11469, 21299, 32767
151
-};
152
-
153
-static void long_term_synth(int16_t *dst, int lag, int gain_idx)
154
-{
155
-    int i;
156
-    const int16_t *src = dst - lag;
157
-    uint16_t gain = long_term_gain_tab[gain_idx];
158
-    for (i = 0; i < 40; i++)
159
-        dst[i] = gsm_mult(gain, src[i]);
160
-}
161
-
162
-static inline int decode_log_area(int coded, int factor, int offset)
163
-{
164
-    coded <<= 10;
165
-    coded -= offset;
166
-    return gsm_mult(coded, factor) << 1;
167
-}
168
-
169
-static av_noinline int get_rrp(int filtered)
170
-{
171
-    int abs = FFABS(filtered);
172
-    if      (abs < 11059) abs <<= 1;
173
-    else if (abs < 20070) abs += 11059;
174
-    else                  abs = (abs >> 2) + 26112;
175
-    return filtered < 0 ? -abs : abs;
176
-}
177
-
178
-static int filter_value(int in, int rrp[8], int v[9])
179
-{
180
-    int i;
181
-    for (i = 7; i >= 0; i--) {
182
-        in -= gsm_mult(rrp[i], v[i]);
183
-        v[i + 1] = v[i] + gsm_mult(rrp[i], in);
184
-    }
185
-    v[0] = in;
186
-    return in;
187
-}
188
-
189
-static void short_term_synth(GSMContext *ctx, int16_t *dst, const int16_t *src)
190
-{
191
-    int i;
192
-    int rrp[8];
193
-    int *lar = ctx->lar[ctx->lar_idx];
194
-    int *lar_prev = ctx->lar[ctx->lar_idx ^ 1];
195
-    for (i = 0; i < 8; i++)
196
-        rrp[i] = get_rrp((lar_prev[i] >> 2) + (lar_prev[i] >> 1) + (lar[i] >> 2));
197
-    for (i = 0; i < 13; 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] >> 1) + (lar     [i] >> 1));
202
-    for (i = 13; i < 27; 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_prev[i] >> 2) + (lar     [i] >> 1) + (lar[i] >> 2));
207
-    for (i = 27; i < 40; i++)
208
-        dst[i] = filter_value(src[i], rrp, ctx->v);
209
-
210
-    for (i = 0; i < 8; i++)
211
-        rrp[i] = get_rrp(lar[i]);
212
-    for (i = 40; i < 160; i++)
213
-        dst[i] = filter_value(src[i], rrp, ctx->v);
214
-
215
-    ctx->lar_idx ^= 1;
216
-}
217
-
218
-static int postprocess(int16_t *data, int msr)
219
-{
220
-    int i;
221
-    for (i = 0; i < 160; i++) {
222
-        msr = av_clip_int16(data[i] + gsm_mult(msr, 28180));
223
-        data[i] = av_clip_int16(msr << 1) & ~7;
224
-    }
225
-    return msr;
226
-}
227
-
228
-static int gsm_decode_block(AVCodecContext *avctx, int16_t *samples,
229
-                            GetBitContext *gb)
230
-{
231
-    GSMContext *ctx = avctx->priv_data;
232
-    int i;
233
-    int16_t *ref_dst = ctx->ref_buf + 120;
234
-    int *lar = ctx->lar[ctx->lar_idx];
235
-    lar[0] = decode_log_area(get_bits(gb, 6), 13107,  1 << 15);
236
-    lar[1] = decode_log_area(get_bits(gb, 6), 13107,  1 << 15);
237
-    lar[2] = decode_log_area(get_bits(gb, 5), 13107, (1 << 14) + 2048*2);
238
-    lar[3] = decode_log_area(get_bits(gb, 5), 13107, (1 << 14) - 2560*2);
239
-    lar[4] = decode_log_area(get_bits(gb, 4), 19223, (1 << 13) +   94*2);
240
-    lar[5] = decode_log_area(get_bits(gb, 4), 17476, (1 << 13) - 1792*2);
241
-    lar[6] = decode_log_area(get_bits(gb, 3), 31454, (1 << 12) -  341*2);
242
-    lar[7] = decode_log_area(get_bits(gb, 3), 29708, (1 << 12) - 1144*2);
243
-
244
-    for (i = 0; i < 4; i++) {
245
-        int lag      = get_bits(gb, 7);
246
-        int gain_idx = get_bits(gb, 2);
247
-        int offset   = get_bits(gb, 2);
248
-        lag = av_clip(lag, 40, 120);
249
-        long_term_synth(ref_dst, lag, gain_idx);
250
-        apcm_dequant_add(gb, ref_dst + offset);
251
-        ref_dst += 40;
252
-    }
253
-    memcpy(ctx->ref_buf, ctx->ref_buf + 160, 120 * sizeof(*ctx->ref_buf));
254
-    short_term_synth(ctx, samples, ctx->ref_buf + 120);
255
-    // for optimal speed this could be merged with short_term_synth,
256
-    // not done yet because it is a bit ugly
257
-    ctx->msr = postprocess(samples, ctx->msr);
258
-    return 0;
259
-}
260
-
261 68
 static int gsm_decode_frame(AVCodecContext *avctx, void *data,
262 69
                             int *data_size, AVPacket *avpkt)
263 70
 {
... ...
@@ -273,10 +65,10 @@ static int gsm_decode_frame(AVCodecContext *avctx, void *data,
273 273
     *data_size = 0;
274 274
     if(buf_size < avctx->block_align)
275 275
         return AVERROR_INVALIDDATA;
276
-    init_get_bits(&gb, buf, buf_size * 8);
277 276
 
278 277
     switch (avctx->codec_id) {
279 278
     case CODEC_ID_GSM:
279
+        init_get_bits(&gb, buf, buf_size * 8);
280 280
         if (get_bits(&gb, 4) != 0xd)
281 281
             av_log(avctx, AV_LOG_WARNING, "Missing GSM magic!\n");
282 282
         res = gsm_decode_block(avctx, samples, &gb);
... ...
@@ -284,10 +76,7 @@ static int gsm_decode_frame(AVCodecContext *avctx, void *data,
284 284
             return res;
285 285
         break;
286 286
     case CODEC_ID_GSM_MS:
287
-        res = gsm_decode_block(avctx, samples, &gb);
288
-        if (res < 0)
289
-            return res;
290
-        res = gsm_decode_block(avctx, samples + GSM_FRAME_SIZE, &gb);
287
+        res = ff_msgsm_decode_block(avctx, samples, buf);
291 288
         if (res < 0)
292 289
             return res;
293 290
     }
294 291
new file mode 100644
... ...
@@ -0,0 +1,94 @@
0
+/*
1
+ * gsm 06.10 decoder data
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
+#include <stdint.h>
22
+#include "gsmdec_data.h"
23
+
24
+const uint16_t ff_gsm_long_term_gain_tab[4] = {
25
+    3277, 11469, 21299, 32767
26
+};
27
+
28
+const int16_t ff_gsm_dequant_tab[64][8] = {
29
+    {   -28,    -20,    -12,     -4,      4,     12,     20,     28},
30
+    {   -56,    -40,    -24,     -8,      8,     24,     40,     56},
31
+    {   -84,    -60,    -36,    -12,     12,     36,     60,     84},
32
+    {  -112,    -80,    -48,    -16,     16,     48,     80,    112},
33
+    {  -140,   -100,    -60,    -20,     20,     60,    100,    140},
34
+    {  -168,   -120,    -72,    -24,     24,     72,    120,    168},
35
+    {  -196,   -140,    -84,    -28,     28,     84,    140,    196},
36
+    {  -224,   -160,    -96,    -32,     32,     96,    160,    224},
37
+    {  -252,   -180,   -108,    -36,     36,    108,    180,    252},
38
+    {  -280,   -200,   -120,    -40,     40,    120,    200,    280},
39
+    {  -308,   -220,   -132,    -44,     44,    132,    220,    308},
40
+    {  -336,   -240,   -144,    -48,     48,    144,    240,    336},
41
+    {  -364,   -260,   -156,    -52,     52,    156,    260,    364},
42
+    {  -392,   -280,   -168,    -56,     56,    168,    280,    392},
43
+    {  -420,   -300,   -180,    -60,     60,    180,    300,    420},
44
+    {  -448,   -320,   -192,    -64,     64,    192,    320,    448},
45
+    {  -504,   -360,   -216,    -72,     72,    216,    360,    504},
46
+    {  -560,   -400,   -240,    -80,     80,    240,    400,    560},
47
+    {  -616,   -440,   -264,    -88,     88,    264,    440,    616},
48
+    {  -672,   -480,   -288,    -96,     96,    288,    480,    672},
49
+    {  -728,   -520,   -312,   -104,    104,    312,    520,    728},
50
+    {  -784,   -560,   -336,   -112,    112,    336,    560,    784},
51
+    {  -840,   -600,   -360,   -120,    120,    360,    600,    840},
52
+    {  -896,   -640,   -384,   -128,    128,    384,    640,    896},
53
+    { -1008,   -720,   -432,   -144,    144,    432,    720,   1008},
54
+    { -1120,   -800,   -480,   -160,    160,    480,    800,   1120},
55
+    { -1232,   -880,   -528,   -176,    176,    528,    880,   1232},
56
+    { -1344,   -960,   -576,   -192,    192,    576,    960,   1344},
57
+    { -1456,  -1040,   -624,   -208,    208,    624,   1040,   1456},
58
+    { -1568,  -1120,   -672,   -224,    224,    672,   1120,   1568},
59
+    { -1680,  -1200,   -720,   -240,    240,    720,   1200,   1680},
60
+    { -1792,  -1280,   -768,   -256,    256,    768,   1280,   1792},
61
+    { -2016,  -1440,   -864,   -288,    288,    864,   1440,   2016},
62
+    { -2240,  -1600,   -960,   -320,    320,    960,   1600,   2240},
63
+    { -2464,  -1760,  -1056,   -352,    352,   1056,   1760,   2464},
64
+    { -2688,  -1920,  -1152,   -384,    384,   1152,   1920,   2688},
65
+    { -2912,  -2080,  -1248,   -416,    416,   1248,   2080,   2912},
66
+    { -3136,  -2240,  -1344,   -448,    448,   1344,   2240,   3136},
67
+    { -3360,  -2400,  -1440,   -480,    480,   1440,   2400,   3360},
68
+    { -3584,  -2560,  -1536,   -512,    512,   1536,   2560,   3584},
69
+    { -4032,  -2880,  -1728,   -576,    576,   1728,   2880,   4032},
70
+    { -4480,  -3200,  -1920,   -640,    640,   1920,   3200,   4480},
71
+    { -4928,  -3520,  -2112,   -704,    704,   2112,   3520,   4928},
72
+    { -5376,  -3840,  -2304,   -768,    768,   2304,   3840,   5376},
73
+    { -5824,  -4160,  -2496,   -832,    832,   2496,   4160,   5824},
74
+    { -6272,  -4480,  -2688,   -896,    896,   2688,   4480,   6272},
75
+    { -6720,  -4800,  -2880,   -960,    960,   2880,   4800,   6720},
76
+    { -7168,  -5120,  -3072,  -1024,   1024,   3072,   5120,   7168},
77
+    { -8063,  -5759,  -3456,  -1152,   1152,   3456,   5760,   8064},
78
+    { -8959,  -6399,  -3840,  -1280,   1280,   3840,   6400,   8960},
79
+    { -9855,  -7039,  -4224,  -1408,   1408,   4224,   7040,   9856},
80
+    {-10751,  -7679,  -4608,  -1536,   1536,   4608,   7680,  10752},
81
+    {-11647,  -8319,  -4992,  -1664,   1664,   4992,   8320,  11648},
82
+    {-12543,  -8959,  -5376,  -1792,   1792,   5376,   8960,  12544},
83
+    {-13439,  -9599,  -5760,  -1920,   1920,   5760,   9600,  13440},
84
+    {-14335, -10239,  -6144,  -2048,   2048,   6144,  10240,  14336},
85
+    {-16127, -11519,  -6912,  -2304,   2304,   6912,  11519,  16127},
86
+    {-17919, -12799,  -7680,  -2560,   2560,   7680,  12799,  17919},
87
+    {-19711, -14079,  -8448,  -2816,   2816,   8448,  14079,  19711},
88
+    {-21503, -15359,  -9216,  -3072,   3072,   9216,  15359,  21503},
89
+    {-23295, -16639,  -9984,  -3328,   3328,   9984,  16639,  23295},
90
+    {-25087, -17919, -10752,  -3584,   3584,  10752,  17919,  25087},
91
+    {-26879, -19199, -11520,  -3840,   3840,  11520,  19199,  26879},
92
+    {-28671, -20479, -12288,  -4096,   4096,  12288,  20479,  28671}
93
+};
0 94
new file mode 100644
... ...
@@ -0,0 +1,47 @@
0
+/*
1
+ * gsm 06.10 decoder data
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
+#ifndef GSMDEC_DATA
22
+#define GSMDEC_DATA
23
+
24
+#include <stdint.h>
25
+
26
+// input and output sizes in byte
27
+#define GSM_BLOCK_SIZE    33
28
+#define GSM_MS_BLOCK_SIZE 65
29
+#define GSM_FRAME_SIZE   160
30
+
31
+typedef struct {
32
+    // Contains first 120 elements from the previous frame
33
+    // (used by long_term_synth according to the "lag"),
34
+    // then in the following 160 elements the current
35
+    // frame is constructed.
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
+extern const uint16_t ff_gsm_long_term_gain_tab[4];
44
+extern const int16_t ff_gsm_dequant_tab[64][8];
45
+
46
+#endif
0 47
new file mode 100644
... ...
@@ -0,0 +1,150 @@
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
+#include "get_bits.h"
27
+#include "gsmdec_data.h"
28
+
29
+static void apcm_dequant_add(GetBitContext *gb, int16_t *dst)
30
+{
31
+    int i;
32
+    int maxidx = get_bits(gb, 6);
33
+    const int16_t *tab = ff_gsm_dequant_tab[maxidx];
34
+    for (i = 0; i < 13; i++)
35
+        dst[3*i] += tab[get_bits(gb, 3)];
36
+}
37
+
38
+static inline int gsm_mult(int a, int b)
39
+{
40
+    return (a * b + (1 << 14)) >> 15;
41
+}
42
+
43
+static void long_term_synth(int16_t *dst, int lag, int gain_idx)
44
+{
45
+    int i;
46
+    const int16_t *src = dst - lag;
47
+    uint16_t gain = ff_gsm_long_term_gain_tab[gain_idx];
48
+    for (i = 0; i < 40; i++)
49
+        dst[i] = gsm_mult(gain, src[i]);
50
+}
51
+
52
+static inline int decode_log_area(int coded, int factor, int offset)
53
+{
54
+    coded <<= 10;
55
+    coded -= offset;
56
+    return gsm_mult(coded, factor) << 1;
57
+}
58
+
59
+static av_noinline int get_rrp(int filtered)
60
+{
61
+    int abs = FFABS(filtered);
62
+    if      (abs < 11059) abs <<= 1;
63
+    else if (abs < 20070) abs += 11059;
64
+    else                  abs = (abs >> 2) + 26112;
65
+    return filtered < 0 ? -abs : abs;
66
+}
67
+
68
+static int filter_value(int in, int rrp[8], int v[9])
69
+{
70
+    int i;
71
+    for (i = 7; i >= 0; i--) {
72
+        in -= gsm_mult(rrp[i], v[i]);
73
+        v[i + 1] = v[i] + gsm_mult(rrp[i], in);
74
+    }
75
+    v[0] = in;
76
+    return in;
77
+}
78
+
79
+static void short_term_synth(GSMContext *ctx, int16_t *dst, const int16_t *src)
80
+{
81
+    int i;
82
+    int rrp[8];
83
+    int *lar = ctx->lar[ctx->lar_idx];
84
+    int *lar_prev = ctx->lar[ctx->lar_idx ^ 1];
85
+    for (i = 0; i < 8; i++)
86
+        rrp[i] = get_rrp((lar_prev[i] >> 2) + (lar_prev[i] >> 1) + (lar[i] >> 2));
87
+    for (i = 0; i < 13; i++)
88
+        dst[i] = filter_value(src[i], rrp, ctx->v);
89
+
90
+    for (i = 0; i < 8; i++)
91
+        rrp[i] = get_rrp((lar_prev[i] >> 1) + (lar     [i] >> 1));
92
+    for (i = 13; i < 27; i++)
93
+        dst[i] = filter_value(src[i], rrp, ctx->v);
94
+
95
+    for (i = 0; i < 8; i++)
96
+        rrp[i] = get_rrp((lar_prev[i] >> 2) + (lar     [i] >> 1) + (lar[i] >> 2));
97
+    for (i = 27; i < 40; i++)
98
+        dst[i] = filter_value(src[i], rrp, ctx->v);
99
+
100
+    for (i = 0; i < 8; i++)
101
+        rrp[i] = get_rrp(lar[i]);
102
+    for (i = 40; i < 160; i++)
103
+        dst[i] = filter_value(src[i], rrp, ctx->v);
104
+
105
+    ctx->lar_idx ^= 1;
106
+}
107
+
108
+static int postprocess(int16_t *data, int msr)
109
+{
110
+    int i;
111
+    for (i = 0; i < 160; i++) {
112
+        msr = av_clip_int16(data[i] + gsm_mult(msr, 28180));
113
+        data[i] = av_clip_int16(msr << 1) & ~7;
114
+    }
115
+    return msr;
116
+}
117
+
118
+static int gsm_decode_block(AVCodecContext *avctx, int16_t *samples,
119
+                            GetBitContext *gb)
120
+{
121
+    GSMContext *ctx = avctx->priv_data;
122
+    int i;
123
+    int16_t *ref_dst = ctx->ref_buf + 120;
124
+    int *lar = ctx->lar[ctx->lar_idx];
125
+    lar[0] = decode_log_area(get_bits(gb, 6), 13107,  1 << 15);
126
+    lar[1] = decode_log_area(get_bits(gb, 6), 13107,  1 << 15);
127
+    lar[2] = decode_log_area(get_bits(gb, 5), 13107, (1 << 14) + 2048*2);
128
+    lar[3] = decode_log_area(get_bits(gb, 5), 13107, (1 << 14) - 2560*2);
129
+    lar[4] = decode_log_area(get_bits(gb, 4), 19223, (1 << 13) +   94*2);
130
+    lar[5] = decode_log_area(get_bits(gb, 4), 17476, (1 << 13) - 1792*2);
131
+    lar[6] = decode_log_area(get_bits(gb, 3), 31454, (1 << 12) -  341*2);
132
+    lar[7] = decode_log_area(get_bits(gb, 3), 29708, (1 << 12) - 1144*2);
133
+
134
+    for (i = 0; i < 4; i++) {
135
+        int lag      = get_bits(gb, 7);
136
+        int gain_idx = get_bits(gb, 2);
137
+        int offset   = get_bits(gb, 2);
138
+        lag = av_clip(lag, 40, 120);
139
+        long_term_synth(ref_dst, lag, gain_idx);
140
+        apcm_dequant_add(gb, ref_dst + offset);
141
+        ref_dst += 40;
142
+    }
143
+    memcpy(ctx->ref_buf, ctx->ref_buf + 160, 120 * sizeof(*ctx->ref_buf));
144
+    short_term_synth(ctx, samples, ctx->ref_buf + 120);
145
+    // for optimal speed this could be merged with short_term_synth,
146
+    // not done yet because it is a bit ugly
147
+    ctx->msr = postprocess(samples, ctx->msr);
148
+    return 0;
149
+}
0 150
new file mode 100644
... ...
@@ -0,0 +1,37 @@
0
+/*
1
+ * gsm 06.10 decoder, Microsoft variant
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
+#define ALT_BITSTREAM_READER_LE
22
+#include "avcodec.h"
23
+#include "msgsmdec.h"
24
+#include "gsmdec_template.c"
25
+
26
+int ff_msgsm_decode_block(AVCodecContext *avctx, int16_t *samples,
27
+                          const uint8_t *buf)
28
+{
29
+    int res;
30
+    GetBitContext gb;
31
+    init_get_bits(&gb, buf, GSM_MS_BLOCK_SIZE * 8);
32
+    res = gsm_decode_block(avctx, samples, &gb);
33
+    if (res < 0)
34
+        return res;
35
+    return gsm_decode_block(avctx, samples + GSM_FRAME_SIZE, &gb);
36
+}
0 37
new file mode 100644
... ...
@@ -0,0 +1,28 @@
0
+/*
1
+ * gsm 06.10 decoder, Microsoft variant
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
+#ifndef MSGSMDEC_H
22
+#define MSGSMDEC_H
23
+
24
+int ff_msgsm_decode_block(AVCodecContext *avctx, int16_t *samples,
25
+                          const uint8_t *buf);
26
+
27
+#endif