]> git.sesse.net Git - ffmpeg/blob - libavcodec/vima.c
vima: generate predict_table once, and share it with all decoders
[ffmpeg] / libavcodec / vima.c
1 /*
2  * LucasArts VIMA decoder
3  * Copyright (c) 2012 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/channel_layout.h"
23 #include "avcodec.h"
24 #include "get_bits.h"
25 #include "internal.h"
26 #include "adpcm_data.h"
27
28 static int predict_table_init = 0;
29 static uint16_t predict_table[5786 * 2];
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     int start_pos;
106
107     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
108
109     if (predict_table_init)
110         return 0;
111
112     for (start_pos = 0; start_pos < 64; start_pos++) {
113         unsigned int dest_pos, table_pos;
114
115         for (table_pos = 0, dest_pos = start_pos;
116              table_pos < FF_ARRAY_ELEMS(ff_adpcm_step_table);
117              table_pos++, dest_pos += 64) {
118             int put = 0, count, table_value;
119
120             table_value = ff_adpcm_step_table[table_pos];
121             for (count = 32; count != 0; count >>= 1) {
122                 if (start_pos & count)
123                     put += table_value;
124                 table_value >>= 1;
125             }
126             predict_table[dest_pos] = put;
127         }
128     }
129     predict_table_init = 1;
130
131     return 0;
132 }
133
134 static int decode_frame(AVCodecContext *avctx, void *data,
135                         int *got_frame_ptr, AVPacket *pkt)
136 {
137     GetBitContext  gb;
138     AVFrame        *frame = data;
139     int16_t        pcm_data[2];
140     uint32_t       samples;
141     int8_t         channel_hint[2];
142     int            ret, chan, channels = 1;
143
144     if (pkt->size < 13)
145         return AVERROR_INVALIDDATA;
146
147     if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
148         return ret;
149
150     samples = get_bits_long(&gb, 32);
151     if (samples == 0xffffffff) {
152         skip_bits_long(&gb, 32);
153         samples = get_bits_long(&gb, 32);
154     }
155
156     if (samples > pkt->size * 2)
157         return AVERROR_INVALIDDATA;
158
159     channel_hint[0] = get_sbits(&gb, 8);
160     if (channel_hint[0] & 0x80) {
161         channel_hint[0] = ~channel_hint[0];
162         channels = 2;
163     }
164     avctx->channels = channels;
165     avctx->channel_layout = (channels == 2) ? AV_CH_LAYOUT_STEREO :
166                                               AV_CH_LAYOUT_MONO;
167     pcm_data[0] = get_sbits(&gb, 16);
168     if (channels > 1) {
169         channel_hint[1] = get_sbits(&gb, 8);
170         pcm_data[1] = get_sbits(&gb, 16);
171     }
172
173     frame->nb_samples = samples;
174     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
175         return ret;
176
177     for (chan = 0; chan < channels; chan++) {
178         uint16_t *dest = (uint16_t*)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          = 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
222     return pkt->size;
223 }
224
225 AVCodec ff_vima_decoder = {
226     .name           = "vima",
227     .type           = AVMEDIA_TYPE_AUDIO,
228     .id             = AV_CODEC_ID_VIMA,
229     .init           = decode_init,
230     .decode         = decode_frame,
231     .capabilities   = CODEC_CAP_DR1,
232     .long_name      = NULL_IF_CONFIG_SMALL("LucasArts VIMA audio"),
233 };