avcodec: add HCOM decoder
authorPaul B Mahol <onemda@gmail.com>
Wed, 2 Jan 2019 17:50:38 +0000 (18:50 +0100)
committerPaul B Mahol <onemda@gmail.com>
Tue, 15 Jan 2019 09:56:35 +0000 (10:56 +0100)
libavcodec/Makefile
libavcodec/allcodecs.c
libavcodec/avcodec.h
libavcodec/codec_desc.c
libavcodec/hcom.c [new file with mode: 0644]
libavcodec/version.h

index 99799ce..bf746c1 100644 (file)
@@ -362,6 +362,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_HCOM_DECODER)            += hcom.o
 OBJS-$(CONFIG_HEVC_DECODER)            += hevcdec.o hevc_mvs.o \
                                           hevc_cabac.o hevc_refs.o hevcpred.o    \
                                           hevcdsp.o hevc_filter.o hevc_data.o
index 4755af7..fe0376e 100644 (file)
@@ -424,6 +424,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_hcom_decoder;
 extern AVCodec ff_iac_decoder;
 extern AVCodec ff_ilbc_decoder;
 extern AVCodec ff_imc_decoder;
index 4414853..90f9f08 100644 (file)
@@ -646,6 +646,7 @@ enum AVCodecID {
     AV_CODEC_ID_APTX_HD,
     AV_CODEC_ID_SBC,
     AV_CODEC_ID_ATRAC9,
+    AV_CODEC_ID_HCOM,
 
     /* subtitle codecs */
     AV_CODEC_ID_FIRST_SUBTITLE = 0x17000,          ///< A dummy ID pointing at the start of subtitle codecs.
index 2363a53..10a6391 100644 (file)
@@ -2943,6 +2943,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("ATRAC9 (Adaptive TRansform Acoustic Coding 9)"),
         .props     = AV_CODEC_PROP_LOSSY,
     },
+    {
+        .id        = AV_CODEC_ID_HCOM,
+        .type      = AVMEDIA_TYPE_AUDIO,
+        .name      = "hcom",
+        .long_name = NULL_IF_CONFIG_SMALL("HCOM Audio"),
+        .props     = AV_CODEC_PROP_LOSSY,
+    },
 
     /* subtitle codecs */
     {
diff --git a/libavcodec/hcom.c b/libavcodec/hcom.c
new file mode 100644 (file)
index 0000000..aab1001
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * HCOM audio decoder
+ *
+ * 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/intreadwrite.h"
+
+#include "avcodec.h"
+#include "get_bits.h"
+#include "internal.h"
+
+typedef struct HEntry {
+    int16_t l, r;
+} HEntry;
+
+typedef struct HCOMContext {
+    AVCodecContext *avctx;
+
+    uint8_t first_sample;
+    uint8_t sample;
+    int dict_entries;
+    int dict_entry;
+    int delta_compression;
+
+    HEntry *dict;
+} HCOMContext;
+
+static av_cold int hcom_init(AVCodecContext *avctx)
+{
+    HCOMContext *s = avctx->priv_data;
+
+    if (avctx->channels != 1) {
+        av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (avctx->extradata_size <= 7)
+        return AVERROR_INVALIDDATA;
+    s->dict_entries = AV_RB16(avctx->extradata);
+    if (avctx->extradata_size < s->dict_entries * 4 + 7)
+        return AVERROR_INVALIDDATA;
+    s->delta_compression = AV_RB32(avctx->extradata + 2);
+    s->sample = s->first_sample = avctx->extradata[avctx->extradata_size - 1];
+
+    s->dict = av_calloc(s->dict_entries, sizeof(*s->dict));
+    if (!s->dict)
+        return AVERROR(ENOMEM);
+    for (int i = 0; i < s->dict_entries; i++) {
+        s->dict[i].l = AV_RB16(avctx->extradata + 6 + 4 * i);
+        s->dict[i].r = AV_RB16(avctx->extradata + 6 + 4 * i + 2);
+    }
+
+    avctx->sample_fmt = AV_SAMPLE_FMT_U8;
+    s->dict_entry = 0;
+
+    return 0;
+}
+
+static int hcom_decode(AVCodecContext *avctx, void *data,
+                       int *got_frame, AVPacket *pkt)
+{
+    HCOMContext *s = avctx->priv_data;
+    AVFrame *frame = data;
+    GetBitContext gb;
+    int ret, n = 0;
+
+    if (pkt->size > INT16_MAX)
+        return AVERROR_INVALIDDATA;
+
+    frame->nb_samples = pkt->size * 8;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
+
+    if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
+        return ret;
+
+    while (get_bits_left(&gb) > 0) {
+        if (get_bits1(&gb)) {
+            s->dict_entry = s->dict[s->dict_entry].r;
+        } else {
+            s->dict_entry = s->dict[s->dict_entry].l;
+        }
+
+        if (s->dict[s->dict_entry].l < 0) {
+            int16_t datum;
+
+            datum = s->dict[s->dict_entry].r;
+
+            if (!s->delta_compression)
+                s->sample = 0;
+            s->sample = (s->sample + datum) & 0xFF;
+
+            frame->data[0][n++] = s->sample;
+
+            s->dict_entry = 0;
+        }
+    }
+
+    frame->nb_samples = n;
+
+    *got_frame = 1;
+
+    return pkt->size;
+}
+
+static av_cold int hcom_close(AVCodecContext *avctx)
+{
+    HCOMContext *s = avctx->priv_data;
+
+    av_freep(&s->dict);
+
+    return 0;
+}
+
+AVCodec ff_hcom_decoder = {
+    .name           = "hcom",
+    .long_name      = NULL_IF_CONFIG_SMALL("HCOM Audio"),
+    .type           = AVMEDIA_TYPE_AUDIO,
+    .id             = AV_CODEC_ID_HCOM,
+    .priv_data_size = sizeof(HCOMContext),
+    .init           = hcom_init,
+    .close          = hcom_close,
+    .decode         = hcom_decode,
+    .capabilities   = AV_CODEC_CAP_DR1,
+};
index 4adde77..4356bb5 100644 (file)
@@ -28,8 +28,8 @@
 #include "libavutil/version.h"
 
 #define LIBAVCODEC_VERSION_MAJOR  58
-#define LIBAVCODEC_VERSION_MINOR  43
-#define LIBAVCODEC_VERSION_MICRO 101
+#define LIBAVCODEC_VERSION_MINOR  44
+#define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \