FFmpeg
Loading...
Searching...
No Matches
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.h"
32#include "adpcm_data.h"
33#include "avcodec.h"
34#include "codec_internal.h"
35#include "decode.h"
36#include "get_bits.h"
37
38static uint16_t predict_table[5786 * 2];
39
40static const uint8_t size_table[] = {
41 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
42 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
43 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
44 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
45 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
46 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
47};
48
49static const int8_t index_table1[] = {
50 -1, 4, -1, 4
51};
52
53static const int8_t index_table2[] = {
54 -1, -1, 2, 6, -1, -1, 2, 6
55};
56
57static const int8_t index_table3[] = {
58 -1, -1, -1, -1, 1, 2, 4, 6, -1, -1, -1, -1, 1, 2, 4, 6
59};
60
61static const int8_t index_table4[] = {
62 -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 2, 2, 4, 5, 6,
63 -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 2, 2, 4, 5, 6
64};
65
66static const int8_t index_table5[] = {
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 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 5, 5, 6, 6
71};
72
73static const int8_t index_table6[] = {
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
75 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
76 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
77 2, 2, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6,
78 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
79 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
80 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
81 2, 2, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6
82};
83
88
89static av_cold void predict_table_init(void)
90{
91 for (int start_pos = 0; start_pos < 64; start_pos++) {
92 unsigned int dest_pos, table_pos;
93
94 for (table_pos = 0, dest_pos = start_pos;
96 table_pos++, dest_pos += 64) {
97 int put = 0, count, table_value;
98
99 table_value = ff_adpcm_step_table[table_pos];
100 for (count = 32; count != 0; count >>= 1) {
101 if (start_pos & count)
102 put += table_value;
103 table_value >>= 1;
104 }
105 predict_table[dest_pos] = put;
106 }
107 }
108}
109
111{
112 static AVOnce init_static_once = AV_ONCE_INIT;
113
115
116 ff_thread_once(&init_static_once, predict_table_init);
117
118 return 0;
119}
120
122 int *got_frame_ptr, AVPacket *pkt)
123{
124 GetBitContext gb;
125 int16_t pcm_data[2];
126 uint32_t samples;
127 int8_t channel_hint[2];
128 int ret, chan;
129 int channels = 1;
130
131 if (pkt->size < 13)
132 return AVERROR_INVALIDDATA;
133
134 if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
135 return ret;
136
137 samples = get_bits_long(&gb, 32);
138 if (samples == 0xffffffff) {
139 skip_bits_long(&gb, 32);
140 samples = get_bits_long(&gb, 32);
141 }
142
143 if (samples > pkt->size * 2)
144 return AVERROR_INVALIDDATA;
145
146 channel_hint[0] = get_sbits(&gb, 8);
147 if (channel_hint[0] & 0x80) {
148 channel_hint[0] = ~channel_hint[0];
149 channels = 2;
150 }
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 if (show_bits_long(&gb, 32) == MKBETAG('I','M','A','4')) {
164 int16_t *dest = (int16_t *)frame->data[0];
166
167 skip_bits_long(&gb, 32); /* skip the 'IMA4' tag */
168 cs.predictor = (int16_t)get_xbits_le(&gb, 16);
169 cs.step_index = av_clip(get_bits(&gb, 8), 0, 88);
170 for (int i = 0; i < samples; i++) {
172 for (int j = 0; j < channels; j++)
173 *dest++ = cs.predictor;
174 }
175 } else {
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 = 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
220 *got_frame_ptr = 1;
221
222 return pkt->size;
223}
224
226 .p.name = "adpcm_vima",
227 CODEC_LONG_NAME("LucasArts VIMA audio"),
228 .p.type = AVMEDIA_TYPE_AUDIO,
230 .init = decode_init,
233};
int16_t ff_adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
Definition adpcm.c:557
ADPCM encoder/decoder common header.
const int16_t ff_adpcm_step_table[89]
This is the step table.
Definition adpcm_data.c:39
ADPCM tables.
const FFCodec ff_adpcm_vima_decoder
Definition vima.c:225
channels
Definition aptx.h:31
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define av_clip
Definition common.h:100
#define av_clip_int16
Definition common.h:115
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVPacket * pkt
static AVFrame * frame
#define sample
bitstream reader API header.
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition get_bits.h:498
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static int get_sbits(GetBitContext *s, int n)
Definition get_bits.h:322
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static int get_xbits_le(GetBitContext *s, int n)
Definition get_bits.h:308
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_ADPCM_VIMA
Definition codec_id.h:400
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
#define MKBETAG(a, b, c, d)
Definition macros.h:56
#define FF_ARRAY_ELEMS(a)
int16_t step_index
Definition adpcm.h:33
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
static const uint8_t size_table[6]
Definition vc1_block.c:1248
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static uint16_t predict_table[5786 *2]
Definition vima.c:38
static const int8_t index_table5[]
Definition vima.c:66
static const int8_t index_table1[]
Definition vima.c:49
static const int8_t index_table4[]
Definition vima.c:61
static const int8_t index_table6[]
Definition vima.c:73
static const int8_t index_table3[]
Definition vima.c:57
static av_cold int decode_init(AVCodecContext *avctx)
Definition vima.c:110
static const int8_t index_table2[]
Definition vima.c:53
static av_cold void predict_table_init(void)
Definition vima.c:89
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
Definition vima.c:121
static const int8_t *const step_index_tables[]
Definition vima.c:84
int lookup