]> git.sesse.net Git - ffmpeg/blob - libavcodec/fastaudio.c
avcodec: add FastAudio decoder
[ffmpeg] / libavcodec / fastaudio.c
1 /*
2  * MOFLEX Fast Audio decoder
3  * Copyright (c) 2020 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/intreadwrite.h"
23
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "mathops.h"
28
29 typedef struct ChannelItems {
30     float f[8];
31     float last;
32 } ChannelItems;
33
34 typedef struct FastAudioContext {
35     float table[8][64];
36
37     ChannelItems *ch;
38 } FastAudioContext;
39
40 static av_cold int fastaudio_init(AVCodecContext *avctx)
41 {
42     FastAudioContext *s = avctx->priv_data;
43
44     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
45
46     for (int i = 0; i < 8; i++)
47         s->table[0][i] = (i - 159.5f) / 160.f;
48     for (int i = 0; i < 11; i++)
49         s->table[0][i + 8] = (i - 37.5f) / 40.f;
50     for (int i = 0; i < 27; i++)
51         s->table[0][i + 8 + 11] = (i - 13.f) / 20.f;
52     for (int i = 0; i < 11; i++)
53         s->table[0][i + 8 + 11 + 27] = (i + 27.5f) / 40.f;
54     for (int i = 0; i < 7; i++)
55         s->table[0][i + 8 + 11 + 27 + 11] = (i + 152.5f) / 160.f;
56
57     memcpy(s->table[1], s->table[0], sizeof(s->table[0]));
58
59     for (int i = 0; i < 7; i++)
60         s->table[2][i] = (i - 33.5f) / 40.f;
61     for (int i = 0; i < 25; i++)
62         s->table[2][i + 7] = (i - 13.f) / 20.f;
63
64     for (int i = 0; i < 32; i++)
65         s->table[3][i] = -s->table[2][31 - i];
66
67     for (int i = 0; i < 16; i++)
68         s->table[4][i] = i * 0.22f / 3.f - 0.6f;
69
70     for (int i = 0; i < 16; i++)
71         s->table[5][i] = i * 0.20f / 3.f - 0.3f;
72
73     for (int i = 0; i < 8; i++)
74         s->table[6][i] = i * 0.36f / 3.f - 0.4f;
75
76     for (int i = 0; i < 8; i++)
77         s->table[7][i] = i * 0.34f / 3.f - 0.2f;
78
79     s->ch = av_calloc(avctx->channels, sizeof(*s->ch));
80     if (!s->ch)
81         return AVERROR(ENOMEM);
82
83     return 0;
84 }
85
86 static int read_bits(int bits, int *ppos, unsigned *src)
87 {
88     int r, pos;
89
90     pos = *ppos;
91     pos += bits;
92     r = src[(pos - 1) / 32] >> (32 - pos % 32);
93     *ppos = pos;
94
95     return r & ((1 << (bits % 32)) - 1);
96 }
97
98 static const uint8_t bits[8] = { 6, 6, 5, 5, 4, 0, 3, 3, };
99
100 static void set_sample(int i, int j, int v, float *result, int *pads, float value)
101 {
102     result[i * 64 + pads[i] + j * 3] = value * (2 * v - 7);
103 }
104
105 static int fastaudio_decode(AVCodecContext *avctx, void *data,
106                             int *got_frame, AVPacket *pkt)
107 {
108     FastAudioContext *s = avctx->priv_data;
109     GetByteContext gb;
110     AVFrame *frame = data;
111     int subframes;
112     int ret;
113
114     subframes = pkt->size / (40 * avctx->channels);
115     frame->nb_samples = subframes * 256;
116     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
117         return ret;
118
119     bytestream2_init(&gb, pkt->data, pkt->size);
120
121     for (int subframe = 0; subframe < subframes; subframe++) {
122         for (int channel = 0; channel < avctx->channels; channel++) {
123             ChannelItems *ch = &s->ch[channel];
124             float result[256] = { 0 };
125             unsigned src[10];
126             int inds[4], pads[4];
127             float m[8];
128             int pos = 0;
129
130             for (int i = 0; i < 10; i++)
131                 src[i] = bytestream2_get_le32(&gb);
132
133             for (int i = 0; i < 8; i++)
134                 m[7 - i] = s->table[i][read_bits(bits[i], &pos, src)];
135
136             for (int i = 0; i < 4; i++)
137                 inds[3 - i] = read_bits(6, &pos, src);
138
139             for (int i = 0; i < 4; i++)
140                 pads[3 - i] = read_bits(2, &pos, src);
141
142             for (int i = 0, index5 = 0; i < 4; i++) {
143                 float value = av_int2float((inds[i] + 1) << 20) * powf(2.f, 116.f);
144
145                 for (int j = 0, tmp = 0; j < 21; j++) {
146                     set_sample(i, j, j == 20 ? tmp / 2 : read_bits(3, &pos, src), result, pads, value);
147                     if (j % 10 == 9)
148                         tmp = 4 * tmp + read_bits(2, &pos, src);
149                     if (j == 20)
150                         index5 = FFMIN(2 * index5 + tmp % 2, 63);
151                 }
152
153                 m[2] = s->table[5][index5];
154             }
155
156             for (int i = 0; i < 256; i++) {
157                 float x = result[i];
158
159                 for (int j = 0; j < 8; j++) {
160                     x -= m[j] * ch->f[j];
161                     ch->f[j] += m[j] * x;
162                 }
163
164                 memmove(&ch->f[0], &ch->f[1], sizeof(float) * 7);
165                 ch->f[7] = x;
166                 ch->last = x + ch->last * 0.86f;
167                 result[i] = ch->last * 2.f;
168             }
169
170             memcpy(frame->extended_data[channel] + 1024 * subframe, result, 256 * sizeof(float));
171         }
172     }
173
174     *got_frame = 1;
175
176     return pkt->size;
177 }
178
179 static av_cold int fastaudio_close(AVCodecContext *avctx)
180 {
181     FastAudioContext *s = avctx->priv_data;
182
183     av_freep(&s->ch);
184
185     return 0;
186 }
187
188 AVCodec ff_fastaudio_decoder = {
189     .name           = "fastaudio",
190     .long_name      = NULL_IF_CONFIG_SMALL("MobiClip FastAudio"),
191     .type           = AVMEDIA_TYPE_AUDIO,
192     .id             = AV_CODEC_ID_FASTAUDIO,
193     .priv_data_size = sizeof(FastAudioContext),
194     .init           = fastaudio_init,
195     .decode         = fastaudio_decode,
196     .close          = fastaudio_close,
197     .capabilities   = AV_CODEC_CAP_DR1,
198     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
199                                                       AV_SAMPLE_FMT_NONE },
200 };