FFmpeg coverage


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