Patches attached.
From 9a85823ed8a616491b9da280f75e03103eb7d38f Mon Sep 17 00:00:00 2001
From: Paul B Mahol <one...@gmail.com>
Date: Thu, 8 Sep 2022 20:44:49 +0200
Subject: [PATCH 1/2] avcodec: add MI-SC4 audio decoder

Signed-off-by: Paul B Mahol <one...@gmail.com>
---
 libavcodec/Makefile     |   1 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/codec_desc.c |   7 ++
 libavcodec/codec_id.h   |   1 +
 libavcodec/misc4.c      | 182 ++++++++++++++++++++++++++++++++++++++++
 libavformat/riff.c      |   1 +
 6 files changed, 193 insertions(+)
 create mode 100644 libavcodec/misc4.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 0b46bc0173..4112a0fb2e 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -471,6 +471,7 @@ OBJS-$(CONFIG_METASOUND_DECODER)       += metasound.o metasound_data.o \
                                           twinvq.o
 OBJS-$(CONFIG_MICRODVD_DECODER)        += microdvddec.o ass.o
 OBJS-$(CONFIG_MIMIC_DECODER)           += mimic.o
+OBJS-$(CONFIG_MISC4_DECODER)           += misc4.o
 OBJS-$(CONFIG_MJPEG_DECODER)           += mjpegdec.o mjpegdec_common.o
 OBJS-$(CONFIG_MJPEG_QSV_DECODER)       += qsvdec.o
 OBJS-$(CONFIG_MJPEG_ENCODER)           += mjpegenc.o mjpegenc_common.o \
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 5d58a5d9f0..447225e26b 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -483,6 +483,7 @@ extern const FFCodec ff_interplay_acm_decoder;
 extern const FFCodec ff_mace3_decoder;
 extern const FFCodec ff_mace6_decoder;
 extern const FFCodec ff_metasound_decoder;
+extern const FFCodec ff_misc4_decoder;
 extern const FFCodec ff_mlp_encoder;
 extern const FFCodec ff_mlp_decoder;
 extern const FFCodec ff_mp1_decoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index c002480d39..648c518b3c 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -3297,6 +3297,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("Bonk audio"),
         .props     = AV_CODEC_PROP_LOSSY | AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
     },
+    {
+        .id        = AV_CODEC_ID_MISC4,
+        .type      = AVMEDIA_TYPE_AUDIO,
+        .name      = "misc4",
+        .long_name = NULL_IF_CONFIG_SMALL("Micronas SC-4 Audio"),
+        .props     = AV_CODEC_PROP_LOSSY | AV_CODEC_PROP_INTRA_ONLY,
+    },
 
     /* subtitle codecs */
     {
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index 858b5c3a75..bc8226ff07 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -528,6 +528,7 @@ enum AVCodecID {
     AV_CODEC_ID_MSNSIREN,
     AV_CODEC_ID_DFPWM,
     AV_CODEC_ID_BONK,
+    AV_CODEC_ID_MISC4,
 
     /* subtitle codecs */
     AV_CODEC_ID_FIRST_SUBTITLE = 0x17000,          ///< A dummy ID pointing at the start of subtitle codecs.
diff --git a/libavcodec/misc4.c b/libavcodec/misc4.c
new file mode 100644
index 0000000000..4376d0d025
--- /dev/null
+++ b/libavcodec/misc4.c
@@ -0,0 +1,182 @@
+/*
+ * Micronas SC-4 audio decoder
+ * Copyright (c) 2022 Paul B Mahol
+ *
+ * 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/internal.h"
+#include "libavutil/intreadwrite.h"
+#include "avcodec.h"
+#include "codec_internal.h"
+#include "decode.h"
+#include "bytestream.h"
+#include "mathops.h"
+
+static const int16_t tab0[16] = {
+    4084, 18, 41, 64, 112, 198, 355, 1122,
+    1122, 355, 198, 112, 64, 41, 18, 4084,
+};
+
+static const int16_t steps[16] = {
+    2048, 4, 135, 213, 273, 323, 373, 425,
+    425, 373, 323, 273, 213, 135, 4, 2048,
+};
+
+typedef struct ChannelContext {
+    unsigned last_step;
+    int64_t new_pred;
+    int64_t pred;
+    int64_t weights_tab[6];
+    int32_t diffs_tab[6];
+} ChannelContext;
+
+typedef struct MISC4Context {
+    GetByteContext gb;
+
+    uint32_t marker;
+
+    ChannelContext ch[2];
+} MISC4Context;
+
+static av_cold int misc4_init(AVCodecContext *avctx)
+{
+    MISC4Context *s = avctx->priv_data;
+
+    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
+    switch (avctx->sample_rate) {
+    case 8000:
+    case 11025:
+        s->marker = 0x11b;
+        break;
+    case 16000:
+    case 32000:
+        s->marker = 0x2b2;
+        break;
+    }
+
+    return 0;
+}
+
+static int64_t prediction(int delta, ChannelContext *c)
+{
+    const int isign = FFDIFFSIGN(delta, 0);
+    int64_t dotpr = 0;
+
+    c->new_pred = delta * (1LL << 12) + c->pred;
+
+    for (int i = 0; i < 6; i++) {
+        const int sign = FFSIGN(c->diffs_tab[i]);
+        c->weights_tab[i] = (c->weights_tab[i] * 255LL) / 256;
+        c->weights_tab[i] += (1LL << (26 + 1)) * sign * isign;
+    }
+
+    memmove(&c->diffs_tab[1], &c->diffs_tab[0], 5 * sizeof(int32_t));
+
+    c->diffs_tab[0] = -delta * (1 << (12-8));
+    c->pred = c->new_pred;
+
+    dotpr = 0;
+    for (int i = 0; i < 6; i++)
+        dotpr += ((int64_t)c->diffs_tab[i] * c->weights_tab[i]) >> 26;
+
+    c->pred += dotpr;
+    c->pred = av_clip64(c->pred, -16383 * (1 << 12), 16383 * (1 << 12));
+    c->pred = c->pred * 9 / 10;
+
+    return c->new_pred;
+}
+
+static int16_t decode(ChannelContext *c, unsigned nibble)
+{
+    int diff, diff_sign, adiff = 0, delta;
+    uint32_t step, newstep;
+    int64_t pred;
+
+    diff_sign = nibble >> 3;
+    diff = steps[nibble];
+    step = diff + (c->last_step >> 2);
+    newstep = step & 0xfff;
+    if (newstep >> 11 == 0)
+        adiff = (((step & 0x7f) + 0x80) * 128) >>
+                 (14 - (newstep >> 7));
+    delta = diff_sign ? -adiff : adiff;
+    delta = av_clip_intp2(delta, 15);
+    pred = prediction(delta, c);
+    nibble = tab0[nibble];
+    newstep = nibble * 32 - c->last_step & 0x1ffff;
+    newstep = ((newstep >> 5) + (newstep & 0x10000 ? 0x1000 : 0) + c->last_step) & 0x1fff;
+    c->last_step = av_clip(newstep, 544, 5120);
+
+    return av_clip_int16(pred >> (12 - 3));
+}
+
+static int misc4_decode(AVCodecContext *avctx, AVFrame *frame,
+                       int *got_frame_ptr, AVPacket *pkt)
+{
+    MISC4Context *s = avctx->priv_data;
+    GetByteContext *gb = &s->gb;
+    uint32_t hdr;
+    int ret;
+
+    bytestream2_init(gb, pkt->data, pkt->size);
+
+    frame->nb_samples = 29 * (1 + (avctx->ch_layout.nb_channels == 1));
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
+
+    hdr = bytestream2_peek_be32(gb);
+    if (hdr == s->marker) {
+        bytestream2_skip(gb, 5);
+    } else if ((hdr >> 16) == s->marker) {
+        bytestream2_skip(gb, 3);
+    }
+
+    {
+        int16_t *samples = (int16_t *)frame->data[0];
+        const int st = avctx->ch_layout.nb_channels == 2;
+        int n;
+
+        for (n = 0; n < 29; n++) {
+            int nibble = bytestream2_get_byte(gb);
+            samples[2*n+0] = decode(&s->ch[0 ], nibble >> 4);
+            samples[2*n+1] = decode(&s->ch[st], nibble & 15);
+            if (bytestream2_get_bytes_left(gb) <= 0)
+                break;
+        }
+
+        if (n == 29 && bytestream2_get_byte(gb) != 0x55)
+            return AVERROR_INVALIDDATA;
+    }
+
+    *got_frame_ptr = 1;
+
+    return bytestream2_tell(gb);
+}
+
+const FFCodec ff_misc4_decoder = {
+    .p.name           = "misc4",
+    CODEC_LONG_NAME("Micronas SC-4 Audio"),
+    .p.type           = AVMEDIA_TYPE_AUDIO,
+    .p.id             = AV_CODEC_ID_MISC4,
+    .priv_data_size   = sizeof(MISC4Context),
+    .init             = misc4_init,
+    FF_CODEC_DECODE_CB(misc4_decode),
+    .p.capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SUBFRAMES,
+    .p.sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
+                                                        AV_SAMPLE_FMT_NONE },
+};
diff --git a/libavformat/riff.c b/libavformat/riff.c
index 6c06ad2d60..5114114364 100644
--- a/libavformat/riff.c
+++ b/libavformat/riff.c
@@ -564,6 +564,7 @@ const AVCodecTag ff_codec_wav_tags[] = {
     { AV_CODEC_ID_ATRAC3,          0x0270 },
     { AV_CODEC_ID_MSNSIREN,        0x028E },
     { AV_CODEC_ID_ADPCM_G722,      0x028F },
+    { AV_CODEC_ID_MISC4,           0x0350 },
     { AV_CODEC_ID_IMC,             0x0401 },
     { AV_CODEC_ID_IAC,             0x0402 },
     { AV_CODEC_ID_ON2AVC,          0x0500 },
-- 
2.37.2

From cd1fc36be5286e8d73145fd3e026e2e9eddea540 Mon Sep 17 00:00:00 2001
From: Paul B Mahol <one...@gmail.com>
Date: Fri, 9 Sep 2022 09:57:12 +0200
Subject: [PATCH 2/2] avcodec: add Micronas SC-4 parser

Signed-off-by: Paul B Mahol <one...@gmail.com>
---
 libavcodec/Makefile       |  1 +
 libavcodec/misc4_parser.c | 81 +++++++++++++++++++++++++++++++++++++++
 libavcodec/parsers.c      |  1 +
 3 files changed, 83 insertions(+)
 create mode 100644 libavcodec/misc4_parser.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 4112a0fb2e..e938564a1f 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1143,6 +1143,7 @@ OBJS-$(CONFIG_HEVC_PARSER)             += hevc_parser.o hevc_data.o
 OBJS-$(CONFIG_HDR_PARSER)              += hdr_parser.o
 OBJS-$(CONFIG_IPU_PARSER)              += ipu_parser.o
 OBJS-$(CONFIG_JPEG2000_PARSER)         += jpeg2000_parser.o
+OBJS-$(CONFIG_MISC4_PARSER)            += misc4_parser.o
 OBJS-$(CONFIG_MJPEG_PARSER)            += mjpeg_parser.o
 OBJS-$(CONFIG_MLP_PARSER)              += mlp_parse.o mlp_parser.o mlp.o
 OBJS-$(CONFIG_MPEG4VIDEO_PARSER)       += mpeg4video_parser.o h263.o \
diff --git a/libavcodec/misc4_parser.c b/libavcodec/misc4_parser.c
new file mode 100644
index 0000000000..d234dbb629
--- /dev/null
+++ b/libavcodec/misc4_parser.c
@@ -0,0 +1,81 @@
+/*
+ * Micronas SC-4 parser
+ *
+ * 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 "parser.h"
+
+typedef struct MISC4Context {
+    ParseContext pc;
+} MISC4Context;
+
+static int misc4_parse(AVCodecParserContext *s, AVCodecContext *avctx,
+                       const uint8_t **poutbuf, int *poutbuf_size,
+                       const uint8_t *buf, int buf_size)
+{
+    MISC4Context *ctx = s->priv_data;
+    uint32_t state = ctx->pc.state;
+    int next = END_NOT_FOUND, i = 0;
+
+    *poutbuf_size = 0;
+    *poutbuf = NULL;
+
+    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
+        next = buf_size;
+    } else {
+        uint32_t marker = 0;
+
+        switch (avctx->sample_rate) {
+        case 8000:
+        case 11025:
+            marker = 0x11b;
+            break;
+        case 16000:
+        case 32000:
+            marker = 0x2b2;
+            break;
+        }
+
+        for (; i < buf_size; i++) {
+            state = (state << 8) | buf[i];
+            if (state == marker && i > 3) {
+                next = i - 3;
+                break;
+            }
+        }
+
+        ctx->pc.state = state;
+        if (ff_combine_frame(&ctx->pc, next, &buf, &buf_size) < 0) {
+            *poutbuf = NULL;
+            *poutbuf_size = 0;
+            return buf_size;
+        }
+    }
+
+    *poutbuf      = buf;
+    *poutbuf_size = buf_size;
+
+    return next;
+}
+
+const AVCodecParser ff_misc4_parser = {
+    .codec_ids      = { AV_CODEC_ID_MISC4 },
+    .priv_data_size = sizeof(MISC4Context),
+    .parser_parse   = misc4_parse,
+    .parser_close   = ff_parse_close,
+};
diff --git a/libavcodec/parsers.c b/libavcodec/parsers.c
index a8d52af6cb..362f6f8bc6 100644
--- a/libavcodec/parsers.c
+++ b/libavcodec/parsers.c
@@ -53,6 +53,7 @@ extern const AVCodecParser ff_hevc_parser;
 extern const AVCodecParser ff_hdr_parser;
 extern const AVCodecParser ff_ipu_parser;
 extern const AVCodecParser ff_jpeg2000_parser;
+extern const AVCodecParser ff_misc4_parser;
 extern const AVCodecParser ff_mjpeg_parser;
 extern const AVCodecParser ff_mlp_parser;
 extern const AVCodecParser ff_mpeg4video_parser;
-- 
2.37.2

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to