FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vima.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 68 72 94.4%
Branches: 28 36 77.8%

Line Branch Exec Source
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
28 #include "libavutil/channel_layout.h"
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[] = {
83 index_table1, index_table2, index_table3,
84 index_table4, index_table5, index_table6
85 };
86
87 2 static av_cold void predict_table_init(void)
88 {
89
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 2 times.
130 for (int start_pos = 0; start_pos < 64; start_pos++) {
90 unsigned int dest_pos, table_pos;
91
92 128 for (table_pos = 0, dest_pos = start_pos;
93
2/2
✓ Branch 0 taken 11392 times.
✓ Branch 1 taken 128 times.
11520 table_pos < FF_ARRAY_ELEMS(ff_adpcm_step_table);
94 11392 table_pos++, dest_pos += 64) {
95 11392 int put = 0, count, table_value;
96
97 11392 table_value = ff_adpcm_step_table[table_pos];
98
2/2
✓ Branch 0 taken 68352 times.
✓ Branch 1 taken 11392 times.
79744 for (count = 32; count != 0; count >>= 1) {
99
2/2
✓ Branch 0 taken 34176 times.
✓ Branch 1 taken 34176 times.
68352 if (start_pos & count)
100 34176 put += table_value;
101 68352 table_value >>= 1;
102 }
103 11392 predict_table[dest_pos] = put;
104 }
105 }
106 2 }
107
108 3 static av_cold int decode_init(AVCodecContext *avctx)
109 {
110 static AVOnce init_static_once = AV_ONCE_INIT;
111
112 3 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
113
114 3 ff_thread_once(&init_static_once, predict_table_init);
115
116 3 return 0;
117 }
118
119 18 static int decode_frame(AVCodecContext *avctx, void *data,
120 int *got_frame_ptr, AVPacket *pkt)
121 {
122 GetBitContext gb;
123 18 AVFrame *frame = data;
124 int16_t pcm_data[2];
125 uint32_t samples;
126 int8_t channel_hint[2];
127 int ret, chan;
128 18 int channels = 1;
129
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (pkt->size < 13)
131 return AVERROR_INVALIDDATA;
132
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
134 return ret;
135
136 18 samples = get_bits_long(&gb, 32);
137
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (samples == 0xffffffff) {
138 18 skip_bits_long(&gb, 32);
139 18 samples = get_bits_long(&gb, 32);
140 }
141
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (samples > pkt->size * 2)
143 return AVERROR_INVALIDDATA;
144
145 18 channel_hint[0] = get_sbits(&gb, 8);
146
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (channel_hint[0] & 0x80) {
147 18 channel_hint[0] = ~channel_hint[0];
148 18 channels = 2;
149 }
150 18 avctx->channels = channels;
151 18 avctx->channel_layout = (channels == 2) ? AV_CH_LAYOUT_STEREO
152
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 : AV_CH_LAYOUT_MONO;
153 18 pcm_data[0] = get_sbits(&gb, 16);
154
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (channels > 1) {
155 18 channel_hint[1] = get_sbits(&gb, 8);
156 18 pcm_data[1] = get_sbits(&gb, 16);
157 }
158
159 18 frame->nb_samples = samples;
160
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
161 return ret;
162
163
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 18 times.
54 for (chan = 0; chan < channels; chan++) {
164 36 uint16_t *dest = (uint16_t *)frame->data[0] + chan;
165 36 int step_index = channel_hint[chan];
166 36 int output = pcm_data[chan];
167 int sample;
168
169
2/2
✓ Branch 0 taken 485100 times.
✓ Branch 1 taken 36 times.
485136 for (sample = 0; sample < samples; sample++) {
170 int lookup_size, lookup, highbit, lowbits;
171
172 485100 step_index = av_clip(step_index, 0, 88);
173 485100 lookup_size = size_table[step_index];
174 485100 lookup = get_bits(&gb, lookup_size);
175 485100 highbit = 1 << (lookup_size - 1);
176 485100 lowbits = highbit - 1;
177
178
2/2
✓ Branch 0 taken 235690 times.
✓ Branch 1 taken 249410 times.
485100 if (lookup & highbit)
179 235690 lookup ^= highbit;
180 else
181 249410 highbit = 0;
182
183
2/2
✓ Branch 0 taken 20815 times.
✓ Branch 1 taken 464285 times.
485100 if (lookup == lowbits) {
184 20815 output = get_sbits(&gb, 16);
185 } else {
186 int predict_index, diff;
187
188 464285 predict_index = (lookup << (7 - lookup_size)) | (step_index << 6);
189 464285 predict_index = av_clip(predict_index, 0, 5785);
190 464285 diff = predict_table[predict_index];
191
2/2
✓ Branch 0 taken 391538 times.
✓ Branch 1 taken 72747 times.
464285 if (lookup)
192 391538 diff += ff_adpcm_step_table[step_index] >> (lookup_size - 1);
193
2/2
✓ Branch 0 taken 225304 times.
✓ Branch 1 taken 238981 times.
464285 if (highbit)
194 225304 diff = -diff;
195
196 464285 output = av_clip_int16(output + diff);
197 }
198
199 485100 *dest = output;
200 485100 dest += channels;
201
202 485100 step_index += step_index_tables[lookup_size - 2][lookup];
203 }
204 }
205
206 18 *got_frame_ptr = 1;
207
208 18 return pkt->size;
209 }
210
211 const AVCodec ff_adpcm_vima_decoder = {
212 .name = "adpcm_vima",
213 .long_name = NULL_IF_CONFIG_SMALL("LucasArts VIMA audio"),
214 .type = AVMEDIA_TYPE_AUDIO,
215 .id = AV_CODEC_ID_ADPCM_VIMA,
216 .init = decode_init,
217 .decode = decode_frame,
218 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
219 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
220 };
221