/src/ffmpeg/libavcodec/misc4.c
Line | Count | Source |
1 | | /* |
2 | | * Micronas SC-4 audio decoder |
3 | | * Copyright (c) 2022 Paul B Mahol |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
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. |
11 | | * |
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. |
16 | | * |
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 |
20 | | */ |
21 | | |
22 | | #include "libavutil/internal.h" |
23 | | #include "libavutil/intreadwrite.h" |
24 | | #include "avcodec.h" |
25 | | #include "codec_internal.h" |
26 | | #include "decode.h" |
27 | | #include "bytestream.h" |
28 | | #include "mathops.h" |
29 | | |
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, |
33 | | }; |
34 | | |
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, |
38 | | }; |
39 | | |
40 | | typedef struct ChannelContext { |
41 | | unsigned last_step; |
42 | | int64_t new_pred; |
43 | | int64_t pred; |
44 | | int64_t weights_tab[6]; |
45 | | int32_t diffs_tab[6]; |
46 | | } ChannelContext; |
47 | | |
48 | | typedef struct MISC4Context { |
49 | | GetByteContext gb; |
50 | | |
51 | | uint32_t marker; |
52 | | |
53 | | ChannelContext ch[2]; |
54 | | } MISC4Context; |
55 | | |
56 | | static av_cold int misc4_init(AVCodecContext *avctx) |
57 | 926 | { |
58 | 926 | MISC4Context *s = avctx->priv_data; |
59 | | |
60 | 926 | avctx->sample_fmt = AV_SAMPLE_FMT_S16; |
61 | 926 | switch (avctx->sample_rate) { |
62 | 1 | case 8000: |
63 | 2 | case 11025: |
64 | 2 | s->marker = 0x11b; |
65 | 2 | break; |
66 | 1 | case 16000: |
67 | 2 | case 32000: |
68 | 2 | s->marker = 0x2b2; |
69 | 2 | break; |
70 | 926 | } |
71 | | |
72 | 926 | return 0; |
73 | 926 | } |
74 | | |
75 | 34.5M | #define FRACBITS 12 |
76 | 82.9M | #define WEIGHTSBITS 26 |
77 | | |
78 | | static int64_t prediction(int delta, ChannelContext *c) |
79 | 6.91M | { |
80 | 6.91M | const int isign = FFDIFFSIGN(delta, 0); |
81 | 6.91M | int64_t dotpr = 0; |
82 | | |
83 | 6.91M | c->new_pred = delta * (1LL << FRACBITS) + c->pred; |
84 | | |
85 | 48.3M | for (int i = 0; i < 6; i++) { |
86 | 41.4M | const int sign = FFSIGN(c->diffs_tab[i]); |
87 | 41.4M | c->weights_tab[i] = (c->weights_tab[i] * 255LL) / 256; |
88 | 41.4M | c->weights_tab[i] += (1LL << (WEIGHTSBITS + 1)) * sign * isign; |
89 | 41.4M | } |
90 | | |
91 | 6.91M | memmove(&c->diffs_tab[1], &c->diffs_tab[0], 5 * sizeof(int32_t)); |
92 | | |
93 | 6.91M | c->diffs_tab[0] = -delta * (1 << (FRACBITS-8)); |
94 | 6.91M | c->pred = c->new_pred; |
95 | | |
96 | 6.91M | dotpr = 0; |
97 | 48.3M | for (int i = 0; i < 6; i++) |
98 | 41.4M | dotpr += ((int64_t)c->diffs_tab[i] * c->weights_tab[i]) >> WEIGHTSBITS; |
99 | | |
100 | 6.91M | c->pred += dotpr; |
101 | 6.91M | c->pred = av_clip64(c->pred, -16383 * (1 << FRACBITS), 16383 * (1 << FRACBITS)); |
102 | 6.91M | c->pred = c->pred * 9 / 10; |
103 | | |
104 | 6.91M | return c->new_pred; |
105 | 6.91M | } |
106 | | |
107 | | static int16_t decode(ChannelContext *c, unsigned nibble) |
108 | 6.91M | { |
109 | 6.91M | int diff, diff_sign, adiff = 0, delta; |
110 | 6.91M | uint32_t step, newstep; |
111 | 6.91M | int64_t pred; |
112 | | |
113 | 6.91M | diff_sign = nibble >> 3; |
114 | 6.91M | diff = diffs[nibble]; |
115 | 6.91M | step = diff + (c->last_step >> 2); |
116 | 6.91M | newstep = step & 0xfff; |
117 | 6.91M | if (newstep >> 11 == 0) |
118 | 1.17M | adiff = (((step & 0x7f) + 0x80) * 128) >> |
119 | 1.17M | (14 - (newstep >> 7)); |
120 | 6.91M | delta = diff_sign ? -adiff : adiff; |
121 | 6.91M | delta = av_clip_intp2(delta, 15); |
122 | 6.91M | pred = prediction(delta, c); |
123 | 6.91M | nibble = steps[nibble]; |
124 | 6.91M | newstep = nibble * 32 - c->last_step & 0x1ffff; |
125 | 6.91M | newstep = ((newstep >> 5) + (newstep & 0x10000 ? 0x1000 : 0) + c->last_step) & 0x1fff; |
126 | 6.91M | c->last_step = av_clip(newstep, 544, 5120); |
127 | | |
128 | 6.91M | return av_clip_int16(pred >> (FRACBITS - 3)); |
129 | 6.91M | } |
130 | | |
131 | | static int misc4_decode(AVCodecContext *avctx, AVFrame *frame, |
132 | | int *got_frame_ptr, AVPacket *pkt) |
133 | 3.66M | { |
134 | 3.66M | MISC4Context *s = avctx->priv_data; |
135 | 3.66M | GetByteContext *gb = &s->gb; |
136 | 3.66M | uint32_t hdr; |
137 | 3.66M | int ret; |
138 | | |
139 | 3.66M | bytestream2_init(gb, pkt->data, pkt->size); |
140 | | |
141 | 3.66M | frame->nb_samples = 29 * (1 + (avctx->ch_layout.nb_channels == 1)); |
142 | 3.66M | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
143 | 1.09M | return ret; |
144 | | |
145 | 2.57M | hdr = bytestream2_peek_be32(gb); |
146 | 2.57M | if (hdr == s->marker) { |
147 | 2.56M | bytestream2_skip(gb, 5); |
148 | 2.56M | } else if ((hdr >> 16) == s->marker) { |
149 | 492 | bytestream2_skip(gb, 3); |
150 | 492 | } |
151 | | |
152 | 2.57M | { |
153 | 2.57M | int16_t *samples = (int16_t *)frame->data[0]; |
154 | 2.57M | const int st = avctx->ch_layout.nb_channels == 2; |
155 | 2.57M | int n; |
156 | | |
157 | 3.47M | for (n = 0; n < 29; n++) { |
158 | 3.45M | int nibble = bytestream2_get_byte(gb); |
159 | 3.45M | samples[2*n+0] = decode(&s->ch[0 ], nibble >> 4); |
160 | 3.45M | samples[2*n+1] = decode(&s->ch[st], nibble & 15); |
161 | 3.45M | if (bytestream2_get_bytes_left(gb) <= 0) |
162 | 2.55M | break; |
163 | 3.45M | } |
164 | | |
165 | 2.57M | if (n == 29 && bytestream2_get_byte(gb) != 0x55) |
166 | 12.4k | return AVERROR_INVALIDDATA; |
167 | 2.57M | } |
168 | | |
169 | 2.55M | *got_frame_ptr = 1; |
170 | | |
171 | 2.55M | return bytestream2_tell(gb); |
172 | 2.57M | } |
173 | | |
174 | | const FFCodec ff_misc4_decoder = { |
175 | | .p.name = "misc4", |
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), |
180 | | .init = misc4_init, |
181 | | FF_CODEC_DECODE_CB(misc4_decode), |
182 | | .p.capabilities = AV_CODEC_CAP_DR1 | |
183 | | AV_CODEC_CAP_CHANNEL_CONF, |
184 | | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16), |
185 | | }; |