Browse code

LucasArts SMUSH VIMA audio decoder

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2012/03/19 11:52:08
Showing 6 changed files
... ...
@@ -830,6 +830,8 @@ following image formats are supported:
830 830
 @item TrueHD                 @tab     @tab  X
831 831
     @tab Used in HD-DVD and Blu-Ray discs.
832 832
 @item TwinVQ (VQF flavor)    @tab     @tab  X
833
+@item VIMA
834
+    @tab Used in LucasArts SMUSH animations.
833 835
 @item Vorbis                 @tab  E  @tab  X
834 836
     @tab A native but very primitive encoder exists.
835 837
 @item WavPack                @tab     @tab  X
... ...
@@ -612,6 +612,7 @@ OBJS-$(CONFIG_ADPCM_THP_DECODER)          += adpcm.o adpcm_data.o
612 612
 OBJS-$(CONFIG_ADPCM_XA_DECODER)           += adpcm.o adpcm_data.o
613 613
 OBJS-$(CONFIG_ADPCM_YAMAHA_DECODER)       += adpcm.o adpcm_data.o
614 614
 OBJS-$(CONFIG_ADPCM_YAMAHA_ENCODER)       += adpcmenc.o adpcm_data.o
615
+OBJS-$(CONFIG_VIMA_DECODER)               += vima.o adpcm_data.o
615 616
 
616 617
 # libavformat dependencies
617 618
 OBJS-$(CONFIG_ADTS_MUXER)              += mpeg4audio.o
... ...
@@ -395,6 +395,7 @@ void avcodec_register_all(void)
395 395
     REGISTER_DECODER (ADPCM_THP, adpcm_thp);
396 396
     REGISTER_DECODER (ADPCM_XA, adpcm_xa);
397 397
     REGISTER_ENCDEC  (ADPCM_YAMAHA, adpcm_yamaha);
398
+    REGISTER_DECODER (VIMA, vima);
398 399
 
399 400
     /* subtitles */
400 401
     REGISTER_ENCDEC  (ASS, ass);
... ...
@@ -332,6 +332,7 @@ enum CodecID {
332 332
     CODEC_ID_ADPCM_IMA_ISS,
333 333
     CODEC_ID_ADPCM_G722,
334 334
     CODEC_ID_ADPCM_IMA_APC,
335
+    CODEC_ID_VIMA       = MKBETAG('V','I','M','A'),
335 336
 
336 337
     /* AMR */
337 338
     CODEC_ID_AMR_NB = 0x12000,
... ...
@@ -27,7 +27,7 @@
27 27
  */
28 28
 
29 29
 #define LIBAVCODEC_VERSION_MAJOR 54
30
-#define LIBAVCODEC_VERSION_MINOR  27
30
+#define LIBAVCODEC_VERSION_MINOR  28
31 31
 #define LIBAVCODEC_VERSION_MICRO 100
32 32
 
33 33
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
34 34
new file mode 100644
... ...
@@ -0,0 +1,236 @@
0
+/*
1
+ * LucasArt VIMA decoder
2
+ * Copyright (c) 2012 Paul B Mahol
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 "libavutil/audioconvert.h"
22
+#include "avcodec.h"
23
+#include "get_bits.h"
24
+#include "adpcm_data.h"
25
+
26
+typedef struct {
27
+    AVFrame     frame;
28
+    uint16_t    predict_table[5786 * 2];
29
+} VimaContext;
30
+
31
+static const uint8_t size_table[] =
32
+{
33
+    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
34
+    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
35
+    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
36
+    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
37
+    6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
38
+    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
39
+};
40
+
41
+static const int8_t index_table1[] =
42
+{
43
+    -1, 4, -1, 4
44
+};
45
+
46
+static const int8_t index_table2[] =
47
+{
48
+    -1, -1, 2, 6, -1, -1, 2, 6
49
+};
50
+
51
+static const int8_t index_table3[] =
52
+{
53
+    -1, -1, -1, -1, 1, 2, 4, 6,
54
+    -1, -1, -1, -1, 1, 2, 4, 6
55
+};
56
+
57
+static const int8_t index_table4[] =
58
+{
59
+    -1, -1, -1, -1, -1, -1, -1, -1,
60
+     1,  1,  1,  2,  2,  4,  5,  6,
61
+    -1, -1, -1, -1, -1, -1, -1, -1,
62
+     1,  1,  1,  2,  2,  4,  5,  6
63
+};
64
+
65
+static const int8_t index_table5[] =
66
+{
67
+    -1, -1, -1, -1, -1, -1, -1, -1,
68
+    -1, -1, -1, -1, -1, -1, -1, -1,
69
+     1,  1,  1,  1,  1,  2,  2,  2,
70
+     2,  4,  4,  4,  5,  5,  6,  6,
71
+    -1, -1, -1, -1, -1, -1, -1, -1,
72
+    -1, -1, -1, -1, -1, -1, -1, -1,
73
+     1,  1,  1,  1,  1,  2,  2,  2,
74
+     2,  4,  4,  4,  5,  5,  6,  6
75
+};
76
+
77
+static const int8_t index_table6[] =
78
+{
79
+    -1, -1, -1, -1, -1, -1, -1, -1,
80
+    -1, -1, -1, -1, -1, -1, -1, -1,
81
+    -1, -1, -1, -1, -1, -1, -1, -1,
82
+    -1, -1, -1, -1, -1, -1, -1, -1,
83
+     1,  1,  1,  1,  1,  1,  1,  1,
84
+     1,  1,  2,  2,  2,  2,  2,  2,
85
+     2,  2,  4,  4,  4,  4,  4,  4,
86
+     5,  5,  5,  5,  6,  6,  6,  6,
87
+    -1, -1, -1, -1, -1, -1, -1, -1,
88
+    -1, -1, -1, -1, -1, -1, -1, -1,
89
+    -1, -1, -1, -1, -1, -1, -1, -1,
90
+    -1, -1, -1, -1, -1, -1, -1, -1,
91
+     1,  1,  1,  1,  1,  1,  1,  1,
92
+     1,  1,  2,  2,  2,  2,  2,  2,
93
+     2,  2,  4,  4,  4,  4,  4,  4,
94
+     5,  5,  5,  5,  6,  6,  6,  6
95
+};
96
+
97
+static const int8_t* const step_index_tables[] =
98
+{
99
+    index_table1, index_table2, index_table3,
100
+    index_table4, index_table5, index_table6
101
+};
102
+
103
+static av_cold int decode_init(AVCodecContext *avctx)
104
+{
105
+    VimaContext *vima = avctx->priv_data;
106
+    int         start_pos;
107
+
108
+    for (start_pos = 0; start_pos < 64; start_pos++) {
109
+        unsigned int dest_pos, table_pos;
110
+
111
+        for (table_pos = 0, dest_pos = start_pos;
112
+             table_pos < FF_ARRAY_ELEMS(ff_adpcm_step_table);
113
+             table_pos++, dest_pos += 64) {
114
+            int put = 0, count, table_value;
115
+
116
+            table_value = ff_adpcm_step_table[table_pos];
117
+            for (count = 32; count != 0; count >>= 1) {
118
+                if (start_pos & count)
119
+                    put += table_value;
120
+                table_value >>= 1;
121
+            }
122
+            vima->predict_table[dest_pos] = put;
123
+        }
124
+    }
125
+
126
+    avcodec_get_frame_defaults(&vima->frame);
127
+    avctx->coded_frame = &vima->frame;
128
+    avctx->sample_fmt  = AV_SAMPLE_FMT_S16;
129
+
130
+    return 0;
131
+}
132
+
133
+static int decode_frame(AVCodecContext *avctx, void *data,
134
+                        int *got_frame_ptr, AVPacket *pkt)
135
+{
136
+    GetBitContext  gb;
137
+    VimaContext    *vima = avctx->priv_data;
138
+    int16_t        pcm_data[2];
139
+    uint32_t       samples;
140
+    int8_t         channel_hint[2];
141
+    int            ret, chan, channels = 1;
142
+
143
+    init_get_bits(&gb, pkt->data, pkt->size * 8);
144
+
145
+    if (pkt->size < 13)
146
+        return AVERROR_INVALIDDATA;
147
+
148
+    samples = get_bits_long(&gb, 32);
149
+    if (samples == 0xffffffff) {
150
+        skip_bits_long(&gb, 32);
151
+        samples = get_bits_long(&gb, 32);
152
+    }
153
+
154
+    if (samples > pkt->size * 2)
155
+        return AVERROR_INVALIDDATA;
156
+
157
+    channel_hint[0] = get_sbits(&gb, 8);
158
+    if (channel_hint[0] & 0x80) {
159
+        channel_hint[0] = ~channel_hint[0];
160
+        channels = 2;
161
+    }
162
+    avctx->channels = channels;
163
+    avctx->channel_layout = (channels == 2) ? AV_CH_LAYOUT_STEREO :
164
+                                              AV_CH_LAYOUT_MONO;
165
+    pcm_data[0] = get_sbits(&gb, 16);
166
+    if (channels > 1) {
167
+        channel_hint[1] = get_sbits(&gb, 8);
168
+        pcm_data[1] = get_sbits(&gb, 16);
169
+    }
170
+
171
+    vima->frame.nb_samples = samples;
172
+    if ((ret = avctx->get_buffer(avctx, &vima->frame)) < 0) {
173
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
174
+        return ret;
175
+    }
176
+
177
+    for (chan = 0; chan < channels; chan++) {
178
+        uint16_t *dest = (uint16_t*)vima->frame.data[0] + chan;
179
+        int step_index = channel_hint[chan];
180
+        int output = pcm_data[chan];
181
+        int sample;
182
+
183
+        for (sample = 0; sample < samples; sample++) {
184
+            int lookup_size, lookup, highbit, lowbits;
185
+
186
+            step_index  = av_clip(step_index, 0, 88);
187
+            lookup_size = size_table[step_index];
188
+            lookup      = get_bits(&gb, lookup_size);
189
+            highbit     = 1 << (lookup_size - 1);
190
+            lowbits     = highbit - 1;
191
+
192
+            if (lookup & highbit)
193
+                lookup ^= highbit;
194
+            else
195
+                highbit = 0;
196
+
197
+            if (lookup == lowbits) {
198
+                output = get_sbits(&gb, 16);
199
+            } else {
200
+                int predict_index, diff;
201
+
202
+                predict_index = (lookup << (7 - lookup_size)) | (step_index << 6);
203
+                predict_index = av_clip(predict_index, 0, 5785);
204
+                diff          = vima->predict_table[predict_index];
205
+                if (lookup)
206
+                    diff += ff_adpcm_step_table[step_index] >> (lookup_size - 1);
207
+                if (highbit)
208
+                    diff  = -diff;
209
+
210
+                output  = av_clip_int16(output + diff);
211
+            }
212
+
213
+            *dest = output;
214
+            dest += channels;
215
+
216
+            step_index += step_index_tables[lookup_size - 2][lookup];
217
+        }
218
+    }
219
+
220
+    *got_frame_ptr   = 1;
221
+    *(AVFrame *)data = vima->frame;
222
+
223
+    return pkt->size;
224
+}
225
+
226
+AVCodec ff_vima_decoder = {
227
+    .name           = "vima",
228
+    .type           = AVMEDIA_TYPE_AUDIO,
229
+    .id             = CODEC_ID_VIMA,
230
+    .priv_data_size = sizeof(VimaContext),
231
+    .init           = decode_init,
232
+    .decode         = decode_frame,
233
+    .capabilities   = CODEC_CAP_DR1,
234
+    .long_name      = NULL_IF_CONFIG_SMALL("LucasArts VIMA audio"),
235
+};