diff mbox series

[FFmpeg-devel,6/7] avcodec: add CRI HCA decoder

Message ID 20200315182206.20851-6-onemda@gmail.com
State Superseded
Headers show
Series [FFmpeg-devel,1/7] avcodec: add ADPCM IMA MTF decoder
Related show

Checks

Context Check Description
andriy/ffmpeg-patchwork pending
andriy/ffmpeg-patchwork success Applied patch
andriy/ffmpeg-patchwork success Configure finished
andriy/ffmpeg-patchwork success Make finished
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

Paul B Mahol March 15, 2020, 6:22 p.m. UTC
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavcodec/Makefile     |   1 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/avcodec.h    |   1 +
 libavcodec/codec_desc.c |   7 +
 libavcodec/hca_data.h   | 128 ++++++++++++++
 libavcodec/hcadec.c     | 373 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 511 insertions(+)
 create mode 100644 libavcodec/hca_data.h
 create mode 100644 libavcodec/hcadec.c

Comments

Andreas Rheinhardt March 15, 2020, 6:53 p.m. UTC | #1
Paul B Mahol:
> +#include "libavutil/avassert.h"

I don't see an assert.

> +    frame->nb_samples = 1024;

If this codec has a fixed number of samples per frame, you should add it
to the list in get_audio_frame_duration() in libavcodec/utils.c.
Haven't checked the rest.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index a88643b7d2..c1c9a44f2b 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -367,6 +367,7 @@  OBJS-$(CONFIG_H264_V4L2M2M_DECODER)    += v4l2_m2m_dec.o
 OBJS-$(CONFIG_H264_V4L2M2M_ENCODER)    += v4l2_m2m_enc.o
 OBJS-$(CONFIG_HAP_DECODER)             += hapdec.o hap.o
 OBJS-$(CONFIG_HAP_ENCODER)             += hapenc.o hap.o
+OBJS-$(CONFIG_HCA_DECODER)             += hcadec.o
 OBJS-$(CONFIG_HCOM_DECODER)            += hcom.o
 OBJS-$(CONFIG_HEVC_DECODER)            += hevcdec.o hevc_mvs.o \
                                           hevc_cabac.o hevc_refs.o hevcpred.o    \
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 71e14c73e3..b3184af954 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -432,6 +432,7 @@  extern AVCodec ff_g723_1_decoder;
 extern AVCodec ff_g729_decoder;
 extern AVCodec ff_gsm_decoder;
 extern AVCodec ff_gsm_ms_decoder;
+extern AVCodec ff_hca_decoder;
 extern AVCodec ff_hcom_decoder;
 extern AVCodec ff_iac_decoder;
 extern AVCodec ff_ilbc_decoder;
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index e9c658fddc..6e03cb5902 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -668,6 +668,7 @@  enum AVCodecID {
     AV_CODEC_ID_ACELP_KELVIN,
     AV_CODEC_ID_MPEGH_3D_AUDIO,
     AV_CODEC_ID_SIREN,
+    AV_CODEC_ID_HCA,
 
     /* subtitle codecs */
     AV_CODEC_ID_FIRST_SUBTITLE = 0x17000,          ///< A dummy ID pointing at the start of subtitle codecs.
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 130e08c1f2..ece6eadae4 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -3079,6 +3079,13 @@  static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("Siren"),
         .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
     },
+    {
+        .id        = AV_CODEC_ID_HCA,
+        .type      = AVMEDIA_TYPE_AUDIO,
+        .name      = "hca",
+        .long_name = NULL_IF_CONFIG_SMALL("CRI HCA"),
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
+    },
 
     /* subtitle codecs */
     {
diff --git a/libavcodec/hca_data.h b/libavcodec/hca_data.h
new file mode 100644
index 0000000000..a2831381b9
--- /dev/null
+++ b/libavcodec/hca_data.h
@@ -0,0 +1,128 @@ 
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+
+#ifndef AVCODEC_HCA_DATA_H
+#define AVCODEC_HCA_DATA_H
+
+#include <stdint.h>
+
+static const uint8_t max_bits_table[] = {
+    0, 2, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12,
+};
+
+static const uint8_t quant_spectrum_bits[] =
+{
+    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+    1,1,2,2,0,0,0,0,0,0,0,0,0,0,0,0,
+    2,2,2,2,2,2,3,3,0,0,0,0,0,0,0,0,
+    2,2,3,3,3,3,3,3,0,0,0,0,0,0,0,0,
+    3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,
+    3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,
+    3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,
+    3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
+};
+
+static const int8_t quant_spectrum_value[] =
+{
+    +0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,
+    +0,+0,+1,-1,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,
+    +0,+0,+1,+1,-1,-1,+2,-2,+0,+0,+0,+0,+0,+0,+0,+0,
+    +0,+0,+1,-1,+2,-2,+3,-3,+0,+0,+0,+0,+0,+0,+0,+0,
+    +0,+0,+1,+1,-1,-1,+2,+2,-2,-2,+3,+3,-3,-3,+4,-4,
+    +0,+0,+1,+1,-1,-1,+2,+2,-2,-2,+3,-3,+4,-4,+5,-5,
+    +0,+0,+1,+1,-1,-1,+2,-2,+3,-3,+4,-4,+5,-5,+6,-6,
+    +0,+0,+1,-1,+2,-2,+3,-3,+4,-4,+5,-5,+6,-6,+7,-7,
+};
+
+static const uint8_t scale_table[] =
+{
+    14, 14, 14, 14, 14, 14, 13, 13,
+    13, 13, 13, 13, 12, 12, 12, 12,
+    12, 12, 11, 11, 11, 11, 11, 11,
+    10, 10, 10, 10, 10, 10, 10,  9,
+    9,  9,  9,  9,  9,  8,  8,  8,
+    8,  8,  8,  7,  6,  6,  5,  4,
+    4,  4,  3,  3,  3,  2,  2,  2,
+    2,  0,  0,  0,  0,  0,  0,  0,
+};
+
+static const uint32_t window[128] =
+{
+    0x3A3504F0,0x3B0183B8,0x3B70C538,0x3BBB9268,0x3C04A809,0x3C308200,0x3C61284C,0x3C8B3F17,
+    0x3CA83992,0x3CC77FBD,0x3CE91110,0x3D0677CD,0x3D198FC4,0x3D2DD35C,0x3D434643,0x3D59ECC1,
+    0x3D71CBA8,0x3D85741E,0x3D92A413,0x3DA078B4,0x3DAEF522,0x3DBE1C9E,0x3DCDF27B,0x3DDE7A1D,
+    0x3DEFB6ED,0x3E00D62B,0x3E0A2EDA,0x3E13E72A,0x3E1E00B1,0x3E287CF2,0x3E335D55,0x3E3EA321,
+    0x3E4A4F75,0x3E56633F,0x3E62DF37,0x3E6FC3D1,0x3E7D1138,0x3E8563A2,0x3E8C72B7,0x3E93B561,
+    0x3E9B2AEF,0x3EA2D26F,0x3EAAAAAB,0x3EB2B222,0x3EBAE706,0x3EC34737,0x3ECBD03D,0x3ED47F46,
+    0x3EDD5128,0x3EE6425C,0x3EEF4EFF,0x3EF872D7,0x3F00D4A9,0x3F0576CA,0x3F0A1D3B,0x3F0EC548,
+    0x3F136C25,0x3F180EF2,0x3F1CAAC2,0x3F213CA2,0x3F25C1A5,0x3F2A36E7,0x3F2E9998,0x3F32E705,
+    0xBF371C9E,0xBF3B37FE,0xBF3F36F2,0xBF431780,0xBF46D7E6,0xBF4A76A4,0xBF4DF27C,0xBF514A6F,
+    0xBF547DC5,0xBF578C03,0xBF5A74EE,0xBF5D3887,0xBF5FD707,0xBF6250DA,0xBF64A699,0xBF66D908,
+    0xBF68E90E,0xBF6AD7B1,0xBF6CA611,0xBF6E5562,0xBF6FE6E7,0xBF715BEF,0xBF72B5D1,0xBF73F5E6,
+    0xBF751D89,0xBF762E13,0xBF7728D7,0xBF780F20,0xBF78E234,0xBF79A34C,0xBF7A5397,0xBF7AF439,
+    0xBF7B8648,0xBF7C0ACE,0xBF7C82C8,0xBF7CEF26,0xBF7D50CB,0xBF7DA88E,0xBF7DF737,0xBF7E3D86,
+    0xBF7E7C2A,0xBF7EB3CC,0xBF7EE507,0xBF7F106C,0xBF7F3683,0xBF7F57CA,0xBF7F74B6,0xBF7F8DB6,
+    0xBF7FA32E,0xBF7FB57B,0xBF7FC4F6,0xBF7FD1ED,0xBF7FDCAD,0xBF7FE579,0xBF7FEC90,0xBF7FF22E,
+    0xBF7FF688,0xBF7FF9D0,0xBF7FFC32,0xBF7FFDDA,0xBF7FFEED,0xBF7FFF8F,0xBF7FFFDF,0xBF7FFFFC,
+};
+
+static const uint32_t intensity_ratio_table[] =
+{
+    0x40000000,0x3FEDB6DB,0x3FDB6DB7,0x3FC92492,0x3FB6DB6E,0x3FA49249,0x3F924925,0x3F800000,
+    0x3F5B6DB7,0x3F36DB6E,0x3F124925,0x3EDB6DB7,0x3E924925,0x3E124925,0x00000000,0x00000000,
+    0x00000000,0x32A0B051,0x32D61B5E,0x330EA43A,0x333E0F68,0x337D3E0C,0x33A8B6D5,0x33E0CCDF,
+    0x3415C3FF,0x34478D75,0x3484F1F6,0x34B123F6,0x34EC0719,0x351D3EDA,0x355184DF,0x358B95C2,
+    0x35B9FCD2,0x35F7D0DF,0x36251958,0x365BFBB8,0x36928E72,0x36C346CD,0x370218AF,0x372D583F,
+    0x3766F85B,0x3799E046,0x37CD078C,0x3808980F,0x38360094,0x38728177,0x38A18FAF,0x38D744FD,
+    0x390F6A81,0x393F179A,0x397E9E11,0x39A9A15B,0x39E2055B,0x3A16942D,0x3A48A2D8,0x3A85AAC3,
+    0x3AB21A32,0x3AED4F30,0x3B1E196E,0x3B52A81E,0x3B8C57CA,0x3BBAFF5B,0x3BF9295A,0x3C25FED7,
+    0x3C5D2D82,0x3C935A2B,0x3CC4563F,0x3D02CD87,0x3D2E4934,0x3D68396A,0x3D9AB62B,0x3DCE248C,
+    0x3E0955EE,0x3E36FD92,0x3E73D290,0x3EA27043,0x3ED87039,0x3F1031DC,0x3F40213B,0x00000000,
+};
+
+static const uint32_t scale_conversion_table[] =
+{
+    0x3F800000,0x3FAA8D26,0x3FE33F89,0x4017657D,0x4049B9BE,0x40866491,0x40B311C4,0x40EE9910,
+    0x411EF532,0x4153CCF1,0x418D1ADF,0x41BC034A,0x41FA83B3,0x4226E595,0x425E60F5,0x429426FF,
+    0x42C5672A,0x43038359,0x432F3B79,0x43697C38,0x439B8D3A,0x43CF4319,0x440A14D5,0x4437FBF0,
+    0x4475257D,0x44A3520F,0x44D99D16,0x4510FA4D,0x45412C4D,0x4580B1ED,0x45AB7A3A,0x45E47B6D,
+    0x461837F0,0x464AD226,0x46871F62,0x46B40AAF,0x46EFE4BA,0x471FD228,0x4754F35B,0x478DDF04,
+    0x47BD08A4,0x47FBDFED,0x4827CD94,0x485F9613,0x4894F4F0,0x48C67991,0x49043A29,0x49302F0E,
+    0x496AC0C7,0x499C6573,0x49D06334,0x4A0AD4C6,0x4A38FBAF,0x4A767A41,0x4AA43516,0x4ADACB94,
+    0x4B11C3D3,0x4B4238D2,0x4B8164D2,0x4BAC6897,0x4BE5B907,0x4C190B88,0x4C4BEC15,0x00000000,
+};
+
+static const uint32_t dequantizer_scaling_table[] =
+{
+    0x342A8D26,0x34633F89,0x3497657D,0x34C9B9BE,0x35066491,0x353311C4,0x356E9910,0x359EF532,
+    0x35D3CCF1,0x360D1ADF,0x363C034A,0x367A83B3,0x36A6E595,0x36DE60F5,0x371426FF,0x3745672A,
+    0x37838359,0x37AF3B79,0x37E97C38,0x381B8D3A,0x384F4319,0x388A14D5,0x38B7FBF0,0x38F5257D,
+    0x3923520F,0x39599D16,0x3990FA4D,0x39C12C4D,0x3A00B1ED,0x3A2B7A3A,0x3A647B6D,0x3A9837F0,
+    0x3ACAD226,0x3B071F62,0x3B340AAF,0x3B6FE4BA,0x3B9FD228,0x3BD4F35B,0x3C0DDF04,0x3C3D08A4,
+    0x3C7BDFED,0x3CA7CD94,0x3CDF9613,0x3D14F4F0,0x3D467991,0x3D843A29,0x3DB02F0E,0x3DEAC0C7,
+    0x3E1C6573,0x3E506334,0x3E8AD4C6,0x3EB8FBAF,0x3EF67A41,0x3F243516,0x3F5ACB94,0x3F91C3D3,
+    0x3FC238D2,0x400164D2,0x402C6897,0x4065B907,0x40990B88,0x40CBEC15,0x4107DB35,0x413504F3,
+};
+static const uint32_t quant_step_size[] =
+{
+    0x00000000,0x3F2AAAAB,0x3ECCCCCD,0x3E924925,0x3E638E39,0x3E3A2E8C,0x3E1D89D9,0x3E088889,
+    0x3D842108,0x3D020821,0x3C810204,0x3C008081,0x3B804020,0x3B002008,0x3A801002,0x3A000801,
+};
+
+#endif /* AVCODEC_HCA_DATA_H */
diff --git a/libavcodec/hcadec.c b/libavcodec/hcadec.c
new file mode 100644
index 0000000000..4163943a6e
--- /dev/null
+++ b/libavcodec/hcadec.c
@@ -0,0 +1,373 @@ 
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/avassert.h"
+#include "libavutil/crc.h"
+#include "libavutil/float_dsp.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/tx.h"
+
+#include "avcodec.h"
+#include "get_bits.h"
+#include "internal.h"
+#include "hca_data.h"
+
+typedef struct ChannelContext {
+    float    base[128];
+    int8_t   scale_factors[128];
+    uint8_t  scale[128];
+    int8_t   intensity[8];
+    int8_t  *hfr_scale;
+    float    imdct_in[128];
+    float    imdct_out[128];
+    float    wav2[128];
+    float    wav3[128];
+    unsigned count;
+    int      chan_type;
+} ChannelContext;
+
+typedef struct HCAContext {
+    GetBitContext gb;
+
+    const AVCRC *crc_table;
+
+    ChannelContext ch[16];
+
+    uint8_t ath[128];
+
+    int     hfr_group_count;
+    uint8_t track_count;
+    uint8_t channel_config;
+    uint8_t total_band_count;
+    uint8_t base_band_count;
+    uint8_t stereo_band_count;
+    uint8_t bands_per_hfr_group;
+
+    av_tx_fn           tx_fn;
+    AVTXContext       *tx_ctx;
+    AVFloatDSPContext *fdsp;
+} HCAContext;
+
+static inline unsigned ceil2(unsigned a, unsigned b)
+{
+    return (b > 0) ? (a / b + ((a % b) ? 1 : 0)) : 0;
+}
+
+static av_cold int decode_init(AVCodecContext *avctx)
+{
+    HCAContext *c = avctx->priv_data;
+    int8_t r[0x10] = { 0 };
+    float scale = 1.f / 8.f;
+    unsigned b;
+
+    avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
+    c->crc_table = av_crc_get_table(AV_CRC_16_ANSI);
+
+    if (avctx->extradata_size < 12)
+        return AVERROR(EINVAL);
+
+    if (avctx->channels < 0 || avctx->channels > 16)
+        return AVERROR(EINVAL);
+
+    if (avctx->extradata[0] != 1 || avctx->extradata[1] != 15 || !avctx->extradata[2])
+        return AVERROR(EINVAL);
+
+    c->track_count = avctx->extradata[2];
+    c->channel_config = avctx->extradata[3];
+    c->total_band_count = avctx->extradata[4];
+    c->base_band_count = avctx->extradata[5];
+    c->stereo_band_count = avctx->extradata[6];
+    c->bands_per_hfr_group = avctx->extradata[7];
+
+    b = avctx->channels / c->track_count;
+    if (c->stereo_band_count && b > 1) {
+        int8_t *x = r;
+
+        for (int i = 0; i < c->track_count; i++, x+=b) {
+            switch (b) {
+            case 2:
+            case 3:
+                x[0] = 1;
+                x[1] = 2;
+                break;
+            case 4:
+                x[0]=1; x[1] = 2;
+                if (c->channel_config == 0) {
+                    x[2]=1;
+                    x[3]=2;
+                }
+                break;
+            case 5:
+                x[0]=1; x[1] = 2;
+                if (c->channel_config <= 2) {
+                    x[3]=1;
+                    x[4]=2;
+                }
+                break;
+            case 6:
+            case 7:
+                x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2;
+                break;
+            case 8:
+                x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2; x[6] = 1; x[7] = 2;
+                break;
+            }
+        }
+    }
+
+    for (int i = 0; i < avctx->channels; i++) {
+        c->ch[i].chan_type = r[i];
+        c->ch[i].count = c->base_band_count + ((r[i] != 2) ? c->stereo_band_count : 0);
+        c->ch[i].hfr_scale = &c->ch[i].scale_factors[c->base_band_count + c->stereo_band_count];
+    }
+
+    c->hfr_group_count = ceil2(c->total_band_count - (c->base_band_count + c->stereo_band_count), c->bands_per_hfr_group);
+
+    c->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
+    if (!c->fdsp)
+        return AVERROR(ENOMEM);
+
+    return av_tx_init(&c->tx_ctx, &c->tx_fn, AV_TX_FLOAT_MDCT, 1, 128, &scale, 0);
+}
+
+static void run_imdct(HCAContext *c, ChannelContext *ch, int index, float *out)
+{
+    float *s, *d, *s1, *s2;
+
+    c->tx_fn(c->tx_ctx, ch->imdct_out, ch->imdct_in, sizeof(float));
+
+    d = ch->wav2;
+    for (int i = 0; i < 128; i++)
+        *(d++) = ch->imdct_out[127 - i];
+
+    s = (float *)window;
+    d = out;
+    s1 = &ch->wav2[64];
+    s2 = ch->wav3;
+    for (int i = 0; i < 64; i++)
+        *(d++) = *(s1++) * *(s++) + *(s2++);
+    for (int i = 0; i < 64; i++)
+        *(d++) = *(s++) * *(--s1) - *(s2++);
+
+    s1 = &ch->wav2[64-1];
+    s2 = ch->wav3;
+    for (int i = 0; i < 64; i++)
+        *(s2++) = *(s1--) * *(--s);
+    for (int i = 0; i < 64; i++)
+        *(s2++) = *(--s) * *(++s1);
+}
+
+static void apply_intensity_stereo(HCAContext *s, ChannelContext *ch, ChannelContext *ch2,
+                                   int index, unsigned band_count, unsigned base_band_count,
+                                   unsigned stereo_band_count)
+{
+    float ratio_l = ((float *)intensity_ratio_table)[ch->intensity[index]];
+    float ratio_r = ratio_l - 2.0f;
+    float *c1 = &ch->imdct_in[base_band_count];
+    float *c2 = &ch2->imdct_in[base_band_count];
+
+    if (ch->chan_type != 1 || !stereo_band_count)
+        return;
+
+    for (int i = 0; i < band_count; i++) {
+        *(c2++)  = *c1 * ratio_r;
+        *(c1++) *= ratio_l;
+    }
+}
+
+static void reconstruct_hfr(HCAContext *s, ChannelContext *ch,
+                            unsigned hfr_group_count,
+                            unsigned bands_per_hfr_group,
+                            unsigned start_band, unsigned total_band_count)
+{
+    static float *listFloat = (float *)scale_conversion_table;
+
+    if (ch->chan_type == 2 || !bands_per_hfr_group)
+        return;
+
+    for (int i = 0, k = start_band, l = start_band - 1; i < hfr_group_count; i++){
+        for (int j = 0; j < bands_per_hfr_group && k < total_band_count; j++, k++, l--){
+            ch->imdct_in[k] = listFloat[ch->hfr_scale[i] - ch->scale_factors[l]] * ch->imdct_in[l];
+        }
+    }
+
+    ch->imdct_in[127] = 0;
+}
+
+static void dequantize_coefficients(HCAContext *c, ChannelContext *ch)
+{
+    GetBitContext *gb = &c->gb;
+
+    for (int i = 0; i < ch->count; i++) {
+        unsigned scale = ch->scale[i];
+        int nb_bits = max_bits_table[scale];
+        int value = get_bitsz(gb, nb_bits);
+        float factor;
+
+        if (scale > 7) {
+            value = (1 - ((value & 1) << 1)) * (value >> 1);
+            if (!value)
+                skip_bits_long(gb, -1);
+            factor = value;
+        } else {
+            value += scale << 4;
+            skip_bits_long(gb, quant_spectrum_bits[value] - nb_bits);
+            factor = quant_spectrum_value[value];
+        }
+        ch->imdct_in[i] = factor * ch->base[i];
+    }
+
+    memset(ch->imdct_in + ch->count, 0,  sizeof(ch->imdct_in) - ch->count * sizeof(ch->imdct_in[0]));
+}
+
+static void unpack(HCAContext *c, ChannelContext *ch,
+                   unsigned hfr_group_count, int packed_noise_level, const uint8_t *ath)
+{
+    GetBitContext *gb = &c->gb;
+
+    static float *valueFloat=(float *)dequantizer_scaling_table;
+    static float *scaleFloat=(float *)quant_step_size;
+    int delta_bits = get_bits(gb, 3);
+
+    if (delta_bits > 5) {
+        for (int i = 0; i < ch->count; i++)
+            ch->scale_factors[i] = get_bits(gb, 6);
+    } else if (delta_bits) {
+        int factor = get_bits(gb, 6);
+        int max_value = (1 << delta_bits) - 1;
+        int half_max = max_value >> 1;
+
+        ch->scale_factors[0] = factor;
+        for (int i = 1; i < ch->count; i++){
+            int delta = get_bits(gb, delta_bits);
+
+            if (delta == max_value) {
+                factor = get_bits(gb, 6);
+            } else {
+                factor += delta - half_max;
+            }
+
+            ch->scale_factors[i] = factor;
+        }
+    } else {
+        memset(ch->scale_factors, 0, 128);
+    }
+
+    if (ch->chan_type == 2){
+        ch->intensity[0] = get_bits(gb, 4);
+        if (ch->intensity[0] < 15) {
+            for (int i = 1; i < 8; i++)
+                ch->intensity[i] = get_bits(gb, 4);
+        }
+    } else {
+        for (int i = 0; i < hfr_group_count; i++)
+            ch->hfr_scale[i] = get_bits(gb, 6);
+    }
+
+    for (int i = 0; i < ch->count; i++) {
+        int scale = ch->scale_factors[i];
+
+        if (scale) {
+            scale = c->ath[i] + ((packed_noise_level + i) >> 8) - ((scale * 5) >> 1) + 1;
+            if (scale < 0) {
+                scale = 15;
+            } else if (scale > 56) {
+                scale = 1;
+            } else {
+                scale = scale_table[scale];
+            }
+        }
+        ch->scale[i] = scale;
+    }
+
+    memset(ch->scale + ch->count, 0, sizeof(ch->scale) - ch->count);
+
+    for (int i = 0; i < ch->count; i++)
+        ch->base[i] = valueFloat[ch->scale_factors[i]] * scaleFloat[ch->scale[i]];
+}
+
+static int decode_frame(AVCodecContext *avctx, void *data,
+                        int *got_frame_ptr, AVPacket *avpkt)
+{
+    AVFrame *frame = data;
+    HCAContext *c = avctx->priv_data;
+    int ch, ret, packed_noise_level;
+    GetBitContext *gb = &c->gb;
+    float **samples;
+
+    if (av_crc(c->crc_table, 0, avpkt->data, avpkt->size))
+        return AVERROR_INVALIDDATA;
+
+    frame->nb_samples = 1024;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
+    samples = (float **)frame->extended_data;
+
+    if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
+        return ret;
+
+    if (get_bits(gb, 16) != 0xFFFF)
+        return AVERROR_INVALIDDATA;
+
+    packed_noise_level = (get_bits(gb, 9) << 8) - get_bits(gb, 7);
+
+    for (ch = 0; ch < avctx->channels; ch++)
+        unpack(c, &c->ch[ch], c->hfr_group_count, packed_noise_level, c->ath);
+
+    for (int i = 0; i < 8; i++) {
+        for (ch = 0; ch < avctx->channels; ch++)
+            dequantize_coefficients(c, &c->ch[ch]);
+        for (ch = 0; ch < avctx->channels; ch++)
+            reconstruct_hfr(c, &c->ch[ch], c->hfr_group_count, c->bands_per_hfr_group,
+                            c->stereo_band_count + c->base_band_count, c->total_band_count);
+        for (ch = 0; ch < avctx->channels - 1; ch++)
+            apply_intensity_stereo(c, &c->ch[ch], &c->ch[ch+1], i,
+                                   c->total_band_count - c->base_band_count,
+                                   c->base_band_count, c->stereo_band_count);
+        for (ch = 0; ch < avctx->channels; ch++)
+            run_imdct(c, &c->ch[ch], i, samples[ch] + i * 128);
+    }
+
+    *got_frame_ptr = 1;
+
+    return avpkt->size;
+}
+
+static av_cold int decode_close(AVCodecContext *avctx)
+{
+    HCAContext *c = avctx->priv_data;
+
+    av_freep(&c->fdsp);
+    av_tx_uninit(&c->tx_ctx);
+
+    return 0;
+}
+
+AVCodec ff_hca_decoder = {
+    .name           = "hca",
+    .long_name      = NULL_IF_CONFIG_SMALL("CRI HCA"),
+    .type           = AVMEDIA_TYPE_AUDIO,
+    .id             = AV_CODEC_ID_HCA,
+    .priv_data_size = sizeof(HCAContext),
+    .init           = decode_init,
+    .decode         = decode_frame,
+    .close          = decode_close,
+    .capabilities   = AV_CODEC_CAP_DR1,
+    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
+                                                      AV_SAMPLE_FMT_NONE },
+};