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