FFmpeg
Loading...
Searching...
No Matches
misc4.c
Go to the documentation of this file.
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"
24#include "avcodec.h"
25#include "codec_internal.h"
26#include "decode.h"
27#include "bytestream.h"
28#include "mathops.h"
29
30static 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
35static 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
40typedef struct ChannelContext {
41 unsigned last_step;
47
48typedef struct MISC4Context {
50
51 uint32_t marker;
52
55
57{
58 MISC4Context *s = avctx->priv_data;
59
60 if (avctx->sample_rate <= 0)
62
63 if (avctx->ch_layout.nb_channels != 1 && avctx->ch_layout.nb_channels != 2)
65
67 switch (avctx->sample_rate) {
68 case 8000:
69 case 11025:
70 s->marker = 0x11b;
71 break;
72 case 16000:
73 case 32000:
74 s->marker = 0x2b2;
75 break;
76 }
77
78 return 0;
79}
80
81#define FRACBITS 12
82#define WEIGHTSBITS 26
83
85{
86 const int isign = FFDIFFSIGN(delta, 0);
87 int64_t dotpr = 0;
88
89 c->new_pred = delta * (1LL << FRACBITS) + c->pred;
90
91 for (int i = 0; i < 6; i++) {
92 const int sign = FFSIGN(c->diffs_tab[i]);
93 c->weights_tab[i] = (c->weights_tab[i] * 255LL) / 256;
94 c->weights_tab[i] += (1LL << (WEIGHTSBITS + 1)) * sign * isign;
95 }
96
97 memmove(&c->diffs_tab[1], &c->diffs_tab[0], 5 * sizeof(int32_t));
98
99 c->diffs_tab[0] = -delta * (1 << (FRACBITS-8));
100 c->pred = c->new_pred;
101
102 dotpr = 0;
103 for (int i = 0; i < 6; i++)
104 dotpr += ((int64_t)c->diffs_tab[i] * c->weights_tab[i]) >> WEIGHTSBITS;
105
106 c->pred += dotpr;
107 c->pred = av_clip64(c->pred, -16383 * (1 << FRACBITS), 16383 * (1 << FRACBITS));
108 c->pred = c->pred * 9 / 10;
109
110 return c->new_pred;
111}
112
113static int16_t decode(ChannelContext *c, unsigned nibble)
114{
115 int diff, diff_sign, adiff = 0, delta;
116 uint32_t step, newstep;
118
119 diff_sign = nibble >> 3;
120 diff = diffs[nibble];
121 step = diff + (c->last_step >> 2);
122 newstep = step & 0xfff;
123 if (newstep >> 11 == 0)
124 adiff = (((step & 0x7f) + 0x80) * 128) >>
125 (14 - (newstep >> 7));
126 delta = diff_sign ? -adiff : adiff;
129 nibble = steps[nibble];
130 newstep = nibble * 32 - c->last_step & 0x1ffff;
131 newstep = ((newstep >> 5) + (newstep & 0x10000 ? 0x1000 : 0) + c->last_step) & 0x1fff;
132 c->last_step = av_clip(newstep, 544, 5120);
133
134 return av_clip_int16(pred >> (FRACBITS - 3));
135}
136
138 int *got_frame_ptr, AVPacket *pkt)
139{
140 MISC4Context *s = avctx->priv_data;
141 GetByteContext *gb = &s->gb;
142 uint32_t hdr;
143 int ret;
144
145 bytestream2_init(gb, pkt->data, pkt->size);
146
147 frame->nb_samples = 29 * (1 + (avctx->ch_layout.nb_channels == 1));
148 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
149 return ret;
150
151 hdr = bytestream2_peek_be32(gb);
152 if (hdr == s->marker) {
153 bytestream2_skip(gb, 5);
154 } else if ((hdr >> 16) == s->marker) {
155 bytestream2_skip(gb, 3);
156 }
157
158 {
159 int16_t *samples = (int16_t *)frame->data[0];
160 const int st = avctx->ch_layout.nb_channels == 2;
161 int n;
162
163 for (n = 0; n < 29; n++) {
164 int nibble = bytestream2_get_byte(gb);
165 samples[2*n+0] = decode(&s->ch[0 ], nibble >> 4);
166 samples[2*n+1] = decode(&s->ch[st], nibble & 15);
167 if (bytestream2_get_bytes_left(gb) <= 0)
168 break;
169 }
170
171 if (n == 29 && bytestream2_get_byte(gb) != 0x55)
172 return AVERROR_INVALIDDATA;
173 }
174
175 *got_frame_ptr = 1;
176
177 return bytestream2_tell(gb);
178}
179
181 .p.name = "misc4",
182 CODEC_LONG_NAME("Micronas SC-4 Audio"),
183 .p.type = AVMEDIA_TYPE_AUDIO,
184 .p.id = AV_CODEC_ID_MISC4,
185 .priv_data_size = sizeof(MISC4Context),
186 .init = misc4_init,
188 .p.capabilities = AV_CODEC_CAP_DR1 |
190};
const FFCodec ff_misc4_decoder
Definition misc4.c:180
int32_t
Libavcodec external API header.
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define av_clip_intp2
Definition common.h:121
#define av_clip
Definition common.h:100
#define av_clip_int16
Definition common.h:115
#define av_clip64
Definition common.h:103
#define FFSIGN(a)
Definition common.h:75
long long int64_t
Definition coverity.c:34
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVPacket * pkt
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_MISC4
Definition codec_id.h:551
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define av_cold
Definition attributes.h:117
common internal API header
#define FFDIFFSIGN(x, y)
Comparator.
Definition macros.h:45
static const int16_t diffs[16]
Definition misc4.c:35
static int misc4_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
Definition misc4.c:137
static int16_t decode(ChannelContext *c, unsigned nibble)
Definition misc4.c:113
#define WEIGHTSBITS
Definition misc4.c:82
static int64_t prediction(int delta, ChannelContext *c)
Definition misc4.c:84
static av_cold int misc4_init(AVCodecContext *avctx)
Definition misc4.c:56
#define FRACBITS
Definition misc4.c:81
static const int16_t steps[16]
Definition misc4.c:30
static const float pred[4]
Definition siprdata.h:259
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int sample_rate
samples per second
Definition avcodec.h:1040
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int64_t weights_tab[6]
Definition misc4.c:44
int64_t pred
Definition misc4.c:43
int64_t new_pred
Definition misc4.c:42
unsigned last_step
Definition misc4.c:41
int32_t diffs_tab[6]
Definition misc4.c:45
uint32_t marker
Definition misc4.c:51
ChannelContext ch[2]
Definition misc4.c:53
GetByteContext gb
Definition misc4.c:49
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
float delta
static double c[64]