FFmpeg
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"
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];
47 
48 typedef struct MISC4Context {
50 
51  uint32_t marker;
52 
54 } MISC4Context;
55 
56 static av_cold int misc4_init(AVCodecContext *avctx)
57 {
58  MISC4Context *s = avctx->priv_data;
59 
61  switch (avctx->sample_rate) {
62  case 8000:
63  case 11025:
64  s->marker = 0x11b;
65  break;
66  case 16000:
67  case 32000:
68  s->marker = 0x2b2;
69  break;
70  }
71 
72  return 0;
73 }
74 
75 #define FRACBITS 12
76 #define WEIGHTSBITS 26
77 
78 static int64_t prediction(int delta, ChannelContext *c)
79 {
80  const int isign = FFDIFFSIGN(delta, 0);
81  int64_t dotpr = 0;
82 
83  c->new_pred = delta * (1LL << FRACBITS) + c->pred;
84 
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;
89  }
90 
91  memmove(&c->diffs_tab[1], &c->diffs_tab[0], 5 * sizeof(int32_t));
92 
93  c->diffs_tab[0] = -delta * (1 << (FRACBITS-8));
94  c->pred = c->new_pred;
95 
96  dotpr = 0;
97  for (int i = 0; i < 6; i++)
98  dotpr += ((int64_t)c->diffs_tab[i] * c->weights_tab[i]) >> WEIGHTSBITS;
99 
100  c->pred += dotpr;
101  c->pred = av_clip64(c->pred, -16383 * (1 << FRACBITS), 16383 * (1 << FRACBITS));
102  c->pred = c->pred * 9 / 10;
103 
104  return c->new_pred;
105 }
106 
107 static int16_t decode(ChannelContext *c, unsigned nibble)
108 {
109  int diff, diff_sign, adiff = 0, delta;
110  uint32_t step, newstep;
111  int64_t pred;
112 
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);
127 
128  return av_clip_int16(pred >> (FRACBITS - 3));
129 }
130 
132  int *got_frame_ptr, AVPacket *pkt)
133 {
134  MISC4Context *s = avctx->priv_data;
135  GetByteContext *gb = &s->gb;
136  uint32_t hdr;
137  int ret;
138 
139  bytestream2_init(gb, pkt->data, pkt->size);
140 
141  frame->nb_samples = 29 * (1 + (avctx->ch_layout.nb_channels == 1));
142  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
143  return ret;
144 
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);
150  }
151 
152  {
153  int16_t *samples = (int16_t *)frame->data[0];
154  const int st = avctx->ch_layout.nb_channels == 2;
155  int n;
156 
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)
162  break;
163  }
164 
165  if (n == 29 && bytestream2_get_byte(gb) != 0x55)
166  return AVERROR_INVALIDDATA;
167  }
168 
169  *got_frame_ptr = 1;
170 
171  return bytestream2_tell(gb);
172 }
173 
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,
182  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SUBFRAMES |
184  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
186 };
av_clip
#define av_clip
Definition: common.h:95
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1007
GetByteContext
Definition: bytestream.h:33
diffs
static const int16_t diffs[16]
Definition: misc4.c:35
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
AVPacket::data
uint8_t * data
Definition: packet.h:374
AV_CODEC_ID_MISC4
@ AV_CODEC_ID_MISC4
Definition: codec_id.h:533
MISC4Context::ch
ChannelContext ch[2]
Definition: misc4.c:53
WEIGHTSBITS
#define WEIGHTSBITS
Definition: misc4.c:76
ChannelContext::diffs_tab
int32_t diffs_tab[6]
Definition: misc4.c:45
FFCodec
Definition: codec_internal.h:119
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:123
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2059
FFSIGN
#define FFSIGN(a)
Definition: common.h:65
ChannelContext::pred
int64_t pred
Definition: misc4.c:43
av_clip64
#define av_clip64
Definition: common.h:98
FFDIFFSIGN
#define FFDIFFSIGN(x, y)
Comparator.
Definition: macros.h:45
pkt
AVPacket * pkt
Definition: movenc.c:59
av_cold
#define av_cold
Definition: attributes.h:90
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:298
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:363
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:264
FRACBITS
#define FRACBITS
Definition: misc4.c:75
ChannelContext::weights_tab
int64_t weights_tab[6]
Definition: misc4.c:44
av_clip_int16
#define av_clip_int16
Definition: common.h:110
av_clip_intp2
#define av_clip_intp2
Definition: common.h:116
MISC4Context::marker
uint32_t marker
Definition: misc4.c:51
mathops.h
decode
static int16_t decode(ChannelContext *c, unsigned nibble)
Definition: misc4.c:107
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
bytestream2_tell
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:109
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1420
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
ff_misc4_decoder
const FFCodec ff_misc4_decoder
Definition: misc4.c:174
AVPacket::size
int size
Definition: packet.h:375
codec_internal.h
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1023
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
prediction
static int64_t prediction(int delta, ChannelContext *c)
Definition: misc4.c:78
ChannelContext::last_step
unsigned last_step
Definition: misc4.c:41
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
internal.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
delta
float delta
Definition: vorbis_enc_data.h:430
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
avcodec.h
ret
ret
Definition: filter_design.txt:187
pred
static const float pred[4]
Definition: siprdata.h:259
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
steps
static const int16_t steps[16]
Definition: misc4.c:30
AVCodecContext
main external API structure.
Definition: avcodec.h:398
MISC4Context
Definition: misc4.c:48
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
ChannelContext::new_pred
int64_t new_pred
Definition: misc4.c:42
ChannelContext
Definition: hcadec.c:31
AV_CODEC_CAP_SUBFRAMES
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time,...
Definition: codec.h:100
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:139
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:425
MISC4Context::gb
GetByteContext gb
Definition: misc4.c:49
misc4_init
static av_cold int misc4_init(AVCodecContext *avctx)
Definition: misc4.c:56
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
misc4_decode
static int misc4_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
Definition: misc4.c:131
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61