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