2 * Micronas SC-4 audio decoder
3 * Copyright (c) 2022 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/internal.h"
23 #include "libavutil/intreadwrite.h"
25 #include "codec_internal.h"
27 #include "bytestream.h"
30 static const int16_t steps[16] = {
31 4084, 18, 41, 64, 112, 198, 355, 1122,
32 1122, 355, 198, 112, 64, 41, 18, 4084,
35 static const int16_t diffs[16] = {
36 2048, 4, 135, 213, 273, 323, 373, 425,
37 425, 373, 323, 273, 213, 135, 4, 2048,
40 typedef struct ChannelContext {
44 int64_t weights_tab[6];
48 typedef struct MISC4Context {
56 static av_cold int misc4_init(AVCodecContext *avctx)
58 MISC4Context *s = avctx->priv_data;
60 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
61 switch (avctx->sample_rate) {
76 #define WEIGHTSBITS 26
78 static int64_t prediction(int delta, ChannelContext *c)
80 const int isign = FFDIFFSIGN(delta, 0);
83 c->new_pred = delta * (1LL << FRACBITS) + c->pred;
85 for (int i = 0; i < 6; i++) {
86 const int sign = FFSIGN(c->diffs_tab[i]);
87 c->weights_tab[i] = (c->weights_tab[i] * 255LL) / 256;
88 c->weights_tab[i] += (1LL << (WEIGHTSBITS + 1)) * sign * isign;
91 memmove(&c->diffs_tab[1], &c->diffs_tab[0], 5 * sizeof(int32_t));
93 c->diffs_tab[0] = -delta * (1 << (FRACBITS-8));
94 c->pred = c->new_pred;
97 for (int i = 0; i < 6; i++)
98 dotpr += ((int64_t)c->diffs_tab[i] * c->weights_tab[i]) >> WEIGHTSBITS;
101 c->pred = av_clip64(c->pred, -16383 * (1 << FRACBITS), 16383 * (1 << FRACBITS));
102 c->pred = c->pred * 9 / 10;
107 static int16_t decode(ChannelContext *c, unsigned nibble)
109 int diff, diff_sign, adiff = 0, delta;
110 uint32_t step, newstep;
113 diff_sign = nibble >> 3;
114 diff = diffs[nibble];
115 step = diff + (c->last_step >> 2);
116 newstep = step & 0xfff;
117 if (newstep >> 11 == 0)
118 adiff = (((step & 0x7f) + 0x80) * 128) >>
119 (14 - (newstep >> 7));
120 delta = diff_sign ? -adiff : adiff;
121 delta = av_clip_intp2(delta, 15);
122 pred = prediction(delta, c);
123 nibble = steps[nibble];
124 newstep = nibble * 32 - c->last_step & 0x1ffff;
125 newstep = ((newstep >> 5) + (newstep & 0x10000 ? 0x1000 : 0) + c->last_step) & 0x1fff;
126 c->last_step = av_clip(newstep, 544, 5120);
128 return av_clip_int16(pred >> (FRACBITS - 3));
131 static int misc4_decode(AVCodecContext *avctx, AVFrame *frame,
132 int *got_frame_ptr, AVPacket *pkt)
134 MISC4Context *s = avctx->priv_data;
135 GetByteContext *gb = &s->gb;
139 bytestream2_init(gb, pkt->data, pkt->size);
141 frame->nb_samples = 29 * (1 + (avctx->ch_layout.nb_channels == 1));
142 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
145 hdr = bytestream2_peek_be32(gb);
146 if (hdr == s->marker) {
147 bytestream2_skip(gb, 5);
148 } else if ((hdr >> 16) == s->marker) {
149 bytestream2_skip(gb, 3);
153 int16_t *samples = (int16_t *)frame->data[0];
154 const int st = avctx->ch_layout.nb_channels == 2;
157 for (n = 0; n < 29; n++) {
158 int nibble = bytestream2_get_byte(gb);
159 samples[2*n+0] = decode(&s->ch[0 ], nibble >> 4);
160 samples[2*n+1] = decode(&s->ch[st], nibble & 15);
161 if (bytestream2_get_bytes_left(gb) <= 0)
165 if (n == 29 && bytestream2_get_byte(gb) != 0x55)
166 return AVERROR_INVALIDDATA;
171 return bytestream2_tell(gb);
174 const FFCodec ff_misc4_decoder = {
176 CODEC_LONG_NAME("Micronas SC-4 Audio"),
177 .p.type = AVMEDIA_TYPE_AUDIO,
178 .p.id = AV_CODEC_ID_MISC4,
179 .priv_data_size = sizeof(MISC4Context),
181 FF_CODEC_DECODE_CB(misc4_decode),
182 .p.capabilities = AV_CODEC_CAP_DR1 |
184 AV_CODEC_CAP_SUBFRAMES |
186 AV_CODEC_CAP_CHANNEL_CONF,
187 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
188 AV_SAMPLE_FMT_NONE },