Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Interplay ACM decoder |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 2004-2008 Marko Kreen |
5 |
|
|
* Copyright (c) 2008 Adam Gashlin |
6 |
|
|
* Copyright (c) 2015 Paul B Mahol |
7 |
|
|
* |
8 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
9 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
10 |
|
|
* copyright notice and this permission notice appear in all copies. |
11 |
|
|
* |
12 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
13 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
14 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
15 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
16 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
17 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
18 |
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include "libavutil/intreadwrite.h" |
22 |
|
|
#include "libavutil/mem.h" |
23 |
|
|
#include "libavutil/thread.h" |
24 |
|
|
|
25 |
|
|
#define BITSTREAM_READER_LE |
26 |
|
|
#include "avcodec.h" |
27 |
|
|
#include "codec_internal.h" |
28 |
|
|
#include "decode.h" |
29 |
|
|
#include "get_bits.h" |
30 |
|
|
|
31 |
|
|
static const int8_t map_1bit[] = { -1, +1 }; |
32 |
|
|
static const int8_t map_2bit_near[] = { -2, -1, +1, +2 }; |
33 |
|
|
static const int8_t map_2bit_far[] = { -3, -2, +2, +3 }; |
34 |
|
|
static const int8_t map_3bit[] = { -4, -3, -2, -1, +1, +2, +3, +4 }; |
35 |
|
|
|
36 |
|
|
static int mul_3x3 [3 * 3 * 3]; |
37 |
|
|
static int mul_3x5 [5 * 5 * 5]; |
38 |
|
|
static int mul_2x11[11 * 11]; |
39 |
|
|
|
40 |
|
|
typedef struct InterplayACMContext { |
41 |
|
|
AVCodecContext *avctx; |
42 |
|
|
GetBitContext gb; |
43 |
|
|
uint8_t *bitstream; |
44 |
|
|
int max_framesize; |
45 |
|
|
uint64_t max_samples; |
46 |
|
|
int bitstream_size; |
47 |
|
|
int bitstream_index; |
48 |
|
|
|
49 |
|
|
int level; |
50 |
|
|
int rows; |
51 |
|
|
int cols; |
52 |
|
|
int wrapbuf_len; |
53 |
|
|
int block_len; |
54 |
|
|
int skip; |
55 |
|
|
|
56 |
|
|
int *block; |
57 |
|
|
int *wrapbuf; |
58 |
|
|
int *ampbuf; |
59 |
|
|
int *midbuf; |
60 |
|
|
} InterplayACMContext; |
61 |
|
|
|
62 |
|
✗ |
static av_cold void decode_init_static(void) |
63 |
|
|
{ |
64 |
|
✗ |
for (int x3 = 0; x3 < 3; x3++) |
65 |
|
✗ |
for (int x2 = 0; x2 < 3; x2++) |
66 |
|
✗ |
for (int x1 = 0; x1 < 3; x1++) |
67 |
|
✗ |
mul_3x3[x1 + x2 * 3 + x3 * 3 * 3] = x1 + (x2 << 4) + (x3 << 8); |
68 |
|
✗ |
for (int x3 = 0; x3 < 5; x3++) |
69 |
|
✗ |
for (int x2 = 0; x2 < 5; x2++) |
70 |
|
✗ |
for (int x1 = 0; x1 < 5; x1++) |
71 |
|
✗ |
mul_3x5[x1 + x2 * 5 + x3 * 5 * 5] = x1 + (x2 << 4) + (x3 << 8); |
72 |
|
✗ |
for (int x2 = 0; x2 < 11; x2++) |
73 |
|
✗ |
for (int x1 = 0; x1 < 11; x1++) |
74 |
|
✗ |
mul_2x11[x1 + x2 * 11] = x1 + (x2 << 4); |
75 |
|
✗ |
} |
76 |
|
|
|
77 |
|
✗ |
static av_cold int decode_init(AVCodecContext *avctx) |
78 |
|
|
{ |
79 |
|
|
static AVOnce init_static_once = AV_ONCE_INIT; |
80 |
|
✗ |
InterplayACMContext *s = avctx->priv_data; |
81 |
|
|
|
82 |
|
✗ |
s->avctx = avctx; |
83 |
|
✗ |
if (avctx->extradata_size < 14) |
84 |
|
✗ |
return AVERROR_INVALIDDATA; |
85 |
|
|
|
86 |
|
✗ |
if (avctx->ch_layout.nb_channels <= 0) { |
87 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Invalid number of channels: %d\n", avctx->ch_layout.nb_channels); |
88 |
|
✗ |
return AVERROR_INVALIDDATA; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
✗ |
s->max_samples = AV_RL32(avctx->extradata + 4) / avctx->ch_layout.nb_channels; |
92 |
|
✗ |
if (s->max_samples == 0) |
93 |
|
✗ |
s->max_samples = UINT64_MAX; |
94 |
|
✗ |
s->level = AV_RL16(avctx->extradata + 12) & 0xf; |
95 |
|
✗ |
s->rows = AV_RL16(avctx->extradata + 12) >> 4; |
96 |
|
✗ |
s->cols = 1 << s->level; |
97 |
|
✗ |
s->wrapbuf_len = 2 * s->cols - 2; |
98 |
|
✗ |
s->block_len = s->rows * s->cols; |
99 |
|
✗ |
s->max_framesize = s->block_len; |
100 |
|
|
|
101 |
|
✗ |
s->block = av_calloc(s->block_len, sizeof(int)); |
102 |
|
✗ |
s->wrapbuf = av_calloc(s->wrapbuf_len, sizeof(int)); |
103 |
|
✗ |
s->ampbuf = av_calloc(0x10000, sizeof(int)); |
104 |
|
✗ |
s->bitstream = av_calloc(s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE / sizeof(*s->bitstream) + 1, sizeof(*s->bitstream)); |
105 |
|
✗ |
if (!s->block || !s->wrapbuf || !s->ampbuf || !s->bitstream) |
106 |
|
✗ |
return AVERROR(ENOMEM); |
107 |
|
|
|
108 |
|
✗ |
s->midbuf = s->ampbuf + 0x8000; |
109 |
|
✗ |
avctx->sample_fmt = AV_SAMPLE_FMT_S16; |
110 |
|
|
|
111 |
|
✗ |
ff_thread_once(&init_static_once, decode_init_static); |
112 |
|
|
|
113 |
|
✗ |
return 0; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
#define set_pos(s, r, c, idx) do { \ |
117 |
|
|
unsigned pos = ((r) << s->level) + (c); \ |
118 |
|
|
s->block[pos] = s->midbuf[(idx)]; \ |
119 |
|
|
} while (0) |
120 |
|
|
|
121 |
|
✗ |
static int zero(InterplayACMContext *s, unsigned ind, unsigned col) |
122 |
|
|
{ |
123 |
|
|
unsigned i; |
124 |
|
|
|
125 |
|
✗ |
for (i = 0; i < s->rows; i++) |
126 |
|
✗ |
set_pos(s, i, col, 0); |
127 |
|
✗ |
return 0; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
✗ |
static int bad(InterplayACMContext *s, unsigned ind, unsigned col) |
131 |
|
|
{ |
132 |
|
✗ |
return AVERROR_INVALIDDATA; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
✗ |
static int linear(InterplayACMContext *s, unsigned ind, unsigned col) |
136 |
|
|
{ |
137 |
|
✗ |
GetBitContext *gb = &s->gb; |
138 |
|
|
unsigned int i; |
139 |
|
✗ |
int b, middle = 1 << (ind - 1); |
140 |
|
|
|
141 |
|
✗ |
for (i = 0; i < s->rows; i++) { |
142 |
|
✗ |
b = get_bits(gb, ind); |
143 |
|
✗ |
set_pos(s, i, col, b - middle); |
144 |
|
|
} |
145 |
|
✗ |
return 0; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
✗ |
static int k13(InterplayACMContext *s, unsigned ind, unsigned col) |
149 |
|
|
{ |
150 |
|
✗ |
GetBitContext *gb = &s->gb; |
151 |
|
|
unsigned i, b; |
152 |
|
|
|
153 |
|
✗ |
for (i = 0; i < s->rows; i++) { |
154 |
|
✗ |
b = get_bits1(gb); |
155 |
|
✗ |
if (b == 0) { |
156 |
|
✗ |
set_pos(s, i++, col, 0); |
157 |
|
✗ |
if (i >= s->rows) |
158 |
|
✗ |
break; |
159 |
|
✗ |
set_pos(s, i, col, 0); |
160 |
|
✗ |
continue; |
161 |
|
|
} |
162 |
|
✗ |
b = get_bits1(gb); |
163 |
|
✗ |
if (b == 0) { |
164 |
|
✗ |
set_pos(s, i, col, 0); |
165 |
|
✗ |
continue; |
166 |
|
|
} |
167 |
|
✗ |
b = get_bits1(gb); |
168 |
|
✗ |
set_pos(s, i, col, map_1bit[b]); |
169 |
|
|
} |
170 |
|
✗ |
return 0; |
171 |
|
|
} |
172 |
|
|
|
173 |
|
✗ |
static int k12(InterplayACMContext *s, unsigned ind, unsigned col) |
174 |
|
|
{ |
175 |
|
✗ |
GetBitContext *gb = &s->gb; |
176 |
|
|
unsigned i, b; |
177 |
|
|
|
178 |
|
✗ |
for (i = 0; i < s->rows; i++) { |
179 |
|
✗ |
b = get_bits1(gb); |
180 |
|
✗ |
if (b == 0) { |
181 |
|
✗ |
set_pos(s, i, col, 0); |
182 |
|
✗ |
continue; |
183 |
|
|
} |
184 |
|
|
|
185 |
|
✗ |
b = get_bits1(gb); |
186 |
|
✗ |
set_pos(s, i, col, map_1bit[b]); |
187 |
|
|
} |
188 |
|
✗ |
return 0; |
189 |
|
|
} |
190 |
|
|
|
191 |
|
✗ |
static int k24(InterplayACMContext *s, unsigned ind, unsigned col) |
192 |
|
|
{ |
193 |
|
✗ |
GetBitContext *gb = &s->gb; |
194 |
|
|
unsigned i, b; |
195 |
|
|
|
196 |
|
✗ |
for (i = 0; i < s->rows; i++) { |
197 |
|
✗ |
b = get_bits1(gb); |
198 |
|
✗ |
if (b == 0) { |
199 |
|
✗ |
set_pos(s, i++, col, 0); |
200 |
|
✗ |
if (i >= s->rows) break; |
201 |
|
✗ |
set_pos(s, i, col, 0); |
202 |
|
✗ |
continue; |
203 |
|
|
} |
204 |
|
|
|
205 |
|
✗ |
b = get_bits1(gb); |
206 |
|
✗ |
if (b == 0) { |
207 |
|
✗ |
set_pos(s, i, col, 0); |
208 |
|
✗ |
continue; |
209 |
|
|
} |
210 |
|
|
|
211 |
|
✗ |
b = get_bits(gb, 2); |
212 |
|
✗ |
set_pos(s, i, col, map_2bit_near[b]); |
213 |
|
|
} |
214 |
|
✗ |
return 0; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
✗ |
static int k23(InterplayACMContext *s, unsigned ind, unsigned col) |
218 |
|
|
{ |
219 |
|
✗ |
GetBitContext *gb = &s->gb; |
220 |
|
|
unsigned i, b; |
221 |
|
|
|
222 |
|
✗ |
for (i = 0; i < s->rows; i++) { |
223 |
|
✗ |
b = get_bits1(gb); |
224 |
|
✗ |
if (b == 0) { |
225 |
|
✗ |
set_pos(s, i, col, 0); |
226 |
|
✗ |
continue; |
227 |
|
|
} |
228 |
|
|
|
229 |
|
✗ |
b = get_bits(gb, 2); |
230 |
|
✗ |
set_pos(s, i, col, map_2bit_near[b]); |
231 |
|
|
} |
232 |
|
✗ |
return 0; |
233 |
|
|
} |
234 |
|
|
|
235 |
|
✗ |
static int k35(InterplayACMContext *s, unsigned ind, unsigned col) |
236 |
|
|
{ |
237 |
|
✗ |
GetBitContext *gb = &s->gb; |
238 |
|
|
unsigned i, b; |
239 |
|
|
|
240 |
|
✗ |
for (i = 0; i < s->rows; i++) { |
241 |
|
✗ |
b = get_bits1(gb); |
242 |
|
✗ |
if (b == 0) { |
243 |
|
✗ |
set_pos(s, i++, col, 0); |
244 |
|
✗ |
if (i >= s->rows) |
245 |
|
✗ |
break; |
246 |
|
✗ |
set_pos(s, i, col, 0); |
247 |
|
✗ |
continue; |
248 |
|
|
} |
249 |
|
|
|
250 |
|
✗ |
b = get_bits1(gb); |
251 |
|
✗ |
if (b == 0) { |
252 |
|
✗ |
set_pos(s, i, col, 0); |
253 |
|
✗ |
continue; |
254 |
|
|
} |
255 |
|
|
|
256 |
|
✗ |
b = get_bits1(gb); |
257 |
|
✗ |
if (b == 0) { |
258 |
|
✗ |
b = get_bits1(gb); |
259 |
|
✗ |
set_pos(s, i, col, map_1bit[b]); |
260 |
|
✗ |
continue; |
261 |
|
|
} |
262 |
|
|
|
263 |
|
✗ |
b = get_bits(gb, 2); |
264 |
|
✗ |
set_pos(s, i, col, map_2bit_far[b]); |
265 |
|
|
} |
266 |
|
✗ |
return 0; |
267 |
|
|
} |
268 |
|
|
|
269 |
|
✗ |
static int k34(InterplayACMContext *s, unsigned ind, unsigned col) |
270 |
|
|
{ |
271 |
|
✗ |
GetBitContext *gb = &s->gb; |
272 |
|
|
unsigned i, b; |
273 |
|
|
|
274 |
|
✗ |
for (i = 0; i < s->rows; i++) { |
275 |
|
✗ |
b = get_bits1(gb); |
276 |
|
✗ |
if (b == 0) { |
277 |
|
✗ |
set_pos(s, i, col, 0); |
278 |
|
✗ |
continue; |
279 |
|
|
} |
280 |
|
|
|
281 |
|
✗ |
b = get_bits1(gb); |
282 |
|
✗ |
if (b == 0) { |
283 |
|
✗ |
b = get_bits1(gb); |
284 |
|
✗ |
set_pos(s, i, col, map_1bit[b]); |
285 |
|
✗ |
continue; |
286 |
|
|
} |
287 |
|
|
|
288 |
|
✗ |
b = get_bits(gb, 2); |
289 |
|
✗ |
set_pos(s, i, col, map_2bit_far[b]); |
290 |
|
|
} |
291 |
|
✗ |
return 0; |
292 |
|
|
} |
293 |
|
|
|
294 |
|
✗ |
static int k45(InterplayACMContext *s, unsigned ind, unsigned col) |
295 |
|
|
{ |
296 |
|
✗ |
GetBitContext *gb = &s->gb; |
297 |
|
|
unsigned i, b; |
298 |
|
|
|
299 |
|
✗ |
for (i = 0; i < s->rows; i++) { |
300 |
|
✗ |
b = get_bits1(gb); |
301 |
|
✗ |
if (b == 0) { |
302 |
|
✗ |
set_pos(s, i, col, 0); i++; |
303 |
|
✗ |
if (i >= s->rows) |
304 |
|
✗ |
break; |
305 |
|
✗ |
set_pos(s, i, col, 0); |
306 |
|
✗ |
continue; |
307 |
|
|
} |
308 |
|
|
|
309 |
|
✗ |
b = get_bits1(gb); |
310 |
|
✗ |
if (b == 0) { |
311 |
|
✗ |
set_pos(s, i, col, 0); |
312 |
|
✗ |
continue; |
313 |
|
|
} |
314 |
|
|
|
315 |
|
✗ |
b = get_bits(gb, 3); |
316 |
|
✗ |
set_pos(s, i, col, map_3bit[b]); |
317 |
|
|
} |
318 |
|
✗ |
return 0; |
319 |
|
|
} |
320 |
|
|
|
321 |
|
✗ |
static int k44(InterplayACMContext *s, unsigned ind, unsigned col) |
322 |
|
|
{ |
323 |
|
✗ |
GetBitContext *gb = &s->gb; |
324 |
|
|
unsigned i, b; |
325 |
|
|
|
326 |
|
✗ |
for (i = 0; i < s->rows; i++) { |
327 |
|
✗ |
b = get_bits1(gb); |
328 |
|
✗ |
if (b == 0) { |
329 |
|
✗ |
set_pos(s, i, col, 0); |
330 |
|
✗ |
continue; |
331 |
|
|
} |
332 |
|
|
|
333 |
|
✗ |
b = get_bits(gb, 3); |
334 |
|
✗ |
set_pos(s, i, col, map_3bit[b]); |
335 |
|
|
} |
336 |
|
✗ |
return 0; |
337 |
|
|
} |
338 |
|
|
|
339 |
|
✗ |
static int t15(InterplayACMContext *s, unsigned ind, unsigned col) |
340 |
|
|
{ |
341 |
|
✗ |
GetBitContext *gb = &s->gb; |
342 |
|
|
unsigned i, b; |
343 |
|
|
int n1, n2, n3; |
344 |
|
|
|
345 |
|
✗ |
for (i = 0; i < s->rows; i++) { |
346 |
|
|
/* b = (x1) + (x2 * 3) + (x3 * 9) */ |
347 |
|
✗ |
b = get_bits(gb, 5); |
348 |
|
✗ |
if (b > 26) { |
349 |
|
✗ |
av_log(s->avctx, AV_LOG_ERROR, "Too large b = %d > 26\n", b); |
350 |
|
✗ |
return AVERROR_INVALIDDATA; |
351 |
|
|
} |
352 |
|
|
|
353 |
|
✗ |
n1 = (mul_3x3[b] & 0x0F) - 1; |
354 |
|
✗ |
n2 = ((mul_3x3[b] >> 4) & 0x0F) - 1; |
355 |
|
✗ |
n3 = ((mul_3x3[b] >> 8) & 0x0F) - 1; |
356 |
|
|
|
357 |
|
✗ |
set_pos(s, i++, col, n1); |
358 |
|
✗ |
if (i >= s->rows) |
359 |
|
✗ |
break; |
360 |
|
✗ |
set_pos(s, i++, col, n2); |
361 |
|
✗ |
if (i >= s->rows) |
362 |
|
✗ |
break; |
363 |
|
✗ |
set_pos(s, i, col, n3); |
364 |
|
|
} |
365 |
|
✗ |
return 0; |
366 |
|
|
} |
367 |
|
|
|
368 |
|
✗ |
static int t27(InterplayACMContext *s, unsigned ind, unsigned col) |
369 |
|
|
{ |
370 |
|
✗ |
GetBitContext *gb = &s->gb; |
371 |
|
|
unsigned i, b; |
372 |
|
|
int n1, n2, n3; |
373 |
|
|
|
374 |
|
✗ |
for (i = 0; i < s->rows; i++) { |
375 |
|
|
/* b = (x1) + (x2 * 5) + (x3 * 25) */ |
376 |
|
✗ |
b = get_bits(gb, 7); |
377 |
|
✗ |
if (b > 124) { |
378 |
|
✗ |
av_log(s->avctx, AV_LOG_ERROR, "Too large b = %d > 124\n", b); |
379 |
|
✗ |
return AVERROR_INVALIDDATA; |
380 |
|
|
} |
381 |
|
|
|
382 |
|
✗ |
n1 = (mul_3x5[b] & 0x0F) - 2; |
383 |
|
✗ |
n2 = ((mul_3x5[b] >> 4) & 0x0F) - 2; |
384 |
|
✗ |
n3 = ((mul_3x5[b] >> 8) & 0x0F) - 2; |
385 |
|
|
|
386 |
|
✗ |
set_pos(s, i++, col, n1); |
387 |
|
✗ |
if (i >= s->rows) |
388 |
|
✗ |
break; |
389 |
|
✗ |
set_pos(s, i++, col, n2); |
390 |
|
✗ |
if (i >= s->rows) |
391 |
|
✗ |
break; |
392 |
|
✗ |
set_pos(s, i, col, n3); |
393 |
|
|
} |
394 |
|
✗ |
return 0; |
395 |
|
|
} |
396 |
|
|
|
397 |
|
✗ |
static int t37(InterplayACMContext *s, unsigned ind, unsigned col) |
398 |
|
|
{ |
399 |
|
✗ |
GetBitContext *gb = &s->gb; |
400 |
|
|
unsigned i, b; |
401 |
|
|
int n1, n2; |
402 |
|
✗ |
for (i = 0; i < s->rows; i++) { |
403 |
|
|
/* b = (x1) + (x2 * 11) */ |
404 |
|
✗ |
b = get_bits(gb, 7); |
405 |
|
✗ |
if (b > 120) { |
406 |
|
✗ |
av_log(s->avctx, AV_LOG_ERROR, "Too large b = %d > 120\n", b); |
407 |
|
✗ |
return AVERROR_INVALIDDATA; |
408 |
|
|
} |
409 |
|
|
|
410 |
|
✗ |
n1 = (mul_2x11[b] & 0x0F) - 5; |
411 |
|
✗ |
n2 = ((mul_2x11[b] >> 4) & 0x0F) - 5; |
412 |
|
|
|
413 |
|
✗ |
set_pos(s, i++, col, n1); |
414 |
|
✗ |
if (i >= s->rows) |
415 |
|
✗ |
break; |
416 |
|
✗ |
set_pos(s, i, col, n2); |
417 |
|
|
} |
418 |
|
✗ |
return 0; |
419 |
|
|
} |
420 |
|
|
|
421 |
|
|
typedef int (*filler)(InterplayACMContext *s, unsigned ind, unsigned col); |
422 |
|
|
|
423 |
|
|
static const filler filler_list[] = { |
424 |
|
|
zero, bad, bad, linear, |
425 |
|
|
linear, linear, linear, linear, |
426 |
|
|
linear, linear, linear, linear, |
427 |
|
|
linear, linear, linear, linear, |
428 |
|
|
linear, k13, k12, t15, |
429 |
|
|
k24, k23, t27, k35, |
430 |
|
|
k34, bad, k45, k44, |
431 |
|
|
bad, t37, bad, bad, |
432 |
|
|
}; |
433 |
|
|
|
434 |
|
✗ |
static int fill_block(InterplayACMContext *s) |
435 |
|
|
{ |
436 |
|
✗ |
GetBitContext *gb = &s->gb; |
437 |
|
|
unsigned i, ind; |
438 |
|
|
int ret; |
439 |
|
|
|
440 |
|
✗ |
for (i = 0; i < s->cols; i++) { |
441 |
|
✗ |
ind = get_bits(gb, 5); |
442 |
|
✗ |
ret = filler_list[ind](s, ind, i); |
443 |
|
✗ |
if (ret < 0) |
444 |
|
✗ |
return ret; |
445 |
|
|
} |
446 |
|
✗ |
return 0; |
447 |
|
|
} |
448 |
|
|
|
449 |
|
✗ |
static void juggle(int *wrap_p, int *block_p, unsigned sub_len, unsigned sub_count) |
450 |
|
|
{ |
451 |
|
|
unsigned i, j; |
452 |
|
|
int *p; |
453 |
|
|
unsigned int r0, r1, r2, r3; |
454 |
|
|
|
455 |
|
✗ |
for (i = 0; i < sub_len; i++) { |
456 |
|
✗ |
p = block_p; |
457 |
|
✗ |
r0 = wrap_p[0]; |
458 |
|
✗ |
r1 = wrap_p[1]; |
459 |
|
✗ |
for (j = 0; j < sub_count/2; j++) { |
460 |
|
✗ |
r2 = *p; |
461 |
|
✗ |
*p = r1 * 2 + (r0 + r2); |
462 |
|
✗ |
p += sub_len; |
463 |
|
✗ |
r3 = *p; |
464 |
|
✗ |
*p = r2 * 2 - (r1 + r3); |
465 |
|
✗ |
p += sub_len; |
466 |
|
✗ |
r0 = r2; |
467 |
|
✗ |
r1 = r3; |
468 |
|
|
} |
469 |
|
|
|
470 |
|
✗ |
*wrap_p++ = r0; |
471 |
|
✗ |
*wrap_p++ = r1; |
472 |
|
✗ |
block_p++; |
473 |
|
|
} |
474 |
|
✗ |
} |
475 |
|
|
|
476 |
|
✗ |
static void juggle_block(InterplayACMContext *s) |
477 |
|
|
{ |
478 |
|
|
unsigned sub_count, sub_len, todo_count, step_subcount, i; |
479 |
|
|
int *wrap_p, *block_p, *p; |
480 |
|
|
|
481 |
|
|
/* juggle only if subblock_len > 1 */ |
482 |
|
✗ |
if (s->level == 0) |
483 |
|
✗ |
return; |
484 |
|
|
|
485 |
|
|
/* 2048 / subblock_len */ |
486 |
|
✗ |
if (s->level > 9) |
487 |
|
✗ |
step_subcount = 1; |
488 |
|
|
else |
489 |
|
✗ |
step_subcount = (2048 >> s->level) - 2; |
490 |
|
|
|
491 |
|
|
/* Apply juggle() (rows)x(cols) |
492 |
|
|
* from (step_subcount * 2) x (subblock_len/2) |
493 |
|
|
* to (step_subcount * subblock_len) x (1) |
494 |
|
|
*/ |
495 |
|
✗ |
todo_count = s->rows; |
496 |
|
✗ |
block_p = s->block; |
497 |
|
|
while (1) { |
498 |
|
✗ |
wrap_p = s->wrapbuf; |
499 |
|
✗ |
sub_count = step_subcount; |
500 |
|
✗ |
if (sub_count > todo_count) |
501 |
|
✗ |
sub_count = todo_count; |
502 |
|
|
|
503 |
|
✗ |
sub_len = s->cols / 2; |
504 |
|
✗ |
sub_count *= 2; |
505 |
|
|
|
506 |
|
✗ |
juggle(wrap_p, block_p, sub_len, sub_count); |
507 |
|
✗ |
wrap_p += sub_len * 2; |
508 |
|
|
|
509 |
|
✗ |
for (i = 0, p = block_p; i < sub_count; i++) { |
510 |
|
✗ |
p[0]++; |
511 |
|
✗ |
p += sub_len; |
512 |
|
|
} |
513 |
|
|
|
514 |
|
✗ |
while (sub_len > 1) { |
515 |
|
✗ |
sub_len /= 2; |
516 |
|
✗ |
sub_count *= 2; |
517 |
|
✗ |
juggle(wrap_p, block_p, sub_len, sub_count); |
518 |
|
✗ |
wrap_p += sub_len * 2; |
519 |
|
|
} |
520 |
|
|
|
521 |
|
✗ |
if (todo_count <= step_subcount) |
522 |
|
✗ |
break; |
523 |
|
|
|
524 |
|
✗ |
todo_count -= step_subcount; |
525 |
|
✗ |
block_p += step_subcount << s->level; |
526 |
|
|
} |
527 |
|
|
} |
528 |
|
|
|
529 |
|
✗ |
static int decode_block(InterplayACMContext *s) |
530 |
|
|
{ |
531 |
|
✗ |
GetBitContext *gb = &s->gb; |
532 |
|
|
int pwr, count, val, i, x, ret; |
533 |
|
|
|
534 |
|
✗ |
pwr = get_bits(gb, 4); |
535 |
|
✗ |
val = get_bits(gb, 16); |
536 |
|
|
|
537 |
|
✗ |
count = 1 << pwr; |
538 |
|
|
|
539 |
|
✗ |
for (i = 0, x = 0; i < count; i++) { |
540 |
|
✗ |
s->midbuf[i] = x; |
541 |
|
✗ |
x += val; |
542 |
|
|
} |
543 |
|
|
|
544 |
|
✗ |
for (i = 1, x = -val; i <= count; i++) { |
545 |
|
✗ |
s->midbuf[-i] = x; |
546 |
|
✗ |
x -= (unsigned)val; |
547 |
|
|
} |
548 |
|
|
|
549 |
|
✗ |
ret = fill_block(s); |
550 |
|
✗ |
if (ret < 0) |
551 |
|
✗ |
return ret; |
552 |
|
|
|
553 |
|
✗ |
juggle_block(s); |
554 |
|
|
|
555 |
|
✗ |
return 0; |
556 |
|
|
} |
557 |
|
|
|
558 |
|
✗ |
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, |
559 |
|
|
int *got_frame_ptr, AVPacket *pkt) |
560 |
|
|
{ |
561 |
|
✗ |
InterplayACMContext *s = avctx->priv_data; |
562 |
|
✗ |
GetBitContext *gb = &s->gb; |
563 |
|
|
const uint8_t *buf; |
564 |
|
|
int16_t *samples; |
565 |
|
|
int ret, n, buf_size, input_buf_size; |
566 |
|
|
|
567 |
|
✗ |
if (!pkt->size && !s->bitstream_size) { |
568 |
|
✗ |
*got_frame_ptr = 0; |
569 |
|
✗ |
return 0; |
570 |
|
|
} |
571 |
|
|
|
572 |
|
✗ |
buf_size = FFMIN(pkt->size, s->max_framesize - s->bitstream_size); |
573 |
|
✗ |
input_buf_size = buf_size; |
574 |
|
✗ |
if (s->bitstream_index + s->bitstream_size + buf_size > s->max_framesize) { |
575 |
|
✗ |
memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size); |
576 |
|
✗ |
s->bitstream_index = 0; |
577 |
|
|
} |
578 |
|
✗ |
if (pkt->data) |
579 |
|
✗ |
memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], pkt->data, buf_size); |
580 |
|
✗ |
buf = &s->bitstream[s->bitstream_index]; |
581 |
|
✗ |
buf_size += s->bitstream_size; |
582 |
|
✗ |
s->bitstream_size = buf_size; |
583 |
|
✗ |
if (buf_size < s->max_framesize && pkt->data) { |
584 |
|
✗ |
*got_frame_ptr = 0; |
585 |
|
✗ |
return input_buf_size; |
586 |
|
|
} |
587 |
|
|
|
588 |
|
✗ |
if ((ret = init_get_bits8(gb, buf, buf_size)) < 0) |
589 |
|
✗ |
return ret; |
590 |
|
|
|
591 |
|
✗ |
frame->nb_samples = FFMIN(s->block_len / avctx->ch_layout.nb_channels, s->max_samples); |
592 |
|
✗ |
s->max_samples -= FFMIN(frame->nb_samples, s->max_samples); |
593 |
|
✗ |
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
594 |
|
✗ |
return ret; |
595 |
|
|
|
596 |
|
✗ |
skip_bits(gb, s->skip); |
597 |
|
✗ |
ret = decode_block(s); |
598 |
|
✗ |
if (ret < 0) |
599 |
|
✗ |
return ret; |
600 |
|
|
|
601 |
|
✗ |
samples = (int16_t *)frame->data[0]; |
602 |
|
✗ |
for (n = 0; n < frame->nb_samples * avctx->ch_layout.nb_channels; n++) { |
603 |
|
✗ |
int val = s->block[n] >> s->level; |
604 |
|
✗ |
*samples++ = val; |
605 |
|
|
} |
606 |
|
|
|
607 |
|
✗ |
*got_frame_ptr = 1; |
608 |
|
✗ |
s->skip = get_bits_count(gb) - 8 * (get_bits_count(gb) / 8); |
609 |
|
✗ |
n = get_bits_count(gb) / 8; |
610 |
|
|
|
611 |
|
✗ |
if (n > buf_size && pkt->data) { |
612 |
|
✗ |
s->bitstream_size = 0; |
613 |
|
✗ |
s->bitstream_index = 0; |
614 |
|
✗ |
return AVERROR_INVALIDDATA; |
615 |
|
|
} |
616 |
|
|
|
617 |
|
✗ |
if (s->bitstream_size > 0) { |
618 |
|
✗ |
s->bitstream_index += n; |
619 |
|
✗ |
s->bitstream_size -= FFMIN(s->bitstream_size, n); |
620 |
|
✗ |
return input_buf_size; |
621 |
|
|
} |
622 |
|
✗ |
return n; |
623 |
|
|
} |
624 |
|
|
|
625 |
|
✗ |
static av_cold int decode_close(AVCodecContext *avctx) |
626 |
|
|
{ |
627 |
|
✗ |
InterplayACMContext *s = avctx->priv_data; |
628 |
|
|
|
629 |
|
✗ |
av_freep(&s->block); |
630 |
|
✗ |
av_freep(&s->wrapbuf); |
631 |
|
✗ |
av_freep(&s->ampbuf); |
632 |
|
✗ |
av_freep(&s->bitstream); |
633 |
|
✗ |
s->bitstream_size = 0; |
634 |
|
|
|
635 |
|
✗ |
return 0; |
636 |
|
|
} |
637 |
|
|
|
638 |
|
|
const FFCodec ff_interplay_acm_decoder = { |
639 |
|
|
.p.name = "interplayacm", |
640 |
|
|
CODEC_LONG_NAME("Interplay ACM"), |
641 |
|
|
.p.type = AVMEDIA_TYPE_AUDIO, |
642 |
|
|
.p.id = AV_CODEC_ID_INTERPLAY_ACM, |
643 |
|
|
.init = decode_init, |
644 |
|
|
.close = decode_close, |
645 |
|
|
FF_CODEC_DECODE_CB(decode_frame), |
646 |
|
|
.p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1, |
647 |
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
648 |
|
|
.priv_data_size = sizeof(InterplayACMContext), |
649 |
|
|
}; |
650 |
|
|
|