]> git.sesse.net Git - ffmpeg/blob - libavcodec/vima.c
Merge commit 'b9ba5253dd1232be4b48cfe61c31ff4b3de3d10a'
[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 typedef struct {
29     AVFrame     frame;
30     uint16_t    predict_table[5786 * 2];
31 } VimaContext;
32
33 static const uint8_t size_table[] =
34 {
35     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
36     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
37     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
38     5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
39     6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
40     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
41 };
42
43 static const int8_t index_table1[] =
44 {
45     -1, 4, -1, 4
46 };
47
48 static const int8_t index_table2[] =
49 {
50     -1, -1, 2, 6, -1, -1, 2, 6
51 };
52
53 static const int8_t index_table3[] =
54 {
55     -1, -1, -1, -1, 1, 2, 4, 6,
56     -1, -1, -1, -1, 1, 2, 4, 6
57 };
58
59 static const int8_t index_table4[] =
60 {
61     -1, -1, -1, -1, -1, -1, -1, -1,
62      1,  1,  1,  2,  2,  4,  5,  6,
63     -1, -1, -1, -1, -1, -1, -1, -1,
64      1,  1,  1,  2,  2,  4,  5,  6
65 };
66
67 static const int8_t index_table5[] =
68 {
69     -1, -1, -1, -1, -1, -1, -1, -1,
70     -1, -1, -1, -1, -1, -1, -1, -1,
71      1,  1,  1,  1,  1,  2,  2,  2,
72      2,  4,  4,  4,  5,  5,  6,  6,
73     -1, -1, -1, -1, -1, -1, -1, -1,
74     -1, -1, -1, -1, -1, -1, -1, -1,
75      1,  1,  1,  1,  1,  2,  2,  2,
76      2,  4,  4,  4,  5,  5,  6,  6
77 };
78
79 static const int8_t index_table6[] =
80 {
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, -1, -1, -1, -1, -1, -1,
85      1,  1,  1,  1,  1,  1,  1,  1,
86      1,  1,  2,  2,  2,  2,  2,  2,
87      2,  2,  4,  4,  4,  4,  4,  4,
88      5,  5,  5,  5,  6,  6,  6,  6,
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, -1, -1, -1, -1, -1, -1,
93      1,  1,  1,  1,  1,  1,  1,  1,
94      1,  1,  2,  2,  2,  2,  2,  2,
95      2,  2,  4,  4,  4,  4,  4,  4,
96      5,  5,  5,  5,  6,  6,  6,  6
97 };
98
99 static const int8_t* const step_index_tables[] =
100 {
101     index_table1, index_table2, index_table3,
102     index_table4, index_table5, index_table6
103 };
104
105 static av_cold int decode_init(AVCodecContext *avctx)
106 {
107     VimaContext *vima = avctx->priv_data;
108     int         start_pos;
109
110     for (start_pos = 0; start_pos < 64; start_pos++) {
111         unsigned int dest_pos, table_pos;
112
113         for (table_pos = 0, dest_pos = start_pos;
114              table_pos < FF_ARRAY_ELEMS(ff_adpcm_step_table);
115              table_pos++, dest_pos += 64) {
116             int put = 0, count, table_value;
117
118             table_value = ff_adpcm_step_table[table_pos];
119             for (count = 32; count != 0; count >>= 1) {
120                 if (start_pos & count)
121                     put += table_value;
122                 table_value >>= 1;
123             }
124             vima->predict_table[dest_pos] = put;
125         }
126     }
127
128     avcodec_get_frame_defaults(&vima->frame);
129     avctx->coded_frame = &vima->frame;
130     avctx->sample_fmt  = AV_SAMPLE_FMT_S16;
131
132     return 0;
133 }
134
135 static int decode_frame(AVCodecContext *avctx, void *data,
136                         int *got_frame_ptr, AVPacket *pkt)
137 {
138     GetBitContext  gb;
139     VimaContext    *vima = avctx->priv_data;
140     int16_t        pcm_data[2];
141     uint32_t       samples;
142     int8_t         channel_hint[2];
143     int            ret, chan, channels = 1;
144
145     if (pkt->size < 13)
146         return AVERROR_INVALIDDATA;
147
148     if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
149         return ret;
150
151     samples = get_bits_long(&gb, 32);
152     if (samples == 0xffffffff) {
153         skip_bits_long(&gb, 32);
154         samples = get_bits_long(&gb, 32);
155     }
156
157     if (samples > pkt->size * 2)
158         return AVERROR_INVALIDDATA;
159
160     channel_hint[0] = get_sbits(&gb, 8);
161     if (channel_hint[0] & 0x80) {
162         channel_hint[0] = ~channel_hint[0];
163         channels = 2;
164     }
165     avctx->channels = channels;
166     avctx->channel_layout = (channels == 2) ? AV_CH_LAYOUT_STEREO :
167                                               AV_CH_LAYOUT_MONO;
168     pcm_data[0] = get_sbits(&gb, 16);
169     if (channels > 1) {
170         channel_hint[1] = get_sbits(&gb, 8);
171         pcm_data[1] = get_sbits(&gb, 16);
172     }
173
174     vima->frame.nb_samples = samples;
175     if ((ret = ff_get_buffer(avctx, &vima->frame)) < 0) {
176         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
177         return ret;
178     }
179
180     for (chan = 0; chan < channels; chan++) {
181         uint16_t *dest = (uint16_t*)vima->frame.data[0] + chan;
182         int step_index = channel_hint[chan];
183         int output = pcm_data[chan];
184         int sample;
185
186         for (sample = 0; sample < samples; sample++) {
187             int lookup_size, lookup, highbit, lowbits;
188
189             step_index  = av_clip(step_index, 0, 88);
190             lookup_size = size_table[step_index];
191             lookup      = get_bits(&gb, lookup_size);
192             highbit     = 1 << (lookup_size - 1);
193             lowbits     = highbit - 1;
194
195             if (lookup & highbit)
196                 lookup ^= highbit;
197             else
198                 highbit = 0;
199
200             if (lookup == lowbits) {
201                 output = get_sbits(&gb, 16);
202             } else {
203                 int predict_index, diff;
204
205                 predict_index = (lookup << (7 - lookup_size)) | (step_index << 6);
206                 predict_index = av_clip(predict_index, 0, 5785);
207                 diff          = vima->predict_table[predict_index];
208                 if (lookup)
209                     diff += ff_adpcm_step_table[step_index] >> (lookup_size - 1);
210                 if (highbit)
211                     diff  = -diff;
212
213                 output  = av_clip_int16(output + diff);
214             }
215
216             *dest = output;
217             dest += channels;
218
219             step_index += step_index_tables[lookup_size - 2][lookup];
220         }
221     }
222
223     *got_frame_ptr   = 1;
224     *(AVFrame *)data = vima->frame;
225
226     return pkt->size;
227 }
228
229 AVCodec ff_vima_decoder = {
230     .name           = "vima",
231     .type           = AVMEDIA_TYPE_AUDIO,
232     .id             = AV_CODEC_ID_VIMA,
233     .priv_data_size = sizeof(VimaContext),
234     .init           = decode_init,
235     .decode         = decode_frame,
236     .capabilities   = CODEC_CAP_DR1,
237     .long_name      = NULL_IF_CONFIG_SMALL("LucasArts VIMA audio"),
238 };