FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/interplayacm.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 0 341 0.0%
Branches: 0 160 0.0%

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