FFmpeg
Loading...
Searching...
No Matches
fastaudio.c
Go to the documentation of this file.
1/*
2 * MOFLEX Fast Audio decoder
3 * Copyright (c) 2015-2016 Florian Nouwt
4 * Copyright (c) 2017 Adib Surani
5 * Copyright (c) 2020 Paul B Mahol
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include "libavutil/intfloat.h"
25#include "libavutil/mem.h"
26#include "avcodec.h"
27#include "bytestream.h"
28#include "codec_internal.h"
29#include "decode.h"
30
31typedef struct ChannelItems {
32 float f[8];
33 float last;
35
36typedef struct FastAudioContext {
37 float table[8][64];
38
41
43{
45
47
48 for (int i = 0; i < 8; i++)
49 s->table[0][i] = (i - 159.5f) / 160.f;
50 for (int i = 0; i < 11; i++)
51 s->table[0][i + 8] = (i - 37.5f) / 40.f;
52 for (int i = 0; i < 27; i++)
53 s->table[0][i + 8 + 11] = (i - 13.f) / 20.f;
54 for (int i = 0; i < 11; i++)
55 s->table[0][i + 8 + 11 + 27] = (i + 27.5f) / 40.f;
56 for (int i = 0; i < 7; i++)
57 s->table[0][i + 8 + 11 + 27 + 11] = (i + 152.5f) / 160.f;
58
59 memcpy(s->table[1], s->table[0], sizeof(s->table[0]));
60
61 for (int i = 0; i < 7; i++)
62 s->table[2][i] = (i - 33.5f) / 40.f;
63 for (int i = 0; i < 25; i++)
64 s->table[2][i + 7] = (i - 13.f) / 20.f;
65
66 for (int i = 0; i < 32; i++)
67 s->table[3][i] = -s->table[2][31 - i];
68
69 for (int i = 0; i < 16; i++)
70 s->table[4][i] = i * 0.22f / 3.f - 0.6f;
71
72 for (int i = 0; i < 16; i++)
73 s->table[5][i] = i * 0.20f / 3.f - 0.3f;
74
75 for (int i = 0; i < 8; i++)
76 s->table[6][i] = i * 0.36f / 3.f - 0.4f;
77
78 for (int i = 0; i < 8; i++)
79 s->table[7][i] = i * 0.34f / 3.f - 0.2f;
80
81 s->ch = av_calloc(avctx->ch_layout.nb_channels, sizeof(*s->ch));
82 if (!s->ch)
83 return AVERROR(ENOMEM);
84
85 return 0;
86}
87
88static int read_bits(int bits, int *ppos, unsigned *src)
89{
90 int r, pos;
91
92 pos = *ppos;
93 pos += bits;
94 r = src[(pos - 1) / 32] >> ((-pos) & 31);
95 *ppos = pos;
96
97 return r & ((1 << bits) - 1);
98}
99
100static const uint8_t bits[8] = { 6, 6, 5, 5, 4, 0, 3, 3, };
101
102static void set_sample(int i, int j, int v, float *result, int *pads, float value)
103{
104 result[i * 64 + pads[i] + j * 3] = value * (2 * v - 7);
105}
106
108 int *got_frame, AVPacket *pkt)
109{
110 FastAudioContext *s = avctx->priv_data;
112 int subframes;
113 int ret;
114
115 subframes = pkt->size / (40 * avctx->ch_layout.nb_channels);
116 if (subframes <= 0 || subframes > INT_MAX / 256)
117 return AVERROR_INVALIDDATA;
118 frame->nb_samples = subframes * 256;
119 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
120 return ret;
121
122 bytestream2_init(&gb, pkt->data, pkt->size);
123
124 for (int subframe = 0; subframe < subframes; subframe++) {
125 for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
126 ChannelItems *ch = &s->ch[channel];
127 float result[256] = { 0 };
128 unsigned src[10];
129 int inds[4], pads[4];
130 float m[8];
131 int pos = 0;
132
133 for (int i = 0; i < 10; i++)
134 src[i] = bytestream2_get_le32(&gb);
135
136 for (int i = 0; i < 8; i++)
137 m[7 - i] = s->table[i][read_bits(bits[i], &pos, src)];
138
139 for (int i = 0; i < 4; i++)
140 inds[3 - i] = read_bits(6, &pos, src);
141
142 for (int i = 0; i < 4; i++)
143 pads[3 - i] = read_bits(2, &pos, src);
144
145 for (int i = 0, index5 = 0; i < 4; i++) {
146 float value = av_int2float((inds[i] + 1) << 20) * powf(2.f, 116.f);
147
148 for (int j = 0, tmp = 0; j < 21; j++) {
149 set_sample(i, j, j == 20 ? tmp / 2 : read_bits(3, &pos, src), result, pads, value);
150 if (j % 10 == 9)
151 tmp = 4 * tmp + read_bits(2, &pos, src);
152 if (j == 20)
153 index5 = FFMIN(2 * index5 + tmp % 2, 63);
154 }
155
156 m[2] = s->table[5][index5];
157 }
158
159 for (int i = 0; i < 256; i++) {
160 float x = result[i];
161
162 for (int j = 0; j < 8; j++) {
163 x -= m[j] * ch->f[j];
164 ch->f[j] += m[j] * x;
165 }
166
167 memmove(&ch->f[0], &ch->f[1], sizeof(float) * 7);
168 ch->f[7] = x;
169 ch->last = x + ch->last * 0.86f;
170 result[i] = ch->last * 2.f;
171 }
172
173 memcpy(frame->extended_data[channel] + 1024 * subframe, result, 256 * sizeof(float));
174 }
175 }
176
177 *got_frame = 1;
178
179 return pkt->size;
180}
181
183{
184 FastAudioContext *s = avctx->priv_data;
185
186 av_freep(&s->ch);
187
188 return 0;
189}
190
192 .p.name = "fastaudio",
193 CODEC_LONG_NAME("MobiClip FastAudio"),
194 .p.type = AVMEDIA_TYPE_AUDIO,
195 .p.id = AV_CODEC_ID_FASTAUDIO,
196 .priv_data_size = sizeof(FastAudioContext),
199 .close = fastaudio_close,
200 .p.capabilities = AV_CODEC_CAP_DR1,
201};
const FFCodec ff_fastaudio_decoder
Definition fastaudio.c:191
Libavcodec external API header.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
#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)
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
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
double value
Definition eval.c:102
static av_cold int fastaudio_init(AVCodecContext *avctx)
Definition fastaudio.c:42
static void set_sample(int i, int j, int v, float *result, int *pads, float value)
Definition fastaudio.c:102
static int fastaudio_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition fastaudio.c:107
static const uint8_t bits[8]
Definition fastaudio.c:100
static int read_bits(int bits, int *ppos, unsigned *src)
Definition fastaudio.c:88
static av_cold int fastaudio_close(AVCodecContext *avctx)
Definition fastaudio.c:182
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_FASTAUDIO
Definition codec_id.h:547
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
#define r
Definition input.c:42
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition intfloat.h:40
#define av_cold
Definition attributes.h:117
#define powf(x, y)
Definition libm.h:52
#define FFMIN(a, b)
Definition macros.h:49
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
unsigned int pos
Definition spdifenc.c:431
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
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
float f[8]
Definition fastaudio.c:32
float table[8][64]
Definition fastaudio.c:37
ChannelItems * ch
Definition fastaudio.c:39
#define av_freep(p)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248