vima.c
Go to the documentation of this file.
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 
23 #include "avcodec.h"
24 #include "get_bits.h"
25 #include "internal.h"
26 #include "adpcm_data.h"
27 
28 typedef struct {
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 {
102 };
103 
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  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
128 
129  return 0;
130 }
131 
132 static int decode_frame(AVCodecContext *avctx, void *data,
133  int *got_frame_ptr, AVPacket *pkt)
134 {
135  GetBitContext gb;
136  VimaContext *vima = avctx->priv_data;
137  AVFrame *frame = 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  if (pkt->size < 13)
144  return AVERROR_INVALIDDATA;
145 
146  if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
147  return ret;
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 :
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  frame->nb_samples = samples;
173  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
174  return ret;
175 
176  for (chan = 0; chan < channels; chan++) {
177  uint16_t *dest = (uint16_t*)frame->data[0] + chan;
178  int step_index = channel_hint[chan];
179  int output = pcm_data[chan];
180  int sample;
181 
182  for (sample = 0; sample < samples; sample++) {
183  int lookup_size, lookup, highbit, lowbits;
184 
185  step_index = av_clip(step_index, 0, 88);
186  lookup_size = size_table[step_index];
187  lookup = get_bits(&gb, lookup_size);
188  highbit = 1 << (lookup_size - 1);
189  lowbits = highbit - 1;
190 
191  if (lookup & highbit)
192  lookup ^= highbit;
193  else
194  highbit = 0;
195 
196  if (lookup == lowbits) {
197  output = get_sbits(&gb, 16);
198  } else {
199  int predict_index, diff;
200 
201  predict_index = (lookup << (7 - lookup_size)) | (step_index << 6);
202  predict_index = av_clip(predict_index, 0, 5785);
203  diff = vima->predict_table[predict_index];
204  if (lookup)
205  diff += ff_adpcm_step_table[step_index] >> (lookup_size - 1);
206  if (highbit)
207  diff = -diff;
208 
209  output = av_clip_int16(output + diff);
210  }
211 
212  *dest = output;
213  dest += channels;
214 
215  step_index += step_index_tables[lookup_size - 2][lookup];
216  }
217  }
218 
219  *got_frame_ptr = 1;
220 
221  return pkt->size;
222 }
223 
225  .name = "vima",
226  .type = AVMEDIA_TYPE_AUDIO,
227  .id = AV_CODEC_ID_VIMA,
228  .priv_data_size = sizeof(VimaContext),
229  .init = decode_init,
230  .decode = decode_frame,
231  .capabilities = CODEC_CAP_DR1,
232  .long_name = NULL_IF_CONFIG_SMALL("LucasArts VIMA audio"),
233 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:198
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static const int8_t *const step_index_tables[]
Definition: vima.c:98
static const int8_t index_table3[]
Definition: vima.c:52
#define FF_ARRAY_ELEMS(a)
#define AV_CH_LAYOUT_STEREO
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *pkt)
Definition: vima.c:132
signed 16 bits
Definition: samplefmt.h:52
#define sample
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:225
enum AVSampleFormat sample_fmt
audio sample format
uint8_t
#define av_cold
Definition: attributes.h:78
static AVPacket pkt
Definition: demuxing.c:56
#define put(d, s)
Definition: dsputil_align.c:51
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
ADPCM tables.
uint8_t * data
AVCodec ff_vima_decoder
Definition: vima.c:224
bitstream reader API header.
static const int8_t index_table2[]
Definition: vima.c:47
frame
Definition: stft.m:14
static const uint8_t size_table[]
Definition: vima.c:32
static const int8_t index_table6[]
Definition: vima.c:78
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
const int16_t ff_adpcm_step_table[89]
This is the step table.
Definition: adpcm_data.c:40
Spectrum Plot time data
const char * name
Name of the codec implementation.
external API header
uint64_t channel_layout
Audio channel layout.
audio channel layout utility functions
ret
Definition: avfilter.c:821
static const int8_t index_table4[]
Definition: vima.c:58
#define diff(a, as, b, bs)
Definition: vf_phase.c:80
static const int8_t index_table5[]
Definition: vima.c:66
static av_cold int decode_init(AVCodecContext *avctx)
Definition: vima.c:104
static const int8_t index_table1[]
Definition: vima.c:42
dest
Definition: start.py:60
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:410
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:306
uint16_t predict_table[5786 *2]
Definition: vima.c:29
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
common internal api header.
these buffered frames must be flushed immediately if a new input produces new output(Example:frame rate-doubling filter:filter_frame must(1) flush the second copy of the previous frame, if it is still there,(2) push the first copy of the incoming frame,(3) keep the second copy for later.) If the input frame is not enough to produce output
int channels
number of audio channels
Filter the word “frame” indicates either a video frame or a group of audio samples
void INT64 INT64 count
Definition: avisynth_c.h:594
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
int lookup
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
for(j=16;j >0;--j)