History

avcodec/wavarc: add 5elp mode support

Paul B Mahol committed 3 years ago

Commit 18b2ecc1 parent 024c30a1 file · +348 −1Browse files

Showing the changes to libavcodec (1 of 1 file). Show all

Unified · Split

libavcodec/wavarc.c+348−1
@@ -30,6 +30,8 @@ · 29 unmodified lines
3030 #include "unary.h"
3131
3232 typedef struct WavArcContext {
33+ AVClass *av_class;
34+
3335 GetBitContext gb;
3436
3537 int shift;
@@ -47,6 +49,16 @@ typedef struct WavArcContext { · 11 unmodified lines
4749 int pred[2][70];
4850 int filter[2][70];
4951 int samples[2][640];
52+ uint8_t model[256];
53+ uint16_t freqs[257];
54+ uint16_t ac_value;
55+ uint16_t ac_low;
56+ uint16_t ac_high;
57+ uint16_t range_high;
58+ uint16_t range_low;
59+ uint16_t freq_range;
60+ int ac_pred[70];
61+ int ac_out[570];
5062 } WavArcContext;
5163
5264 static av_cold int wavarc_init(AVCodecContext *avctx)
@@ -90,6 +102,7 @@ static av_cold int wavarc_init(AVCodecContext *avctx) · 37 unmodified lines
90102 case MKTAG('2','S','L','P'):
91103 case MKTAG('3','N','L','P'):
92104 case MKTAG('4','A','L','P'):
105+ case MKTAG('5','E','L','P'):
93106 s->nb_samples = 570;
94107 s->offset = 70;
95108 break;
@@ -343,7 +356,7 @@ static int decode_2slp(AVCodecContext *avctx, · 247 unmodified lines
343356 break;
344357 case 0:
345358 order = get_urice(gb, 2);
346− if ((unsigned)order >= FF_ARRAY_ELEMS(s->filter[ch]))
359+ if ((unsigned)order > FF_ARRAY_ELEMS(s->filter[ch]))
347360 return AVERROR_INVALIDDATA;
348361 for (int o = 0; o < order; o++)
349362 s->filter[ch][o] = get_srice(gb, 2);
@@ -378,6 +391,337 @@ static int decode_2slp(AVCodecContext *avctx, · 28 unmodified lines
378391 return 0;
379392 }
380393
394+static int ac_init(AVCodecContext *avctx,
395+ WavArcContext *s, GetBitContext *gb)
396+{
397+ s->ac_low = 0;
398+ s->ac_high = 0xffffu;
399+ s->ac_value = get_bits(gb, 16);
400+
401+ s->freq_range = s->freqs[256];
402+ if (!s->freq_range)
403+ return AVERROR_INVALIDDATA;
404+ return 0;
405+}
406+
407+static unsigned ac_get_prob(WavArcContext *s)
408+{
409+ return ((s->freq_range - 1) + ((unsigned)s->ac_value - (unsigned)s->ac_low) *
410+ (unsigned)s->freq_range) / (((unsigned)s->ac_high - (unsigned)s->ac_low) + 1) & 0xffffu;
411+}
412+
413+static unsigned ac_map_symbol(WavArcContext *s, unsigned prob)
414+{
415+ int idx = 255;
416+
417+ while (prob < s->freqs[idx])
418+ idx--;
419+
420+ s->range_high = s->freqs[idx + 1];
421+ s->range_low = s->freqs[idx];
422+
423+ return idx;
424+}
425+
426+static int ac_normalize(AVCodecContext *avctx, WavArcContext *s, GetBitContext *gb)
427+{
428+ int range;
429+
430+ range = (unsigned)s->ac_high - (unsigned)s->ac_low + 1;
431+ s->ac_high = (int16_t)((range * (unsigned)s->range_high) / (unsigned)s->freq_range) + -1 + s->ac_low;
432+ s->ac_low += (int16_t)((range * (unsigned)s->range_low) / (unsigned)s->freq_range);
433+
434+ for (;;) {
435+ if ((s->ac_high & 0x8000) != (s->ac_low & 0x8000)) {
436+ if ((s->ac_low & 0x4000) == 0 || (s->ac_high & 0x4000) != 0)
437+ return 0;
438+ s->ac_value ^= 0x4000;
439+ s->ac_low &= 0x3fff;
440+ s->ac_high |= 0x4000;
441+ }
442+
443+ s->ac_low = s->ac_low << 1;
444+ s->ac_high = s->ac_high * 2 + 1;
445+ if (s->ac_high < s->ac_low) {
446+ av_log(avctx, AV_LOG_ERROR, "invalid state\n");
447+ return AVERROR_INVALIDDATA;
448+ }
449+
450+ if (get_bits_left(gb) <= 0) {
451+ av_log(avctx, AV_LOG_ERROR, "overread in arithmetic coder\n");
452+ return AVERROR_INVALIDDATA;
453+ }
454+
455+ s->ac_value = s->ac_value * 2 + get_bits1(gb);
456+ }
457+}
458+
459+static void ac_init_model(WavArcContext *s)
460+{
461+ memset(s->freqs, 0, sizeof(s->freqs));
462+
463+ for (int n = 0; n < 256; n++)
464+ s->freqs[n+1] = s->model[n] + s->freqs[n];
465+}
466+
467+static int ac_read_model(AVCodecContext *avctx,
468+ WavArcContext *s,
469+ GetBitContext *gb)
470+{
471+ unsigned start, end;
472+
473+ memset(s->model, 0, sizeof(s->model));
474+
475+ start = get_bits(gb, 8);
476+ end = get_bits(gb, 8);
477+
478+ for (;;) {
479+ while (start <= end)
480+ s->model[start++] = get_bits(gb, 8);
481+
482+ if (get_bits_left(gb) < 8)
483+ return AVERROR_INVALIDDATA;
484+
485+ if (get_bits_left(gb) < 8)
486+ return AVERROR_INVALIDDATA;
487+
488+ start = get_bits(gb, 8);
489+ if (!start)
490+ break;
491+
492+ end = get_bits(gb, 8);
493+ }
494+
495+ ac_init_model(s);
496+
497+ return 0;
498+}
499+
500+static int decode_5elp(AVCodecContext *avctx,
501+ WavArcContext *s, GetBitContext *gb)
502+{
503+ int ch, finished, fill, correlated, order = 0;
504+
505+ ch = 0;
506+ finished = 0;
507+ while (!finished) {
508+ int *samples = s->samples[ch];
509+ int *ac_pred = s->ac_pred;
510+ int *ac_out = s->ac_out;
511+ int k, block_type;
512+
513+ if (get_bits_left(gb) <= 0)
514+ return AVERROR_INVALIDDATA;
515+
516+ memset(s->ac_out, 0, sizeof(s->ac_out));
517+
518+ block_type = get_urice(gb, 1);
519+ av_log(avctx, AV_LOG_DEBUG, "block_type : %d\n", block_type);
520+
521+ if (block_type >= 0 && block_type <= 7) {
522+ k = 1 + (avctx->sample_fmt == AV_SAMPLE_FMT_S16P);
523+ k = get_urice(gb, k) + 1;
524+ }
525+
526+ if (block_type <= 2 || block_type == 6 || block_type == 13 ||
527+ block_type == 14 || block_type == 15 || block_type == 19) {
528+ order = get_urice(gb, 2);
529+ if ((unsigned)order > FF_ARRAY_ELEMS(s->filter[ch]))
530+ return AVERROR_INVALIDDATA;
531+ for (int o = 0; o < order; o++)
532+ s->filter[ch][o] = get_srice(gb, 2);
533+ }
534+
535+ if (block_type >= 0 && block_type <= 7) {
536+ for (int n = 0; n < s->nb_samples; n++)
537+ samples[n + 70] = get_srice(gb, k);
538+ } else {
539+ for (int n = 0; n < s->nb_samples; n++)
540+ samples[n + 70] = 0;
541+ }
542+
543+ if (block_type >= 13 && block_type <= 20) {
544+ const int ac_size = get_bits(gb, 12);
545+ const int ac_pos = get_bits_count(gb);
546+ GetBitContext ac_gb = *gb;
547+ int ret;
548+
549+ skip_bits_long(gb, ac_size);
550+ ret = ac_read_model(avctx, s, &ac_gb);
551+ if (ret < 0) {
552+ av_log(avctx, AV_LOG_ERROR, "bad arithmetic model\n");
553+ return ret;
554+ }
555+
556+ ret = ac_init(avctx, s, &ac_gb);
557+ if (ret < 0) {
558+ av_log(avctx, AV_LOG_ERROR, "cannot init arithmetic decoder\n");
559+ return ret;
560+ }
561+
562+ for (int n = 0; n < s->nb_samples; n++) {
563+ unsigned prob = ac_get_prob(s);
564+ prob = ac_map_symbol(s, prob);
565+ ac_out[n] = prob - 0x80;
566+ if ((ret = ac_normalize(avctx, s, &ac_gb)) < 0)
567+ return ret;
568+ }
569+
570+ if (get_bits_count(&ac_gb) != ac_pos + ac_size) {
571+ av_log(avctx, AV_LOG_DEBUG, "over/under-read in arithmetic coder: %d\n",
572+ ac_pos + ac_size - get_bits_count(&ac_gb));
573+ }
574+ }
575+
576+ switch (block_type) {
577+ case 12:
578+ s->eof = 1;
579+ return AVERROR_EOF;
580+ case 11:
581+ s->nb_samples = get_urice(gb, 8);
582+ if (s->nb_samples > 570) {
583+ s->nb_samples = 570;
584+ return AVERROR_INVALIDDATA;
585+ }
586+ continue;
587+ case 10:
588+ s->shift = get_urice(gb, 2);
589+ continue;
590+ case 9:
591+ if (avctx->sample_fmt == AV_SAMPLE_FMT_U8P) {
592+ fill = (int8_t)get_bits(gb, 8);
593+ fill -= 0x80;
594+ } else {
595+ fill = (int16_t)get_bits(gb, 16);
596+ fill -= 0x8000;
597+ }
598+
599+ for (int n = 0; n < s->nb_samples; n++)
600+ samples[n + 70] = fill;
601+ finished = 1;
602+ break;
603+ case 8:
604+ for (int n = 0; n < s->nb_samples; n++)
605+ samples[n + 70] = 0;
606+ finished = 1;
607+ break;
608+ case 20:
609+ case 7:
610+ for (int n = 0; n < s->nb_samples; n++)
611+ samples[n + 70] = ac_out[n] + samples[n + 69] * 3 - samples[n + 68] * 3 + samples[n + 67];
612+ finished = 1;
613+ break;
614+ case 19:
615+ case 6:
616+ for (int n = 0; n < s->nb_samples; n++) {
617+ int sum = 15;
618+
619+ for (int o = 0; o < order; o++)
620+ sum += s->filter[ch][o] * (unsigned)samples[n + 70 - o - 1];
621+
622+ samples[n + 70] += ac_out[n] + (sum >> 4);
623+ }
624+ finished = 1;
625+ break;
626+ case 18:
627+ case 5:
628+ for (int n = 0; n < s->nb_samples; n++)
629+ samples[n + 70] = ac_out[n] + samples[n + 69] * 2 - samples[n + 68];
630+ finished = 1;
631+ break;
632+ case 17:
633+ case 4:
634+ for (int n = 0; n < s->nb_samples; n++)
635+ samples[n + 70] = ac_out[n];
636+ finished = 1;
637+ break;
638+ case 16:
639+ case 3:
640+ for (int n = 0; n < s->nb_samples; n++)
641+ samples[n + 70] = ac_out[n] + samples[n + 69];
642+ finished = 1;
643+ break;
644+ case 15:
645+ case 2:
646+ for (int n = 0; n < 70; n++) {
647+ ac_pred[n] = samples[n];
648+ samples[n] = 0;
649+ }
650+
651+ for (int n = 0; n < s->nb_samples; n++) {
652+ int sum = 15;
653+
654+ for (int o = 0; o < order; o++)
655+ sum += s->filter[ch][o] * (unsigned)samples[n + 70 - o - 1];
656+
657+ samples[n + 70] += ac_out[n] + (sum >> 4);
658+ }
659+
660+ for (int n = 0; n < 70; n++)
661+ samples[n] = ac_pred[n];
662+
663+ for (int n = 0; n < s->nb_samples; n++)
664+ samples[n + 70] += samples[n + 69] * 2 - samples[n + 68];
665+
666+ finished = 1;
667+ break;
668+ case 14:
669+ case 1:
670+ for (int n = 0; n < 70; n++) {
671+ ac_pred[n] = samples[n];
672+ samples[n] = 0;
673+ }
674+
675+ for (int n = 0; n < s->nb_samples; n++) {
676+ int sum = 15;
677+
678+ for (int o = 0; o < order; o++)
679+ sum += s->filter[ch][o] * (unsigned)samples[n + 70 - o - 1];
680+
681+ samples[n + 70] += ac_out[n] + (sum >> 4);
682+ }
683+
684+ for (int n = 0; n < 70; n++)
685+ samples[n] = ac_pred[n];
686+
687+ for (int n = 0; n < s->nb_samples; n++)
688+ samples[n + 70] += samples[n + 69];
689+
690+ finished = 1;
691+ break;
692+ case 13:
693+ case 0:
694+ for (int n = 0; n < s->nb_samples; n++) {
695+ int sum = 15;
696+
697+ for (int o = 0; o < order; o++)
698+ sum += s->filter[ch][o] * (unsigned)samples[n + 70 - o - 1];
699+
700+ samples[n + 70] += ac_out[n] + (sum >> 4);
701+ }
702+ finished = 1;
703+ break;
704+ default:
705+ return AVERROR_INVALIDDATA;
706+ }
707+
708+ if (finished == 1 && avctx->ch_layout.nb_channels == 2) {
709+ if (ch == 0)
710+ correlated = get_bits1(gb);
711+ finished = ch != 0;
712+ do_stereo(s, ch, correlated, 70);
713+ ch = 1;
714+ }
715+ }
716+
717+ if (avctx->ch_layout.nb_channels == 1) {
718+ for (int n = 0; n < 70; n++)
719+ s->samples[0][n] = s->samples[0][s->nb_samples + n];
720+ }
721+
722+ return 0;
723+}
724+
381725 static int wavarc_decode(AVCodecContext *avctx, AVFrame *frame,
382726 int *got_frame_ptr, AVPacket *pkt)
383727 {
@@ -424,6 +768,9 @@ static int wavarc_decode(AVCodecContext *avctx, AVFrame *frame, · 40 unmodified lines
424768 case MKTAG('4','A','L','P'):
425769 ret = decode_2slp(avctx, s, gb);
426770 break;
771+ case MKTAG('5','E','L','P'):
772+ ret = decode_5elp(avctx, s, gb);
773+ break;
427774 default:
428775 ret = AVERROR_INVALIDDATA;
429776 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t