diff mbox series

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

Message ID 20200316103050.22529-6-onemda@gmail.com
State Accepted
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 16, 2020, 10:30 a.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   | 129 +++++++++++++++
 libavcodec/hcadec.c     | 349 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 488 insertions(+)
 create mode 100644 libavcodec/hca_data.h
 create mode 100644 libavcodec/hcadec.c
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..cf95f8a5f6
--- /dev/null
+++ b/libavcodec/hca_data.h
@@ -0,0 +1,129 @@ 
+/*
+ * 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 float window[128] =
+{
+    0.000690534, 0.00197623, 0.00367386, 0.00572424, 0.0080967, 0.0107732, 0.0137425, 0.0169979,
+    0.0205353, 0.0243529, 0.0284505, 0.0328291, 0.0374906, 0.0424379, 0.0476744, 0.0532043,
+    0.0590321, 0.0651629, 0.071602, 0.0783552, 0.0854285, 0.092828, 0.10056, 0.108631,
+    0.117048, 0.125817, 0.134944, 0.144437, 0.1543, 0.164539, 0.175161, 0.186169,
+    0.197569, 0.209363, 0.221555, 0.234145, 0.247136, 0.260526, 0.274313, 0.288493,
+    0.303062, 0.318012, 0.333333, 0.349015, 0.365044, 0.381403, 0.398073, 0.415034,
+    0.43226, 0.449725, 0.4674, 0.485251, 0.503245, 0.521344, 0.539509, 0.557698,
+    0.575869, 0.593978, 0.611981, 0.629831, 0.647486, 0.6649, 0.682031, 0.698838,
+    0.71528, 0.731323, 0.746932, 0.762077, 0.776732, 0.790873, 0.804481, 0.817542,
+    0.830044, 0.84198, 0.853347, 0.864144, 0.874375, 0.884046, 0.893167, 0.901749,
+    0.909806, 0.917354, 0.924409, 0.93099, 0.937117, 0.942809, 0.948087, 0.952971,
+    0.957482, 0.961641, 0.965467, 0.968981, 0.972202, 0.975148, 0.977838, 0.980289,
+    0.982518, 0.98454, 0.986371, 0.988024, 0.989514, 0.990853, 0.992053, 0.993126,
+    0.994082, 0.994931, 0.995682, 0.996344, 0.996926, 0.997433, 0.997875, 0.998256,
+    0.998584, 0.998863, 0.999099, 0.999297, 0.999461, 0.999595, 0.999703, 0.999789,
+    0.999856, 0.999906, 0.999942, 0.999967, 0.999984, 0.999993, 0.999998, 1.0,
+};
+
+static const float intensity_ratio_table[] =
+{
+    2.0, 1.85714, 1.71429, 1.57143, 1.42857, 1.28571, 1.14286, 1.0,
+    0.857143, 0.714286, 0.571429, 0.428571, 0.285714, 0.142857, 0.0, 0.0,
+    0, 1.87066e-08, 2.49253e-08, 3.32113e-08, 4.42518e-08, 5.89626e-08, 7.85637e-08, 1.04681e-07,
+    1.3948e-07, 1.85848e-07, 2.4763e-07, 3.2995e-07, 4.39636e-07, 5.85785e-07, 7.80519e-07, 1.03999e-06,
+    1.38572e-06, 1.84637e-06, 2.46017e-06, 3.27801e-06, 4.36772e-06, 5.8197e-06, 7.75435e-06, 1.03321e-05,
+    1.37669e-05, 1.83435e-05, 2.44414e-05, 3.25665e-05, 4.33927e-05, 5.78179e-05, 7.70384e-05, 0.000102648,
+    0.000136772, 0.00018224, 0.000242822, 0.000323544, 0.000431101, 0.000574413, 0.000765366, 0.0010198,
+    0.00135881, 0.00181053, 0.0024124, 0.00321437, 0.00428293, 0.00570671, 0.00760381, 0.0101316,
+    0.0134996, 0.0179873, 0.0239669, 0.0319343, 0.0425503, 0.0566954, 0.0755428, 0.100656,
+    0.134117, 0.178702, 0.238108, 0.317263, 0.422731, 0.563261, 0.750507, 0.0,
+};
+
+static const float scale_conversion_table[] =
+{
+    1.0, 1.33243, 1.77538, 2.36557, 3.15196, 4.19978, 5.59592, 7.45618,
+    9.93486, 13.2375, 17.6381, 23.5016, 31.3143, 41.7242, 55.5947, 74.0762,
+    98.7015, 131.513, 175.232, 233.485, 311.103, 414.524, 552.326, 735.937,
+    980.586, 1306.56, 1740.91, 2319.64, 3090.77, 4118.24, 5487.28, 7311.43,
+    9741.98, 12980.5, 17295.7, 23045.3, 30706.4, 40914.2, 54515.4, 72638,
+    96785.3, 128960, 171830, 228952, 305064, 406477, 541603, 721649,
+    961548, 1.2812e+06, 1.70711e+06, 2.27461e+06, 3.03076e+06, 4.03829e+06, 5.38075e+06, 7.16948e+06,
+    9.55285e+06, 1.27285e+07, 1.69599e+07, 2.25979e+07, 3.01102e+07, 4.01198e+07, 5.3457e+07, 0,
+};
+
+static const float dequantizer_scaling_table[] =
+{
+    1.58838e-07, 2.11641e-07, 2.81998e-07, 3.75743e-07, 5.00652e-07, 6.67085e-07, 8.88846e-07, 1.18433e-06,
+    1.57804e-06, 2.10263e-06, 2.80161e-06, 3.73296e-06, 4.97391e-06, 6.6274e-06, 8.83057e-06, 1.17661e-05,
+    1.56776e-05, 2.08893e-05, 2.78336e-05, 3.70864e-05, 4.94151e-05, 6.58423e-05, 8.77305e-05, 0.000116895,
+    0.000155755, 0.000207533, 0.000276523, 0.000368448, 0.000490933, 0.000654135, 0.00087159, 0.00116134,
+    0.0015474, 0.00206181, 0.00274722, 0.00366048, 0.00487735, 0.00649874, 0.00865913, 0.0115377,
+    0.0153732, 0.0204838, 0.0272932, 0.0363664, 0.0484558, 0.0645641, 0.0860272, 0.114626,
+    0.152731, 0.203503, 0.271155, 0.361295, 0.481401, 0.641435, 0.854669, 1.13879,
+    1.51736, 2.02178, 2.69388, 3.58942, 4.78266, 6.37257, 8.49102, 11.3137,
+};
+
+static const float quant_step_size[] =
+{
+    0.0, 0.666667, 0.4, 0.285714, 0.222222, 0.181818, 0.153846, 0.133333, 0.0645161,
+    0.031746, 0.015748, 0.00784314, 0.00391389, 0.00195503, 0.00097704, 0.000488401,
+};
+
+#endif /* AVCODEC_HCA_DATA_H */
diff --git a/libavcodec/hcadec.c b/libavcodec/hcadec.c
new file mode 100644
index 0000000000..e737edde79
--- /dev/null
+++ b/libavcodec/hcadec.c
@@ -0,0 +1,349 @@ 
+/*
+ * 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/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];
+    DECLARE_ALIGNED(32, float, imdct_in)[128];
+    DECLARE_ALIGNED(32, float, imdct_out)[128];
+    DECLARE_ALIGNED(32, float, imdct_prev)[128];
+    int8_t   scale_factors[128];
+    uint8_t  scale[128];
+    int8_t   intensity[8];
+    int8_t  *hfr_scale;
+    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[16] = { 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)
+{
+    c->tx_fn(c->tx_ctx, ch->imdct_out, ch->imdct_in, sizeof(float));
+
+    c->fdsp->vector_fmul_window(out, ch->imdct_prev + (128 >> 1),
+                                ch->imdct_out, window, 128 >> 1);
+
+    memcpy(ch->imdct_prev, ch->imdct_out, 128 * sizeof(float));
+}
+
+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 = 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)
+{
+    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] = scale_conversion_table[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;
+    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] = dequantizer_scaling_table[ch->scale_factors[i]] * quant_step_size[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 },
+};