Browse code

LucasArts SMUSH VIMA audio decoder

Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>

Paul B Mahol authored on 2012/03/19 11:52:08
Showing 8 changed files
... ...
@@ -15,6 +15,7 @@ version <next>:
15 15
 - support decoding 16-bit RLE SGI images
16 16
 - VP7 video decoder
17 17
 - LucasArts SMUSH SANM video decoder
18
+- LucasArts SMUSH VIMA audio decoder (ADPCM)
18 19
 
19 20
 
20 21
 version 10:
... ...
@@ -780,6 +780,8 @@ following image formats are supported:
780 780
 @item ADPCM Sound Blaster Pro 2-bit  @tab     @tab  X
781 781
 @item ADPCM Sound Blaster Pro 2.6-bit  @tab     @tab  X
782 782
 @item ADPCM Sound Blaster Pro 4-bit  @tab     @tab  X
783
+@item ADPCM VIMA
784
+    @tab Used in LucasArts SMUSH animations.
783 785
 @item ADPCM Westwood Studios IMA @tab     @tab  X
784 786
     @tab Used in Westwood Studios games like Command and Conquer.
785 787
 @item ADPCM Yamaha           @tab  X  @tab  X
... ...
@@ -514,6 +514,7 @@ OBJS-$(CONFIG_ADPCM_SBPRO_4_DECODER)      += adpcm.o adpcm_data.o
514 514
 OBJS-$(CONFIG_ADPCM_SWF_DECODER)          += adpcm.o adpcm_data.o
515 515
 OBJS-$(CONFIG_ADPCM_SWF_ENCODER)          += adpcmenc.o adpcm_data.o
516 516
 OBJS-$(CONFIG_ADPCM_THP_DECODER)          += adpcm.o adpcm_data.o
517
+OBJS-$(CONFIG_ADPCM_VIMA_DECODER)         += vima.o adpcm_data.o
517 518
 OBJS-$(CONFIG_ADPCM_XA_DECODER)           += adpcm.o adpcm_data.o
518 519
 OBJS-$(CONFIG_ADPCM_YAMAHA_DECODER)       += adpcm.o adpcm_data.o
519 520
 OBJS-$(CONFIG_ADPCM_YAMAHA_ENCODER)       += adpcmenc.o adpcm_data.o
... ...
@@ -415,6 +415,7 @@ void avcodec_register_all(void)
415 415
     REGISTER_DECODER(ADPCM_SBPRO_4,     adpcm_sbpro_4);
416 416
     REGISTER_ENCDEC (ADPCM_SWF,         adpcm_swf);
417 417
     REGISTER_DECODER(ADPCM_THP,         adpcm_thp);
418
+    REGISTER_DECODER(ADPCM_VIMA,        adpcm_vima);
418 419
     REGISTER_DECODER(ADPCM_XA,          adpcm_xa);
419 420
     REGISTER_ENCDEC (ADPCM_YAMAHA,      adpcm_yamaha);
420 421
 
... ...
@@ -355,6 +355,7 @@ enum AVCodecID {
355 355
     AV_CODEC_ID_ADPCM_IMA_ISS,
356 356
     AV_CODEC_ID_ADPCM_G722,
357 357
     AV_CODEC_ID_ADPCM_IMA_APC,
358
+    AV_CODEC_ID_ADPCM_VIMA,
358 359
 
359 360
     /* AMR */
360 361
     AV_CODEC_ID_AMR_NB = 0x12000,
... ...
@@ -1729,6 +1729,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
1729 1729
         .long_name = NULL_IF_CONFIG_SMALL("ADPCM IMA CRYO APC"),
1730 1730
         .props     = AV_CODEC_PROP_LOSSY,
1731 1731
     },
1732
+    {
1733
+        .id        = AV_CODEC_ID_ADPCM_VIMA,
1734
+        .type      = AVMEDIA_TYPE_AUDIO,
1735
+        .name      = "adpcm_vima",
1736
+        .long_name = NULL_IF_CONFIG_SMALL("LucasArts VIMA audio"),
1737
+        .props     = AV_CODEC_PROP_LOSSY,
1738
+    },
1732 1739
 
1733 1740
     /* AMR */
1734 1741
     {
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/version.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 55
32
-#define LIBAVCODEC_VERSION_MINOR 44
32
+#define LIBAVCODEC_VERSION_MINOR 45
33 33
 #define LIBAVCODEC_VERSION_MICRO  0
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
36 36
new file mode 100644
... ...
@@ -0,0 +1,218 @@
0
+/*
1
+ * LucasArts VIMA decoder
2
+ * Copyright (c) 2012 Paul B Mahol
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
+/**
22
+ * @file
23
+ * LucasArts VIMA audio decoder
24
+ * @author Paul B Mahol
25
+ */
26
+
27
+#include "libavutil/channel_layout.h"
28
+
29
+#include "adpcm_data.h"
30
+#include "avcodec.h"
31
+#include "get_bits.h"
32
+#include "internal.h"
33
+
34
+static int predict_table_init = 0;
35
+static uint16_t predict_table[5786 * 2];
36
+
37
+static const uint8_t size_table[] = {
38
+    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
39
+    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
40
+    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
41
+    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
42
+    6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
43
+    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
44
+};
45
+
46
+static const int8_t index_table1[] = {
47
+    -1, 4, -1, 4
48
+};
49
+
50
+static const int8_t index_table2[] = {
51
+    -1, -1, 2, 6, -1, -1, 2, 6
52
+};
53
+
54
+static const int8_t index_table3[] = {
55
+    -1, -1, -1, -1, 1, 2, 4, 6, -1, -1, -1, -1, 1, 2, 4, 6
56
+};
57
+
58
+static const int8_t index_table4[] = {
59
+    -1, -1, -1, -1, -1, -1, -1, -1, 1,  1,  1,  2,  2,  4,  5,  6,
60
+    -1, -1, -1, -1, -1, -1, -1, -1, 1,  1,  1,  2,  2,  4,  5,  6
61
+};
62
+
63
+static const int8_t index_table5[] = {
64
+    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
65
+     1,  1,  1,  1,  1,  2,  2,  2,  2,  4,  4,  4,  5,  5,  6,  6,
66
+    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
67
+     1,  1,  1,  1,  1,  2,  2,  2,  2,  4,  4,  4,  5,  5,  6,  6
68
+};
69
+
70
+static const int8_t index_table6[] = {
71
+    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72
+    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73
+     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,
74
+     2,  2,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,  6,
75
+    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
76
+    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
77
+     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,
78
+     2,  2,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,  6
79
+};
80
+
81
+static const int8_t *const step_index_tables[] = {
82
+    index_table1, index_table2, index_table3,
83
+    index_table4, index_table5, index_table6
84
+};
85
+
86
+static av_cold int decode_init(AVCodecContext *avctx)
87
+{
88
+    int start_pos;
89
+
90
+    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
91
+
92
+    if (predict_table_init)
93
+        return 0;
94
+
95
+    for (start_pos = 0; start_pos < 64; start_pos++) {
96
+        unsigned int dest_pos, table_pos;
97
+
98
+        for (table_pos = 0, dest_pos = start_pos;
99
+             table_pos < FF_ARRAY_ELEMS(ff_adpcm_step_table);
100
+             table_pos++, dest_pos += 64) {
101
+            int put = 0, count, table_value;
102
+
103
+            table_value = ff_adpcm_step_table[table_pos];
104
+            for (count = 32; count != 0; count >>= 1) {
105
+                if (start_pos & count)
106
+                    put += table_value;
107
+                table_value >>= 1;
108
+            }
109
+            predict_table[dest_pos] = put;
110
+        }
111
+    }
112
+    predict_table_init = 1;
113
+
114
+    return 0;
115
+}
116
+
117
+static int decode_frame(AVCodecContext *avctx, void *data,
118
+                        int *got_frame_ptr, AVPacket *pkt)
119
+{
120
+    GetBitContext gb;
121
+    AVFrame *frame = data;
122
+    int16_t pcm_data[2];
123
+    uint32_t samples;
124
+    int8_t channel_hint[2];
125
+    int ret, chan;
126
+    int channels = 1;
127
+
128
+    if (pkt->size < 13)
129
+        return AVERROR_INVALIDDATA;
130
+
131
+    if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
132
+        return ret;
133
+
134
+    samples = get_bits_long(&gb, 32);
135
+    if (samples == 0xffffffff) {
136
+        skip_bits_long(&gb, 32);
137
+        samples = get_bits_long(&gb, 32);
138
+    }
139
+
140
+    if (samples > pkt->size * 2)
141
+        return AVERROR_INVALIDDATA;
142
+
143
+    channel_hint[0] = get_sbits(&gb, 8);
144
+    if (channel_hint[0] & 0x80) {
145
+        channel_hint[0] = ~channel_hint[0];
146
+        channels = 2;
147
+    }
148
+    avctx->channels = channels;
149
+    avctx->channel_layout = (channels == 2) ? AV_CH_LAYOUT_STEREO
150
+                                            : AV_CH_LAYOUT_MONO;
151
+    pcm_data[0] = get_sbits(&gb, 16);
152
+    if (channels > 1) {
153
+        channel_hint[1] = get_sbits(&gb, 8);
154
+        pcm_data[1]     = get_sbits(&gb, 16);
155
+    }
156
+
157
+    frame->nb_samples = samples;
158
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
159
+        return ret;
160
+
161
+    for (chan = 0; chan < channels; chan++) {
162
+        uint16_t *dest = (uint16_t *)frame->data[0] + chan;
163
+        int step_index = channel_hint[chan];
164
+        int output = pcm_data[chan];
165
+        int sample;
166
+
167
+        for (sample = 0; sample < samples; sample++) {
168
+            int lookup_size, lookup, highbit, lowbits;
169
+
170
+            step_index  = av_clip(step_index, 0, 88);
171
+            lookup_size = size_table[step_index];
172
+            lookup      = get_bits(&gb, lookup_size);
173
+            highbit     = 1 << (lookup_size - 1);
174
+            lowbits     = highbit - 1;
175
+
176
+            if (lookup & highbit)
177
+                lookup ^= highbit;
178
+            else
179
+                highbit = 0;
180
+
181
+            if (lookup == lowbits) {
182
+                output = get_sbits(&gb, 16);
183
+            } else {
184
+                int predict_index, diff;
185
+
186
+                predict_index = (lookup << (7 - lookup_size)) | (step_index << 6);
187
+                predict_index = av_clip(predict_index, 0, 5785);
188
+                diff          = predict_table[predict_index];
189
+                if (lookup)
190
+                    diff += ff_adpcm_step_table[step_index] >> (lookup_size - 1);
191
+                if (highbit)
192
+                    diff = -diff;
193
+
194
+                output = av_clip_int16(output + diff);
195
+            }
196
+
197
+            *dest = output;
198
+            dest += channels;
199
+
200
+            step_index += step_index_tables[lookup_size - 2][lookup];
201
+        }
202
+    }
203
+
204
+    *got_frame_ptr = 1;
205
+
206
+    return pkt->size;
207
+}
208
+
209
+AVCodec ff_adpcm_vima_decoder = {
210
+    .name         = "adpcm_vima",
211
+    .long_name    = NULL_IF_CONFIG_SMALL("LucasArts VIMA audio"),
212
+    .type         = AVMEDIA_TYPE_AUDIO,
213
+    .id           = AV_CODEC_ID_ADPCM_VIMA,
214
+    .init         = decode_init,
215
+    .decode       = decode_frame,
216
+    .capabilities = CODEC_CAP_DR1,
217
+};