Browse code

lavc: Add DSS SP decoder

Signed-off-by: Oleksij Rempel <linux@rempel-privat.de>
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>

Oleksij Rempel authored on 2015/02/13 16:36:16
Showing 8 changed files
... ...
@@ -15,6 +15,7 @@ version <next>:
15 15
 - DXVA2-accelerated HEVC decoding
16 16
 - AAC ELD 480 decoding
17 17
 - Intel QSV-accelerated H.264 decoding
18
+- DSS SP decoder
18 19
 
19 20
 
20 21
 version 11:
... ...
@@ -820,6 +820,7 @@ following image formats are supported:
820 820
     @tab Used in Bink and Smacker files in many games.
821 821
 @item Delphine Software International CIN audio  @tab     @tab  X
822 822
     @tab Codec used in Delphine Software International games.
823
+@item Digital Speech Standard - Standard Play mode (DSS SP) @tab     @tab  X
823 824
 @item Discworld II BMV Audio @tab     @tab  X
824 825
 @item COOK                   @tab     @tab  X
825 826
     @tab All versions except 5.1 are supported.
... ...
@@ -169,6 +169,7 @@ OBJS-$(CONFIG_DPX_DECODER)             += dpx.o
169 169
 OBJS-$(CONFIG_DPX_ENCODER)             += dpxenc.o
170 170
 OBJS-$(CONFIG_DSICINAUDIO_DECODER)     += dsicinaudio.o
171 171
 OBJS-$(CONFIG_DSICINVIDEO_DECODER)     += dsicinvideo.o
172
+OBJS-$(CONFIG_DSS_SP_DECODER)          += dss_sp.o
172 173
 OBJS-$(CONFIG_DVBSUB_DECODER)          += dvbsubdec.o
173 174
 OBJS-$(CONFIG_DVBSUB_ENCODER)          += dvbsub.o
174 175
 OBJS-$(CONFIG_DVDSUB_DECODER)          += dvdsubdec.o
... ...
@@ -308,6 +308,7 @@ void avcodec_register_all(void)
308 308
     REGISTER_DECODER(COOK,              cook);
309 309
     REGISTER_DECODER(DCA,               dca);
310 310
     REGISTER_DECODER(DSICINAUDIO,       dsicinaudio);
311
+    REGISTER_DECODER(DSS_SP,            dss_sp);
311 312
     REGISTER_ENCDEC (EAC3,              eac3);
312 313
     REGISTER_ENCDEC (FLAC,              flac);
313 314
     REGISTER_DECODER(G723_1,            g723_1);
... ...
@@ -444,6 +444,7 @@ enum AVCodecID {
444 444
     AV_CODEC_ID_METASOUND,
445 445
     AV_CODEC_ID_PAF_AUDIO,
446 446
     AV_CODEC_ID_ON2AVC,
447
+    AV_CODEC_ID_DSS_SP,
447 448
 
448 449
     /* subtitle codecs */
449 450
     AV_CODEC_ID_FIRST_SUBTITLE = 0x17000,          ///< A dummy ID pointing at the start of subtitle codecs.
... ...
@@ -2202,6 +2202,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
2202 2202
         .props     = AV_CODEC_PROP_LOSSY,
2203 2203
     },
2204 2204
     {
2205
+        .id        = AV_CODEC_ID_DSS_SP,
2206
+        .type      = AVMEDIA_TYPE_AUDIO,
2207
+        .name      = "dss_sp",
2208
+        .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard - Standard Play mode (DSS SP)"),
2209
+        .props     = AV_CODEC_PROP_LOSSY,
2210
+    },
2211
+    {
2205 2212
         .id        = AV_CODEC_ID_G729,
2206 2213
         .type      = AVMEDIA_TYPE_AUDIO,
2207 2214
         .name      = "g729",
2208 2215
new file mode 100644
... ...
@@ -0,0 +1,780 @@
0
+/*
1
+ * Digital Speech Standard - Standard Play mode (DSS SP) audio decoder.
2
+ * Copyright (C) 2014 Oleksij Rempel <linux@rempel-privat.de>
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav 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
+ * Libav 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 Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavutil/channel_layout.h"
22
+#include "libavutil/common.h"
23
+#include "libavutil/mem.h"
24
+#include "libavutil/opt.h"
25
+
26
+#include "avcodec.h"
27
+#include "get_bits.h"
28
+#include "internal.h"
29
+
30
+#define SUBFRAMES 4
31
+#define PULSE_MAX 8
32
+
33
+#define DSS_SP_FRAME_SIZE        42
34
+#define DSS_SP_SAMPLE_COUNT     (66 * SUBFRAMES)
35
+#define DSS_SP_FORMULA(a, b, c) ((((a) << 15) + (b) * (c)) + 0x4000) >> 15
36
+
37
+typedef struct DssSpSubframe {
38
+    int16_t gain;
39
+    int32_t combined_pulse_pos;
40
+    int16_t pulse_pos[7];
41
+    int16_t pulse_val[7];
42
+} DssSpSubframe;
43
+
44
+typedef struct DssSpFrame {
45
+    int16_t filter_idx[14];
46
+    int16_t sf_adaptive_gain[SUBFRAMES];
47
+    int16_t pitch_lag[SUBFRAMES];
48
+    struct DssSpSubframe sf[SUBFRAMES];
49
+} DssSpFrame;
50
+
51
+typedef struct DssSpContext {
52
+    int32_t excitation[288 + 6];
53
+    int32_t history[187];
54
+    DssSpFrame fparam;
55
+    int32_t working_buffer[SUBFRAMES][72];
56
+    int32_t audio_buf[15];
57
+    int32_t err_buf1[15];
58
+    int32_t lpc_filter[14];
59
+    int32_t filter[15];
60
+    int32_t vector_buf[72];
61
+    int noise_state;
62
+    int32_t err_buf2[15];
63
+
64
+    int pulse_dec_mode;
65
+
66
+    DECLARE_ALIGNED(16, uint8_t, bits)[DSS_SP_FRAME_SIZE +
67
+                                       FF_INPUT_BUFFER_PADDING_SIZE];
68
+} DssSpContext;
69
+
70
+/*
71
+ * Used for the coding/decoding of the pulse positions for the MP-MLQ codebook.
72
+ */
73
+static const uint32_t dss_sp_combinatorial_table[PULSE_MAX][72] = {
74
+    {       0,         0,         0,          0,          0,          0,
75
+            0,         0,         0,          0,          0,          0,
76
+            0,         0,         0,          0,          0,          0,
77
+            0,         0,         0,          0,          0,          0,
78
+            0,         0,         0,          0,          0,          0,
79
+            0,         0,         0,          0,          0,          0,
80
+            0,         0,         0,          0,          0,          0,
81
+            0,         0,         0,          0,          0,          0,
82
+            0,         0,         0,          0,          0,          0,
83
+            0,         0,         0,          0,          0,          0,
84
+            0,         0,         0,          0,          0,          0,
85
+            0,         0,         0,          0,          0,          0 },
86
+    {       0,         1,         2,          3,          4,          5,
87
+            6,         7,         8,          9,         10,         11,
88
+           12,        13,        14,         15,         16,         17,
89
+           18,        19,        20,         21,         22,         23,
90
+           24,        25,        26,         27,         28,         29,
91
+           30,        31,        32,         33,         34,         35,
92
+           36,        37,        38,         39,         40,         41,
93
+           42,        43,        44,         45,         46,         47,
94
+           48,        49,        50,         51,         52,         53,
95
+           54,        55,        56,         57,         58,         59,
96
+           60,        61,        62,         63,         64,         65,
97
+           66,        67,        68,         69,         70,         71 },
98
+    {       0,         0,         1,          3,          6,         10,
99
+           15,        21,        28,         36,         45,         55,
100
+           66,        78,        91,        105,        120,        136,
101
+          153,       171,       190,        210,        231,        253,
102
+          276,       300,       325,        351,        378,        406,
103
+          435,       465,       496,        528,        561,        595,
104
+          630,       666,       703,        741,        780,        820,
105
+          861,       903,       946,        990,       1035,       1081,
106
+         1128,      1176,      1225,       1275,       1326,       1378,
107
+         1431,      1485,      1540,       1596,       1653,       1711,
108
+         1770,      1830,      1891,       1953,       2016,       2080,
109
+         2145,      2211,      2278,       2346,       2415,       2485 },
110
+    {       0,         0,         0,          1,          4,         10,
111
+           20,        35,        56,         84,        120,        165,
112
+          220,       286,       364,        455,        560,        680,
113
+          816,       969,      1140,       1330,       1540,       1771,
114
+         2024,      2300,      2600,       2925,       3276,       3654,
115
+         4060,      4495,      4960,       5456,       5984,       6545,
116
+         7140,      7770,      8436,       9139,       9880,      10660,
117
+        11480,     12341,     13244,      14190,      15180,      16215,
118
+        17296,     18424,     19600,      20825,      22100,      23426,
119
+        24804,     26235,     27720,      29260,      30856,      32509,
120
+        34220,     35990,     37820,      39711,      41664,      43680,
121
+        45760,     47905,     50116,      52394,      54740,      57155 },
122
+    {       0,         0,         0,          0,          1,          5,
123
+           15,        35,        70,        126,        210,        330,
124
+          495,       715,      1001,       1365,       1820,       2380,
125
+         3060,      3876,      4845,       5985,       7315,       8855,
126
+        10626,     12650,     14950,      17550,      20475,      23751,
127
+        27405,     31465,     35960,      40920,      46376,      52360,
128
+        58905,     66045,     73815,      82251,      91390,     101270,
129
+       111930,    123410,    135751,     148995,     163185,     178365,
130
+       194580,    211876,    230300,     249900,     270725,     292825,
131
+       316251,    341055,    367290,     395010,     424270,     455126,
132
+       487635,    521855,    557845,     595665,     635376,     677040,
133
+       720720,    766480,    814385,     864501,     916895,     971635 },
134
+    {       0,         0,         0,          0,          0,          1,
135
+            6,        21,        56,        126,        252,        462,
136
+          792,      1287,      2002,       3003,       4368,       6188,
137
+         8568,     11628,     15504,      20349,      26334,      33649,
138
+        42504,     53130,     65780,      80730,      98280,     118755,
139
+       142506,    169911,    201376,     237336,     278256,     324632,
140
+       376992,    435897,    501942,     575757,     658008,     749398,
141
+       850668,    962598,   1086008,    1221759,    1370754,    1533939,
142
+      1712304,   1906884,   2118760,    2349060,    2598960,    2869685,
143
+      3162510,   3478761,   3819816,    4187106,    4582116,    5006386,
144
+      5461512,   5949147,   6471002,    7028847,    7624512,    8259888,
145
+      8936928,   9657648,  10424128,   11238513,   12103014,   13019909 },
146
+    {       0,         0,         0,          0,          0,          0,
147
+            1,         7,        28,         84,        210,        462,
148
+          924,      1716,      3003,       5005,       8008,      12376,
149
+        18564,     27132,     38760,      54264,      74613,     100947,
150
+       134596,    177100,    230230,     296010,     376740,     475020,
151
+       593775,    736281,    906192,    1107568,    1344904,    1623160,
152
+      1947792,   2324784,   2760681,    3262623,    3838380,    4496388,
153
+      5245786,   6096454,   7059052,    8145060,    9366819,   10737573,
154
+     12271512,  13983816,  15890700,   18009460,   20358520,   22957480,
155
+     25827165,  28989675,  32468436,   36288252,   40475358,   45057474,
156
+     50063860,  55525372,  61474519,   67945521,   74974368,   82598880,
157
+     90858768,  99795696, 109453344,  119877472,  131115985,  143218999 },
158
+    {       0,         0,         0,          0,          0,          0,
159
+            0,         1,         8,         36,        120,        330,
160
+          792,      1716,      3432,       6435,      11440,      19448,
161
+        31824,     50388,     77520,     116280,     170544,     245157,
162
+       346104,    480700,    657800,     888030,    1184040,    1560780,
163
+      2035800,   2629575,   3365856,    4272048,    5379616,    6724520,
164
+      8347680,  10295472,  12620256,   15380937,   18643560,   22481940,
165
+     26978328,  32224114,  38320568,   45379620,   53524680,   62891499,
166
+     73629072,  85900584,  99884400,  115775100,  133784560,  154143080,
167
+    177100560, 202927725, 231917400,  264385836,  300674088,  341149446,
168
+    386206920, 436270780, 491796152,  553270671,  621216192,  696190560,
169
+    778789440, 869648208, 969443904, 1078897248, 1198774720, 1329890705 },
170
+};
171
+
172
+static const int16_t dss_sp_filter_cb[14][32] = {
173
+    { -32653, -32587, -32515, -32438, -32341, -32216, -32062, -31881,
174
+      -31665, -31398, -31080, -30724, -30299, -29813, -29248, -28572,
175
+      -27674, -26439, -24666, -22466, -19433, -16133, -12218,  -7783,
176
+       -2834,   1819,   6544,  11260,  16050,  20220,  24774,  28120 },
177
+
178
+    { -27503, -24509, -20644, -17496, -14187, -11277,  -8420,  -5595,
179
+       -3013,   -624,   1711,   3880,   5844,   7774,   9739,  11592,
180
+       13364,  14903,  16426,  17900,  19250,  20586,  21803,  23006,
181
+       24142,  25249,  26275,  27300,  28359,  29249,  30118,  31183 },
182
+
183
+    { -27827, -24208, -20943, -17781, -14843, -11848,  -9066,  -6297,
184
+       -3660,   -910,   1918,   5025,   8223,  11649,  15086,  18423,
185
+           0,      0,      0,      0,      0,      0,      0,      0,
186
+           0,      0,      0,      0,      0,      0,      0,      0 },
187
+
188
+    { -17128, -11975,  -8270,  -5123,  -2296,    183,   2503,   4707,
189
+        6798,   8945,  11045,  13239,  15528,  18248,  21115,  24785,
190
+           0,      0,      0,      0,      0,      0,      0,      0,
191
+           0,      0,      0,      0,      0,      0,      0,      0 },
192
+
193
+    { -21557, -17280, -14286, -11644,  -9268,  -7087,  -4939,  -2831,
194
+        -691,   1407,   3536,   5721,   8125,  10677,  13721,  17731,
195
+           0,      0,      0,      0,      0,      0,      0,      0,
196
+           0,      0,      0,      0,      0,      0,      0,      0 },
197
+
198
+    { -15030, -10377,  -7034,  -4327,  -1900,    364,   2458,   4450,
199
+        6422,   8374,  10374,  12486,  14714,  16997,  19626,  22954,
200
+           0,      0,      0,      0,      0,      0,      0,      0,
201
+           0,      0,      0,      0,      0,      0,      0,      0 },
202
+
203
+    { -16155, -12362,  -9698,  -7460,  -5258,  -3359,  -1547,    219,
204
+        1916,   3599,   5299,   6994,   8963,  11226,  13716,  16982,
205
+           0,      0,      0,      0,      0,      0,      0,      0,
206
+           0,      0,      0,      0,      0,      0,      0,      0 },
207
+
208
+    { -14742,  -9848,  -6921,  -4648,  -2769,  -1065,    499,   2083,
209
+        3633,   5219,   6857,   8580,  10410,  12672,  15561,  20101,
210
+           0,      0,      0,      0,      0,      0,      0,      0,
211
+           0,      0,      0,      0,      0,      0,      0,      0 },
212
+
213
+    { -11099,  -7014,  -3855,  -1025,   1680,   4544,   7807,  11932,
214
+           0,      0,      0,      0,      0,      0,      0,      0,
215
+           0,      0,      0,      0,      0,      0,      0,      0,
216
+           0,      0,      0,      0,      0,      0,      0,      0 },
217
+
218
+    {  -9060,  -4570,  -1381,   1419,   4034,   6728,   9865,  14149,
219
+           0,      0,      0,      0,      0,      0,      0,      0,
220
+           0,      0,      0,      0,      0,      0,      0,      0,
221
+           0,      0,      0,      0,      0,      0,      0,      0 },
222
+
223
+    { -12450,  -7985,  -4596,  -1734,    961,   3629,   6865,  11142,
224
+           0,      0,      0,      0,      0,      0,      0,      0,
225
+           0,      0,      0,      0,      0,      0,      0,      0,
226
+           0,      0,      0,      0,      0,      0,      0,      0 },
227
+
228
+    { -11831,  -7404,  -4010,  -1096,   1606,   4291,   7386,  11482,
229
+           0,      0,      0,      0,      0,      0,      0,      0,
230
+           0,      0,      0,      0,      0,      0,      0,      0,
231
+           0,      0,      0,      0,      0,      0,      0,      0 },
232
+
233
+    { -13404,  -9250,  -5995,  -3312,   -890,   1594,   4464,   8198,
234
+           0,      0,      0,      0,      0,      0,      0,      0,
235
+           0,      0,      0,      0,      0,      0,      0,      0,
236
+           0,      0,      0,      0,      0,      0,      0,      0 },
237
+
238
+    { -11239,  -7220,  -4040,  -1406,    971,   3321,   6006,   9697,
239
+           0,      0,      0,      0,      0,      0,      0,      0,
240
+           0,      0,      0,      0,      0,      0,      0,      0,
241
+           0,      0,      0,      0,      0,      0,      0,      0 },
242
+};
243
+
244
+static const uint16_t  dss_sp_fixed_cb_gain[64] = {
245
+       0,    4,    8,   13,   17,   22,   26,   31,
246
+      35,   40,   44,   48,   53,   58,   63,   69,
247
+      76,   83,   91,   99,  109,  119,  130,  142,
248
+     155,  170,  185,  203,  222,  242,  265,  290,
249
+     317,  346,  378,  414,  452,  494,  540,  591,
250
+     646,  706,  771,  843,  922, 1007, 1101, 1204,
251
+    1316, 1438, 1572, 1719, 1879, 2053, 2244, 2453,
252
+    2682, 2931, 3204, 3502, 3828, 4184, 4574, 5000,
253
+};
254
+
255
+static const int16_t  dss_sp_pulse_val[8] = {
256
+    -31182, -22273, -13364, -4455, 4455, 13364, 22273, 31182
257
+};
258
+
259
+static const uint16_t binary_decreasing_array[] = {
260
+    32767, 16384, 8192, 4096, 2048, 1024, 512, 256,
261
+    128, 64, 32, 16, 8, 4, 2,
262
+};
263
+
264
+static const uint16_t dss_sp_unc_decreasing_array[] = {
265
+    32767, 26214, 20972, 16777, 13422, 10737, 8590, 6872,
266
+    5498, 4398, 3518, 2815, 2252, 1801, 1441,
267
+};
268
+
269
+static const uint16_t dss_sp_adaptive_gain[] = {
270
+     102,  231,  360,  488,  617,  746,  875, 1004,
271
+    1133, 1261, 1390, 1519, 1648, 1777, 1905, 2034,
272
+    2163, 2292, 2421, 2550, 2678, 2807, 2936, 3065,
273
+    3194, 3323, 3451, 3580, 3709, 3838, 3967, 4096,
274
+};
275
+
276
+static const int32_t dss_sp_sinc[67] = {
277
+      262,   293,   323,   348,   356,   336,   269,   139,
278
+      -67,  -358,  -733, -1178, -1668, -2162, -2607, -2940,
279
+    -3090, -2986, -2562, -1760,  -541,  1110,  3187,  5651,
280
+     8435, 11446, 14568, 17670, 20611, 23251, 25460, 27125,
281
+    28160, 28512, 28160,
282
+    27125, 25460, 23251, 20611, 17670, 14568, 11446,  8435,
283
+     5651,  3187,  1110,  -541, -1760, -2562, -2986, -3090,
284
+    -2940, -2607, -2162, -1668, -1178,  -733,  -358,   -67,
285
+      139,   269,   336,   356,   348,   323,   293,   262,
286
+};
287
+
288
+static av_cold int dss_sp_decode_init(AVCodecContext *avctx)
289
+{
290
+    DssSpContext *p = avctx->priv_data;
291
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
292
+    avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
293
+    avctx->channels       = 1;
294
+    avctx->sample_rate    = 11025;
295
+
296
+    memset(p->history, 0, sizeof(p->history));
297
+    p->pulse_dec_mode = 1;
298
+
299
+    return 0;
300
+}
301
+
302
+static void dss_sp_unpack_coeffs(DssSpContext *p, const uint8_t *src)
303
+{
304
+    GetBitContext gb;
305
+    DssSpFrame *fparam = &p->fparam;
306
+    int i;
307
+    int subframe_idx;
308
+    uint32_t combined_pitch;
309
+    uint32_t tmp;
310
+    uint32_t pitch_lag;
311
+
312
+    for (i = 0; i < DSS_SP_FRAME_SIZE; i += 2) {
313
+        p->bits[i]     = src[i + 1];
314
+        p->bits[i + 1] = src[i];
315
+    }
316
+
317
+    init_get_bits(&gb, p->bits, DSS_SP_FRAME_SIZE * 8);
318
+
319
+    for (i = 0; i < 2; i++)
320
+        fparam->filter_idx[i] = get_bits(&gb, 5);
321
+    for (; i < 8; i++)
322
+        fparam->filter_idx[i] = get_bits(&gb, 4);
323
+    for (; i < 14; i++)
324
+        fparam->filter_idx[i] = get_bits(&gb, 3);
325
+
326
+    for (subframe_idx = 0; subframe_idx < 4; subframe_idx++) {
327
+        fparam->sf_adaptive_gain[subframe_idx] = get_bits(&gb, 5);
328
+
329
+        fparam->sf[subframe_idx].combined_pulse_pos = get_bits_long(&gb, 31);
330
+
331
+        fparam->sf[subframe_idx].gain = get_bits(&gb, 6);
332
+
333
+        for (i = 0; i < 7; i++)
334
+            fparam->sf[subframe_idx].pulse_val[i] = get_bits(&gb, 3);
335
+    }
336
+
337
+    for (subframe_idx = 0; subframe_idx < 4; subframe_idx++) {
338
+        unsigned int C72_binomials[PULSE_MAX] = {
339
+            72, 2556, 59640, 1028790, 13991544, 156238908, 1473109704,
340
+            3379081753
341
+        };
342
+        unsigned int combined_pulse_pos =
343
+            fparam->sf[subframe_idx].combined_pulse_pos;
344
+        int index = 6;
345
+
346
+        if (combined_pulse_pos < C72_binomials[PULSE_MAX - 1]) {
347
+            if (p->pulse_dec_mode) {
348
+                int pulse, pulse_idx;
349
+                pulse              = PULSE_MAX - 1;
350
+                pulse_idx          = 71;
351
+                combined_pulse_pos =
352
+                    fparam->sf[subframe_idx].combined_pulse_pos;
353
+
354
+                /* this part seems to be close to g723.1 gen_fcb_excitation()
355
+                 * RATE_6300 */
356
+
357
+                /* TODO: what is 7? size of subframe? */
358
+                for (i = 0; i < 7; i++) {
359
+                    for (;
360
+                         combined_pulse_pos <
361
+                         dss_sp_combinatorial_table[pulse][pulse_idx];
362
+                         --pulse_idx)
363
+                        ;
364
+                    combined_pulse_pos -=
365
+                        dss_sp_combinatorial_table[pulse][pulse_idx];
366
+                    pulse--;
367
+                    fparam->sf[subframe_idx].pulse_pos[i] = pulse_idx;
368
+                }
369
+            }
370
+        } else {
371
+            p->pulse_dec_mode = 0;
372
+
373
+            /* why do we need this? */
374
+            fparam->sf[subframe_idx].pulse_pos[6] = 0;
375
+
376
+            for (i = 71; i >= 0; i--) {
377
+                if (C72_binomials[index] <= combined_pulse_pos) {
378
+                    combined_pulse_pos -= C72_binomials[index];
379
+
380
+                    fparam->sf[subframe_idx].pulse_pos[(index ^ 7) - 1] = i;
381
+
382
+                    if (!index)
383
+                        break;
384
+                    --index;
385
+                }
386
+                --C72_binomials[0];
387
+                if (index) {
388
+                    int a;
389
+                    for (a = 0; a < index; a++)
390
+                        C72_binomials[a + 1] -= C72_binomials[a];
391
+                }
392
+            }
393
+        }
394
+    }
395
+
396
+    combined_pitch = get_bits(&gb, 24);
397
+
398
+    fparam->pitch_lag[0] = (combined_pitch % 151) + 36;
399
+
400
+    combined_pitch /= 151;
401
+
402
+    for (i = 1; i < SUBFRAMES; i++) {
403
+        fparam->pitch_lag[i] = combined_pitch % 48;
404
+        combined_pitch      /= 48;
405
+    }
406
+
407
+    pitch_lag = fparam->pitch_lag[0];
408
+    for (i = 1; i < SUBFRAMES; i++) {
409
+        if (pitch_lag > 162) {
410
+            fparam->pitch_lag[i] += 162 - 23;
411
+        } else {
412
+            tmp = pitch_lag - 23;
413
+            if (tmp < 36)
414
+                tmp = 36;
415
+            fparam->pitch_lag[i] += tmp;
416
+        }
417
+        pitch_lag = fparam->pitch_lag[i];
418
+    }
419
+}
420
+
421
+static void dss_sp_unpack_filter(DssSpContext *p)
422
+{
423
+    int i;
424
+
425
+    for (i = 0; i < 14; i++)
426
+        p->lpc_filter[i] = dss_sp_filter_cb[i][p->fparam.filter_idx[i]];
427
+}
428
+
429
+static void dss_sp_convert_coeffs(int32_t *lpc_filter, int32_t *coeffs)
430
+{
431
+    int a, a_plus, i;
432
+
433
+    coeffs[0] = 0x2000;
434
+    for (a = 0; a < 14; a++) {
435
+        a_plus         = a + 1;
436
+        coeffs[a_plus] = lpc_filter[a] >> 2;
437
+        if (a_plus / 2 >= 1) {
438
+            for (i = 1; i <= a_plus / 2; i++) {
439
+                int coeff_1, coeff_2, tmp;
440
+
441
+                coeff_1 = coeffs[i];
442
+                coeff_2 = coeffs[a_plus - i];
443
+
444
+                tmp = DSS_SP_FORMULA(coeff_1, lpc_filter[a], coeff_2);
445
+                coeffs[i] = av_clip_int16(tmp);
446
+
447
+                tmp = DSS_SP_FORMULA(coeff_2, lpc_filter[a], coeff_1);
448
+                coeffs[a_plus - i] = av_clip_int16(tmp);
449
+            }
450
+        }
451
+    }
452
+}
453
+
454
+static void dss_sp_add_pulses(int32_t *vector_buf,
455
+                              const struct DssSpSubframe *sf)
456
+{
457
+    int i;
458
+
459
+    for (i = 0; i < 7; i++)
460
+        vector_buf[sf->pulse_pos[i]] += (dss_sp_fixed_cb_gain[sf->gain] *
461
+                                         dss_sp_pulse_val[sf->pulse_val[i]] +
462
+                                         0x4000) >> 15;
463
+}
464
+
465
+static void dss_sp_gen_exc(int32_t *vector, int32_t *prev_exc,
466
+                           int pitch_lag, int gain)
467
+{
468
+    int i;
469
+
470
+    /* do we actually need this check? we can use just [a3 - i % a3]
471
+     * for both cases */
472
+    if (pitch_lag < 72)
473
+        for (i = 0; i < 72; i++)
474
+            vector[i] = prev_exc[pitch_lag - i % pitch_lag];
475
+    else
476
+        for (i = 0; i < 72; i++)
477
+            vector[i] = prev_exc[pitch_lag - i];
478
+
479
+    for (i = 0; i < 72; i++) {
480
+        int tmp = gain * vector[i] >> 11;
481
+        vector[i] = av_clip_int16(tmp);
482
+    }
483
+}
484
+
485
+static void dss_sp_scale_vector(int32_t *vec, int bits, int size)
486
+{
487
+    int i;
488
+
489
+    if (bits < 0)
490
+        for (i = 0; i < size; i++)
491
+            vec[i] = vec[i] >> -bits;
492
+    else
493
+        for (i = 0; i < size; i++)
494
+            vec[i] = vec[i] << bits;
495
+}
496
+
497
+static void dss_sp_update_buf(int32_t *hist, int32_t *vector)
498
+{
499
+    int i;
500
+
501
+    for (i = 114; i > 0; i--)
502
+        vector[i + 72] = vector[i];
503
+
504
+    for (i = 0; i < 72; i++)
505
+        vector[72 - i] = hist[i];
506
+}
507
+
508
+static void dss_sp_shift_sq_sub(const int32_t *filter_buf,
509
+                                int32_t *error_buf, int32_t *dst)
510
+{
511
+    int a;
512
+
513
+    for (a = 0; a < 72; a++) {
514
+        int i, tmp;
515
+
516
+        tmp = dst[a] * filter_buf[0];
517
+
518
+        for (i = 14; i > 0; i--)
519
+            tmp -= error_buf[i] * filter_buf[i];
520
+
521
+        for (i = 14; i > 0; i--)
522
+            error_buf[i] = error_buf[i - 1];
523
+
524
+        tmp = (tmp + 4096) >> 13;
525
+
526
+        error_buf[1] = tmp;
527
+
528
+        dst[a] = av_clip_int16(tmp);
529
+    }
530
+}
531
+
532
+static void dss_sp_shift_sq_add(const int32_t *filter_buf, int32_t *audio_buf,
533
+                                int32_t *dst)
534
+{
535
+    int a;
536
+
537
+    for (a = 0; a < 72; a++) {
538
+        int i, tmp = 0;
539
+
540
+        audio_buf[0] = dst[a];
541
+
542
+        for (i = 14; i >= 0; i--)
543
+            tmp += audio_buf[i] * filter_buf[i];
544
+
545
+        for (i = 14; i > 0; i--)
546
+            audio_buf[i] = audio_buf[i - 1];
547
+
548
+        tmp = (tmp + 4096) >> 13;
549
+
550
+        dst[a] = av_clip_int16(tmp);
551
+    }
552
+}
553
+
554
+static void dss_sp_vec_mult(const int32_t *src, int32_t *dst,
555
+                            const int16_t *mult)
556
+{
557
+    int i;
558
+
559
+    dst[0] = src[0];
560
+
561
+    for (i = 1; i < 15; i++)
562
+        dst[i] = (src[i] * mult[i] + 0x4000) >> 15;
563
+}
564
+
565
+static int dss_sp_get_normalize_bits(int32_t *vector_buf, int16_t size)
566
+{
567
+    unsigned int val;
568
+    int max_val;
569
+    int i;
570
+
571
+    val = 1;
572
+    for (i = 0; i < size; i++)
573
+        val |= FFABS(vector_buf[i]);
574
+
575
+    for (max_val = 0; val <= 0x4000; ++max_val)
576
+        val *= 2;
577
+    return max_val;
578
+}
579
+
580
+static int dss_sp_vector_sum(DssSpContext *p, int size)
581
+{
582
+    int i, sum = 0;
583
+    for (i = 0; i < size; i++)
584
+        sum += FFABS(p->vector_buf[i]);
585
+    return sum;
586
+}
587
+
588
+static void dss_sp_sf_synthesis(DssSpContext *p, int32_t lpc_filter,
589
+                                int32_t *dst, int size)
590
+{
591
+    int32_t tmp_buf[15];
592
+    int32_t noise[72];
593
+    int bias, vsum_2 = 0, vsum_1 = 0, v36, normalize_bits;
594
+    int i, tmp;
595
+
596
+    if (size > 0) {
597
+        vsum_1 = dss_sp_vector_sum(p, size);
598
+
599
+        if (vsum_1 > 0xFFFFF)
600
+            vsum_1 = 0xFFFFF;
601
+    }
602
+
603
+    normalize_bits = dss_sp_get_normalize_bits(p->vector_buf, size);
604
+
605
+    dss_sp_scale_vector(p->vector_buf, normalize_bits - 3, size);
606
+    dss_sp_scale_vector(p->audio_buf, normalize_bits, 15);
607
+    dss_sp_scale_vector(p->err_buf1, normalize_bits, 15);
608
+
609
+    v36 = p->err_buf1[1];
610
+
611
+    dss_sp_vec_mult(p->filter, tmp_buf, binary_decreasing_array);
612
+    dss_sp_shift_sq_add(tmp_buf, p->audio_buf, p->vector_buf);
613
+
614
+    dss_sp_vec_mult(p->filter, tmp_buf, dss_sp_unc_decreasing_array);
615
+    dss_sp_shift_sq_sub(tmp_buf, p->err_buf1, p->vector_buf);
616
+
617
+    /* lpc_filter can be negative */
618
+    lpc_filter = lpc_filter >> 1;
619
+    if (lpc_filter >= 0)
620
+        lpc_filter = 0;
621
+
622
+    if (size > 1) {
623
+        for (i = size - 1; i > 0; i--) {
624
+            tmp = DSS_SP_FORMULA(p->vector_buf[i], lpc_filter,
625
+                                 p->vector_buf[i - 1]);
626
+            p->vector_buf[i] = av_clip_int16(tmp);
627
+        }
628
+    }
629
+
630
+    tmp              = DSS_SP_FORMULA(p->vector_buf[0], lpc_filter, v36);
631
+    p->vector_buf[0] = av_clip_int16(tmp);
632
+
633
+    dss_sp_scale_vector(p->vector_buf, -normalize_bits, size);
634
+    dss_sp_scale_vector(p->audio_buf, -normalize_bits, 15);
635
+    dss_sp_scale_vector(p->err_buf1, -normalize_bits, 15);
636
+
637
+    if (size > 0)
638
+        vsum_2 = dss_sp_vector_sum(p, size);
639
+
640
+    if (vsum_2 >= 0x40)
641
+        tmp = (vsum_1 << 11) / vsum_2;
642
+    else
643
+        tmp = 1;
644
+
645
+    bias     = 409 * tmp >> 15 << 15;
646
+    tmp      = (bias + 32358 * p->noise_state) >> 15;
647
+    noise[0] = av_clip_int16(tmp);
648
+
649
+    for (i = 1; i < size; i++) {
650
+        tmp      = (bias + 32358 * noise[i - 1]) >> 15;
651
+        noise[i] = av_clip_int16(tmp);
652
+    }
653
+
654
+    p->noise_state = noise[size - 1];
655
+    for (i = 0; i < size; i++) {
656
+        tmp    = (p->vector_buf[i] * noise[i]) >> 11;
657
+        dst[i] = av_clip_int16(tmp);
658
+    }
659
+}
660
+
661
+static void dss_sp_update_state(DssSpContext *p, int32_t *dst)
662
+{
663
+    int i, offset = 6, counter = 0, a = 0;
664
+
665
+    for (i = 0; i < 6; i++)
666
+        p->excitation[i] = p->excitation[288 + i];
667
+
668
+    for (i = 0; i < 72 * SUBFRAMES; i++)
669
+        p->excitation[6 + i] = dst[i];
670
+
671
+    do {
672
+        int tmp = 0;
673
+
674
+        for (i = 0; i < 6; i++)
675
+            tmp += p->excitation[offset--] * dss_sp_sinc[a + i * 11];
676
+
677
+        offset += 7;
678
+
679
+        tmp >>= 15;
680
+        dst[counter] = av_clip_int16(tmp);
681
+
682
+        counter++;
683
+
684
+        a = (a + 1) % 11;
685
+        if (!a)
686
+            offset++;
687
+    } while (offset < FF_ARRAY_ELEMS(p->excitation));
688
+}
689
+
690
+static void dss_sp_32to16bit(int16_t *dst, int32_t *src, int size)
691
+{
692
+    int i;
693
+
694
+    for (i = 0; i < size; i++)
695
+        dst[i] = av_clip_int16(src[i]);
696
+}
697
+
698
+static int dss_sp_decode_one_frame(DssSpContext *p,
699
+                                   int16_t *abuf_dst, const uint8_t *abuf_src)
700
+{
701
+    int i, j;
702
+
703
+    dss_sp_unpack_coeffs(p, abuf_src);
704
+
705
+    dss_sp_unpack_filter(p);
706
+
707
+    dss_sp_convert_coeffs(p->lpc_filter, p->filter);
708
+
709
+    for (j = 0; j < SUBFRAMES; j++) {
710
+        dss_sp_gen_exc(p->vector_buf, p->history,
711
+                       p->fparam.pitch_lag[j],
712
+                       dss_sp_adaptive_gain[p->fparam.sf_adaptive_gain[j]]);
713
+
714
+        dss_sp_add_pulses(p->vector_buf, &p->fparam.sf[j]);
715
+
716
+        dss_sp_update_buf(p->vector_buf, p->history);
717
+
718
+        for (i = 0; i < 72; i++)
719
+            p->vector_buf[i] = p->history[72 - i];
720
+
721
+        dss_sp_shift_sq_sub(p->filter,
722
+                            p->err_buf2, p->vector_buf);
723
+
724
+        dss_sp_sf_synthesis(p, p->lpc_filter[0],
725
+                            &p->working_buffer[j][0], 72);
726
+    }
727
+
728
+    dss_sp_update_state(p, &p->working_buffer[0][0]);
729
+
730
+    dss_sp_32to16bit(abuf_dst,
731
+                     &p->working_buffer[0][0], 264);
732
+    return 0;
733
+}
734
+
735
+static int dss_sp_decode_frame(AVCodecContext *avctx, void *data,
736
+                               int *got_frame_ptr, AVPacket *avpkt)
737
+{
738
+    DssSpContext *p    = avctx->priv_data;
739
+    AVFrame *frame     = data;
740
+    const uint8_t *buf = avpkt->data;
741
+    int buf_size       = avpkt->size;
742
+
743
+    int16_t *out;
744
+    int ret;
745
+
746
+    if (buf_size < DSS_SP_FRAME_SIZE) {
747
+        if (buf_size)
748
+            av_log(avctx, AV_LOG_WARNING,
749
+                   "Expected %d bytes, got %d - skipping packet.\n",
750
+                   DSS_SP_FRAME_SIZE, buf_size);
751
+        *got_frame_ptr = 0;
752
+        return AVERROR_INVALIDDATA;
753
+    }
754
+
755
+    frame->nb_samples = DSS_SP_SAMPLE_COUNT;
756
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
757
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
758
+        return ret;
759
+    }
760
+
761
+    out = (int16_t *)frame->data[0];
762
+
763
+    dss_sp_decode_one_frame(p, out, buf);
764
+
765
+    *got_frame_ptr = 1;
766
+
767
+    return DSS_SP_FRAME_SIZE;
768
+}
769
+
770
+AVCodec ff_dss_sp_decoder = {
771
+    .name           = "DSS SP",
772
+    .long_name      = NULL_IF_CONFIG_SMALL("Digital Speech Standard - Standard Play mode (DSS SP)"),
773
+    .type           = AVMEDIA_TYPE_AUDIO,
774
+    .id             = AV_CODEC_ID_DSS_SP,
775
+    .priv_data_size = sizeof(DssSpContext),
776
+    .init           = dss_sp_decode_init,
777
+    .decode         = dss_sp_decode_frame,
778
+    .capabilities   = CODEC_CAP_DR1,
779
+};
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/version.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 56
32
-#define LIBAVCODEC_VERSION_MINOR 14
32
+#define LIBAVCODEC_VERSION_MINOR 15
33 33
 #define LIBAVCODEC_VERSION_MICRO  0
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \