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