FFmpeg
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 
22 /**
23  * @file
24  * LucasArts VIMA audio decoder
25  * @author Paul B Mahol
26  */
27 
29 #include "libavutil/thread.h"
30 
31 #include "adpcm_data.h"
32 #include "avcodec.h"
33 #include "get_bits.h"
34 #include "internal.h"
35 
36 static uint16_t predict_table[5786 * 2];
37 
38 static const uint8_t size_table[] = {
39  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
40  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
41  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
42  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
43  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
44  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
45 };
46 
47 static const int8_t index_table1[] = {
48  -1, 4, -1, 4
49 };
50 
51 static const int8_t index_table2[] = {
52  -1, -1, 2, 6, -1, -1, 2, 6
53 };
54 
55 static const int8_t index_table3[] = {
56  -1, -1, -1, -1, 1, 2, 4, 6, -1, -1, -1, -1, 1, 2, 4, 6
57 };
58 
59 static const int8_t index_table4[] = {
60  -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 2, 2, 4, 5, 6,
61  -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 2, 2, 4, 5, 6
62 };
63 
64 static const int8_t index_table5[] = {
65  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
66  1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 5, 5, 6, 6,
67  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68  1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 5, 5, 6, 6
69 };
70 
71 static const int8_t index_table6[] = {
72  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
74  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
75  2, 2, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6,
76  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
77  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
78  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
79  2, 2, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6
80 };
81 
82 static const int8_t *const step_index_tables[] = {
85 };
86 
87 static av_cold void predict_table_init(void)
88 {
89  for (int start_pos = 0; start_pos < 64; start_pos++) {
90  unsigned int dest_pos, table_pos;
91 
92  for (table_pos = 0, dest_pos = start_pos;
94  table_pos++, dest_pos += 64) {
95  int put = 0, count, table_value;
96 
97  table_value = ff_adpcm_step_table[table_pos];
98  for (count = 32; count != 0; count >>= 1) {
99  if (start_pos & count)
100  put += table_value;
101  table_value >>= 1;
102  }
103  predict_table[dest_pos] = put;
104  }
105  }
106 }
107 
109 {
110  static AVOnce init_static_once = AV_ONCE_INIT;
111 
112  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
113 
114  ff_thread_once(&init_static_once, predict_table_init);
115 
116  return 0;
117 }
118 
119 static int decode_frame(AVCodecContext *avctx, void *data,
120  int *got_frame_ptr, AVPacket *pkt)
121 {
122  GetBitContext gb;
123  AVFrame *frame = data;
124  int16_t pcm_data[2];
125  uint32_t samples;
126  int8_t channel_hint[2];
127  int ret, chan;
128  int channels = 1;
129 
130  if (pkt->size < 13)
131  return AVERROR_INVALIDDATA;
132 
133  if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
134  return ret;
135 
136  samples = get_bits_long(&gb, 32);
137  if (samples == 0xffffffff) {
138  skip_bits_long(&gb, 32);
139  samples = get_bits_long(&gb, 32);
140  }
141 
142  if (samples > pkt->size * 2)
143  return AVERROR_INVALIDDATA;
144 
145  channel_hint[0] = get_sbits(&gb, 8);
146  if (channel_hint[0] & 0x80) {
147  channel_hint[0] = ~channel_hint[0];
148  channels = 2;
149  }
150  avctx->channels = channels;
153  pcm_data[0] = get_sbits(&gb, 16);
154  if (channels > 1) {
155  channel_hint[1] = get_sbits(&gb, 8);
156  pcm_data[1] = get_sbits(&gb, 16);
157  }
158 
159  frame->nb_samples = samples;
160  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
161  return ret;
162 
163  for (chan = 0; chan < channels; chan++) {
164  uint16_t *dest = (uint16_t *)frame->data[0] + chan;
165  int step_index = channel_hint[chan];
166  int output = pcm_data[chan];
167  int sample;
168 
169  for (sample = 0; sample < samples; sample++) {
170  int lookup_size, lookup, highbit, lowbits;
171 
172  step_index = av_clip(step_index, 0, 88);
173  lookup_size = size_table[step_index];
174  lookup = get_bits(&gb, lookup_size);
175  highbit = 1 << (lookup_size - 1);
176  lowbits = highbit - 1;
177 
178  if (lookup & highbit)
179  lookup ^= highbit;
180  else
181  highbit = 0;
182 
183  if (lookup == lowbits) {
184  output = get_sbits(&gb, 16);
185  } else {
186  int predict_index, diff;
187 
188  predict_index = (lookup << (7 - lookup_size)) | (step_index << 6);
189  predict_index = av_clip(predict_index, 0, 5785);
190  diff = predict_table[predict_index];
191  if (lookup)
192  diff += ff_adpcm_step_table[step_index] >> (lookup_size - 1);
193  if (highbit)
194  diff = -diff;
195 
197  }
198 
199  *dest = output;
200  dest += channels;
201 
202  step_index += step_index_tables[lookup_size - 2][lookup];
203  }
204  }
205 
206  *got_frame_ptr = 1;
207 
208  return pkt->size;
209 }
210 
212  .name = "adpcm_vima",
213  .long_name = NULL_IF_CONFIG_SMALL("LucasArts VIMA audio"),
214  .type = AVMEDIA_TYPE_AUDIO,
216  .init = decode_init,
217  .decode = decode_frame,
219  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
220 };
AVCodec
AVCodec.
Definition: codec.h:197
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
av_clip
#define av_clip
Definition: common.h:122
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1149
thread.h
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:365
data
const char data[16]
Definition: mxf.c:142
index_table2
static const int8_t index_table2[]
Definition: vima.c:51
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
index_table4
static const int8_t index_table4[]
Definition: vima.c:59
GetBitContext
Definition: get_bits.h:61
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:91
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
pkt
AVPacket * pkt
Definition: movenc.c:59
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
adpcm_data.h
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
get_sbits
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:359
channels
channels
Definition: aptx.h:33
get_bits.h
index_table5
static const int8_t index_table5[]
Definition: vima.c:64
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:173
av_clip_int16
#define av_clip_int16
Definition: common.h:137
index_table1
static const int8_t index_table1[]
Definition: vima.c:47
predict_table_init
static av_cold void predict_table_init(void)
Definition: vima.c:87
AV_CODEC_ID_ADPCM_VIMA
@ AV_CODEC_ID_ADPCM_VIMA
Definition: codec_id.h:381
AVOnce
#define AVOnce
Definition: thread.h:172
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:104
decode_frame
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *pkt)
Definition: vima.c:119
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1638
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:366
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
ff_adpcm_step_table
const int16_t ff_adpcm_step_table[89]
This is the step table.
Definition: adpcm_data.c:61
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1106
sample
#define sample
Definition: flacdsp_template.c:44
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1099
index_table6
static const int8_t index_table6[]
Definition: vima.c:71
lookup
int lookup
Definition: vorbis_enc_data.h:428
step_index_tables
static const int8_t *const step_index_tables[]
Definition: vima.c:82
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
predict_table
static uint16_t predict_table[5786 *2]
Definition: vima.c:36
AVCodecContext
main external API structure.
Definition: avcodec.h:501
channel_layout.h
index_table3
static const int8_t index_table3[]
Definition: vima.c:55
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:136
AVPacket
This structure stores compressed data.
Definition: packet.h:342
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ff_adpcm_vima_decoder
const AVCodec ff_adpcm_vima_decoder
Definition: vima.c:211
size_table
static const uint8_t size_table[]
Definition: vima.c:38
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: vima.c:108