Initial implementation by Andrew D'Addesio <modchipv12@gmail.com> during
GSoC 2012.
Completion by Anton Khirnov <anton@khirnov.net>, sponsored by the
Mozilla Corporation.
Further contributions by:
Christophe Gisquet <christophe.gisquet@gmail.com>
Janne Grunau <janne-libav@jannau.net>
Luca Barbato <lu_zero@gentoo.org>
... | ... |
@@ -1822,6 +1822,7 @@ nellymoser_decoder_select="mdct sinewin" |
1822 | 1822 |
nellymoser_encoder_select="audio_frame_queue mdct sinewin" |
1823 | 1823 |
nuv_decoder_select="dsputil lzo" |
1824 | 1824 |
on2avc_decoder_select="mdct" |
1825 |
+opus_decoder_deps="avresample" |
|
1825 | 1826 |
png_decoder_deps="zlib" |
1826 | 1827 |
png_encoder_deps="zlib" |
1827 | 1828 |
png_encoder_select="dsputil" |
... | ... |
@@ -4387,6 +4388,8 @@ enabled movie_filter && prepend avfilter_deps "avformat avcodec" |
4387 | 4387 |
enabled resample_filter && prepend avfilter_deps "avresample" |
4388 | 4388 |
enabled scale_filter && prepend avfilter_deps "swscale" |
4389 | 4389 |
|
4390 |
+enabled opus_decoder && prepend avcodec_deps "avresample" |
|
4391 |
+ |
|
4390 | 4392 |
expand_deps(){ |
4391 | 4393 |
lib_deps=${1}_deps |
4392 | 4394 |
eval "deps=\$$lib_deps" |
... | ... |
@@ -279,6 +279,9 @@ OBJS-$(CONFIG_NELLYMOSER_DECODER) += nellymoserdec.o nellymoser.o |
279 | 279 |
OBJS-$(CONFIG_NELLYMOSER_ENCODER) += nellymoserenc.o nellymoser.o |
280 | 280 |
OBJS-$(CONFIG_NUV_DECODER) += nuv.o rtjpeg.o |
281 | 281 |
OBJS-$(CONFIG_ON2AVC_DECODER) += on2avc.o on2avcdata.o |
282 |
+OBJS-$(CONFIG_OPUS_DECODER) += opusdec.o opus.o opus_celt.o \ |
|
283 |
+ opus_imdct.o opus_silk.o \ |
|
284 |
+ vorbis_data.o |
|
282 | 285 |
OBJS-$(CONFIG_PAF_VIDEO_DECODER) += paf.o |
283 | 286 |
OBJS-$(CONFIG_PAF_AUDIO_DECODER) += paf.o |
284 | 287 |
OBJS-$(CONFIG_PAM_DECODER) += pnmdec.o pnm.o |
... | ... |
@@ -653,6 +656,7 @@ OBJS-$(CONFIG_MPEGAUDIO_PARSER) += mpegaudio_parser.o \ |
653 | 653 |
mpegaudiodecheader.o mpegaudiodata.o |
654 | 654 |
OBJS-$(CONFIG_MPEGVIDEO_PARSER) += mpegvideo_parser.o \ |
655 | 655 |
mpeg12.o mpeg12data.o |
656 |
+OBJS-$(CONFIG_OPUS_PARSER) += opus_parser.o opus.o vorbis_data.o |
|
656 | 657 |
OBJS-$(CONFIG_PNG_PARSER) += png_parser.o |
657 | 658 |
OBJS-$(CONFIG_PNM_PARSER) += pnm_parser.o pnm.o |
658 | 659 |
OBJS-$(CONFIG_RV30_PARSER) += rv34_parser.o |
... | ... |
@@ -331,6 +331,7 @@ void avcodec_register_all(void) |
331 | 331 |
REGISTER_DECODER(MPC8, mpc8); |
332 | 332 |
REGISTER_ENCDEC (NELLYMOSER, nellymoser); |
333 | 333 |
REGISTER_DECODER(ON2AVC, on2avc); |
334 |
+ REGISTER_DECODER(OPUS, opus); |
|
334 | 335 |
REGISTER_DECODER(PAF_AUDIO, paf_audio); |
335 | 336 |
REGISTER_DECODER(QCELP, qcelp); |
336 | 337 |
REGISTER_DECODER(QDM2, qdm2); |
... | ... |
@@ -483,6 +484,7 @@ void avcodec_register_all(void) |
483 | 483 |
REGISTER_PARSER(MPEG4VIDEO, mpeg4video); |
484 | 484 |
REGISTER_PARSER(MPEGAUDIO, mpegaudio); |
485 | 485 |
REGISTER_PARSER(MPEGVIDEO, mpegvideo); |
486 |
+ REGISTER_PARSER(OPUS, opus); |
|
486 | 487 |
REGISTER_PARSER(PNG, png); |
487 | 488 |
REGISTER_PARSER(PNM, pnm); |
488 | 489 |
REGISTER_PARSER(RV30, rv30); |
489 | 490 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,428 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2012 Andrew D'Addesio |
|
2 |
+ * Copyright (c) 2013-2014 Mozilla Corporation |
|
3 |
+ * |
|
4 |
+ * This file is part of Libav. |
|
5 |
+ * |
|
6 |
+ * Libav is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * Libav is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with Libav; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+/** |
|
22 |
+ * @file |
|
23 |
+ * Opus decoder/parser shared code |
|
24 |
+ */ |
|
25 |
+ |
|
26 |
+#include <stdint.h> |
|
27 |
+ |
|
28 |
+#include "libavutil/error.h" |
|
29 |
+ |
|
30 |
+#include "opus.h" |
|
31 |
+#include "vorbis.h" |
|
32 |
+ |
|
33 |
+static const uint16_t opus_frame_duration[32] = { |
|
34 |
+ 480, 960, 1920, 2880, |
|
35 |
+ 480, 960, 1920, 2880, |
|
36 |
+ 480, 960, 1920, 2880, |
|
37 |
+ 480, 960, |
|
38 |
+ 480, 960, |
|
39 |
+ 120, 240, 480, 960, |
|
40 |
+ 120, 240, 480, 960, |
|
41 |
+ 120, 240, 480, 960, |
|
42 |
+ 120, 240, 480, 960, |
|
43 |
+}; |
|
44 |
+ |
|
45 |
+/** |
|
46 |
+ * Read a 1- or 2-byte frame length |
|
47 |
+ */ |
|
48 |
+static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end) |
|
49 |
+{ |
|
50 |
+ int val; |
|
51 |
+ |
|
52 |
+ if (*ptr >= end) |
|
53 |
+ return AVERROR_INVALIDDATA; |
|
54 |
+ val = *(*ptr)++; |
|
55 |
+ if (val >= 252) { |
|
56 |
+ if (*ptr >= end) |
|
57 |
+ return AVERROR_INVALIDDATA; |
|
58 |
+ val += 4 * *(*ptr)++; |
|
59 |
+ } |
|
60 |
+ return val; |
|
61 |
+} |
|
62 |
+ |
|
63 |
+/** |
|
64 |
+ * Read a multi-byte length (used for code 3 packet padding size) |
|
65 |
+ */ |
|
66 |
+static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end) |
|
67 |
+{ |
|
68 |
+ int val = 0; |
|
69 |
+ int next; |
|
70 |
+ |
|
71 |
+ while (1) { |
|
72 |
+ if (*ptr >= end || val > INT_MAX - 254) |
|
73 |
+ return AVERROR_INVALIDDATA; |
|
74 |
+ next = *(*ptr)++; |
|
75 |
+ val += next; |
|
76 |
+ if (next < 255) |
|
77 |
+ break; |
|
78 |
+ else |
|
79 |
+ val--; |
|
80 |
+ } |
|
81 |
+ return val; |
|
82 |
+} |
|
83 |
+ |
|
84 |
+/** |
|
85 |
+ * Parse Opus packet info from raw packet data |
|
86 |
+ */ |
|
87 |
+int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, |
|
88 |
+ int self_delimiting) |
|
89 |
+{ |
|
90 |
+ const uint8_t *ptr = buf; |
|
91 |
+ const uint8_t *end = buf + buf_size; |
|
92 |
+ int padding = 0; |
|
93 |
+ int frame_bytes, i; |
|
94 |
+ |
|
95 |
+ if (buf_size < 1) |
|
96 |
+ goto fail; |
|
97 |
+ |
|
98 |
+ /* TOC byte */ |
|
99 |
+ i = *ptr++; |
|
100 |
+ pkt->code = (i ) & 0x3; |
|
101 |
+ pkt->stereo = (i >> 2) & 0x1; |
|
102 |
+ pkt->config = (i >> 3) & 0x1F; |
|
103 |
+ |
|
104 |
+ /* code 2 and code 3 packets have at least 1 byte after the TOC */ |
|
105 |
+ if (pkt->code >= 2 && buf_size < 2) |
|
106 |
+ goto fail; |
|
107 |
+ |
|
108 |
+ switch (pkt->code) { |
|
109 |
+ case 0: |
|
110 |
+ /* 1 frame */ |
|
111 |
+ pkt->frame_count = 1; |
|
112 |
+ pkt->vbr = 0; |
|
113 |
+ |
|
114 |
+ if (self_delimiting) { |
|
115 |
+ int len = xiph_lacing_16bit(&ptr, end); |
|
116 |
+ if (len < 0 || len > end - ptr) |
|
117 |
+ goto fail; |
|
118 |
+ end = ptr + len; |
|
119 |
+ buf_size = end - buf; |
|
120 |
+ } |
|
121 |
+ |
|
122 |
+ frame_bytes = end - ptr; |
|
123 |
+ if (frame_bytes > MAX_FRAME_SIZE) |
|
124 |
+ goto fail; |
|
125 |
+ pkt->frame_offset[0] = ptr - buf; |
|
126 |
+ pkt->frame_size[0] = frame_bytes; |
|
127 |
+ break; |
|
128 |
+ case 1: |
|
129 |
+ /* 2 frames, equal size */ |
|
130 |
+ pkt->frame_count = 2; |
|
131 |
+ pkt->vbr = 0; |
|
132 |
+ |
|
133 |
+ if (self_delimiting) { |
|
134 |
+ int len = xiph_lacing_16bit(&ptr, end); |
|
135 |
+ if (len < 0 || 2 * len > end - ptr) |
|
136 |
+ goto fail; |
|
137 |
+ end = ptr + 2 * len; |
|
138 |
+ buf_size = end - buf; |
|
139 |
+ } |
|
140 |
+ |
|
141 |
+ frame_bytes = end - ptr; |
|
142 |
+ if (frame_bytes & 1 || frame_bytes >> 1 > MAX_FRAME_SIZE) |
|
143 |
+ goto fail; |
|
144 |
+ pkt->frame_offset[0] = ptr - buf; |
|
145 |
+ pkt->frame_size[0] = frame_bytes >> 1; |
|
146 |
+ pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0]; |
|
147 |
+ pkt->frame_size[1] = frame_bytes >> 1; |
|
148 |
+ break; |
|
149 |
+ case 2: |
|
150 |
+ /* 2 frames, different sizes */ |
|
151 |
+ pkt->frame_count = 2; |
|
152 |
+ pkt->vbr = 1; |
|
153 |
+ |
|
154 |
+ /* read 1st frame size */ |
|
155 |
+ frame_bytes = xiph_lacing_16bit(&ptr, end); |
|
156 |
+ if (frame_bytes < 0) |
|
157 |
+ goto fail; |
|
158 |
+ |
|
159 |
+ if (self_delimiting) { |
|
160 |
+ int len = xiph_lacing_16bit(&ptr, end); |
|
161 |
+ if (len < 0 || len + frame_bytes > end - ptr) |
|
162 |
+ goto fail; |
|
163 |
+ end = ptr + frame_bytes + len; |
|
164 |
+ buf_size = end - buf; |
|
165 |
+ } |
|
166 |
+ |
|
167 |
+ pkt->frame_offset[0] = ptr - buf; |
|
168 |
+ pkt->frame_size[0] = frame_bytes; |
|
169 |
+ |
|
170 |
+ /* calculate 2nd frame size */ |
|
171 |
+ frame_bytes = end - ptr - pkt->frame_size[0]; |
|
172 |
+ if (frame_bytes < 0 || frame_bytes > MAX_FRAME_SIZE) |
|
173 |
+ goto fail; |
|
174 |
+ pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0]; |
|
175 |
+ pkt->frame_size[1] = frame_bytes; |
|
176 |
+ break; |
|
177 |
+ case 3: |
|
178 |
+ /* 1 to 48 frames, can be different sizes */ |
|
179 |
+ i = *ptr++; |
|
180 |
+ pkt->frame_count = (i ) & 0x3F; |
|
181 |
+ padding = (i >> 6) & 0x01; |
|
182 |
+ pkt->vbr = (i >> 7) & 0x01; |
|
183 |
+ |
|
184 |
+ if (pkt->frame_count == 0 || pkt->frame_count > MAX_FRAMES) |
|
185 |
+ goto fail; |
|
186 |
+ |
|
187 |
+ /* read padding size */ |
|
188 |
+ if (padding) { |
|
189 |
+ padding = xiph_lacing_full(&ptr, end); |
|
190 |
+ if (padding < 0) |
|
191 |
+ goto fail; |
|
192 |
+ } |
|
193 |
+ |
|
194 |
+ /* read frame sizes */ |
|
195 |
+ if (pkt->vbr) { |
|
196 |
+ /* for VBR, all frames except the final one have their size coded |
|
197 |
+ in the bitstream. the last frame size is implicit. */ |
|
198 |
+ int total_bytes = 0; |
|
199 |
+ for (i = 0; i < pkt->frame_count - 1; i++) { |
|
200 |
+ frame_bytes = xiph_lacing_16bit(&ptr, end); |
|
201 |
+ if (frame_bytes < 0) |
|
202 |
+ goto fail; |
|
203 |
+ pkt->frame_size[i] = frame_bytes; |
|
204 |
+ total_bytes += frame_bytes; |
|
205 |
+ } |
|
206 |
+ |
|
207 |
+ if (self_delimiting) { |
|
208 |
+ int len = xiph_lacing_16bit(&ptr, end); |
|
209 |
+ if (len < 0 || len + total_bytes + padding > end - ptr) |
|
210 |
+ goto fail; |
|
211 |
+ end = ptr + total_bytes + len + padding; |
|
212 |
+ buf_size = end - buf; |
|
213 |
+ } |
|
214 |
+ |
|
215 |
+ frame_bytes = end - ptr - padding; |
|
216 |
+ if (total_bytes > frame_bytes) |
|
217 |
+ goto fail; |
|
218 |
+ pkt->frame_offset[0] = ptr - buf; |
|
219 |
+ for (i = 1; i < pkt->frame_count; i++) |
|
220 |
+ pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1]; |
|
221 |
+ pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes; |
|
222 |
+ } else { |
|
223 |
+ /* for CBR, the remaining packet bytes are divided evenly between |
|
224 |
+ the frames */ |
|
225 |
+ if (self_delimiting) { |
|
226 |
+ frame_bytes = xiph_lacing_16bit(&ptr, end); |
|
227 |
+ if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr) |
|
228 |
+ goto fail; |
|
229 |
+ end = ptr + pkt->frame_count * frame_bytes + padding; |
|
230 |
+ buf_size = end - buf; |
|
231 |
+ } else { |
|
232 |
+ frame_bytes = end - ptr - padding; |
|
233 |
+ if (frame_bytes % pkt->frame_count || |
|
234 |
+ frame_bytes / pkt->frame_count > MAX_FRAME_SIZE) |
|
235 |
+ goto fail; |
|
236 |
+ frame_bytes /= pkt->frame_count; |
|
237 |
+ } |
|
238 |
+ |
|
239 |
+ pkt->frame_offset[0] = ptr - buf; |
|
240 |
+ pkt->frame_size[0] = frame_bytes; |
|
241 |
+ for (i = 1; i < pkt->frame_count; i++) { |
|
242 |
+ pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1]; |
|
243 |
+ pkt->frame_size[i] = frame_bytes; |
|
244 |
+ } |
|
245 |
+ } |
|
246 |
+ } |
|
247 |
+ |
|
248 |
+ pkt->packet_size = buf_size; |
|
249 |
+ pkt->data_size = pkt->packet_size - padding; |
|
250 |
+ |
|
251 |
+ /* total packet duration cannot be larger than 120ms */ |
|
252 |
+ pkt->frame_duration = opus_frame_duration[pkt->config]; |
|
253 |
+ if (pkt->frame_duration * pkt->frame_count > MAX_PACKET_DUR) |
|
254 |
+ goto fail; |
|
255 |
+ |
|
256 |
+ /* set mode and bandwidth */ |
|
257 |
+ if (pkt->config < 12) { |
|
258 |
+ pkt->mode = OPUS_MODE_SILK; |
|
259 |
+ pkt->bandwidth = pkt->config >> 2; |
|
260 |
+ } else if (pkt->config < 16) { |
|
261 |
+ pkt->mode = OPUS_MODE_HYBRID; |
|
262 |
+ pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14); |
|
263 |
+ } else { |
|
264 |
+ pkt->mode = OPUS_MODE_CELT; |
|
265 |
+ pkt->bandwidth = (pkt->config - 16) >> 2; |
|
266 |
+ /* skip mediumband */ |
|
267 |
+ if (pkt->bandwidth) |
|
268 |
+ pkt->bandwidth++; |
|
269 |
+ } |
|
270 |
+ |
|
271 |
+ return 0; |
|
272 |
+ |
|
273 |
+fail: |
|
274 |
+ memset(pkt, 0, sizeof(*pkt)); |
|
275 |
+ return AVERROR_INVALIDDATA; |
|
276 |
+} |
|
277 |
+ |
|
278 |
+static int channel_reorder_vorbis(int nb_channels, int channel_idx) |
|
279 |
+{ |
|
280 |
+ return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx]; |
|
281 |
+} |
|
282 |
+ |
|
283 |
+static int channel_reorder_unknown(int nb_channels, int channel_idx) |
|
284 |
+{ |
|
285 |
+ return channel_idx; |
|
286 |
+} |
|
287 |
+ |
|
288 |
+av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, |
|
289 |
+ OpusContext *s) |
|
290 |
+{ |
|
291 |
+ static const uint8_t default_channel_map[2] = { 0, 1 }; |
|
292 |
+ uint8_t default_extradata[19] = { |
|
293 |
+ 'O', 'p', 'u', 's', 'H', 'e', 'a', 'd', |
|
294 |
+ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
295 |
+ }; |
|
296 |
+ |
|
297 |
+ int (*channel_reorder)(int, int) = channel_reorder_unknown; |
|
298 |
+ |
|
299 |
+ const uint8_t *extradata, *channel_map; |
|
300 |
+ int extradata_size; |
|
301 |
+ int version, channels, map_type, streams, stereo_streams, i, j; |
|
302 |
+ uint64_t layout; |
|
303 |
+ |
|
304 |
+ if (!avctx->extradata) { |
|
305 |
+ if (avctx->channels > 2) { |
|
306 |
+ av_log(avctx, AV_LOG_ERROR, |
|
307 |
+ "Multichannel configuration without extradata.\n"); |
|
308 |
+ return AVERROR(EINVAL); |
|
309 |
+ } |
|
310 |
+ default_extradata[9] = (avctx->channels == 1) ? 1 : 2; |
|
311 |
+ extradata = default_extradata; |
|
312 |
+ extradata_size = sizeof(default_extradata); |
|
313 |
+ } else { |
|
314 |
+ extradata = avctx->extradata; |
|
315 |
+ extradata_size = avctx->extradata_size; |
|
316 |
+ } |
|
317 |
+ |
|
318 |
+ if (extradata_size < 19) { |
|
319 |
+ av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", |
|
320 |
+ extradata_size); |
|
321 |
+ return AVERROR_INVALIDDATA; |
|
322 |
+ } |
|
323 |
+ |
|
324 |
+ version = extradata[8]; |
|
325 |
+ if (version > 15) { |
|
326 |
+ avpriv_request_sample(avctx, "Extradata version %d", version); |
|
327 |
+ return AVERROR_PATCHWELCOME; |
|
328 |
+ } |
|
329 |
+ |
|
330 |
+ avctx->delay = AV_RL16(extradata + 10); |
|
331 |
+ |
|
332 |
+ channels = extradata[9]; |
|
333 |
+ if (!channels) { |
|
334 |
+ av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extadata\n"); |
|
335 |
+ return AVERROR_INVALIDDATA; |
|
336 |
+ } |
|
337 |
+ |
|
338 |
+ s->gain_i = AV_RL16(extradata + 16); |
|
339 |
+ if (s->gain_i) |
|
340 |
+ s->gain = pow(10, s->gain_i / (20.0 * 256)); |
|
341 |
+ |
|
342 |
+ map_type = extradata[18]; |
|
343 |
+ if (!map_type) { |
|
344 |
+ if (channels > 2) { |
|
345 |
+ av_log(avctx, AV_LOG_ERROR, |
|
346 |
+ "Channel mapping 0 is only specified for up to 2 channels\n"); |
|
347 |
+ return AVERROR_INVALIDDATA; |
|
348 |
+ } |
|
349 |
+ layout = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO; |
|
350 |
+ streams = 1; |
|
351 |
+ stereo_streams = channels - 1; |
|
352 |
+ channel_map = default_channel_map; |
|
353 |
+ } else if (map_type == 1 || map_type == 255) { |
|
354 |
+ if (extradata_size < 21 + channels) { |
|
355 |
+ av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", |
|
356 |
+ extradata_size); |
|
357 |
+ return AVERROR_INVALIDDATA; |
|
358 |
+ } |
|
359 |
+ |
|
360 |
+ streams = extradata[19]; |
|
361 |
+ stereo_streams = extradata[20]; |
|
362 |
+ if (!streams || stereo_streams > streams || |
|
363 |
+ streams + stereo_streams > 255) { |
|
364 |
+ av_log(avctx, AV_LOG_ERROR, |
|
365 |
+ "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams); |
|
366 |
+ return AVERROR_INVALIDDATA; |
|
367 |
+ } |
|
368 |
+ |
|
369 |
+ if (map_type == 1) { |
|
370 |
+ if (channels > 8) { |
|
371 |
+ av_log(avctx, AV_LOG_ERROR, |
|
372 |
+ "Channel mapping 1 is only specified for up to 8 channels\n"); |
|
373 |
+ return AVERROR_INVALIDDATA; |
|
374 |
+ } |
|
375 |
+ layout = ff_vorbis_channel_layouts[channels - 1]; |
|
376 |
+ channel_reorder = channel_reorder_vorbis; |
|
377 |
+ } else |
|
378 |
+ layout = 0; |
|
379 |
+ |
|
380 |
+ channel_map = extradata + 21; |
|
381 |
+ } else { |
|
382 |
+ avpriv_request_sample(avctx, "Mapping type %d", map_type); |
|
383 |
+ return AVERROR_PATCHWELCOME; |
|
384 |
+ } |
|
385 |
+ |
|
386 |
+ s->channel_maps = av_mallocz_array(channels, sizeof(*s->channel_maps)); |
|
387 |
+ if (!s->channel_maps) |
|
388 |
+ return AVERROR(ENOMEM); |
|
389 |
+ |
|
390 |
+ for (i = 0; i < channels; i++) { |
|
391 |
+ ChannelMap *map = &s->channel_maps[i]; |
|
392 |
+ uint8_t idx = channel_map[channel_reorder(channels, i)]; |
|
393 |
+ |
|
394 |
+ if (idx == 255) { |
|
395 |
+ map->silence = 1; |
|
396 |
+ continue; |
|
397 |
+ } else if (idx >= streams + stereo_streams) { |
|
398 |
+ av_log(avctx, AV_LOG_ERROR, |
|
399 |
+ "Invalid channel map for output channel %d: %d\n", i, idx); |
|
400 |
+ return AVERROR_INVALIDDATA; |
|
401 |
+ } |
|
402 |
+ |
|
403 |
+ /* check that we din't see this index yet */ |
|
404 |
+ map->copy = 0; |
|
405 |
+ for (j = 0; j < i; j++) |
|
406 |
+ if (channel_map[channel_reorder(channels, j)] == idx) { |
|
407 |
+ map->copy = 1; |
|
408 |
+ map->copy_idx = j; |
|
409 |
+ break; |
|
410 |
+ } |
|
411 |
+ |
|
412 |
+ if (idx < 2 * stereo_streams) { |
|
413 |
+ map->stream_idx = idx / 2; |
|
414 |
+ map->channel_idx = idx & 1; |
|
415 |
+ } else { |
|
416 |
+ map->stream_idx = idx - stereo_streams; |
|
417 |
+ map->channel_idx = 0; |
|
418 |
+ } |
|
419 |
+ } |
|
420 |
+ |
|
421 |
+ avctx->channels = channels; |
|
422 |
+ avctx->channel_layout = layout; |
|
423 |
+ s->nb_streams = streams; |
|
424 |
+ s->nb_stereo_streams = stereo_streams; |
|
425 |
+ |
|
426 |
+ return 0; |
|
427 |
+} |
0 | 428 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,429 @@ |
0 |
+/* |
|
1 |
+ * Opus decoder/demuxer common functions |
|
2 |
+ * Copyright (c) 2012 Andrew D'Addesio |
|
3 |
+ * Copyright (c) 2013-2014 Mozilla Corporation |
|
4 |
+ * |
|
5 |
+ * This file is part of Libav. |
|
6 |
+ * |
|
7 |
+ * Libav 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 |
+ * Libav 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 Libav; if not, write to the Free Software |
|
19 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
20 |
+ */ |
|
21 |
+ |
|
22 |
+#ifndef AVCODEC_OPUS_H |
|
23 |
+#define AVCODEC_OPUS_H |
|
24 |
+ |
|
25 |
+#include <stdint.h> |
|
26 |
+ |
|
27 |
+#include "libavutil/audio_fifo.h" |
|
28 |
+#include "libavutil/float_dsp.h" |
|
29 |
+#include "libavutil/frame.h" |
|
30 |
+ |
|
31 |
+#include "libavresample/avresample.h" |
|
32 |
+ |
|
33 |
+#include "avcodec.h" |
|
34 |
+#include "get_bits.h" |
|
35 |
+ |
|
36 |
+#define MAX_FRAME_SIZE 1275 |
|
37 |
+#define MAX_FRAMES 48 |
|
38 |
+#define MAX_PACKET_DUR 5760 |
|
39 |
+ |
|
40 |
+#define CELT_SHORT_BLOCKSIZE 120 |
|
41 |
+#define CELT_OVERLAP CELT_SHORT_BLOCKSIZE |
|
42 |
+#define CELT_MAX_LOG_BLOCKS 3 |
|
43 |
+#define CELT_MAX_FRAME_SIZE (CELT_SHORT_BLOCKSIZE * (1 << CELT_MAX_LOG_BLOCKS)) |
|
44 |
+#define CELT_MAX_BANDS 21 |
|
45 |
+#define CELT_VECTORS 11 |
|
46 |
+#define CELT_ALLOC_STEPS 6 |
|
47 |
+#define CELT_FINE_OFFSET 21 |
|
48 |
+#define CELT_MAX_FINE_BITS 8 |
|
49 |
+#define CELT_NORM_SCALE 16384 |
|
50 |
+#define CELT_QTHETA_OFFSET 4 |
|
51 |
+#define CELT_QTHETA_OFFSET_TWOPHASE 16 |
|
52 |
+#define CELT_DEEMPH_COEFF 0.85000610f |
|
53 |
+#define CELT_POSTFILTER_MINPERIOD 15 |
|
54 |
+#define CELT_ENERGY_SILENCE (-28.0f) |
|
55 |
+ |
|
56 |
+#define SILK_HISTORY 322 |
|
57 |
+#define SILK_MAX_LPC 16 |
|
58 |
+ |
|
59 |
+#define ROUND_MULL(a,b,s) (((MUL64(a, b) >> (s - 1)) + 1) >> 1) |
|
60 |
+#define ROUND_MUL16(a,b) ((MUL16(a, b) + 16384) >> 15) |
|
61 |
+#define opus_ilog(i) (av_log2(i) + !!(i)) |
|
62 |
+ |
|
63 |
+enum OpusMode { |
|
64 |
+ OPUS_MODE_SILK, |
|
65 |
+ OPUS_MODE_HYBRID, |
|
66 |
+ OPUS_MODE_CELT |
|
67 |
+}; |
|
68 |
+ |
|
69 |
+enum OpusBandwidth { |
|
70 |
+ OPUS_BANDWIDTH_NARROWBAND, |
|
71 |
+ OPUS_BANDWIDTH_MEDIUMBAND, |
|
72 |
+ OPUS_BANDWIDTH_WIDEBAND, |
|
73 |
+ OPUS_BANDWIDTH_SUPERWIDEBAND, |
|
74 |
+ OPUS_BANDWIDTH_FULLBAND |
|
75 |
+}; |
|
76 |
+ |
|
77 |
+typedef struct RawBitsContext { |
|
78 |
+ const uint8_t *position; |
|
79 |
+ unsigned int bytes; |
|
80 |
+ unsigned int cachelen; |
|
81 |
+ unsigned int cacheval; |
|
82 |
+} RawBitsContext; |
|
83 |
+ |
|
84 |
+typedef struct OpusRangeCoder { |
|
85 |
+ GetBitContext gb; |
|
86 |
+ RawBitsContext rb; |
|
87 |
+ unsigned int range; |
|
88 |
+ unsigned int value; |
|
89 |
+ unsigned int total_read_bits; |
|
90 |
+} OpusRangeCoder; |
|
91 |
+ |
|
92 |
+typedef struct SilkContext SilkContext; |
|
93 |
+ |
|
94 |
+typedef struct CeltIMDCTContext CeltIMDCTContext; |
|
95 |
+ |
|
96 |
+typedef struct CeltContext CeltContext; |
|
97 |
+ |
|
98 |
+typedef struct OpusPacket { |
|
99 |
+ int packet_size; /** packet size */ |
|
100 |
+ int data_size; /** size of the useful data -- packet size - padding */ |
|
101 |
+ int code; /** packet code: specifies the frame layout */ |
|
102 |
+ int stereo; /** whether this packet is mono or stereo */ |
|
103 |
+ int vbr; /** vbr flag */ |
|
104 |
+ int config; /** configuration: tells the audio mode, |
|
105 |
+ ** bandwidth, and frame duration */ |
|
106 |
+ int frame_count; /** frame count */ |
|
107 |
+ int frame_offset[MAX_FRAMES]; /** frame offsets */ |
|
108 |
+ int frame_size[MAX_FRAMES]; /** frame sizes */ |
|
109 |
+ int frame_duration; /** frame duration, in samples @ 48kHz */ |
|
110 |
+ enum OpusMode mode; /** mode */ |
|
111 |
+ enum OpusBandwidth bandwidth; /** bandwidth */ |
|
112 |
+} OpusPacket; |
|
113 |
+ |
|
114 |
+typedef struct OpusStreamContext { |
|
115 |
+ AVCodecContext *avctx; |
|
116 |
+ int output_channels; |
|
117 |
+ |
|
118 |
+ OpusRangeCoder rc; |
|
119 |
+ OpusRangeCoder redundancy_rc; |
|
120 |
+ SilkContext *silk; |
|
121 |
+ CeltContext *celt; |
|
122 |
+ AVFloatDSPContext *fdsp; |
|
123 |
+ |
|
124 |
+ float silk_buf[2][960]; |
|
125 |
+ float *silk_output[2]; |
|
126 |
+ DECLARE_ALIGNED(32, float, celt_buf)[2][960]; |
|
127 |
+ float *celt_output[2]; |
|
128 |
+ |
|
129 |
+ float redundancy_buf[2][960]; |
|
130 |
+ float *redundancy_output[2]; |
|
131 |
+ |
|
132 |
+ /* data buffers for the final output data */ |
|
133 |
+ float *out[2]; |
|
134 |
+ int out_size; |
|
135 |
+ |
|
136 |
+ float *out_dummy; |
|
137 |
+ int out_dummy_allocated_size; |
|
138 |
+ |
|
139 |
+ AVAudioResampleContext *avr; |
|
140 |
+ AVAudioFifo *celt_delay; |
|
141 |
+ int silk_samplerate; |
|
142 |
+ /* number of samples we still want to get from the resampler */ |
|
143 |
+ int delayed_samples; |
|
144 |
+ |
|
145 |
+ OpusPacket packet; |
|
146 |
+ |
|
147 |
+ int redundancy_idx; |
|
148 |
+} OpusStreamContext; |
|
149 |
+ |
|
150 |
+// a mapping between an opus stream and an output channel |
|
151 |
+typedef struct ChannelMap { |
|
152 |
+ int stream_idx; |
|
153 |
+ int channel_idx; |
|
154 |
+ |
|
155 |
+ // when a single decoded channel is mapped to multiple output channels, we |
|
156 |
+ // write to the first output directly and copy from it to the others |
|
157 |
+ // this field is set to 1 for those copied output channels |
|
158 |
+ int copy; |
|
159 |
+ // this is the index of the output channel to copy from |
|
160 |
+ int copy_idx; |
|
161 |
+ |
|
162 |
+ // this channel is silent |
|
163 |
+ int silence; |
|
164 |
+} ChannelMap; |
|
165 |
+ |
|
166 |
+typedef struct OpusContext { |
|
167 |
+ OpusStreamContext *streams; |
|
168 |
+ int nb_streams; |
|
169 |
+ int nb_stereo_streams; |
|
170 |
+ |
|
171 |
+ AVFloatDSPContext fdsp; |
|
172 |
+ int16_t gain_i; |
|
173 |
+ float gain; |
|
174 |
+ |
|
175 |
+ ChannelMap *channel_maps; |
|
176 |
+} OpusContext; |
|
177 |
+ |
|
178 |
+static av_always_inline void opus_rc_normalize(OpusRangeCoder *rc) |
|
179 |
+{ |
|
180 |
+ while (rc->range <= 1<<23) { |
|
181 |
+ rc->value = ((rc->value << 8) | (get_bits(&rc->gb, 8) ^ 0xFF)) & ((1u << 31) - 1); |
|
182 |
+ rc->range <<= 8; |
|
183 |
+ rc->total_read_bits += 8; |
|
184 |
+ } |
|
185 |
+} |
|
186 |
+ |
|
187 |
+static av_always_inline void opus_rc_update(OpusRangeCoder *rc, unsigned int scale, |
|
188 |
+ unsigned int low, unsigned int high, |
|
189 |
+ unsigned int total) |
|
190 |
+{ |
|
191 |
+ rc->value -= scale * (total - high); |
|
192 |
+ rc->range = low ? scale * (high - low) |
|
193 |
+ : rc->range - scale * (total - high); |
|
194 |
+ opus_rc_normalize(rc); |
|
195 |
+} |
|
196 |
+ |
|
197 |
+static av_always_inline unsigned int opus_rc_getsymbol(OpusRangeCoder *rc, const uint16_t *cdf) |
|
198 |
+{ |
|
199 |
+ unsigned int k, scale, total, symbol, low, high; |
|
200 |
+ |
|
201 |
+ total = *cdf++; |
|
202 |
+ |
|
203 |
+ scale = rc->range / total; |
|
204 |
+ symbol = rc->value / scale + 1; |
|
205 |
+ symbol = total - FFMIN(symbol, total); |
|
206 |
+ |
|
207 |
+ for (k = 0; cdf[k] <= symbol; k++); |
|
208 |
+ high = cdf[k]; |
|
209 |
+ low = k ? cdf[k-1] : 0; |
|
210 |
+ |
|
211 |
+ opus_rc_update(rc, scale, low, high, total); |
|
212 |
+ |
|
213 |
+ return k; |
|
214 |
+} |
|
215 |
+ |
|
216 |
+static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits) |
|
217 |
+{ |
|
218 |
+ unsigned int k, scale; |
|
219 |
+ scale = rc->range >> bits; // in this case, scale = symbol |
|
220 |
+ |
|
221 |
+ if (rc->value >= scale) { |
|
222 |
+ rc->value -= scale; |
|
223 |
+ rc->range -= scale; |
|
224 |
+ k = 0; |
|
225 |
+ } else { |
|
226 |
+ rc->range = scale; |
|
227 |
+ k = 1; |
|
228 |
+ } |
|
229 |
+ opus_rc_normalize(rc); |
|
230 |
+ return k; |
|
231 |
+} |
|
232 |
+ |
|
233 |
+/** |
|
234 |
+ * CELT: estimate bits of entropy that have thus far been consumed for the |
|
235 |
+ * current CELT frame, to integer and fractional (1/8th bit) precision |
|
236 |
+ */ |
|
237 |
+static av_always_inline unsigned int opus_rc_tell(const OpusRangeCoder *rc) |
|
238 |
+{ |
|
239 |
+ return rc->total_read_bits - av_log2(rc->range) - 1; |
|
240 |
+} |
|
241 |
+ |
|
242 |
+static av_always_inline unsigned int opus_rc_tell_frac(const OpusRangeCoder *rc) |
|
243 |
+{ |
|
244 |
+ unsigned int i, total_bits, rcbuffer, range; |
|
245 |
+ |
|
246 |
+ total_bits = rc->total_read_bits << 3; |
|
247 |
+ rcbuffer = av_log2(rc->range) + 1; |
|
248 |
+ range = rc->range >> (rcbuffer-16); |
|
249 |
+ |
|
250 |
+ for (i = 0; i < 3; i++) { |
|
251 |
+ int bit; |
|
252 |
+ range = range * range >> 15; |
|
253 |
+ bit = range >> 16; |
|
254 |
+ rcbuffer = rcbuffer << 1 | bit; |
|
255 |
+ range >>= bit; |
|
256 |
+ } |
|
257 |
+ |
|
258 |
+ return total_bits - rcbuffer; |
|
259 |
+} |
|
260 |
+ |
|
261 |
+/** |
|
262 |
+ * CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise |
|
263 |
+ */ |
|
264 |
+static av_always_inline unsigned int opus_getrawbits(OpusRangeCoder *rc, unsigned int count) |
|
265 |
+{ |
|
266 |
+ unsigned int value = 0; |
|
267 |
+ |
|
268 |
+ while (rc->rb.bytes && rc->rb.cachelen < count) { |
|
269 |
+ rc->rb.cacheval |= *--rc->rb.position << rc->rb.cachelen; |
|
270 |
+ rc->rb.cachelen += 8; |
|
271 |
+ rc->rb.bytes--; |
|
272 |
+ } |
|
273 |
+ |
|
274 |
+ value = rc->rb.cacheval & ((1<<count)-1); |
|
275 |
+ rc->rb.cacheval >>= count; |
|
276 |
+ rc->rb.cachelen -= count; |
|
277 |
+ rc->total_read_bits += count; |
|
278 |
+ |
|
279 |
+ return value; |
|
280 |
+} |
|
281 |
+ |
|
282 |
+/** |
|
283 |
+ * CELT: read a uniform distribution |
|
284 |
+ */ |
|
285 |
+static av_always_inline unsigned int opus_rc_unimodel(OpusRangeCoder *rc, unsigned int size) |
|
286 |
+{ |
|
287 |
+ unsigned int bits, k, scale, total; |
|
288 |
+ |
|
289 |
+ bits = opus_ilog(size - 1); |
|
290 |
+ total = (bits > 8) ? ((size - 1) >> (bits - 8)) + 1 : size; |
|
291 |
+ |
|
292 |
+ scale = rc->range / total; |
|
293 |
+ k = rc->value / scale + 1; |
|
294 |
+ k = total - FFMIN(k, total); |
|
295 |
+ opus_rc_update(rc, scale, k, k + 1, total); |
|
296 |
+ |
|
297 |
+ if (bits > 8) { |
|
298 |
+ k = k << (bits - 8) | opus_getrawbits(rc, bits - 8); |
|
299 |
+ return FFMIN(k, size - 1); |
|
300 |
+ } else |
|
301 |
+ return k; |
|
302 |
+} |
|
303 |
+ |
|
304 |
+static av_always_inline int opus_rc_laplace(OpusRangeCoder *rc, unsigned int symbol, int decay) |
|
305 |
+{ |
|
306 |
+ /* extends the range coder to model a Laplace distribution */ |
|
307 |
+ int value = 0; |
|
308 |
+ unsigned int scale, low = 0, center; |
|
309 |
+ |
|
310 |
+ scale = rc->range >> 15; |
|
311 |
+ center = rc->value / scale + 1; |
|
312 |
+ center = (1 << 15) - FFMIN(center, 1 << 15); |
|
313 |
+ |
|
314 |
+ if (center >= symbol) { |
|
315 |
+ value++; |
|
316 |
+ low = symbol; |
|
317 |
+ symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15); |
|
318 |
+ |
|
319 |
+ while (symbol > 1 && center >= low + 2 * symbol) { |
|
320 |
+ value++; |
|
321 |
+ symbol *= 2; |
|
322 |
+ low += symbol; |
|
323 |
+ symbol = (((symbol - 2) * decay) >> 15) + 1; |
|
324 |
+ } |
|
325 |
+ |
|
326 |
+ if (symbol <= 1) { |
|
327 |
+ int distance = (center - low) >> 1; |
|
328 |
+ value += distance; |
|
329 |
+ low += 2 * distance; |
|
330 |
+ } |
|
331 |
+ |
|
332 |
+ if (center < low + symbol) |
|
333 |
+ value *= -1; |
|
334 |
+ else |
|
335 |
+ low += symbol; |
|
336 |
+ } |
|
337 |
+ |
|
338 |
+ opus_rc_update(rc, scale, low, FFMIN(low + symbol, 32768), 32768); |
|
339 |
+ |
|
340 |
+ return value; |
|
341 |
+} |
|
342 |
+ |
|
343 |
+static av_always_inline unsigned int opus_rc_stepmodel(OpusRangeCoder *rc, int k0) |
|
344 |
+{ |
|
345 |
+ /* Use a probability of 3 up to itheta=8192 and then use 1 after */ |
|
346 |
+ unsigned int k, scale, symbol, total = (k0+1)*3 + k0; |
|
347 |
+ scale = rc->range / total; |
|
348 |
+ symbol = rc->value / scale + 1; |
|
349 |
+ symbol = total - FFMIN(symbol, total); |
|
350 |
+ |
|
351 |
+ k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2; |
|
352 |
+ |
|
353 |
+ opus_rc_update(rc, scale, (k <= k0) ? 3*(k+0) : (k-1-k0) + 3*(k0+1), |
|
354 |
+ (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total); |
|
355 |
+ return k; |
|
356 |
+} |
|
357 |
+ |
|
358 |
+static av_always_inline unsigned int opus_rc_trimodel(OpusRangeCoder *rc, int qn) |
|
359 |
+{ |
|
360 |
+ unsigned int k, scale, symbol, total, low, center; |
|
361 |
+ |
|
362 |
+ total = ((qn>>1) + 1) * ((qn>>1) + 1); |
|
363 |
+ scale = rc->range / total; |
|
364 |
+ center = rc->value / scale + 1; |
|
365 |
+ center = total - FFMIN(center, total); |
|
366 |
+ |
|
367 |
+ if (center < total >> 1) { |
|
368 |
+ k = (ff_sqrt(8 * center + 1) - 1) >> 1; |
|
369 |
+ low = k * (k + 1) >> 1; |
|
370 |
+ symbol = k + 1; |
|
371 |
+ } else { |
|
372 |
+ k = (2*(qn + 1) - ff_sqrt(8*(total - center - 1) + 1)) >> 1; |
|
373 |
+ low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1); |
|
374 |
+ symbol = qn + 1 - k; |
|
375 |
+ } |
|
376 |
+ |
|
377 |
+ opus_rc_update(rc, scale, low, low + symbol, total); |
|
378 |
+ |
|
379 |
+ return k; |
|
380 |
+} |
|
381 |
+ |
|
382 |
+int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, |
|
383 |
+ int self_delimited); |
|
384 |
+ |
|
385 |
+int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s); |
|
386 |
+ |
|
387 |
+int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels); |
|
388 |
+void ff_silk_free(SilkContext **ps); |
|
389 |
+void ff_silk_flush(SilkContext *s); |
|
390 |
+ |
|
391 |
+/** |
|
392 |
+ * Decode the LP layer of one Opus frame (which may correspond to several SILK |
|
393 |
+ * frames). |
|
394 |
+ */ |
|
395 |
+int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, |
|
396 |
+ float *output[2], |
|
397 |
+ enum OpusBandwidth bandwidth, int coded_channels, |
|
398 |
+ int duration_ms); |
|
399 |
+ |
|
400 |
+/** |
|
401 |
+ * Init an iMDCT of the length 2 * 15 * (2^N) |
|
402 |
+ */ |
|
403 |
+int ff_celt_imdct_init(CeltIMDCTContext **s, int N); |
|
404 |
+ |
|
405 |
+/** |
|
406 |
+ * Free an iMDCT. |
|
407 |
+ */ |
|
408 |
+void ff_celt_imdct_uninit(CeltIMDCTContext **s); |
|
409 |
+ |
|
410 |
+/** |
|
411 |
+ * Calculate the middle half of the iMDCT |
|
412 |
+ */ |
|
413 |
+void ff_celt_imdct_half(CeltIMDCTContext *s, float *dst, const float *src, |
|
414 |
+ int src_stride, float scale); |
|
415 |
+ |
|
416 |
+int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels); |
|
417 |
+ |
|
418 |
+void ff_celt_free(CeltContext **s); |
|
419 |
+ |
|
420 |
+void ff_celt_flush(CeltContext *s); |
|
421 |
+ |
|
422 |
+int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc, |
|
423 |
+ float **output, int coded_channels, int frame_size, |
|
424 |
+ int startband, int endband); |
|
425 |
+ |
|
426 |
+extern const float ff_celt_window2[120]; |
|
427 |
+ |
|
428 |
+#endif /* AVCODEC_OPUS_H */ |
0 | 429 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,2220 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2012 Andrew D'Addesio |
|
2 |
+ * Copyright (c) 2013-2014 Mozilla Corporation |
|
3 |
+ * |
|
4 |
+ * This file is part of Libav. |
|
5 |
+ * |
|
6 |
+ * Libav is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * Libav is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with Libav; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+/** |
|
22 |
+ * @file |
|
23 |
+ * Opus CELT decoder |
|
24 |
+ */ |
|
25 |
+ |
|
26 |
+#include <stdint.h> |
|
27 |
+ |
|
28 |
+#include "libavutil/float_dsp.h" |
|
29 |
+ |
|
30 |
+#include "opus.h" |
|
31 |
+ |
|
32 |
+enum CeltSpread { |
|
33 |
+ CELT_SPREAD_NONE, |
|
34 |
+ CELT_SPREAD_LIGHT, |
|
35 |
+ CELT_SPREAD_NORMAL, |
|
36 |
+ CELT_SPREAD_AGGRESSIVE |
|
37 |
+}; |
|
38 |
+ |
|
39 |
+typedef struct CeltFrame { |
|
40 |
+ float energy[CELT_MAX_BANDS]; |
|
41 |
+ float prev_energy[2][CELT_MAX_BANDS]; |
|
42 |
+ |
|
43 |
+ uint8_t collapse_masks[CELT_MAX_BANDS]; |
|
44 |
+ |
|
45 |
+ /* buffer for mdct output + postfilter */ |
|
46 |
+ DECLARE_ALIGNED(32, float, buf)[2048]; |
|
47 |
+ |
|
48 |
+ /* postfilter parameters */ |
|
49 |
+ int pf_period_new; |
|
50 |
+ float pf_gains_new[3]; |
|
51 |
+ int pf_period; |
|
52 |
+ float pf_gains[3]; |
|
53 |
+ int pf_period_old; |
|
54 |
+ float pf_gains_old[3]; |
|
55 |
+ |
|
56 |
+ float deemph_coeff; |
|
57 |
+} CeltFrame; |
|
58 |
+ |
|
59 |
+struct CeltContext { |
|
60 |
+ // constant values that do not change during context lifetime |
|
61 |
+ AVCodecContext *avctx; |
|
62 |
+ CeltIMDCTContext *imdct[4]; |
|
63 |
+ AVFloatDSPContext dsp; |
|
64 |
+ int output_channels; |
|
65 |
+ |
|
66 |
+ // values that have inter-frame effect and must be reset on flush |
|
67 |
+ CeltFrame frame[2]; |
|
68 |
+ uint32_t seed; |
|
69 |
+ int flushed; |
|
70 |
+ |
|
71 |
+ // values that only affect a single frame |
|
72 |
+ int coded_channels; |
|
73 |
+ int framebits; |
|
74 |
+ int duration; |
|
75 |
+ |
|
76 |
+ /* number of iMDCT blocks in the frame */ |
|
77 |
+ int blocks; |
|
78 |
+ /* size of each block */ |
|
79 |
+ int blocksize; |
|
80 |
+ |
|
81 |
+ int startband; |
|
82 |
+ int endband; |
|
83 |
+ int codedbands; |
|
84 |
+ |
|
85 |
+ int anticollapse_bit; |
|
86 |
+ |
|
87 |
+ int intensitystereo; |
|
88 |
+ int dualstereo; |
|
89 |
+ enum CeltSpread spread; |
|
90 |
+ |
|
91 |
+ int remaining; |
|
92 |
+ int remaining2; |
|
93 |
+ int fine_bits [CELT_MAX_BANDS]; |
|
94 |
+ int fine_priority[CELT_MAX_BANDS]; |
|
95 |
+ int pulses [CELT_MAX_BANDS]; |
|
96 |
+ int tf_change [CELT_MAX_BANDS]; |
|
97 |
+ |
|
98 |
+ DECLARE_ALIGNED(32, float, coeffs)[2][CELT_MAX_FRAME_SIZE]; |
|
99 |
+ DECLARE_ALIGNED(32, float, scratch)[22 * 8]; // MAX(celt_freq_range) * 1<<CELT_MAX_LOG_BLOCKS |
|
100 |
+}; |
|
101 |
+ |
|
102 |
+static const uint16_t celt_model_tapset[] = { 4, 2, 3, 4 }; |
|
103 |
+ |
|
104 |
+static const uint16_t celt_model_spread[] = { 32, 7, 9, 30, 32 }; |
|
105 |
+ |
|
106 |
+static const uint16_t celt_model_alloc_trim[] = { |
|
107 |
+ 128, 2, 4, 9, 19, 41, 87, 109, 119, 124, 126, 128 |
|
108 |
+}; |
|
109 |
+ |
|
110 |
+static const uint16_t celt_model_energy_small[] = { 4, 2, 3, 4 }; |
|
111 |
+ |
|
112 |
+static const uint8_t celt_freq_bands[] = { /* in steps of 200Hz */ |
|
113 |
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100 |
|
114 |
+}; |
|
115 |
+ |
|
116 |
+static const uint8_t celt_freq_range[] = { |
|
117 |
+ 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 6, 6, 8, 12, 18, 22 |
|
118 |
+}; |
|
119 |
+ |
|
120 |
+static const uint8_t celt_log_freq_range[] = { |
|
121 |
+ 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 16, 16, 16, 21, 21, 24, 29, 34, 36 |
|
122 |
+}; |
|
123 |
+ |
|
124 |
+static const int8_t celt_tf_select[4][2][2][2] = { |
|
125 |
+ { { { 0, -1 }, { 0, -1 } }, { { 0, -1 }, { 0, -1 } } }, |
|
126 |
+ { { { 0, -1 }, { 0, -2 } }, { { 1, 0 }, { 1, -1 } } }, |
|
127 |
+ { { { 0, -2 }, { 0, -3 } }, { { 2, 0 }, { 1, -1 } } }, |
|
128 |
+ { { { 0, -2 }, { 0, -3 } }, { { 3, 0 }, { 1, -1 } } } |
|
129 |
+}; |
|
130 |
+ |
|
131 |
+static const float celt_mean_energy[] = { |
|
132 |
+ 6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f, |
|
133 |
+ 4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f, |
|
134 |
+ 4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f, |
|
135 |
+ 4.500000f, 4.375000f, 4.625000f, 4.750000f, 4.437500f, |
|
136 |
+ 3.750000f, 3.750000f, 3.750000f, 3.750000f, 3.750000f |
|
137 |
+}; |
|
138 |
+ |
|
139 |
+static const float celt_alpha_coef[] = { |
|
140 |
+ 29440.0f/32768.0f, 26112.0f/32768.0f, 21248.0f/32768.0f, 16384.0f/32768.0f |
|
141 |
+}; |
|
142 |
+ |
|
143 |
+static const float celt_beta_coef[] = { /* TODO: precompute 1 minus this if the code ends up neater */ |
|
144 |
+ 30147.0f/32768.0f, 22282.0f/32768.0f, 12124.0f/32768.0f, 6554.0f/32768.0f |
|
145 |
+}; |
|
146 |
+ |
|
147 |
+static const uint8_t celt_coarse_energy_dist[4][2][42] = { |
|
148 |
+ { |
|
149 |
+ { // 120-sample inter |
|
150 |
+ 72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128, |
|
151 |
+ 64, 128, 92, 78, 92, 79, 92, 78, 90, 79, 116, 41, 115, 40, |
|
152 |
+ 114, 40, 132, 26, 132, 26, 145, 17, 161, 12, 176, 10, 177, 11 |
|
153 |
+ }, { // 120-sample intra |
|
154 |
+ 24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132, |
|
155 |
+ 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66, |
|
156 |
+ 91, 67, 100, 59, 108, 50, 120, 40, 122, 37, 97, 43, 78, 50 |
|
157 |
+ } |
|
158 |
+ }, { |
|
159 |
+ { // 240-sample inter |
|
160 |
+ 83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74, |
|
161 |
+ 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18, |
|
162 |
+ 146, 19, 162, 12, 165, 10, 178, 7, 189, 6, 190, 8, 177, 9 |
|
163 |
+ }, { // 240-sample intra |
|
164 |
+ 23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91, |
|
165 |
+ 73, 91, 78, 89, 86, 80, 92, 66, 93, 64, 102, 59, 103, 60, |
|
166 |
+ 104, 60, 117, 52, 123, 44, 138, 35, 133, 31, 97, 38, 77, 45 |
|
167 |
+ } |
|
168 |
+ }, { |
|
169 |
+ { // 480-sample inter |
|
170 |
+ 61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38, |
|
171 |
+ 112, 38, 124, 26, 132, 27, 136, 19, 140, 20, 155, 14, 159, 16, |
|
172 |
+ 158, 18, 170, 13, 177, 10, 187, 8, 192, 6, 175, 9, 159, 10 |
|
173 |
+ }, { // 480-sample intra |
|
174 |
+ 21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73, |
|
175 |
+ 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55, |
|
176 |
+ 112, 56, 129, 51, 132, 40, 150, 33, 140, 29, 98, 35, 77, 42 |
|
177 |
+ } |
|
178 |
+ }, { |
|
179 |
+ { // 960-sample inter |
|
180 |
+ 42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36, |
|
181 |
+ 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25, |
|
182 |
+ 154, 26, 166, 21, 173, 16, 184, 13, 184, 10, 150, 13, 139, 15 |
|
183 |
+ }, { // 960-sample intra |
|
184 |
+ 22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72, |
|
185 |
+ 96, 67, 101, 73, 107, 72, 113, 55, 118, 52, 125, 52, 118, 52, |
|
186 |
+ 117, 55, 135, 49, 137, 39, 157, 32, 145, 29, 97, 33, 77, 40 |
|
187 |
+ } |
|
188 |
+ } |
|
189 |
+}; |
|
190 |
+ |
|
191 |
+static const uint8_t celt_static_alloc[11][21] = { /* 1/32 bit/sample */ |
|
192 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
|
193 |
+ { 90, 80, 75, 69, 63, 56, 49, 40, 34, 29, 20, 18, 10, 0, 0, 0, 0, 0, 0, 0, 0 }, |
|
194 |
+ { 110, 100, 90, 84, 78, 71, 65, 58, 51, 45, 39, 32, 26, 20, 12, 0, 0, 0, 0, 0, 0 }, |
|
195 |
+ { 118, 110, 103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23, 15, 4, 0, 0, 0, 0 }, |
|
196 |
+ { 126, 119, 112, 104, 95, 89, 83, 78, 72, 66, 60, 54, 47, 39, 32, 25, 17, 12, 1, 0, 0 }, |
|
197 |
+ { 134, 127, 120, 114, 103, 97, 91, 85, 78, 72, 66, 60, 54, 47, 41, 35, 29, 23, 16, 10, 1 }, |
|
198 |
+ { 144, 137, 130, 124, 113, 107, 101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1 }, |
|
199 |
+ { 152, 145, 138, 132, 123, 117, 111, 105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1 }, |
|
200 |
+ { 162, 155, 148, 142, 133, 127, 121, 115, 108, 102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1 }, |
|
201 |
+ { 172, 165, 158, 152, 143, 137, 131, 125, 118, 112, 106, 100, 94, 87, 81, 75, 69, 63, 56, 45, 20 }, |
|
202 |
+ { 200, 200, 200, 200, 200, 200, 200, 200, 198, 193, 188, 183, 178, 173, 168, 163, 158, 153, 148, 129, 104 } |
|
203 |
+}; |
|
204 |
+ |
|
205 |
+static const uint8_t celt_static_caps[4][2][21] = { |
|
206 |
+ { // 120-sample |
|
207 |
+ {224, 224, 224, 224, 224, 224, 224, 224, 160, 160, |
|
208 |
+ 160, 160, 185, 185, 185, 178, 178, 168, 134, 61, 37}, |
|
209 |
+ {224, 224, 224, 224, 224, 224, 224, 224, 240, 240, |
|
210 |
+ 240, 240, 207, 207, 207, 198, 198, 183, 144, 66, 40}, |
|
211 |
+ }, { // 240-sample |
|
212 |
+ {160, 160, 160, 160, 160, 160, 160, 160, 185, 185, |
|
213 |
+ 185, 185, 193, 193, 193, 183, 183, 172, 138, 64, 38}, |
|
214 |
+ {240, 240, 240, 240, 240, 240, 240, 240, 207, 207, |
|
215 |
+ 207, 207, 204, 204, 204, 193, 193, 180, 143, 66, 40}, |
|
216 |
+ }, { // 480-sample |
|
217 |
+ {185, 185, 185, 185, 185, 185, 185, 185, 193, 193, |
|
218 |
+ 193, 193, 193, 193, 193, 183, 183, 172, 138, 65, 39}, |
|
219 |
+ {207, 207, 207, 207, 207, 207, 207, 207, 204, 204, |
|
220 |
+ 204, 204, 201, 201, 201, 188, 188, 176, 141, 66, 40}, |
|
221 |
+ }, { // 960-sample |
|
222 |
+ {193, 193, 193, 193, 193, 193, 193, 193, 193, 193, |
|
223 |
+ 193, 193, 194, 194, 194, 184, 184, 173, 139, 65, 39}, |
|
224 |
+ {204, 204, 204, 204, 204, 204, 204, 204, 201, 201, |
|
225 |
+ 201, 201, 198, 198, 198, 187, 187, 175, 140, 66, 40} |
|
226 |
+ } |
|
227 |
+}; |
|
228 |
+ |
|
229 |
+static const uint8_t celt_cache_bits[392] = { |
|
230 |
+ 40, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
|
231 |
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
|
232 |
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 40, 15, 23, 28, |
|
233 |
+ 31, 34, 36, 38, 39, 41, 42, 43, 44, 45, 46, 47, 47, 49, 50, |
|
234 |
+ 51, 52, 53, 54, 55, 55, 57, 58, 59, 60, 61, 62, 63, 63, 65, |
|
235 |
+ 66, 67, 68, 69, 70, 71, 71, 40, 20, 33, 41, 48, 53, 57, 61, |
|
236 |
+ 64, 66, 69, 71, 73, 75, 76, 78, 80, 82, 85, 87, 89, 91, 92, |
|
237 |
+ 94, 96, 98, 101, 103, 105, 107, 108, 110, 112, 114, 117, 119, 121, 123, |
|
238 |
+ 124, 126, 128, 40, 23, 39, 51, 60, 67, 73, 79, 83, 87, 91, 94, |
|
239 |
+ 97, 100, 102, 105, 107, 111, 115, 118, 121, 124, 126, 129, 131, 135, 139, |
|
240 |
+ 142, 145, 148, 150, 153, 155, 159, 163, 166, 169, 172, 174, 177, 179, 35, |
|
241 |
+ 28, 49, 65, 78, 89, 99, 107, 114, 120, 126, 132, 136, 141, 145, 149, |
|
242 |
+ 153, 159, 165, 171, 176, 180, 185, 189, 192, 199, 205, 211, 216, 220, 225, |
|
243 |
+ 229, 232, 239, 245, 251, 21, 33, 58, 79, 97, 112, 125, 137, 148, 157, |
|
244 |
+ 166, 174, 182, 189, 195, 201, 207, 217, 227, 235, 243, 251, 17, 35, 63, |
|
245 |
+ 86, 106, 123, 139, 152, 165, 177, 187, 197, 206, 214, 222, 230, 237, 250, |
|
246 |
+ 25, 31, 55, 75, 91, 105, 117, 128, 138, 146, 154, 161, 168, 174, 180, |
|
247 |
+ 185, 190, 200, 208, 215, 222, 229, 235, 240, 245, 255, 16, 36, 65, 89, |
|
248 |
+ 110, 128, 144, 159, 173, 185, 196, 207, 217, 226, 234, 242, 250, 11, 41, |
|
249 |
+ 74, 103, 128, 151, 172, 191, 209, 225, 241, 255, 9, 43, 79, 110, 138, |
|
250 |
+ 163, 186, 207, 227, 246, 12, 39, 71, 99, 123, 144, 164, 182, 198, 214, |
|
251 |
+ 228, 241, 253, 9, 44, 81, 113, 142, 168, 192, 214, 235, 255, 7, 49, |
|
252 |
+ 90, 127, 160, 191, 220, 247, 6, 51, 95, 134, 170, 203, 234, 7, 47, |
|
253 |
+ 87, 123, 155, 184, 212, 237, 6, 52, 97, 137, 174, 208, 240, 5, 57, |
|
254 |
+ 106, 151, 192, 231, 5, 59, 111, 158, 202, 243, 5, 55, 103, 147, 187, |
|
255 |
+ 224, 5, 60, 113, 161, 206, 248, 4, 65, 122, 175, 224, 4, 67, 127, |
|
256 |
+ 182, 234 |
|
257 |
+}; |
|
258 |
+ |
|
259 |
+static const int16_t celt_cache_index[105] = { |
|
260 |
+ -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 41, 41, 41, |
|
261 |
+ 82, 82, 123, 164, 200, 222, 0, 0, 0, 0, 0, 0, 0, 0, 41, |
|
262 |
+ 41, 41, 41, 123, 123, 123, 164, 164, 240, 266, 283, 295, 41, 41, 41, |
|
263 |
+ 41, 41, 41, 41, 41, 123, 123, 123, 123, 240, 240, 240, 266, 266, 305, |
|
264 |
+ 318, 328, 336, 123, 123, 123, 123, 123, 123, 123, 123, 240, 240, 240, 240, |
|
265 |
+ 305, 305, 305, 318, 318, 343, 351, 358, 364, 240, 240, 240, 240, 240, 240, |
|
266 |
+ 240, 240, 305, 305, 305, 305, 343, 343, 343, 351, 351, 370, 376, 382, 387, |
|
267 |
+}; |
|
268 |
+ |
|
269 |
+static const uint8_t celt_log2_frac[] = { |
|
270 |
+ 0, 8, 13, 16, 19, 21, 23, 24, 26, 27, 28, 29, 30, 31, 32, 32, 33, 34, 34, 35, 36, 36, 37, 37 |
|
271 |
+}; |
|
272 |
+ |
|
273 |
+static const uint8_t celt_bit_interleave[] = { |
|
274 |
+ 0, 1, 1, 1, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3 |
|
275 |
+}; |
|
276 |
+ |
|
277 |
+static const uint8_t celt_bit_deinterleave[] = { |
|
278 |
+ 0x00, 0x03, 0x0C, 0x0F, 0x30, 0x33, 0x3C, 0x3F, |
|
279 |
+ 0xC0, 0xC3, 0xCC, 0xCF, 0xF0, 0xF3, 0xFC, 0xFF |
|
280 |
+}; |
|
281 |
+ |
|
282 |
+static const uint8_t celt_hadamard_ordery[] = { |
|
283 |
+ 1, 0, |
|
284 |
+ 3, 0, 2, 1, |
|
285 |
+ 7, 0, 4, 3, 6, 1, 5, 2, |
|
286 |
+ 15, 0, 8, 7, 12, 3, 11, 4, 14, 1, 9, 6, 13, 2, 10, 5 |
|
287 |
+}; |
|
288 |
+ |
|
289 |
+static const uint16_t celt_qn_exp2[] = { |
|
290 |
+ 16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048 |
|
291 |
+}; |
|
292 |
+ |
|
293 |
+static const uint32_t celt_pvq_u[1272] = { |
|
294 |
+ /* N = 0, K = 0...176 */ |
|
295 |
+ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
296 |
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
297 |
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
298 |
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
299 |
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
300 |
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
301 |
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
302 |
+ /* N = 1, K = 1...176 */ |
|
303 |
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
304 |
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
305 |
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
306 |
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
307 |
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
308 |
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
309 |
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
310 |
+ /* N = 2, K = 2...176 */ |
|
311 |
+ 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, |
|
312 |
+ 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, |
|
313 |
+ 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, |
|
314 |
+ 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, |
|
315 |
+ 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, |
|
316 |
+ 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, |
|
317 |
+ 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, |
|
318 |
+ 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, 257, 259, 261, 263, |
|
319 |
+ 265, 267, 269, 271, 273, 275, 277, 279, 281, 283, 285, 287, 289, 291, 293, |
|
320 |
+ 295, 297, 299, 301, 303, 305, 307, 309, 311, 313, 315, 317, 319, 321, 323, |
|
321 |
+ 325, 327, 329, 331, 333, 335, 337, 339, 341, 343, 345, 347, 349, 351, |
|
322 |
+ /* N = 3, K = 3...176 */ |
|
323 |
+ 13, 25, 41, 61, 85, 113, 145, 181, 221, 265, 313, 365, 421, 481, 545, 613, |
|
324 |
+ 685, 761, 841, 925, 1013, 1105, 1201, 1301, 1405, 1513, 1625, 1741, 1861, |
|
325 |
+ 1985, 2113, 2245, 2381, 2521, 2665, 2813, 2965, 3121, 3281, 3445, 3613, 3785, |
|
326 |
+ 3961, 4141, 4325, 4513, 4705, 4901, 5101, 5305, 5513, 5725, 5941, 6161, 6385, |
|
327 |
+ 6613, 6845, 7081, 7321, 7565, 7813, 8065, 8321, 8581, 8845, 9113, 9385, 9661, |
|
328 |
+ 9941, 10225, 10513, 10805, 11101, 11401, 11705, 12013, 12325, 12641, 12961, |
|
329 |
+ 13285, 13613, 13945, 14281, 14621, 14965, 15313, 15665, 16021, 16381, 16745, |
|
330 |
+ 17113, 17485, 17861, 18241, 18625, 19013, 19405, 19801, 20201, 20605, 21013, |
|
331 |
+ 21425, 21841, 22261, 22685, 23113, 23545, 23981, 24421, 24865, 25313, 25765, |
|
332 |
+ 26221, 26681, 27145, 27613, 28085, 28561, 29041, 29525, 30013, 30505, 31001, |
|
333 |
+ 31501, 32005, 32513, 33025, 33541, 34061, 34585, 35113, 35645, 36181, 36721, |
|
334 |
+ 37265, 37813, 38365, 38921, 39481, 40045, 40613, 41185, 41761, 42341, 42925, |
|
335 |
+ 43513, 44105, 44701, 45301, 45905, 46513, 47125, 47741, 48361, 48985, 49613, |
|
336 |
+ 50245, 50881, 51521, 52165, 52813, 53465, 54121, 54781, 55445, 56113, 56785, |
|
337 |
+ 57461, 58141, 58825, 59513, 60205, 60901, 61601, |
|
338 |
+ /* N = 4, K = 4...176 */ |
|
339 |
+ 63, 129, 231, 377, 575, 833, 1159, 1561, 2047, 2625, 3303, 4089, 4991, 6017, |
|
340 |
+ 7175, 8473, 9919, 11521, 13287, 15225, 17343, 19649, 22151, 24857, 27775, |
|
341 |
+ 30913, 34279, 37881, 41727, 45825, 50183, 54809, 59711, 64897, 70375, 76153, |
|
342 |
+ 82239, 88641, 95367, 102425, 109823, 117569, 125671, 134137, 142975, 152193, |
|
343 |
+ 161799, 171801, 182207, 193025, 204263, 215929, 228031, 240577, 253575, |
|
344 |
+ 267033, 280959, 295361, 310247, 325625, 341503, 357889, 374791, 392217, |
|
345 |
+ 410175, 428673, 447719, 467321, 487487, 508225, 529543, 551449, 573951, |
|
346 |
+ 597057, 620775, 645113, 670079, 695681, 721927, 748825, 776383, 804609, |
|
347 |
+ 833511, 863097, 893375, 924353, 956039, 988441, 1021567, 1055425, 1090023, |
|
348 |
+ 1125369, 1161471, 1198337, 1235975, 1274393, 1313599, 1353601, 1394407, |
|
349 |
+ 1436025, 1478463, 1521729, 1565831, 1610777, 1656575, 1703233, 1750759, |
|
350 |
+ 1799161, 1848447, 1898625, 1949703, 2001689, 2054591, 2108417, 2163175, |
|
351 |
+ 2218873, 2275519, 2333121, 2391687, 2451225, 2511743, 2573249, 2635751, |
|
352 |
+ 2699257, 2763775, 2829313, 2895879, 2963481, 3032127, 3101825, 3172583, |
|
353 |
+ 3244409, 3317311, 3391297, 3466375, 3542553, 3619839, 3698241, 3777767, |
|
354 |
+ 3858425, 3940223, 4023169, 4107271, 4192537, 4278975, 4366593, 4455399, |
|
355 |
+ 4545401, 4636607, 4729025, 4822663, 4917529, 5013631, 5110977, 5209575, |
|
356 |
+ 5309433, 5410559, 5512961, 5616647, 5721625, 5827903, 5935489, 6044391, |
|
357 |
+ 6154617, 6266175, 6379073, 6493319, 6608921, 6725887, 6844225, 6963943, |
|
358 |
+ 7085049, 7207551, |
|
359 |
+ /* N = 5, K = 5...176 */ |
|
360 |
+ 321, 681, 1289, 2241, 3649, 5641, 8361, 11969, 16641, 22569, 29961, 39041, |
|
361 |
+ 50049, 63241, 78889, 97281, 118721, 143529, 172041, 204609, 241601, 283401, |
|
362 |
+ 330409, 383041, 441729, 506921, 579081, 658689, 746241, 842249, 947241, |
|
363 |
+ 1061761, 1186369, 1321641, 1468169, 1626561, 1797441, 1981449, 2179241, |
|
364 |
+ 2391489, 2618881, 2862121, 3121929, 3399041, 3694209, 4008201, 4341801, |
|
365 |
+ 4695809, 5071041, 5468329, 5888521, 6332481, 6801089, 7295241, 7815849, |
|
366 |
+ 8363841, 8940161, 9545769, 10181641, 10848769, 11548161, 12280841, 13047849, |
|
367 |
+ 13850241, 14689089, 15565481, 16480521, 17435329, 18431041, 19468809, |
|
368 |
+ 20549801, 21675201, 22846209, 24064041, 25329929, 26645121, 28010881, |
|
369 |
+ 29428489, 30899241, 32424449, 34005441, 35643561, 37340169, 39096641, |
|
370 |
+ 40914369, 42794761, 44739241, 46749249, 48826241, 50971689, 53187081, |
|
371 |
+ 55473921, 57833729, 60268041, 62778409, 65366401, 68033601, 70781609, |
|
372 |
+ 73612041, 76526529, 79526721, 82614281, 85790889, 89058241, 92418049, |
|
373 |
+ 95872041, 99421961, 103069569, 106816641, 110664969, 114616361, 118672641, |
|
374 |
+ 122835649, 127107241, 131489289, 135983681, 140592321, 145317129, 150160041, |
|
375 |
+ 155123009, 160208001, 165417001, 170752009, 176215041, 181808129, 187533321, |
|
376 |
+ 193392681, 199388289, 205522241, 211796649, 218213641, 224775361, 231483969, |
|
377 |
+ 238341641, 245350569, 252512961, 259831041, 267307049, 274943241, 282741889, |
|
378 |
+ 290705281, 298835721, 307135529, 315607041, 324252609, 333074601, 342075401, |
|
379 |
+ 351257409, 360623041, 370174729, 379914921, 389846081, 399970689, 410291241, |
|
380 |
+ 420810249, 431530241, 442453761, 453583369, 464921641, 476471169, 488234561, |
|
381 |
+ 500214441, 512413449, 524834241, 537479489, 550351881, 563454121, 576788929, |
|
382 |
+ 590359041, 604167209, 618216201, 632508801, |
|
383 |
+ /* N = 6, K = 6...96 (technically V(109,5) fits in 32 bits, but that can't be |
|
384 |
+ achieved by splitting an Opus band) */ |
|
385 |
+ 1683, 3653, 7183, 13073, 22363, 36365, 56695, 85305, 124515, 177045, 246047, |
|
386 |
+ 335137, 448427, 590557, 766727, 982729, 1244979, 1560549, 1937199, 2383409, |
|
387 |
+ 2908411, 3522221, 4235671, 5060441, 6009091, 7095093, 8332863, 9737793, |
|
388 |
+ 11326283, 13115773, 15124775, 17372905, 19880915, 22670725, 25765455, |
|
389 |
+ 29189457, 32968347, 37129037, 41699767, 46710137, 52191139, 58175189, |
|
390 |
+ 64696159, 71789409, 79491819, 87841821, 96879431, 106646281, 117185651, |
|
391 |
+ 128542501, 140763503, 153897073, 167993403, 183104493, 199284183, 216588185, |
|
392 |
+ 235074115, 254801525, 275831935, 298228865, 322057867, 347386557, 374284647, |
|
393 |
+ 402823977, 433078547, 465124549, 499040399, 534906769, 572806619, 612825229, |
|
394 |
+ 655050231, 699571641, 746481891, 795875861, 847850911, 902506913, 959946283, |
|
395 |
+ 1020274013, 1083597703, 1150027593, 1219676595, 1292660325, 1369097135, |
|
396 |
+ 1449108145, 1532817275, 1620351277, 1711839767, 1807415257, 1907213187, |
|
397 |
+ 2011371957, 2120032959, |
|
398 |
+ /* N = 7, K = 7...54 (technically V(60,6) fits in 32 bits, but that can't be |
|
399 |
+ achieved by splitting an Opus band) */ |
|
400 |
+ 8989, 19825, 40081, 75517, 134245, 227305, 369305, 579125, 880685, 1303777, |
|
401 |
+ 1884961, 2668525, 3707509, 5064793, 6814249, 9041957, 11847485, 15345233, |
|
402 |
+ 19665841, 24957661, 31388293, 39146185, 48442297, 59511829, 72616013, |
|
403 |
+ 88043969, 106114625, 127178701, 151620757, 179861305, 212358985, 249612805, |
|
404 |
+ 292164445, 340600625, 395555537, 457713341, 527810725, 606639529, 695049433, |
|
405 |
+ 793950709, 904317037, 1027188385, 1163673953, 1314955181, 1482288821, |
|
406 |
+ 1667010073, 1870535785, 2094367717, |
|
407 |
+ /* N = 8, K = 8...37 (technically V(40,7) fits in 32 bits, but that can't be |
|
408 |
+ achieved by splitting an Opus band) */ |
|
409 |
+ 48639, 108545, 224143, 433905, 795455, 1392065, 2340495, 3800305, 5984767, |
|
410 |
+ 9173505, 13726991, 20103025, 28875327, 40754369, 56610575, 77500017, |
|
411 |
+ 104692735, 139703809, 184327311, 240673265, 311207743, 398796225, 506750351, |
|
412 |
+ 638878193, 799538175, 993696769, 1226990095, 1505789553, 1837271615, |
|
413 |
+ 2229491905, |
|
414 |
+ /* N = 9, K = 9...28 (technically V(29,8) fits in 32 bits, but that can't be |
|
415 |
+ achieved by splitting an Opus band) */ |
|
416 |
+ 265729, 598417, 1256465, 2485825, 4673345, 8405905, 14546705, 24331777, |
|
417 |
+ 39490049, 62390545, 96220561, 145198913, 214828609, 312193553, 446304145, |
|
418 |
+ 628496897, 872893441, 1196924561, 1621925137, 2173806145, |
|
419 |
+ /* N = 10, K = 10...24 */ |
|
420 |
+ 1462563, 3317445, 7059735, 14218905, 27298155, 50250765, 89129247, 152951073, |
|
421 |
+ 254831667, 413442773, 654862247, 1014889769, 1541911931, 2300409629, |
|
422 |
+ 3375210671, |
|
423 |
+ /* N = 11, K = 11...19 (technically V(20,10) fits in 32 bits, but that can't be |
|
424 |
+ achieved by splitting an Opus band) */ |
|
425 |
+ 8097453, 18474633, 39753273, 81270333, 158819253, 298199265, 540279585, |
|
426 |
+ 948062325, 1616336765, |
|
427 |
+ /* N = 12, K = 12...18 */ |
|
428 |
+ 45046719, 103274625, 224298231, 464387817, 921406335, 1759885185, |
|
429 |
+ 3248227095, |
|
430 |
+ /* N = 13, K = 13...16 */ |
|
431 |
+ 251595969, 579168825, 1267854873, 2653649025, |
|
432 |
+ /* N = 14, K = 14 */ |
|
433 |
+ 1409933619 |
|
434 |
+}; |
|
435 |
+ |
|
436 |
+DECLARE_ALIGNED(32, static const float, celt_window)[120] = { |
|
437 |
+ 6.7286966e-05f, 0.00060551348f, 0.0016815970f, 0.0032947962f, 0.0054439943f, |
|
438 |
+ 0.0081276923f, 0.011344001f, 0.015090633f, 0.019364886f, 0.024163635f, |
|
439 |
+ 0.029483315f, 0.035319905f, 0.041668911f, 0.048525347f, 0.055883718f, |
|
440 |
+ 0.063737999f, 0.072081616f, 0.080907428f, 0.090207705f, 0.099974111f, |
|
441 |
+ 0.11019769f, 0.12086883f, 0.13197729f, 0.14351214f, 0.15546177f, |
|
442 |
+ 0.16781389f, 0.18055550f, 0.19367290f, 0.20715171f, 0.22097682f, |
|
443 |
+ 0.23513243f, 0.24960208f, 0.26436860f, 0.27941419f, 0.29472040f, |
|
444 |
+ 0.31026818f, 0.32603788f, 0.34200931f, 0.35816177f, 0.37447407f, |
|
445 |
+ 0.39092462f, 0.40749142f, 0.42415215f, 0.44088423f, 0.45766484f, |
|
446 |
+ 0.47447104f, 0.49127978f, 0.50806798f, 0.52481261f, 0.54149077f, |
|
447 |
+ 0.55807973f, 0.57455701f, 0.59090049f, 0.60708841f, 0.62309951f, |
|
448 |
+ 0.63891306f, 0.65450896f, 0.66986776f, 0.68497077f, 0.69980010f, |
|
449 |
+ 0.71433873f, 0.72857055f, 0.74248043f, 0.75605424f, 0.76927895f, |
|
450 |
+ 0.78214257f, 0.79463430f, 0.80674445f, 0.81846456f, 0.82978733f, |
|
451 |
+ 0.84070669f, 0.85121779f, 0.86131698f, 0.87100183f, 0.88027111f, |
|
452 |
+ 0.88912479f, 0.89756398f, 0.90559094f, 0.91320904f, 0.92042270f, |
|
453 |
+ 0.92723738f, 0.93365955f, 0.93969656f, 0.94535671f, 0.95064907f, |
|
454 |
+ 0.95558353f, 0.96017067f, 0.96442171f, 0.96834849f, 0.97196334f, |
|
455 |
+ 0.97527906f, 0.97830883f, 0.98106616f, 0.98356480f, 0.98581869f, |
|
456 |
+ 0.98784191f, 0.98964856f, 0.99125274f, 0.99266849f, 0.99390969f, |
|
457 |
+ 0.99499004f, 0.99592297f, 0.99672162f, 0.99739874f, 0.99796667f, |
|
458 |
+ 0.99843728f, 0.99882195f, 0.99913147f, 0.99937606f, 0.99956527f, |
|
459 |
+ 0.99970802f, 0.99981248f, 0.99988613f, 0.99993565f, 0.99996697f, |
|
460 |
+ 0.99998518f, 0.99999457f, 0.99999859f, 0.99999982f, 1.0000000f, |
|
461 |
+}; |
|
462 |
+ |
|
463 |
+/* square of the window, used for the postfilter */ |
|
464 |
+const float ff_celt_window2[120] = { |
|
465 |
+ 4.5275357e-09f, 3.66647e-07f, 2.82777e-06f, 1.08557e-05f, 2.96371e-05f, 6.60594e-05f, |
|
466 |
+ 0.000128686f, 0.000227727f, 0.000374999f, 0.000583881f, 0.000869266f, 0.0012475f, |
|
467 |
+ 0.0017363f, 0.00235471f, 0.00312299f, 0.00406253f, 0.00519576f, 0.00654601f, |
|
468 |
+ 0.00813743f, 0.00999482f, 0.0121435f, 0.0146093f, 0.017418f, 0.0205957f, 0.0241684f, |
|
469 |
+ 0.0281615f, 0.0326003f, 0.0375092f, 0.0429118f, 0.0488308f, 0.0552873f, 0.0623012f, |
|
470 |
+ 0.0698908f, 0.0780723f, 0.0868601f, 0.0962664f, 0.106301f, 0.11697f, 0.12828f, |
|
471 |
+ 0.140231f, 0.152822f, 0.166049f, 0.179905f, 0.194379f, 0.209457f, 0.225123f, 0.241356f, |
|
472 |
+ 0.258133f, 0.275428f, 0.293212f, 0.311453f, 0.330116f, 0.349163f, 0.368556f, 0.388253f, |
|
473 |
+ 0.40821f, 0.428382f, 0.448723f, 0.469185f, 0.48972f, 0.51028f, 0.530815f, 0.551277f, |
|
474 |
+ 0.571618f, 0.59179f, 0.611747f, 0.631444f, 0.650837f, 0.669884f, 0.688547f, 0.706788f, |
|
475 |
+ 0.724572f, 0.741867f, 0.758644f, 0.774877f, 0.790543f, 0.805621f, 0.820095f, 0.833951f, |
|
476 |
+ 0.847178f, 0.859769f, 0.87172f, 0.88303f, 0.893699f, 0.903734f, 0.91314f, 0.921928f, |
|
477 |
+ 0.930109f, 0.937699f, 0.944713f, 0.951169f, 0.957088f, 0.962491f, 0.9674f, 0.971838f, |
|
478 |
+ 0.975832f, 0.979404f, 0.982582f, 0.985391f, 0.987857f, 0.990005f, 0.991863f, 0.993454f, |
|
479 |
+ 0.994804f, 0.995937f, 0.996877f, 0.997645f, 0.998264f, 0.998753f, 0.999131f, 0.999416f, |
|
480 |
+ 0.999625f, 0.999772f, 0.999871f, 0.999934f, 0.99997f, 0.999989f, 0.999997f, 0.99999964f, 1.0f, |
|
481 |
+}; |
|
482 |
+ |
|
483 |
+static const uint32_t * const celt_pvq_u_row[15] = { |
|
484 |
+ celt_pvq_u + 0, celt_pvq_u + 176, celt_pvq_u + 351, |
|
485 |
+ celt_pvq_u + 525, celt_pvq_u + 698, celt_pvq_u + 870, |
|
486 |
+ celt_pvq_u + 1041, celt_pvq_u + 1131, celt_pvq_u + 1178, |
|
487 |
+ celt_pvq_u + 1207, celt_pvq_u + 1226, celt_pvq_u + 1240, |
|
488 |
+ celt_pvq_u + 1248, celt_pvq_u + 1254, celt_pvq_u + 1257 |
|
489 |
+}; |
|
490 |
+ |
|
491 |
+static inline int16_t celt_cos(int16_t x) |
|
492 |
+{ |
|
493 |
+ x = (MUL16(x, x) + 4096) >> 13; |
|
494 |
+ x = (32767-x) + ROUND_MUL16(x, (-7651 + ROUND_MUL16(x, (8277 + ROUND_MUL16(-626, x))))); |
|
495 |
+ return 1+x; |
|
496 |
+} |
|
497 |
+ |
|
498 |
+static inline int celt_log2tan(int isin, int icos) |
|
499 |
+{ |
|
500 |
+ int lc, ls; |
|
501 |
+ lc = opus_ilog(icos); |
|
502 |
+ ls = opus_ilog(isin); |
|
503 |
+ icos <<= 15 - lc; |
|
504 |
+ isin <<= 15 - ls; |
|
505 |
+ return (ls << 11) - (lc << 11) + |
|
506 |
+ ROUND_MUL16(isin, ROUND_MUL16(isin, -2597) + 7932) - |
|
507 |
+ ROUND_MUL16(icos, ROUND_MUL16(icos, -2597) + 7932); |
|
508 |
+} |
|
509 |
+ |
|
510 |
+static inline uint32_t celt_rng(CeltContext *s) |
|
511 |
+{ |
|
512 |
+ s->seed = 1664525 * s->seed + 1013904223; |
|
513 |
+ return s->seed; |
|
514 |
+} |
|
515 |
+ |
|
516 |
+static void celt_decode_coarse_energy(CeltContext *s, OpusRangeCoder *rc) |
|
517 |
+{ |
|
518 |
+ int i, j; |
|
519 |
+ float prev[2] = {0}; |
|
520 |
+ float alpha, beta; |
|
521 |
+ const uint8_t *model; |
|
522 |
+ |
|
523 |
+ /* use the 2D z-transform to apply prediction in both */ |
|
524 |
+ /* the time domain (alpha) and the frequency domain (beta) */ |
|
525 |
+ |
|
526 |
+ if (opus_rc_tell(rc)+3 <= s->framebits && opus_rc_p2model(rc, 3)) { |
|
527 |
+ /* intra frame */ |
|
528 |
+ alpha = 0; |
|
529 |
+ beta = 1.0f - 4915.0f/32768.0f; |
|
530 |
+ model = celt_coarse_energy_dist[s->duration][1]; |
|
531 |
+ } else { |
|
532 |
+ alpha = celt_alpha_coef[s->duration]; |
|
533 |
+ beta = 1.0f - celt_beta_coef[s->duration]; |
|
534 |
+ model = celt_coarse_energy_dist[s->duration][0]; |
|
535 |
+ } |
|
536 |
+ |
|
537 |
+ for (i = 0; i < CELT_MAX_BANDS; i++) { |
|
538 |
+ for (j = 0; j < s->coded_channels; j++) { |
|
539 |
+ CeltFrame *frame = &s->frame[j]; |
|
540 |
+ float value; |
|
541 |
+ int available; |
|
542 |
+ |
|
543 |
+ if (i < s->startband || i >= s->endband) { |
|
544 |
+ frame->energy[i] = 0.0; |
|
545 |
+ continue; |
|
546 |
+ } |
|
547 |
+ |
|
548 |
+ available = s->framebits - opus_rc_tell(rc); |
|
549 |
+ if (available >= 15) { |
|
550 |
+ /* decode using a Laplace distribution */ |
|
551 |
+ int k = FFMIN(i, 20) << 1; |
|
552 |
+ value = opus_rc_laplace(rc, model[k] << 7, model[k+1] << 6); |
|
553 |
+ } else if (available >= 2) { |
|
554 |
+ int x = opus_rc_getsymbol(rc, celt_model_energy_small); |
|
555 |
+ value = (x>>1) ^ -(x&1); |
|
556 |
+ } else if (available >= 1) { |
|
557 |
+ value = -(float)opus_rc_p2model(rc, 1); |
|
558 |
+ } else value = -1; |
|
559 |
+ |
|
560 |
+ frame->energy[i] = FFMAX(-9.0f, frame->energy[i]) * alpha + prev[j] + value; |
|
561 |
+ prev[j] += beta * value; |
|
562 |
+ } |
|
563 |
+ } |
|
564 |
+} |
|
565 |
+ |
|
566 |
+static void celt_decode_fine_energy(CeltContext *s, OpusRangeCoder *rc) |
|
567 |
+{ |
|
568 |
+ int i; |
|
569 |
+ for (i = s->startband; i < s->endband; i++) { |
|
570 |
+ int j; |
|
571 |
+ if (!s->fine_bits[i]) |
|
572 |
+ continue; |
|
573 |
+ |
|
574 |
+ for (j = 0; j < s->coded_channels; j++) { |
|
575 |
+ CeltFrame *frame = &s->frame[j]; |
|
576 |
+ int q2; |
|
577 |
+ float offset; |
|
578 |
+ q2 = opus_getrawbits(rc, s->fine_bits[i]); |
|
579 |
+ offset = (q2 + 0.5f) * (1 << (14 - s->fine_bits[i])) / 16384.0f - 0.5f; |
|
580 |
+ frame->energy[i] += offset; |
|
581 |
+ } |
|
582 |
+ } |
|
583 |
+} |
|
584 |
+ |
|
585 |
+static void celt_decode_final_energy(CeltContext *s, OpusRangeCoder *rc, |
|
586 |
+ int bits_left) |
|
587 |
+{ |
|
588 |
+ int priority, i, j; |
|
589 |
+ |
|
590 |
+ for (priority = 0; priority < 2; priority++) { |
|
591 |
+ for (i = s->startband; i < s->endband && bits_left >= s->coded_channels; i++) { |
|
592 |
+ if (s->fine_priority[i] != priority || s->fine_bits[i] >= CELT_MAX_FINE_BITS) |
|
593 |
+ continue; |
|
594 |
+ |
|
595 |
+ for (j = 0; j < s->coded_channels; j++) { |
|
596 |
+ int q2; |
|
597 |
+ float offset; |
|
598 |
+ q2 = opus_getrawbits(rc, 1); |
|
599 |
+ offset = (q2 - 0.5f) * (1 << (14 - s->fine_bits[i] - 1)) / 16384.0f; |
|
600 |
+ s->frame[j].energy[i] += offset; |
|
601 |
+ bits_left--; |
|
602 |
+ } |
|
603 |
+ } |
|
604 |
+ } |
|
605 |
+} |
|
606 |
+ |
|
607 |
+static void celt_decode_tf_changes(CeltContext *s, OpusRangeCoder *rc, |
|
608 |
+ int transient) |
|
609 |
+{ |
|
610 |
+ int i, diff = 0, tf_select = 0, tf_changed = 0, tf_select_bit; |
|
611 |
+ int consumed, bits = transient ? 2 : 4; |
|
612 |
+ |
|
613 |
+ consumed = opus_rc_tell(rc); |
|
614 |
+ tf_select_bit = (s->duration != 0 && consumed+bits+1 <= s->framebits); |
|
615 |
+ |
|
616 |
+ for (i = s->startband; i < s->endband; i++) { |
|
617 |
+ if (consumed+bits+tf_select_bit <= s->framebits) { |
|
618 |
+ diff ^= opus_rc_p2model(rc, bits); |
|
619 |
+ consumed = opus_rc_tell(rc); |
|
620 |
+ tf_changed |= diff; |
|
621 |
+ } |
|
622 |
+ s->tf_change[i] = diff; |
|
623 |
+ bits = transient ? 4 : 5; |
|
624 |
+ } |
|
625 |
+ |
|
626 |
+ if (tf_select_bit && celt_tf_select[s->duration][transient][0][tf_changed] != |
|
627 |
+ celt_tf_select[s->duration][transient][1][tf_changed]) |
|
628 |
+ tf_select = opus_rc_p2model(rc, 1); |
|
629 |
+ |
|
630 |
+ for (i = s->startband; i < s->endband; i++) { |
|
631 |
+ s->tf_change[i] = celt_tf_select[s->duration][transient][tf_select][s->tf_change[i]]; |
|
632 |
+ } |
|
633 |
+} |
|
634 |
+ |
|
635 |
+static void celt_decode_allocation(CeltContext *s, OpusRangeCoder *rc) |
|
636 |
+{ |
|
637 |
+ // approx. maximum bit allocation for each band before boost/trim |
|
638 |
+ int cap[CELT_MAX_BANDS]; |
|
639 |
+ int boost[CELT_MAX_BANDS]; |
|
640 |
+ int threshold[CELT_MAX_BANDS]; |
|
641 |
+ int bits1[CELT_MAX_BANDS]; |
|
642 |
+ int bits2[CELT_MAX_BANDS]; |
|
643 |
+ int trim_offset[CELT_MAX_BANDS]; |
|
644 |
+ |
|
645 |
+ int skip_startband = s->startband; |
|
646 |
+ int dynalloc = 6; |
|
647 |
+ int alloctrim = 5; |
|
648 |
+ int extrabits = 0; |
|
649 |
+ |
|
650 |
+ int skip_bit = 0; |
|
651 |
+ int intensitystereo_bit = 0; |
|
652 |
+ int dualstereo_bit = 0; |
|
653 |
+ |
|
654 |
+ int remaining, bandbits; |
|
655 |
+ int low, high, total, done; |
|
656 |
+ int totalbits; |
|
657 |
+ int consumed; |
|
658 |
+ int i, j; |
|
659 |
+ |
|
660 |
+ consumed = opus_rc_tell(rc); |
|
661 |
+ |
|
662 |
+ /* obtain spread flag */ |
|
663 |
+ s->spread = CELT_SPREAD_NORMAL; |
|
664 |
+ if (consumed + 4 <= s->framebits) |
|
665 |
+ s->spread = opus_rc_getsymbol(rc, celt_model_spread); |
|
666 |
+ |
|
667 |
+ /* generate static allocation caps */ |
|
668 |
+ for (i = 0; i < CELT_MAX_BANDS; i++) { |
|
669 |
+ cap[i] = (celt_static_caps[s->duration][s->coded_channels - 1][i] + 64) |
|
670 |
+ * celt_freq_range[i] << (s->coded_channels - 1) << s->duration >> 2; |
|
671 |
+ } |
|
672 |
+ |
|
673 |
+ /* obtain band boost */ |
|
674 |
+ totalbits = s->framebits << 3; // convert to 1/8 bits |
|
675 |
+ consumed = opus_rc_tell_frac(rc); |
|
676 |
+ for (i = s->startband; i < s->endband; i++) { |
|
677 |
+ int quanta, band_dynalloc; |
|
678 |
+ |
|
679 |
+ boost[i] = 0; |
|
680 |
+ |
|
681 |
+ quanta = celt_freq_range[i] << (s->coded_channels - 1) << s->duration; |
|
682 |
+ quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta)); |
|
683 |
+ band_dynalloc = dynalloc; |
|
684 |
+ while (consumed + (band_dynalloc<<3) < totalbits && boost[i] < cap[i]) { |
|
685 |
+ int add = opus_rc_p2model(rc, band_dynalloc); |
|
686 |
+ consumed = opus_rc_tell_frac(rc); |
|
687 |
+ if (!add) |
|
688 |
+ break; |
|
689 |
+ |
|
690 |
+ boost[i] += quanta; |
|
691 |
+ totalbits -= quanta; |
|
692 |
+ band_dynalloc = 1; |
|
693 |
+ } |
|
694 |
+ /* dynalloc is more likely to occur if it's already been used for earlier bands */ |
|
695 |
+ if (boost[i]) |
|
696 |
+ dynalloc = FFMAX(2, dynalloc - 1); |
|
697 |
+ } |
|
698 |
+ |
|
699 |
+ /* obtain allocation trim */ |
|
700 |
+ if (consumed + (6 << 3) <= totalbits) |
|
701 |
+ alloctrim = opus_rc_getsymbol(rc, celt_model_alloc_trim); |
|
702 |
+ |
|
703 |
+ /* anti-collapse bit reservation */ |
|
704 |
+ totalbits = (s->framebits << 3) - opus_rc_tell_frac(rc) - 1; |
|
705 |
+ s->anticollapse_bit = 0; |
|
706 |
+ if (s->blocks > 1 && s->duration >= 2 && |
|
707 |
+ totalbits >= ((s->duration + 2) << 3)) |
|
708 |
+ s->anticollapse_bit = 1 << 3; |
|
709 |
+ totalbits -= s->anticollapse_bit; |
|
710 |
+ |
|
711 |
+ /* band skip bit reservation */ |
|
712 |
+ if (totalbits >= 1 << 3) |
|
713 |
+ skip_bit = 1 << 3; |
|
714 |
+ totalbits -= skip_bit; |
|
715 |
+ |
|
716 |
+ /* intensity/dual stereo bit reservation */ |
|
717 |
+ if (s->coded_channels == 2) { |
|
718 |
+ intensitystereo_bit = celt_log2_frac[s->endband - s->startband]; |
|
719 |
+ if (intensitystereo_bit <= totalbits) { |
|
720 |
+ totalbits -= intensitystereo_bit; |
|
721 |
+ if (totalbits >= 1 << 3) { |
|
722 |
+ dualstereo_bit = 1 << 3; |
|
723 |
+ totalbits -= 1 << 3; |
|
724 |
+ } |
|
725 |
+ } else |
|
726 |
+ intensitystereo_bit = 0; |
|
727 |
+ } |
|
728 |
+ |
|
729 |
+ for (i = s->startband; i < s->endband; i++) { |
|
730 |
+ int trim = alloctrim - 5 - s->duration; |
|
731 |
+ int band = celt_freq_range[i] * (s->endband - i - 1); |
|
732 |
+ int duration = s->duration + 3; |
|
733 |
+ int scale = duration + s->coded_channels - 1; |
|
734 |
+ |
|
735 |
+ /* PVQ minimum allocation threshold, below this value the band is |
|
736 |
+ * skipped */ |
|
737 |
+ threshold[i] = FFMAX(3 * celt_freq_range[i] << duration >> 4, |
|
738 |
+ s->coded_channels << 3); |
|
739 |
+ |
|
740 |
+ trim_offset[i] = trim * (band << scale) >> 6; |
|
741 |
+ |
|
742 |
+ if (celt_freq_range[i] << s->duration == 1) |
|
743 |
+ trim_offset[i] -= s->coded_channels << 3; |
|
744 |
+ } |
|
745 |
+ |
|
746 |
+ /* bisection */ |
|
747 |
+ low = 1; |
|
748 |
+ high = CELT_VECTORS - 1; |
|
749 |
+ while (low <= high) { |
|
750 |
+ int center = (low + high) >> 1; |
|
751 |
+ done = total = 0; |
|
752 |
+ |
|
753 |
+ for (i = s->endband - 1; i >= s->startband; i--) { |
|
754 |
+ bandbits = celt_freq_range[i] * celt_static_alloc[center][i] |
|
755 |
+ << (s->coded_channels - 1) << s->duration >> 2; |
|
756 |
+ |
|
757 |
+ if (bandbits) |
|
758 |
+ bandbits = FFMAX(0, bandbits + trim_offset[i]); |
|
759 |
+ bandbits += boost[i]; |
|
760 |
+ |
|
761 |
+ if (bandbits >= threshold[i] || done) { |
|
762 |
+ done = 1; |
|
763 |
+ total += FFMIN(bandbits, cap[i]); |
|
764 |
+ } else if (bandbits >= s->coded_channels << 3) |
|
765 |
+ total += s->coded_channels << 3; |
|
766 |
+ } |
|
767 |
+ |
|
768 |
+ if (total > totalbits) |
|
769 |
+ high = center - 1; |
|
770 |
+ else |
|
771 |
+ low = center + 1; |
|
772 |
+ } |
|
773 |
+ high = low--; |
|
774 |
+ |
|
775 |
+ for (i = s->startband; i < s->endband; i++) { |
|
776 |
+ bits1[i] = celt_freq_range[i] * celt_static_alloc[low][i] |
|
777 |
+ << (s->coded_channels - 1) << s->duration >> 2; |
|
778 |
+ bits2[i] = high >= CELT_VECTORS ? cap[i] : |
|
779 |
+ celt_freq_range[i] * celt_static_alloc[high][i] |
|
780 |
+ << (s->coded_channels - 1) << s->duration >> 2; |
|
781 |
+ |
|
782 |
+ if (bits1[i]) |
|
783 |
+ bits1[i] = FFMAX(0, bits1[i] + trim_offset[i]); |
|
784 |
+ if (bits2[i]) |
|
785 |
+ bits2[i] = FFMAX(0, bits2[i] + trim_offset[i]); |
|
786 |
+ if (low) |
|
787 |
+ bits1[i] += boost[i]; |
|
788 |
+ bits2[i] += boost[i]; |
|
789 |
+ |
|
790 |
+ if (boost[i]) |
|
791 |
+ skip_startband = i; |
|
792 |
+ bits2[i] = FFMAX(0, bits2[i] - bits1[i]); |
|
793 |
+ } |
|
794 |
+ |
|
795 |
+ /* bisection */ |
|
796 |
+ low = 0; |
|
797 |
+ high = 1 << CELT_ALLOC_STEPS; |
|
798 |
+ for (i = 0; i < CELT_ALLOC_STEPS; i++) { |
|
799 |
+ int center = (low + high) >> 1; |
|
800 |
+ done = total = 0; |
|
801 |
+ |
|
802 |
+ for (j = s->endband - 1; j >= s->startband; j--) { |
|
803 |
+ bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS); |
|
804 |
+ |
|
805 |
+ if (bandbits >= threshold[j] || done) { |
|
806 |
+ done = 1; |
|
807 |
+ total += FFMIN(bandbits, cap[j]); |
|
808 |
+ } else if (bandbits >= s->coded_channels << 3) |
|
809 |
+ total += s->coded_channels << 3; |
|
810 |
+ } |
|
811 |
+ if (total > totalbits) |
|
812 |
+ high = center; |
|
813 |
+ else |
|
814 |
+ low = center; |
|
815 |
+ } |
|
816 |
+ |
|
817 |
+ done = total = 0; |
|
818 |
+ for (i = s->endband - 1; i >= s->startband; i--) { |
|
819 |
+ bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS); |
|
820 |
+ |
|
821 |
+ if (bandbits >= threshold[i] || done) |
|
822 |
+ done = 1; |
|
823 |
+ else |
|
824 |
+ bandbits = (bandbits >= s->coded_channels << 3) ? |
|
825 |
+ s->coded_channels << 3 : 0; |
|
826 |
+ |
|
827 |
+ bandbits = FFMIN(bandbits, cap[i]); |
|
828 |
+ s->pulses[i] = bandbits; |
|
829 |
+ total += bandbits; |
|
830 |
+ } |
|
831 |
+ |
|
832 |
+ /* band skipping */ |
|
833 |
+ for (s->codedbands = s->endband; ; s->codedbands--) { |
|
834 |
+ int allocation; |
|
835 |
+ j = s->codedbands - 1; |
|
836 |
+ |
|
837 |
+ if (j == skip_startband) { |
|
838 |
+ /* all remaining bands are not skipped */ |
|
839 |
+ totalbits += skip_bit; |
|
840 |
+ break; |
|
841 |
+ } |
|
842 |
+ |
|
843 |
+ /* determine the number of bits available for coding "do not skip" markers */ |
|
844 |
+ remaining = totalbits - total; |
|
845 |
+ bandbits = remaining / (celt_freq_bands[j+1] - celt_freq_bands[s->startband]); |
|
846 |
+ remaining -= bandbits * (celt_freq_bands[j+1] - celt_freq_bands[s->startband]); |
|
847 |
+ allocation = s->pulses[j] + bandbits * celt_freq_range[j] |
|
848 |
+ + FFMAX(0, remaining - (celt_freq_bands[j] - celt_freq_bands[s->startband])); |
|
849 |
+ |
|
850 |
+ /* a "do not skip" marker is only coded if the allocation is |
|
851 |
+ above the chosen threshold */ |
|
852 |
+ if (allocation >= FFMAX(threshold[j], (s->coded_channels + 1) <<3 )) { |
|
853 |
+ if (opus_rc_p2model(rc, 1)) |
|
854 |
+ break; |
|
855 |
+ |
|
856 |
+ total += 1 << 3; |
|
857 |
+ allocation -= 1 << 3; |
|
858 |
+ } |
|
859 |
+ |
|
860 |
+ /* the band is skipped, so reclaim its bits */ |
|
861 |
+ total -= s->pulses[j]; |
|
862 |
+ if (intensitystereo_bit) { |
|
863 |
+ total -= intensitystereo_bit; |
|
864 |
+ intensitystereo_bit = celt_log2_frac[j - s->startband]; |
|
865 |
+ total += intensitystereo_bit; |
|
866 |
+ } |
|
867 |
+ |
|
868 |
+ total += s->pulses[j] = (allocation >= s->coded_channels << 3) ? |
|
869 |
+ s->coded_channels << 3 : 0; |
|
870 |
+ } |
|
871 |
+ |
|
872 |
+ /* obtain stereo flags */ |
|
873 |
+ s->intensitystereo = 0; |
|
874 |
+ s->dualstereo = 0; |
|
875 |
+ if (intensitystereo_bit) |
|
876 |
+ s->intensitystereo = s->startband + |
|
877 |
+ opus_rc_unimodel(rc, s->codedbands + 1 - s->startband); |
|
878 |
+ if (s->intensitystereo <= s->startband) |
|
879 |
+ totalbits += dualstereo_bit; /* no intensity stereo means no dual stereo */ |
|
880 |
+ else if (dualstereo_bit) |
|
881 |
+ s->dualstereo = opus_rc_p2model(rc, 1); |
|
882 |
+ |
|
883 |
+ /* supply the remaining bits in this frame to lower bands */ |
|
884 |
+ remaining = totalbits - total; |
|
885 |
+ bandbits = remaining / (celt_freq_bands[s->codedbands] - celt_freq_bands[s->startband]); |
|
886 |
+ remaining -= bandbits * (celt_freq_bands[s->codedbands] - celt_freq_bands[s->startband]); |
|
887 |
+ for (i = s->startband; i < s->codedbands; i++) { |
|
888 |
+ int bits = FFMIN(remaining, celt_freq_range[i]); |
|
889 |
+ |
|
890 |
+ s->pulses[i] += bits + bandbits * celt_freq_range[i]; |
|
891 |
+ remaining -= bits; |
|
892 |
+ } |
|
893 |
+ |
|
894 |
+ for (i = s->startband; i < s->codedbands; i++) { |
|
895 |
+ int N = celt_freq_range[i] << s->duration; |
|
896 |
+ int prev_extra = extrabits; |
|
897 |
+ s->pulses[i] += extrabits; |
|
898 |
+ |
|
899 |
+ if (N > 1) { |
|
900 |
+ int dof; // degrees of freedom |
|
901 |
+ int temp; // dof * channels * log(dof) |
|
902 |
+ int offset; // fine energy quantization offset, i.e. |
|
903 |
+ // extra bits assigned over the standard |
|
904 |
+ // totalbits/dof |
|
905 |
+ int fine_bits, max_bits; |
|
906 |
+ |
|
907 |
+ extrabits = FFMAX(0, s->pulses[i] - cap[i]); |
|
908 |
+ s->pulses[i] -= extrabits; |
|
909 |
+ |
|
910 |
+ /* intensity stereo makes use of an extra degree of freedom */ |
|
911 |
+ dof = N * s->coded_channels |
|
912 |
+ + (s->coded_channels == 2 && N > 2 && !s->dualstereo && i < s->intensitystereo); |
|
913 |
+ temp = dof * (celt_log_freq_range[i] + (s->duration<<3)); |
|
914 |
+ offset = (temp >> 1) - dof * CELT_FINE_OFFSET; |
|
915 |
+ if (N == 2) /* dof=2 is the only case that doesn't fit the model */ |
|
916 |
+ offset += dof<<1; |
|
917 |
+ |
|
918 |
+ /* grant an additional bias for the first and second pulses */ |
|
919 |
+ if (s->pulses[i] + offset < 2 * (dof << 3)) |
|
920 |
+ offset += temp >> 2; |
|
921 |
+ else if (s->pulses[i] + offset < 3 * (dof << 3)) |
|
922 |
+ offset += temp >> 3; |
|
923 |
+ |
|
924 |
+ fine_bits = (s->pulses[i] + offset + (dof << 2)) / (dof << 3); |
|
925 |
+ max_bits = FFMIN((s->pulses[i]>>3) >> (s->coded_channels - 1), |
|
926 |
+ CELT_MAX_FINE_BITS); |
|
927 |
+ |
|
928 |
+ max_bits = FFMAX(max_bits, 0); |
|
929 |
+ |
|
930 |
+ s->fine_bits[i] = av_clip(fine_bits, 0, max_bits); |
|
931 |
+ |
|
932 |
+ /* if fine_bits was rounded down or capped, |
|
933 |
+ give priority for the final fine energy pass */ |
|
934 |
+ s->fine_priority[i] = (s->fine_bits[i] * (dof<<3) >= s->pulses[i] + offset); |
|
935 |
+ |
|
936 |
+ /* the remaining bits are assigned to PVQ */ |
|
937 |
+ s->pulses[i] -= s->fine_bits[i] << (s->coded_channels - 1) << 3; |
|
938 |
+ } else { |
|
939 |
+ /* all bits go to fine energy except for the sign bit */ |
|
940 |
+ extrabits = FFMAX(0, s->pulses[i] - (s->coded_channels << 3)); |
|
941 |
+ s->pulses[i] -= extrabits; |
|
942 |
+ s->fine_bits[i] = 0; |
|
943 |
+ s->fine_priority[i] = 1; |
|
944 |
+ } |
|
945 |
+ |
|
946 |
+ /* hand back a limited number of extra fine energy bits to this band */ |
|
947 |
+ if (extrabits > 0) { |
|
948 |
+ int fineextra = FFMIN(extrabits >> (s->coded_channels + 2), |
|
949 |
+ CELT_MAX_FINE_BITS - s->fine_bits[i]); |
|
950 |
+ s->fine_bits[i] += fineextra; |
|
951 |
+ |
|
952 |
+ fineextra <<= s->coded_channels + 2; |
|
953 |
+ s->fine_priority[i] = (fineextra >= extrabits - prev_extra); |
|
954 |
+ extrabits -= fineextra; |
|
955 |
+ } |
|
956 |
+ } |
|
957 |
+ s->remaining = extrabits; |
|
958 |
+ |
|
959 |
+ /* skipped bands dedicate all of their bits for fine energy */ |
|
960 |
+ for (; i < s->endband; i++) { |
|
961 |
+ s->fine_bits[i] = s->pulses[i] >> (s->coded_channels - 1) >> 3; |
|
962 |
+ s->pulses[i] = 0; |
|
963 |
+ s->fine_priority[i] = s->fine_bits[i] < 1; |
|
964 |
+ } |
|
965 |
+} |
|
966 |
+ |
|
967 |
+static inline int celt_bits2pulses(const uint8_t *cache, int bits) |
|
968 |
+{ |
|
969 |
+ // TODO: Find the size of cache and make it into an array in the parameters list |
|
970 |
+ int i, low = 0, high; |
|
971 |
+ |
|
972 |
+ high = cache[0]; |
|
973 |
+ bits--; |
|
974 |
+ |
|
975 |
+ for (i = 0; i < 6; i++) { |
|
976 |
+ int center = (low + high + 1) >> 1; |
|
977 |
+ if (cache[center] >= bits) |
|
978 |
+ high = center; |
|
979 |
+ else |
|
980 |
+ low = center; |
|
981 |
+ } |
|
982 |
+ |
|
983 |
+ return (bits - (low == 0 ? -1 : cache[low]) <= cache[high] - bits) ? low : high; |
|
984 |
+} |
|
985 |
+ |
|
986 |
+static inline int celt_pulses2bits(const uint8_t *cache, int pulses) |
|
987 |
+{ |
|
988 |
+ // TODO: Find the size of cache and make it into an array in the parameters list |
|
989 |
+ return (pulses == 0) ? 0 : cache[pulses] + 1; |
|
990 |
+} |
|
991 |
+ |
|
992 |
+static inline void celt_normalize_residual(const int * restrict iy, float * restrict X, |
|
993 |
+ int N, float g) |
|
994 |
+{ |
|
995 |
+ int i; |
|
996 |
+ for (i = 0; i < N; i++) |
|
997 |
+ X[i] = g * iy[i]; |
|
998 |
+} |
|
999 |
+ |
|
1000 |
+static void celt_exp_rotation1(float *X, unsigned int len, unsigned int stride, |
|
1001 |
+ float c, float s) |
|
1002 |
+{ |
|
1003 |
+ float *Xptr; |
|
1004 |
+ int i; |
|
1005 |
+ |
|
1006 |
+ Xptr = X; |
|
1007 |
+ for (i = 0; i < len - stride; i++) { |
|
1008 |
+ float x1, x2; |
|
1009 |
+ x1 = Xptr[0]; |
|
1010 |
+ x2 = Xptr[stride]; |
|
1011 |
+ Xptr[stride] = c * x2 + s * x1; |
|
1012 |
+ *Xptr++ = c * x1 - s * x2; |
|
1013 |
+ } |
|
1014 |
+ |
|
1015 |
+ Xptr = &X[len - 2 * stride - 1]; |
|
1016 |
+ for (i = len - 2 * stride - 1; i >= 0; i--) { |
|
1017 |
+ float x1, x2; |
|
1018 |
+ x1 = Xptr[0]; |
|
1019 |
+ x2 = Xptr[stride]; |
|
1020 |
+ Xptr[stride] = c * x2 + s * x1; |
|
1021 |
+ *Xptr-- = c * x1 - s * x2; |
|
1022 |
+ } |
|
1023 |
+} |
|
1024 |
+ |
|
1025 |
+static inline void celt_exp_rotation(float *X, unsigned int len, |
|
1026 |
+ unsigned int stride, unsigned int K, |
|
1027 |
+ enum CeltSpread spread) |
|
1028 |
+{ |
|
1029 |
+ unsigned int stride2 = 0; |
|
1030 |
+ float c, s; |
|
1031 |
+ float gain, theta; |
|
1032 |
+ int i; |
|
1033 |
+ |
|
1034 |
+ if (2*K >= len || spread == CELT_SPREAD_NONE) |
|
1035 |
+ return; |
|
1036 |
+ |
|
1037 |
+ gain = (float)len / (len + (20 - 5*spread) * K); |
|
1038 |
+ theta = M_PI * gain * gain / 4; |
|
1039 |
+ |
|
1040 |
+ c = cos(theta); |
|
1041 |
+ s = sin(theta); |
|
1042 |
+ |
|
1043 |
+ if (len >= stride << 3) { |
|
1044 |
+ stride2 = 1; |
|
1045 |
+ /* This is just a simple (equivalent) way of computing sqrt(len/stride) with rounding. |
|
1046 |
+ It's basically incrementing long as (stride2+0.5)^2 < len/stride. */ |
|
1047 |
+ while ((stride2 * stride2 + stride2) * stride + (stride >> 2) < len) |
|
1048 |
+ stride2++; |
|
1049 |
+ } |
|
1050 |
+ |
|
1051 |
+ /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for |
|
1052 |
+ extract_collapse_mask().*/ |
|
1053 |
+ len /= stride; |
|
1054 |
+ for (i = 0; i < stride; i++) { |
|
1055 |
+ if (stride2) |
|
1056 |
+ celt_exp_rotation1(X + i * len, len, stride2, s, c); |
|
1057 |
+ celt_exp_rotation1(X + i * len, len, 1, c, s); |
|
1058 |
+ } |
|
1059 |
+} |
|
1060 |
+ |
|
1061 |
+static inline unsigned int celt_extract_collapse_mask(const int *iy, |
|
1062 |
+ unsigned int N, |
|
1063 |
+ unsigned int B) |
|
1064 |
+{ |
|
1065 |
+ unsigned int collapse_mask; |
|
1066 |
+ int N0; |
|
1067 |
+ int i, j; |
|
1068 |
+ |
|
1069 |
+ if (B <= 1) |
|
1070 |
+ return 1; |
|
1071 |
+ |
|
1072 |
+ /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for |
|
1073 |
+ exp_rotation().*/ |
|
1074 |
+ N0 = N/B; |
|
1075 |
+ collapse_mask = 0; |
|
1076 |
+ for (i = 0; i < B; i++) |
|
1077 |
+ for (j = 0; j < N0; j++) |
|
1078 |
+ collapse_mask |= (iy[i*N0+j]!=0)<<i; |
|
1079 |
+ return collapse_mask; |
|
1080 |
+} |
|
1081 |
+ |
|
1082 |
+static inline void celt_renormalize_vector(float *X, int N, float gain) |
|
1083 |
+{ |
|
1084 |
+ int i; |
|
1085 |
+ float g = 1e-15f; |
|
1086 |
+ for (i = 0; i < N; i++) |
|
1087 |
+ g += X[i] * X[i]; |
|
1088 |
+ g = gain / sqrtf(g); |
|
1089 |
+ |
|
1090 |
+ for (i = 0; i < N; i++) |
|
1091 |
+ X[i] *= g; |
|
1092 |
+} |
|
1093 |
+ |
|
1094 |
+static inline void celt_stereo_merge(float *X, float *Y, float mid, int N) |
|
1095 |
+{ |
|
1096 |
+ int i; |
|
1097 |
+ float xp = 0, side = 0; |
|
1098 |
+ float E[2]; |
|
1099 |
+ float mid2; |
|
1100 |
+ float t, gain[2]; |
|
1101 |
+ |
|
1102 |
+ /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */ |
|
1103 |
+ for (i = 0; i < N; i++) { |
|
1104 |
+ xp += X[i] * Y[i]; |
|
1105 |
+ side += Y[i] * Y[i]; |
|
1106 |
+ } |
|
1107 |
+ |
|
1108 |
+ /* Compensating for the mid normalization */ |
|
1109 |
+ xp *= mid; |
|
1110 |
+ mid2 = mid; |
|
1111 |
+ E[0] = mid2 * mid2 + side - 2 * xp; |
|
1112 |
+ E[1] = mid2 * mid2 + side + 2 * xp; |
|
1113 |
+ if (E[0] < 6e-4f || E[1] < 6e-4f) { |
|
1114 |
+ for (i = 0; i < N; i++) |
|
1115 |
+ Y[i] = X[i]; |
|
1116 |
+ return; |
|
1117 |
+ } |
|
1118 |
+ |
|
1119 |
+ t = E[0]; |
|
1120 |
+ gain[0] = 1.0f / sqrtf(t); |
|
1121 |
+ t = E[1]; |
|
1122 |
+ gain[1] = 1.0f / sqrtf(t); |
|
1123 |
+ |
|
1124 |
+ for (i = 0; i < N; i++) { |
|
1125 |
+ float value[2]; |
|
1126 |
+ /* Apply mid scaling (side is already scaled) */ |
|
1127 |
+ value[0] = mid * X[i]; |
|
1128 |
+ value[1] = Y[i]; |
|
1129 |
+ X[i] = gain[0] * (value[0] - value[1]); |
|
1130 |
+ Y[i] = gain[1] * (value[0] + value[1]); |
|
1131 |
+ } |
|
1132 |
+} |
|
1133 |
+ |
|
1134 |
+static void celt_interleave_hadamard(float *tmp, float *X, int N0, |
|
1135 |
+ int stride, int hadamard) |
|
1136 |
+{ |
|
1137 |
+ int i, j; |
|
1138 |
+ int N = N0*stride; |
|
1139 |
+ |
|
1140 |
+ if (hadamard) { |
|
1141 |
+ const uint8_t *ordery = celt_hadamard_ordery + stride - 2; |
|
1142 |
+ for (i = 0; i < stride; i++) |
|
1143 |
+ for (j = 0; j < N0; j++) |
|
1144 |
+ tmp[j*stride+i] = X[ordery[i]*N0+j]; |
|
1145 |
+ } else { |
|
1146 |
+ for (i = 0; i < stride; i++) |
|
1147 |
+ for (j = 0; j < N0; j++) |
|
1148 |
+ tmp[j*stride+i] = X[i*N0+j]; |
|
1149 |
+ } |
|
1150 |
+ |
|
1151 |
+ for (i = 0; i < N; i++) |
|
1152 |
+ X[i] = tmp[i]; |
|
1153 |
+} |
|
1154 |
+ |
|
1155 |
+static void celt_deinterleave_hadamard(float *tmp, float *X, int N0, |
|
1156 |
+ int stride, int hadamard) |
|
1157 |
+{ |
|
1158 |
+ int i, j; |
|
1159 |
+ int N = N0*stride; |
|
1160 |
+ |
|
1161 |
+ if (hadamard) { |
|
1162 |
+ const uint8_t *ordery = celt_hadamard_ordery + stride - 2; |
|
1163 |
+ for (i = 0; i < stride; i++) |
|
1164 |
+ for (j = 0; j < N0; j++) |
|
1165 |
+ tmp[ordery[i]*N0+j] = X[j*stride+i]; |
|
1166 |
+ } else { |
|
1167 |
+ for (i = 0; i < stride; i++) |
|
1168 |
+ for (j = 0; j < N0; j++) |
|
1169 |
+ tmp[i*N0+j] = X[j*stride+i]; |
|
1170 |
+ } |
|
1171 |
+ |
|
1172 |
+ for (i = 0; i < N; i++) |
|
1173 |
+ X[i] = tmp[i]; |
|
1174 |
+} |
|
1175 |
+ |
|
1176 |
+static void celt_haar1(float *X, int N0, int stride) |
|
1177 |
+{ |
|
1178 |
+ int i, j; |
|
1179 |
+ N0 >>= 1; |
|
1180 |
+ for (i = 0; i < stride; i++) { |
|
1181 |
+ for (j = 0; j < N0; j++) { |
|
1182 |
+ float x0 = X[stride * (2 * j + 0) + i]; |
|
1183 |
+ float x1 = X[stride * (2 * j + 1) + i]; |
|
1184 |
+ X[stride * (2 * j + 0) + i] = (x0 + x1) * M_SQRT1_2; |
|
1185 |
+ X[stride * (2 * j + 1) + i] = (x0 - x1) * M_SQRT1_2; |
|
1186 |
+ } |
|
1187 |
+ } |
|
1188 |
+} |
|
1189 |
+ |
|
1190 |
+static inline int celt_compute_qn(int N, int b, int offset, int pulse_cap, |
|
1191 |
+ int dualstereo) |
|
1192 |
+{ |
|
1193 |
+ int qn, qb; |
|
1194 |
+ int N2 = 2 * N - 1; |
|
1195 |
+ if (dualstereo && N == 2) |
|
1196 |
+ N2--; |
|
1197 |
+ |
|
1198 |
+ /* The upper limit ensures that in a stereo split with itheta==16384, we'll |
|
1199 |
+ * always have enough bits left over to code at least one pulse in the |
|
1200 |
+ * side; otherwise it would collapse, since it doesn't get folded. */ |
|
1201 |
+ qb = FFMIN3(b - pulse_cap - (4 << 3), (b + N2 * offset) / N2, 8 << 3); |
|
1202 |
+ qn = (qb < (1 << 3 >> 1)) ? 1 : ((celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1; |
|
1203 |
+ return qn; |
|
1204 |
+} |
|
1205 |
+ |
|
1206 |
+// this code was adapted from libopus |
|
1207 |
+static inline uint64_t celt_cwrsi(unsigned int N, unsigned int K, unsigned int i, int *y) |
|
1208 |
+{ |
|
1209 |
+ uint64_t norm = 0; |
|
1210 |
+ uint32_t p; |
|
1211 |
+ int s, val; |
|
1212 |
+ int k0; |
|
1213 |
+ |
|
1214 |
+ while (N > 2) { |
|
1215 |
+ uint32_t q; |
|
1216 |
+ |
|
1217 |
+ /*Lots of pulses case:*/ |
|
1218 |
+ if (K >= N) { |
|
1219 |
+ const uint32_t *row = celt_pvq_u_row[N]; |
|
1220 |
+ |
|
1221 |
+ /* Are the pulses in this dimension negative? */ |
|
1222 |
+ p = row[K + 1]; |
|
1223 |
+ s = -(i >= p); |
|
1224 |
+ i -= p & s; |
|
1225 |
+ |
|
1226 |
+ /*Count how many pulses were placed in this dimension.*/ |
|
1227 |
+ k0 = K; |
|
1228 |
+ q = row[N]; |
|
1229 |
+ if (q > i) { |
|
1230 |
+ K = N; |
|
1231 |
+ do { |
|
1232 |
+ p = celt_pvq_u_row[--K][N]; |
|
1233 |
+ } while (p > i); |
|
1234 |
+ } else |
|
1235 |
+ for (p = row[K]; p > i; p = row[K]) |
|
1236 |
+ K--; |
|
1237 |
+ |
|
1238 |
+ i -= p; |
|
1239 |
+ val = (k0 - K + s) ^ s; |
|
1240 |
+ norm += val * val; |
|
1241 |
+ *y++ = val; |
|
1242 |
+ } else { /*Lots of dimensions case:*/ |
|
1243 |
+ /*Are there any pulses in this dimension at all?*/ |
|
1244 |
+ p = celt_pvq_u_row[K ][N]; |
|
1245 |
+ q = celt_pvq_u_row[K + 1][N]; |
|
1246 |
+ |
|
1247 |
+ if (p <= i && i < q) { |
|
1248 |
+ i -= p; |
|
1249 |
+ *y++ = 0; |
|
1250 |
+ } else { |
|
1251 |
+ /*Are the pulses in this dimension negative?*/ |
|
1252 |
+ s = -(i >= q); |
|
1253 |
+ i -= q & s; |
|
1254 |
+ |
|
1255 |
+ /*Count how many pulses were placed in this dimension.*/ |
|
1256 |
+ k0 = K; |
|
1257 |
+ do p = celt_pvq_u_row[--K][N]; |
|
1258 |
+ while (p > i); |
|
1259 |
+ |
|
1260 |
+ i -= p; |
|
1261 |
+ val = (k0 - K + s) ^ s; |
|
1262 |
+ norm += val * val; |
|
1263 |
+ *y++ = val; |
|
1264 |
+ } |
|
1265 |
+ } |
|
1266 |
+ N--; |
|
1267 |
+ } |
|
1268 |
+ |
|
1269 |
+ /* N == 2 */ |
|
1270 |
+ p = 2 * K + 1; |
|
1271 |
+ s = -(i >= p); |
|
1272 |
+ i -= p & s; |
|
1273 |
+ k0 = K; |
|
1274 |
+ K = (i + 1) / 2; |
|
1275 |
+ |
|
1276 |
+ if (K) |
|
1277 |
+ i -= 2 * K - 1; |
|
1278 |
+ |
|
1279 |
+ val = (k0 - K + s) ^ s; |
|
1280 |
+ norm += val * val; |
|
1281 |
+ *y++ = val; |
|
1282 |
+ |
|
1283 |
+ /* N==1 */ |
|
1284 |
+ s = -i; |
|
1285 |
+ val = (K + s) ^ s; |
|
1286 |
+ norm += val * val; |
|
1287 |
+ *y = val; |
|
1288 |
+ |
|
1289 |
+ return norm; |
|
1290 |
+} |
|
1291 |
+ |
|
1292 |
+static inline float celt_decode_pulses(OpusRangeCoder *rc, int *y, unsigned int N, unsigned int K) |
|
1293 |
+{ |
|
1294 |
+ unsigned int idx; |
|
1295 |
+#define CELT_PVQ_U(n, k) (celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)]) |
|
1296 |
+#define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, k + 1)) |
|
1297 |
+ idx = opus_rc_unimodel(rc, CELT_PVQ_V(N, K)); |
|
1298 |
+ return celt_cwrsi(N, K, idx, y); |
|
1299 |
+} |
|
1300 |
+ |
|
1301 |
+/** Decode pulse vector and combine the result with the pitch vector to produce |
|
1302 |
+ the final normalised signal in the current band. */ |
|
1303 |
+static inline unsigned int celt_alg_unquant(OpusRangeCoder *rc, float *X, |
|
1304 |
+ unsigned int N, unsigned int K, |
|
1305 |
+ enum CeltSpread spread, |
|
1306 |
+ unsigned int blocks, float gain) |
|
1307 |
+{ |
|
1308 |
+ int y[176]; |
|
1309 |
+ |
|
1310 |
+ gain /= sqrtf(celt_decode_pulses(rc, y, N, K)); |
|
1311 |
+ celt_normalize_residual(y, X, N, gain); |
|
1312 |
+ celt_exp_rotation(X, N, blocks, K, spread); |
|
1313 |
+ return celt_extract_collapse_mask(y, N, blocks); |
|
1314 |
+} |
|
1315 |
+ |
|
1316 |
+static unsigned int celt_decode_band(CeltContext *s, OpusRangeCoder *rc, |
|
1317 |
+ const int band, float *X, float *Y, |
|
1318 |
+ int N, int b, unsigned int blocks, |
|
1319 |
+ float *lowband, int duration, |
|
1320 |
+ float *lowband_out, int level, |
|
1321 |
+ float gain, float *lowband_scratch, |
|
1322 |
+ int fill) |
|
1323 |
+{ |
|
1324 |
+ const uint8_t *cache; |
|
1325 |
+ int dualstereo, split; |
|
1326 |
+ int imid = 0, iside = 0; |
|
1327 |
+ unsigned int N0 = N; |
|
1328 |
+ int N_B; |
|
1329 |
+ int N_B0; |
|
1330 |
+ int B0 = blocks; |
|
1331 |
+ int time_divide = 0; |
|
1332 |
+ int recombine = 0; |
|
1333 |
+ int inv = 0; |
|
1334 |
+ float mid = 0, side = 0; |
|
1335 |
+ int longblocks = (B0 == 1); |
|
1336 |
+ unsigned int cm = 0; |
|
1337 |
+ |
|
1338 |
+ N_B0 = N_B = N / blocks; |
|
1339 |
+ split = dualstereo = (Y != NULL); |
|
1340 |
+ |
|
1341 |
+ if (N == 1) { |
|
1342 |
+ /* special case for one sample */ |
|
1343 |
+ int i; |
|
1344 |
+ float *x = X; |
|
1345 |
+ for (i = 0; i <= dualstereo; i++) { |
|
1346 |
+ int sign = 0; |
|
1347 |
+ if (s->remaining2 >= 1<<3) { |
|
1348 |
+ sign = opus_getrawbits(rc, 1); |
|
1349 |
+ s->remaining2 -= 1 << 3; |
|
1350 |
+ b -= 1 << 3; |
|
1351 |
+ } |
|
1352 |
+ x[0] = sign ? -1.0f : 1.0f; |
|
1353 |
+ x = Y; |
|
1354 |
+ } |
|
1355 |
+ if (lowband_out) |
|
1356 |
+ lowband_out[0] = X[0]; |
|
1357 |
+ return 1; |
|
1358 |
+ } |
|
1359 |
+ |
|
1360 |
+ if (!dualstereo && level == 0) { |
|
1361 |
+ int tf_change = s->tf_change[band]; |
|
1362 |
+ int k; |
|
1363 |
+ if (tf_change > 0) |
|
1364 |
+ recombine = tf_change; |
|
1365 |
+ /* Band recombining to increase frequency resolution */ |
|
1366 |
+ |
|
1367 |
+ if (lowband && |
|
1368 |
+ (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) { |
|
1369 |
+ int j; |
|
1370 |
+ for (j = 0; j < N; j++) |
|
1371 |
+ lowband_scratch[j] = lowband[j]; |
|
1372 |
+ lowband = lowband_scratch; |
|
1373 |
+ } |
|
1374 |
+ |
|
1375 |
+ for (k = 0; k < recombine; k++) { |
|
1376 |
+ if (lowband) |
|
1377 |
+ celt_haar1(lowband, N >> k, 1 << k); |
|
1378 |
+ fill = celt_bit_interleave[fill & 0xF] | celt_bit_interleave[fill >> 4] << 2; |
|
1379 |
+ } |
|
1380 |
+ blocks >>= recombine; |
|
1381 |
+ N_B <<= recombine; |
|
1382 |
+ |
|
1383 |
+ /* Increasing the time resolution */ |
|
1384 |
+ while ((N_B & 1) == 0 && tf_change < 0) { |
|
1385 |
+ if (lowband) |
|
1386 |
+ celt_haar1(lowband, N_B, blocks); |
|
1387 |
+ fill |= fill << blocks; |
|
1388 |
+ blocks <<= 1; |
|
1389 |
+ N_B >>= 1; |
|
1390 |
+ time_divide++; |
|
1391 |
+ tf_change++; |
|
1392 |
+ } |
|
1393 |
+ B0 = blocks; |
|
1394 |
+ N_B0 = N_B; |
|
1395 |
+ |
|
1396 |
+ /* Reorganize the samples in time order instead of frequency order */ |
|
1397 |
+ if (B0 > 1 && lowband) |
|
1398 |
+ celt_deinterleave_hadamard(s->scratch, lowband, N_B >> recombine, |
|
1399 |
+ B0 << recombine, longblocks); |
|
1400 |
+ } |
|
1401 |
+ |
|
1402 |
+ /* If we need 1.5 more bit than we can produce, split the band in two. */ |
|
1403 |
+ cache = celt_cache_bits + |
|
1404 |
+ celt_cache_index[(duration + 1) * CELT_MAX_BANDS + band]; |
|
1405 |
+ if (!dualstereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) { |
|
1406 |
+ N >>= 1; |
|
1407 |
+ Y = X + N; |
|
1408 |
+ split = 1; |
|
1409 |
+ duration -= 1; |
|
1410 |
+ if (blocks == 1) |
|
1411 |
+ fill = (fill & 1) | (fill << 1); |
|
1412 |
+ blocks = (blocks + 1) >> 1; |
|
1413 |
+ } |
|
1414 |
+ |
|
1415 |
+ if (split) { |
|
1416 |
+ int qn; |
|
1417 |
+ int itheta = 0; |
|
1418 |
+ int mbits, sbits, delta; |
|
1419 |
+ int qalloc; |
|
1420 |
+ int pulse_cap; |
|
1421 |
+ int offset; |
|
1422 |
+ int orig_fill; |
|
1423 |
+ int tell; |
|
1424 |
+ |
|
1425 |
+ /* Decide on the resolution to give to the split parameter theta */ |
|
1426 |
+ pulse_cap = celt_log_freq_range[band] + duration * 8; |
|
1427 |
+ offset = (pulse_cap >> 1) - (dualstereo && N == 2 ? CELT_QTHETA_OFFSET_TWOPHASE : |
|
1428 |
+ CELT_QTHETA_OFFSET); |
|
1429 |
+ qn = (dualstereo && band >= s->intensitystereo) ? 1 : |
|
1430 |
+ celt_compute_qn(N, b, offset, pulse_cap, dualstereo); |
|
1431 |
+ tell = opus_rc_tell_frac(rc); |
|
1432 |
+ if (qn != 1) { |
|
1433 |
+ /* Entropy coding of the angle. We use a uniform pdf for the |
|
1434 |
+ time split, a step for stereo, and a triangular one for the rest. */ |
|
1435 |
+ if (dualstereo && N > 2) |
|
1436 |
+ itheta = opus_rc_stepmodel(rc, qn/2); |
|
1437 |
+ else if (dualstereo || B0 > 1) |
|
1438 |
+ itheta = opus_rc_unimodel(rc, qn+1); |
|
1439 |
+ else |
|
1440 |
+ itheta = opus_rc_trimodel(rc, qn); |
|
1441 |
+ itheta = itheta * 16384 / qn; |
|
1442 |
+ /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate. |
|
1443 |
+ Let's do that at higher complexity */ |
|
1444 |
+ } else if (dualstereo) { |
|
1445 |
+ inv = (b > 2 << 3 && s->remaining2 > 2 << 3) ? opus_rc_p2model(rc, 2) : 0; |
|
1446 |
+ itheta = 0; |
|
1447 |
+ } |
|
1448 |
+ qalloc = opus_rc_tell_frac(rc) - tell; |
|
1449 |
+ b -= qalloc; |
|
1450 |
+ |
|
1451 |
+ orig_fill = fill; |
|
1452 |
+ if (itheta == 0) { |
|
1453 |
+ imid = 32767; |
|
1454 |
+ iside = 0; |
|
1455 |
+ fill &= (1 << blocks) - 1; |
|
1456 |
+ delta = -16384; |
|
1457 |
+ } else if (itheta == 16384) { |
|
1458 |
+ imid = 0; |
|
1459 |
+ iside = 32767; |
|
1460 |
+ fill &= ((1 << blocks) - 1) << blocks; |
|
1461 |
+ delta = 16384; |
|
1462 |
+ } else { |
|
1463 |
+ imid = celt_cos(itheta); |
|
1464 |
+ iside = celt_cos(16384-itheta); |
|
1465 |
+ /* This is the mid vs side allocation that minimizes squared error |
|
1466 |
+ in that band. */ |
|
1467 |
+ delta = ROUND_MUL16((N - 1) << 7, celt_log2tan(iside, imid)); |
|
1468 |
+ } |
|
1469 |
+ |
|
1470 |
+ mid = imid / 32768.0f; |
|
1471 |
+ side = iside / 32768.0f; |
|
1472 |
+ |
|
1473 |
+ /* This is a special case for N=2 that only works for stereo and takes |
|
1474 |
+ advantage of the fact that mid and side are orthogonal to encode |
|
1475 |
+ the side with just one bit. */ |
|
1476 |
+ if (N == 2 && dualstereo) { |
|
1477 |
+ int c; |
|
1478 |
+ int sign = 0; |
|
1479 |
+ float tmp; |
|
1480 |
+ float *x2, *y2; |
|
1481 |
+ mbits = b; |
|
1482 |
+ /* Only need one bit for the side */ |
|
1483 |
+ sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0; |
|
1484 |
+ mbits -= sbits; |
|
1485 |
+ c = (itheta > 8192); |
|
1486 |
+ s->remaining2 -= qalloc+sbits; |
|
1487 |
+ |
|
1488 |
+ x2 = c ? Y : X; |
|
1489 |
+ y2 = c ? X : Y; |
|
1490 |
+ if (sbits) |
|
1491 |
+ sign = opus_getrawbits(rc, 1); |
|
1492 |
+ sign = 1 - 2 * sign; |
|
1493 |
+ /* We use orig_fill here because we want to fold the side, but if |
|
1494 |
+ itheta==16384, we'll have cleared the low bits of fill. */ |
|
1495 |
+ cm = celt_decode_band(s, rc, band, x2, NULL, N, mbits, blocks, |
|
1496 |
+ lowband, duration, lowband_out, level, gain, |
|
1497 |
+ lowband_scratch, orig_fill); |
|
1498 |
+ /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse), |
|
1499 |
+ and there's no need to worry about mixing with the other channel. */ |
|
1500 |
+ y2[0] = -sign * x2[1]; |
|
1501 |
+ y2[1] = sign * x2[0]; |
|
1502 |
+ X[0] *= mid; |
|
1503 |
+ X[1] *= mid; |
|
1504 |
+ Y[0] *= side; |
|
1505 |
+ Y[1] *= side; |
|
1506 |
+ tmp = X[0]; |
|
1507 |
+ X[0] = tmp - Y[0]; |
|
1508 |
+ Y[0] = tmp + Y[0]; |
|
1509 |
+ tmp = X[1]; |
|
1510 |
+ X[1] = tmp - Y[1]; |
|
1511 |
+ Y[1] = tmp + Y[1]; |
|
1512 |
+ } else { |
|
1513 |
+ /* "Normal" split code */ |
|
1514 |
+ float *next_lowband2 = NULL; |
|
1515 |
+ float *next_lowband_out1 = NULL; |
|
1516 |
+ int next_level = 0; |
|
1517 |
+ int rebalance; |
|
1518 |
+ |
|
1519 |
+ /* Give more bits to low-energy MDCTs than they would |
|
1520 |
+ * otherwise deserve */ |
|
1521 |
+ if (B0 > 1 && !dualstereo && (itheta & 0x3fff)) { |
|
1522 |
+ if (itheta > 8192) |
|
1523 |
+ /* Rough approximation for pre-echo masking */ |
|
1524 |
+ delta -= delta >> (4 - duration); |
|
1525 |
+ else |
|
1526 |
+ /* Corresponds to a forward-masking slope of |
|
1527 |
+ * 1.5 dB per 10 ms */ |
|
1528 |
+ delta = FFMIN(0, delta + (N << 3 >> (5 - duration))); |
|
1529 |
+ } |
|
1530 |
+ mbits = av_clip((b - delta) / 2, 0, b); |
|
1531 |
+ sbits = b - mbits; |
|
1532 |
+ s->remaining2 -= qalloc; |
|
1533 |
+ |
|
1534 |
+ if (lowband && !dualstereo) |
|
1535 |
+ next_lowband2 = lowband + N; /* >32-bit split case */ |
|
1536 |
+ |
|
1537 |
+ /* Only stereo needs to pass on lowband_out. |
|
1538 |
+ * Otherwise, it's handled at the end */ |
|
1539 |
+ if (dualstereo) |
|
1540 |
+ next_lowband_out1 = lowband_out; |
|
1541 |
+ else |
|
1542 |
+ next_level = level + 1; |
|
1543 |
+ |
|
1544 |
+ rebalance = s->remaining2; |
|
1545 |
+ if (mbits >= sbits) { |
|
1546 |
+ /* In stereo mode, we do not apply a scaling to the mid |
|
1547 |
+ * because we need the normalized mid for folding later */ |
|
1548 |
+ cm = celt_decode_band(s, rc, band, X, NULL, N, mbits, blocks, |
|
1549 |
+ lowband, duration, next_lowband_out1, |
|
1550 |
+ next_level, dualstereo ? 1.0f : (gain * mid), |
|
1551 |
+ lowband_scratch, fill); |
|
1552 |
+ |
|
1553 |
+ rebalance = mbits - (rebalance - s->remaining2); |
|
1554 |
+ if (rebalance > 3 << 3 && itheta != 0) |
|
1555 |
+ sbits += rebalance - (3 << 3); |
|
1556 |
+ |
|
1557 |
+ /* For a stereo split, the high bits of fill are always zero, |
|
1558 |
+ * so no folding will be done to the side. */ |
|
1559 |
+ cm |= celt_decode_band(s, rc, band, Y, NULL, N, sbits, blocks, |
|
1560 |
+ next_lowband2, duration, NULL, |
|
1561 |
+ next_level, gain * side, NULL, |
|
1562 |
+ fill >> blocks) << ((B0 >> 1) & (dualstereo - 1)); |
|
1563 |
+ } else { |
|
1564 |
+ /* For a stereo split, the high bits of fill are always zero, |
|
1565 |
+ * so no folding will be done to the side. */ |
|
1566 |
+ cm = celt_decode_band(s, rc, band, Y, NULL, N, sbits, blocks, |
|
1567 |
+ next_lowband2, duration, NULL, |
|
1568 |
+ next_level, gain * side, NULL, |
|
1569 |
+ fill >> blocks) << ((B0 >> 1) & (dualstereo - 1)); |
|
1570 |
+ |
|
1571 |
+ rebalance = sbits - (rebalance - s->remaining2); |
|
1572 |
+ if (rebalance > 3 << 3 && itheta != 16384) |
|
1573 |
+ mbits += rebalance - (3 << 3); |
|
1574 |
+ |
|
1575 |
+ /* In stereo mode, we do not apply a scaling to the mid because |
|
1576 |
+ * we need the normalized mid for folding later */ |
|
1577 |
+ cm |= celt_decode_band(s, rc, band, X, NULL, N, mbits, blocks, |
|
1578 |
+ lowband, duration, next_lowband_out1, |
|
1579 |
+ next_level, dualstereo ? 1.0f : (gain * mid), |
|
1580 |
+ lowband_scratch, fill); |
|
1581 |
+ } |
|
1582 |
+ } |
|
1583 |
+ } else { |
|
1584 |
+ /* This is the basic no-split case */ |
|
1585 |
+ unsigned int q = celt_bits2pulses(cache, b); |
|
1586 |
+ unsigned int curr_bits = celt_pulses2bits(cache, q); |
|
1587 |
+ s->remaining2 -= curr_bits; |
|
1588 |
+ |
|
1589 |
+ /* Ensures we can never bust the budget */ |
|
1590 |
+ while (s->remaining2 < 0 && q > 0) { |
|
1591 |
+ s->remaining2 += curr_bits; |
|
1592 |
+ curr_bits = celt_pulses2bits(cache, --q); |
|
1593 |
+ s->remaining2 -= curr_bits; |
|
1594 |
+ } |
|
1595 |
+ |
|
1596 |
+ if (q != 0) { |
|
1597 |
+ /* Finally do the actual quantization */ |
|
1598 |
+ cm = celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1), |
|
1599 |
+ s->spread, blocks, gain); |
|
1600 |
+ } else { |
|
1601 |
+ /* If there's no pulse, fill the band anyway */ |
|
1602 |
+ int j; |
|
1603 |
+ unsigned int cm_mask = (1 << blocks) - 1; |
|
1604 |
+ fill &= cm_mask; |
|
1605 |
+ if (!fill) { |
|
1606 |
+ for (j = 0; j < N; j++) |
|
1607 |
+ X[j] = 0.0f; |
|
1608 |
+ } else { |
|
1609 |
+ if (lowband == NULL) { |
|
1610 |
+ /* Noise */ |
|
1611 |
+ for (j = 0; j < N; j++) |
|
1612 |
+ X[j] = (((int32_t)celt_rng(s)) >> 20); |
|
1613 |
+ cm = cm_mask; |
|
1614 |
+ } else { |
|
1615 |
+ /* Folded spectrum */ |
|
1616 |
+ for (j = 0; j < N; j++) { |
|
1617 |
+ /* About 48 dB below the "normal" folding level */ |
|
1618 |
+ X[j] = lowband[j] + (((celt_rng(s)) & 0x8000) ? 1.0f / 256 : -1.0f / 256); |
|
1619 |
+ } |
|
1620 |
+ cm = fill; |
|
1621 |
+ } |
|
1622 |
+ celt_renormalize_vector(X, N, gain); |
|
1623 |
+ } |
|
1624 |
+ } |
|
1625 |
+ } |
|
1626 |
+ |
|
1627 |
+ /* This code is used by the decoder and by the resynthesis-enabled encoder */ |
|
1628 |
+ if (dualstereo) { |
|
1629 |
+ int j; |
|
1630 |
+ if (N != 2) |
|
1631 |
+ celt_stereo_merge(X, Y, mid, N); |
|
1632 |
+ if (inv) { |
|
1633 |
+ for (j = 0; j < N; j++) |
|
1634 |
+ Y[j] *= -1; |
|
1635 |
+ } |
|
1636 |
+ } else if (level == 0) { |
|
1637 |
+ int k; |
|
1638 |
+ |
|
1639 |
+ /* Undo the sample reorganization going from time order to frequency order */ |
|
1640 |
+ if (B0 > 1) |
|
1641 |
+ celt_interleave_hadamard(s->scratch, X, N_B>>recombine, |
|
1642 |
+ B0<<recombine, longblocks); |
|
1643 |
+ |
|
1644 |
+ /* Undo time-freq changes that we did earlier */ |
|
1645 |
+ N_B = N_B0; |
|
1646 |
+ blocks = B0; |
|
1647 |
+ for (k = 0; k < time_divide; k++) { |
|
1648 |
+ blocks >>= 1; |
|
1649 |
+ N_B <<= 1; |
|
1650 |
+ cm |= cm >> blocks; |
|
1651 |
+ celt_haar1(X, N_B, blocks); |
|
1652 |
+ } |
|
1653 |
+ |
|
1654 |
+ for (k = 0; k < recombine; k++) { |
|
1655 |
+ cm = celt_bit_deinterleave[cm]; |
|
1656 |
+ celt_haar1(X, N0>>k, 1<<k); |
|
1657 |
+ } |
|
1658 |
+ blocks <<= recombine; |
|
1659 |
+ |
|
1660 |
+ /* Scale output for later folding */ |
|
1661 |
+ if (lowband_out) { |
|
1662 |
+ int j; |
|
1663 |
+ float n = sqrtf(N0); |
|
1664 |
+ for (j = 0; j < N0; j++) |
|
1665 |
+ lowband_out[j] = n * X[j]; |
|
1666 |
+ } |
|
1667 |
+ cm &= (1 << blocks) - 1; |
|
1668 |
+ } |
|
1669 |
+ return cm; |
|
1670 |
+} |
|
1671 |
+ |
|
1672 |
+static void celt_denormalize(CeltContext *s, CeltFrame *frame, float *data) |
|
1673 |
+{ |
|
1674 |
+ int i, j; |
|
1675 |
+ |
|
1676 |
+ for (i = s->startband; i < s->endband; i++) { |
|
1677 |
+ float *dst = data + (celt_freq_bands[i] << s->duration); |
|
1678 |
+ float norm = pow(2, frame->energy[i] + celt_mean_energy[i]); |
|
1679 |
+ |
|
1680 |
+ for (j = 0; j < celt_freq_range[i] << s->duration; j++) |
|
1681 |
+ dst[j] *= norm; |
|
1682 |
+ } |
|
1683 |
+} |
|
1684 |
+ |
|
1685 |
+static void celt_postfilter_apply_transition(CeltFrame *frame, float *data) |
|
1686 |
+{ |
|
1687 |
+ const int T0 = frame->pf_period_old; |
|
1688 |
+ const int T1 = frame->pf_period; |
|
1689 |
+ |
|
1690 |
+ float g00, g01, g02; |
|
1691 |
+ float g10, g11, g12; |
|
1692 |
+ |
|
1693 |
+ float x0, x1, x2, x3, x4; |
|
1694 |
+ |
|
1695 |
+ int i; |
|
1696 |
+ |
|
1697 |
+ if (frame->pf_gains[0] == 0.0 && |
|
1698 |
+ frame->pf_gains_old[0] == 0.0) |
|
1699 |
+ return; |
|
1700 |
+ |
|
1701 |
+ g00 = frame->pf_gains_old[0]; |
|
1702 |
+ g01 = frame->pf_gains_old[1]; |
|
1703 |
+ g02 = frame->pf_gains_old[2]; |
|
1704 |
+ g10 = frame->pf_gains[0]; |
|
1705 |
+ g11 = frame->pf_gains[1]; |
|
1706 |
+ g12 = frame->pf_gains[2]; |
|
1707 |
+ |
|
1708 |
+ x1 = data[-T1 + 1]; |
|
1709 |
+ x2 = data[-T1]; |
|
1710 |
+ x3 = data[-T1 - 1]; |
|
1711 |
+ x4 = data[-T1 - 2]; |
|
1712 |
+ |
|
1713 |
+ for (i = 0; i < CELT_OVERLAP; i++) { |
|
1714 |
+ float w = ff_celt_window2[i]; |
|
1715 |
+ x0 = data[i - T1 + 2]; |
|
1716 |
+ |
|
1717 |
+ data[i] += (1.0 - w) * g00 * data[i - T0] + |
|
1718 |
+ (1.0 - w) * g01 * (data[i - T0 - 1] + data[i - T0 + 1]) + |
|
1719 |
+ (1.0 - w) * g02 * (data[i - T0 - 2] + data[i - T0 + 2]) + |
|
1720 |
+ w * g10 * x2 + |
|
1721 |
+ w * g11 * (x1 + x3) + |
|
1722 |
+ w * g12 * (x0 + x4); |
|
1723 |
+ x4 = x3; |
|
1724 |
+ x3 = x2; |
|
1725 |
+ x2 = x1; |
|
1726 |
+ x1 = x0; |
|
1727 |
+ } |
|
1728 |
+} |
|
1729 |
+ |
|
1730 |
+static void celt_postfilter_apply(CeltFrame *frame, |
|
1731 |
+ float *data, int len) |
|
1732 |
+{ |
|
1733 |
+ const int T = frame->pf_period; |
|
1734 |
+ float g0, g1, g2; |
|
1735 |
+ float x0, x1, x2, x3, x4; |
|
1736 |
+ int i; |
|
1737 |
+ |
|
1738 |
+ if (frame->pf_gains[0] == 0.0 || len <= 0) |
|
1739 |
+ return; |
|
1740 |
+ |
|
1741 |
+ g0 = frame->pf_gains[0]; |
|
1742 |
+ g1 = frame->pf_gains[1]; |
|
1743 |
+ g2 = frame->pf_gains[2]; |
|
1744 |
+ |
|
1745 |
+ x4 = data[-T - 2]; |
|
1746 |
+ x3 = data[-T - 1]; |
|
1747 |
+ x2 = data[-T]; |
|
1748 |
+ x1 = data[-T + 1]; |
|
1749 |
+ |
|
1750 |
+ for (i = 0; i < len; i++) { |
|
1751 |
+ x0 = data[i - T + 2]; |
|
1752 |
+ data[i] += g0 * x2 + |
|
1753 |
+ g1 * (x1 + x3) + |
|
1754 |
+ g2 * (x0 + x4); |
|
1755 |
+ x4 = x3; |
|
1756 |
+ x3 = x2; |
|
1757 |
+ x2 = x1; |
|
1758 |
+ x1 = x0; |
|
1759 |
+ } |
|
1760 |
+} |
|
1761 |
+ |
|
1762 |
+static void celt_postfilter(CeltContext *s, CeltFrame *frame) |
|
1763 |
+{ |
|
1764 |
+ int len = s->blocksize * s->blocks; |
|
1765 |
+ |
|
1766 |
+ celt_postfilter_apply_transition(frame, frame->buf + 1024); |
|
1767 |
+ |
|
1768 |
+ frame->pf_period_old = frame->pf_period; |
|
1769 |
+ memcpy(frame->pf_gains_old, frame->pf_gains, sizeof(frame->pf_gains)); |
|
1770 |
+ |
|
1771 |
+ frame->pf_period = frame->pf_period_new; |
|
1772 |
+ memcpy(frame->pf_gains, frame->pf_gains_new, sizeof(frame->pf_gains)); |
|
1773 |
+ |
|
1774 |
+ if (len > CELT_OVERLAP) { |
|
1775 |
+ celt_postfilter_apply_transition(frame, frame->buf + 1024 + CELT_OVERLAP); |
|
1776 |
+ celt_postfilter_apply(frame, frame->buf + 1024 + 2 * CELT_OVERLAP, |
|
1777 |
+ len - 2 * CELT_OVERLAP); |
|
1778 |
+ |
|
1779 |
+ frame->pf_period_old = frame->pf_period; |
|
1780 |
+ memcpy(frame->pf_gains_old, frame->pf_gains, sizeof(frame->pf_gains)); |
|
1781 |
+ } |
|
1782 |
+ |
|
1783 |
+ memmove(frame->buf, frame->buf + len, (1024 + CELT_OVERLAP / 2) * sizeof(float)); |
|
1784 |
+} |
|
1785 |
+ |
|
1786 |
+static int parse_postfilter(CeltContext *s, OpusRangeCoder *rc, int consumed) |
|
1787 |
+{ |
|
1788 |
+ static const float postfilter_taps[3][3] = { |
|
1789 |
+ { 0.3066406250f, 0.2170410156f, 0.1296386719f }, |
|
1790 |
+ { 0.4638671875f, 0.2680664062f, 0.0 }, |
|
1791 |
+ { 0.7998046875f, 0.1000976562f, 0.0 } |
|
1792 |
+ }; |
|
1793 |
+ int i; |
|
1794 |
+ |
|
1795 |
+ memset(s->frame[0].pf_gains_new, 0, sizeof(s->frame[0].pf_gains_new)); |
|
1796 |
+ memset(s->frame[1].pf_gains_new, 0, sizeof(s->frame[1].pf_gains_new)); |
|
1797 |
+ |
|
1798 |
+ if (s->startband == 0 && consumed + 16 <= s->framebits) { |
|
1799 |
+ int has_postfilter = opus_rc_p2model(rc, 1); |
|
1800 |
+ if (has_postfilter) { |
|
1801 |
+ float gain; |
|
1802 |
+ int tapset, octave, period; |
|
1803 |
+ |
|
1804 |
+ octave = opus_rc_unimodel(rc, 6); |
|
1805 |
+ period = (16 << octave) + opus_getrawbits(rc, 4 + octave) - 1; |
|
1806 |
+ gain = 0.09375f * (opus_getrawbits(rc, 3) + 1); |
|
1807 |
+ tapset = (opus_rc_tell(rc) + 2 <= s->framebits) ? |
|
1808 |
+ opus_rc_getsymbol(rc, celt_model_tapset) : 0; |
|
1809 |
+ |
|
1810 |
+ for (i = 0; i < 2; i++) { |
|
1811 |
+ CeltFrame *frame = &s->frame[i]; |
|
1812 |
+ |
|
1813 |
+ frame->pf_period_new = FFMAX(period, CELT_POSTFILTER_MINPERIOD); |
|
1814 |
+ frame->pf_gains_new[0] = gain * postfilter_taps[tapset][0]; |
|
1815 |
+ frame->pf_gains_new[1] = gain * postfilter_taps[tapset][1]; |
|
1816 |
+ frame->pf_gains_new[2] = gain * postfilter_taps[tapset][2]; |
|
1817 |
+ } |
|
1818 |
+ } |
|
1819 |
+ |
|
1820 |
+ consumed = opus_rc_tell(rc); |
|
1821 |
+ } |
|
1822 |
+ |
|
1823 |
+ return consumed; |
|
1824 |
+} |
|
1825 |
+ |
|
1826 |
+static void process_anticollapse(CeltContext *s, CeltFrame *frame, float *X) |
|
1827 |
+{ |
|
1828 |
+ int i, j, k; |
|
1829 |
+ |
|
1830 |
+ for (i = s->startband; i < s->endband; i++) { |
|
1831 |
+ int renormalize = 0; |
|
1832 |
+ float *xptr; |
|
1833 |
+ float prev[2]; |
|
1834 |
+ float Ediff, r; |
|
1835 |
+ float thresh, sqrt_1; |
|
1836 |
+ int depth; |
|
1837 |
+ |
|
1838 |
+ /* depth in 1/8 bits */ |
|
1839 |
+ depth = (1 + s->pulses[i]) / (celt_freq_range[i] << s->duration); |
|
1840 |
+ thresh = pow(2, -1.0 - 0.125f * depth); |
|
1841 |
+ sqrt_1 = 1.0f / sqrtf(celt_freq_range[i] << s->duration); |
|
1842 |
+ |
|
1843 |
+ xptr = X + (celt_freq_bands[i] << s->duration); |
|
1844 |
+ |
|
1845 |
+ prev[0] = frame->prev_energy[0][i]; |
|
1846 |
+ prev[1] = frame->prev_energy[1][i]; |
|
1847 |
+ if (s->coded_channels == 1) { |
|
1848 |
+ CeltFrame *frame1 = &s->frame[1]; |
|
1849 |
+ |
|
1850 |
+ prev[0] = FFMAX(prev[0], frame1->prev_energy[0][i]); |
|
1851 |
+ prev[1] = FFMAX(prev[1], frame1->prev_energy[1][i]); |
|
1852 |
+ } |
|
1853 |
+ Ediff = frame->energy[i] - FFMIN(prev[0], prev[1]); |
|
1854 |
+ Ediff = FFMAX(0, Ediff); |
|
1855 |
+ |
|
1856 |
+ /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because |
|
1857 |
+ short blocks don't have the same energy as long */ |
|
1858 |
+ r = pow(2, 1 - Ediff); |
|
1859 |
+ if (s->duration == 3) |
|
1860 |
+ r *= M_SQRT2; |
|
1861 |
+ r = FFMIN(thresh, r) * sqrt_1; |
|
1862 |
+ for (k = 0; k < 1 << s->duration; k++) { |
|
1863 |
+ /* Detect collapse */ |
|
1864 |
+ if (!(frame->collapse_masks[i] & 1 << k)) { |
|
1865 |
+ /* Fill with noise */ |
|
1866 |
+ for (j = 0; j < celt_freq_range[i]; j++) |
|
1867 |
+ xptr[(j << s->duration) + k] = (celt_rng(s) & 0x8000) ? r : -r; |
|
1868 |
+ renormalize = 1; |
|
1869 |
+ } |
|
1870 |
+ } |
|
1871 |
+ |
|
1872 |
+ /* We just added some energy, so we need to renormalize */ |
|
1873 |
+ if (renormalize) |
|
1874 |
+ celt_renormalize_vector(xptr, celt_freq_range[i] << s->duration, 1.0f); |
|
1875 |
+ } |
|
1876 |
+} |
|
1877 |
+ |
|
1878 |
+static void celt_decode_bands(CeltContext *s, OpusRangeCoder *rc) |
|
1879 |
+{ |
|
1880 |
+ float lowband_scratch[8 * 22]; |
|
1881 |
+ float norm[2 * 8 * 100]; |
|
1882 |
+ |
|
1883 |
+ int totalbits = (s->framebits << 3) - s->anticollapse_bit; |
|
1884 |
+ |
|
1885 |
+ int update_lowband = 1; |
|
1886 |
+ int lowband_offset = 0; |
|
1887 |
+ |
|
1888 |
+ int i, j; |
|
1889 |
+ |
|
1890 |
+ memset(s->coeffs, 0, sizeof(s->coeffs)); |
|
1891 |
+ |
|
1892 |
+ for (i = s->startband; i < s->endband; i++) { |
|
1893 |
+ int band_offset = celt_freq_bands[i] << s->duration; |
|
1894 |
+ int band_size = celt_freq_range[i] << s->duration; |
|
1895 |
+ float *X = s->coeffs[0] + band_offset; |
|
1896 |
+ float *Y = (s->coded_channels == 2) ? s->coeffs[1] + band_offset : NULL; |
|
1897 |
+ |
|
1898 |
+ int consumed = opus_rc_tell_frac(rc); |
|
1899 |
+ float *norm2 = norm + 8 * 100; |
|
1900 |
+ int effective_lowband = -1; |
|
1901 |
+ unsigned int cm[2]; |
|
1902 |
+ int b; |
|
1903 |
+ |
|
1904 |
+ /* Compute how many bits we want to allocate to this band */ |
|
1905 |
+ if (i != s->startband) |
|
1906 |
+ s->remaining -= consumed; |
|
1907 |
+ s->remaining2 = totalbits - consumed - 1; |
|
1908 |
+ if (i <= s->codedbands - 1) { |
|
1909 |
+ int curr_balance = s->remaining / FFMIN(3, s->codedbands-i); |
|
1910 |
+ b = av_clip(FFMIN(s->remaining2 + 1, s->pulses[i] + curr_balance), 0, 16383); |
|
1911 |
+ } else |
|
1912 |
+ b = 0; |
|
1913 |
+ |
|
1914 |
+ if (celt_freq_bands[i] - celt_freq_range[i] >= celt_freq_bands[s->startband] && |
|
1915 |
+ (update_lowband || lowband_offset == 0)) |
|
1916 |
+ lowband_offset = i; |
|
1917 |
+ |
|
1918 |
+ /* Get a conservative estimate of the collapse_mask's for the bands we're |
|
1919 |
+ going to be folding from. */ |
|
1920 |
+ if (lowband_offset != 0 && (s->spread != CELT_SPREAD_AGGRESSIVE || |
|
1921 |
+ s->blocks > 1 || s->tf_change[i] < 0)) { |
|
1922 |
+ int foldstart, foldend; |
|
1923 |
+ |
|
1924 |
+ /* This ensures we never repeat spectral content within one band */ |
|
1925 |
+ effective_lowband = FFMAX(celt_freq_bands[s->startband], |
|
1926 |
+ celt_freq_bands[lowband_offset] - celt_freq_range[i]); |
|
1927 |
+ foldstart = lowband_offset; |
|
1928 |
+ while (celt_freq_bands[--foldstart] > effective_lowband); |
|
1929 |
+ foldend = lowband_offset - 1; |
|
1930 |
+ while (celt_freq_bands[++foldend] < effective_lowband + celt_freq_range[i]); |
|
1931 |
+ |
|
1932 |
+ cm[0] = cm[1] = 0; |
|
1933 |
+ for (j = foldstart; j < foldend; j++) { |
|
1934 |
+ cm[0] |= s->frame[0].collapse_masks[j]; |
|
1935 |
+ cm[1] |= s->frame[s->coded_channels - 1].collapse_masks[j]; |
|
1936 |
+ } |
|
1937 |
+ } else |
|
1938 |
+ /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost |
|
1939 |
+ always) be non-zero.*/ |
|
1940 |
+ cm[0] = cm[1] = (1 << s->blocks) - 1; |
|
1941 |
+ |
|
1942 |
+ if (s->dualstereo && i == s->intensitystereo) { |
|
1943 |
+ /* Switch off dual stereo to do intensity */ |
|
1944 |
+ s->dualstereo = 0; |
|
1945 |
+ for (j = celt_freq_bands[s->startband] << s->duration; j < band_offset; j++) |
|
1946 |
+ norm[j] = (norm[j] + norm2[j]) / 2; |
|
1947 |
+ } |
|
1948 |
+ |
|
1949 |
+ if (s->dualstereo) { |
|
1950 |
+ cm[0] = celt_decode_band(s, rc, i, X, NULL, band_size, b / 2, s->blocks, |
|
1951 |
+ effective_lowband != -1 ? norm + (effective_lowband << s->duration) : NULL, s->duration, |
|
1952 |
+ norm + band_offset, 0, 1.0f, lowband_scratch, cm[0]); |
|
1953 |
+ |
|
1954 |
+ cm[1] = celt_decode_band(s, rc, i, Y, NULL, band_size, b/2, s->blocks, |
|
1955 |
+ effective_lowband != -1 ? norm2 + (effective_lowband << s->duration) : NULL, s->duration, |
|
1956 |
+ norm2 + band_offset, 0, 1.0f, lowband_scratch, cm[1]); |
|
1957 |
+ } else { |
|
1958 |
+ cm[0] = celt_decode_band(s, rc, i, X, Y, band_size, b, s->blocks, |
|
1959 |
+ effective_lowband != -1 ? norm + (effective_lowband << s->duration) : NULL, s->duration, |
|
1960 |
+ norm + band_offset, 0, 1.0f, lowband_scratch, cm[0]|cm[1]); |
|
1961 |
+ |
|
1962 |
+ cm[1] = cm[0]; |
|
1963 |
+ } |
|
1964 |
+ |
|
1965 |
+ s->frame[0].collapse_masks[i] = (uint8_t)cm[0]; |
|
1966 |
+ s->frame[s->coded_channels - 1].collapse_masks[i] = (uint8_t)cm[1]; |
|
1967 |
+ s->remaining += s->pulses[i] + consumed; |
|
1968 |
+ |
|
1969 |
+ /* Update the folding position only as long as we have 1 bit/sample depth */ |
|
1970 |
+ update_lowband = (b > band_size << 3); |
|
1971 |
+ } |
|
1972 |
+} |
|
1973 |
+ |
|
1974 |
+int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc, |
|
1975 |
+ float **output, int coded_channels, int frame_size, |
|
1976 |
+ int startband, int endband) |
|
1977 |
+{ |
|
1978 |
+ int i, j; |
|
1979 |
+ |
|
1980 |
+ int consumed; // bits of entropy consumed thus far for this frame |
|
1981 |
+ int silence = 0; |
|
1982 |
+ int transient = 0; |
|
1983 |
+ int anticollapse = 0; |
|
1984 |
+ CeltIMDCTContext *imdct; |
|
1985 |
+ float imdct_scale = 1.0; |
|
1986 |
+ |
|
1987 |
+ if (coded_channels != 1 && coded_channels != 2) { |
|
1988 |
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coded channels: %d\n", |
|
1989 |
+ coded_channels); |
|
1990 |
+ return AVERROR_INVALIDDATA; |
|
1991 |
+ } |
|
1992 |
+ if (startband < 0 || startband > endband || endband > CELT_MAX_BANDS) { |
|
1993 |
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid start/end band: %d %d\n", |
|
1994 |
+ startband, endband); |
|
1995 |
+ return AVERROR_INVALIDDATA; |
|
1996 |
+ } |
|
1997 |
+ |
|
1998 |
+ s->flushed = 0; |
|
1999 |
+ s->coded_channels = coded_channels; |
|
2000 |
+ s->startband = startband; |
|
2001 |
+ s->endband = endband; |
|
2002 |
+ s->framebits = rc->rb.bytes * 8; |
|
2003 |
+ |
|
2004 |
+ s->duration = av_log2(frame_size / CELT_SHORT_BLOCKSIZE); |
|
2005 |
+ if (s->duration > CELT_MAX_LOG_BLOCKS || |
|
2006 |
+ frame_size != CELT_SHORT_BLOCKSIZE * (1 << s->duration)) { |
|
2007 |
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid CELT frame size: %d\n", |
|
2008 |
+ frame_size); |
|
2009 |
+ return AVERROR_INVALIDDATA; |
|
2010 |
+ } |
|
2011 |
+ |
|
2012 |
+ if (!s->output_channels) |
|
2013 |
+ s->output_channels = coded_channels; |
|
2014 |
+ |
|
2015 |
+ memset(s->frame[0].collapse_masks, 0, sizeof(s->frame[0].collapse_masks)); |
|
2016 |
+ memset(s->frame[1].collapse_masks, 0, sizeof(s->frame[1].collapse_masks)); |
|
2017 |
+ |
|
2018 |
+ consumed = opus_rc_tell(rc); |
|
2019 |
+ |
|
2020 |
+ /* obtain silence flag */ |
|
2021 |
+ if (consumed >= s->framebits) |
|
2022 |
+ silence = 1; |
|
2023 |
+ else if (consumed == 1) |
|
2024 |
+ silence = opus_rc_p2model(rc, 15); |
|
2025 |
+ |
|
2026 |
+ |
|
2027 |
+ if (silence) { |
|
2028 |
+ consumed = s->framebits; |
|
2029 |
+ rc->total_read_bits += s->framebits - opus_rc_tell(rc); |
|
2030 |
+ } |
|
2031 |
+ |
|
2032 |
+ /* obtain post-filter options */ |
|
2033 |
+ consumed = parse_postfilter(s, rc, consumed); |
|
2034 |
+ |
|
2035 |
+ /* obtain transient flag */ |
|
2036 |
+ if (s->duration != 0 && consumed+3 <= s->framebits) |
|
2037 |
+ transient = opus_rc_p2model(rc, 3); |
|
2038 |
+ |
|
2039 |
+ s->blocks = transient ? 1 << s->duration : 1; |
|
2040 |
+ s->blocksize = frame_size / s->blocks; |
|
2041 |
+ |
|
2042 |
+ imdct = s->imdct[transient ? 0 : s->duration]; |
|
2043 |
+ |
|
2044 |
+ if (coded_channels == 1) { |
|
2045 |
+ for (i = 0; i < CELT_MAX_BANDS; i++) |
|
2046 |
+ s->frame[0].energy[i] = FFMAX(s->frame[0].energy[i], s->frame[1].energy[i]); |
|
2047 |
+ } |
|
2048 |
+ |
|
2049 |
+ celt_decode_coarse_energy(s, rc); |
|
2050 |
+ celt_decode_tf_changes (s, rc, transient); |
|
2051 |
+ celt_decode_allocation (s, rc); |
|
2052 |
+ celt_decode_fine_energy (s, rc); |
|
2053 |
+ celt_decode_bands (s, rc); |
|
2054 |
+ |
|
2055 |
+ if (s->anticollapse_bit) |
|
2056 |
+ anticollapse = opus_getrawbits(rc, 1); |
|
2057 |
+ |
|
2058 |
+ celt_decode_final_energy(s, rc, s->framebits - opus_rc_tell(rc)); |
|
2059 |
+ |
|
2060 |
+ /* apply anti-collapse processing and denormalization to |
|
2061 |
+ * each coded channel */ |
|
2062 |
+ for (i = 0; i < s->coded_channels; i++) { |
|
2063 |
+ CeltFrame *frame = &s->frame[i]; |
|
2064 |
+ |
|
2065 |
+ if (anticollapse) |
|
2066 |
+ process_anticollapse(s, frame, s->coeffs[i]); |
|
2067 |
+ |
|
2068 |
+ celt_denormalize(s, frame, s->coeffs[i]); |
|
2069 |
+ } |
|
2070 |
+ |
|
2071 |
+ /* stereo -> mono downmix */ |
|
2072 |
+ if (s->output_channels < s->coded_channels) { |
|
2073 |
+ s->dsp.vector_fmac_scalar(s->coeffs[0], s->coeffs[1], 1.0, FFALIGN(frame_size, 16)); |
|
2074 |
+ imdct_scale = 0.5; |
|
2075 |
+ } else if (s->output_channels > s->coded_channels) |
|
2076 |
+ memcpy(s->coeffs[1], s->coeffs[0], frame_size * sizeof(float)); |
|
2077 |
+ |
|
2078 |
+ if (silence) { |
|
2079 |
+ for (i = 0; i < 2; i++) { |
|
2080 |
+ CeltFrame *frame = &s->frame[i]; |
|
2081 |
+ |
|
2082 |
+ for (j = 0; j < FF_ARRAY_ELEMS(frame->energy); j++) |
|
2083 |
+ frame->energy[j] = CELT_ENERGY_SILENCE; |
|
2084 |
+ } |
|
2085 |
+ memset(s->coeffs, 0, sizeof(s->coeffs)); |
|
2086 |
+ } |
|
2087 |
+ |
|
2088 |
+ /* transform and output for each output channel */ |
|
2089 |
+ for (i = 0; i < s->output_channels; i++) { |
|
2090 |
+ CeltFrame *frame = &s->frame[i]; |
|
2091 |
+ float m = frame->deemph_coeff; |
|
2092 |
+ |
|
2093 |
+ /* iMDCT and overlap-add */ |
|
2094 |
+ for (j = 0; j < s->blocks; j++) { |
|
2095 |
+ float *dst = frame->buf + 1024 + j * s->blocksize; |
|
2096 |
+ |
|
2097 |
+ ff_celt_imdct_half(imdct, dst + CELT_OVERLAP / 2, s->coeffs[i] + j, |
|
2098 |
+ s->blocks, imdct_scale); |
|
2099 |
+ s->dsp.vector_fmul_window(dst, dst, dst + CELT_OVERLAP / 2, |
|
2100 |
+ celt_window, CELT_OVERLAP / 2); |
|
2101 |
+ } |
|
2102 |
+ |
|
2103 |
+ /* postfilter */ |
|
2104 |
+ celt_postfilter(s, frame); |
|
2105 |
+ |
|
2106 |
+ /* deemphasis and output scaling */ |
|
2107 |
+ for (j = 0; j < frame_size; j++) { |
|
2108 |
+ float tmp = frame->buf[1024 - frame_size + j] + m; |
|
2109 |
+ m = tmp * CELT_DEEMPH_COEFF; |
|
2110 |
+ output[i][j] = tmp / 32768.; |
|
2111 |
+ } |
|
2112 |
+ frame->deemph_coeff = m; |
|
2113 |
+ } |
|
2114 |
+ |
|
2115 |
+ if (coded_channels == 1) |
|
2116 |
+ memcpy(s->frame[1].energy, s->frame[0].energy, sizeof(s->frame[0].energy)); |
|
2117 |
+ |
|
2118 |
+ for (i = 0; i < 2; i++ ) { |
|
2119 |
+ CeltFrame *frame = &s->frame[i]; |
|
2120 |
+ |
|
2121 |
+ if (!transient) { |
|
2122 |
+ memcpy(frame->prev_energy[1], frame->prev_energy[0], sizeof(frame->prev_energy[0])); |
|
2123 |
+ memcpy(frame->prev_energy[0], frame->energy, sizeof(frame->prev_energy[0])); |
|
2124 |
+ } else { |
|
2125 |
+ for (j = 0; j < CELT_MAX_BANDS; j++) |
|
2126 |
+ frame->prev_energy[0][j] = FFMIN(frame->prev_energy[0][j], frame->energy[j]); |
|
2127 |
+ } |
|
2128 |
+ |
|
2129 |
+ for (j = 0; j < s->startband; j++) { |
|
2130 |
+ frame->prev_energy[0][j] = CELT_ENERGY_SILENCE; |
|
2131 |
+ frame->energy[j] = 0.0; |
|
2132 |
+ } |
|
2133 |
+ for (j = s->endband; j < CELT_MAX_BANDS; j++) { |
|
2134 |
+ frame->prev_energy[0][j] = CELT_ENERGY_SILENCE; |
|
2135 |
+ frame->energy[j] = 0.0; |
|
2136 |
+ } |
|
2137 |
+ } |
|
2138 |
+ |
|
2139 |
+ s->seed = rc->range; |
|
2140 |
+ |
|
2141 |
+ return 0; |
|
2142 |
+} |
|
2143 |
+ |
|
2144 |
+void ff_celt_flush(CeltContext *s) |
|
2145 |
+{ |
|
2146 |
+ int i, j; |
|
2147 |
+ |
|
2148 |
+ if (s->flushed) |
|
2149 |
+ return; |
|
2150 |
+ |
|
2151 |
+ for (i = 0; i < 2; i++) { |
|
2152 |
+ CeltFrame *frame = &s->frame[i]; |
|
2153 |
+ |
|
2154 |
+ for (j = 0; j < CELT_MAX_BANDS; j++) |
|
2155 |
+ frame->prev_energy[0][j] = frame->prev_energy[1][j] = CELT_ENERGY_SILENCE; |
|
2156 |
+ |
|
2157 |
+ memset(frame->energy, 0, sizeof(frame->energy)); |
|
2158 |
+ memset(frame->buf, 0, sizeof(frame->buf)); |
|
2159 |
+ |
|
2160 |
+ memset(frame->pf_gains, 0, sizeof(frame->pf_gains)); |
|
2161 |
+ memset(frame->pf_gains_old, 0, sizeof(frame->pf_gains_old)); |
|
2162 |
+ memset(frame->pf_gains_new, 0, sizeof(frame->pf_gains_new)); |
|
2163 |
+ |
|
2164 |
+ frame->deemph_coeff = 0.0; |
|
2165 |
+ } |
|
2166 |
+ s->seed = 0; |
|
2167 |
+ |
|
2168 |
+ s->flushed = 1; |
|
2169 |
+} |
|
2170 |
+ |
|
2171 |
+void ff_celt_free(CeltContext **ps) |
|
2172 |
+{ |
|
2173 |
+ CeltContext *s = *ps; |
|
2174 |
+ int i; |
|
2175 |
+ |
|
2176 |
+ if (!s) |
|
2177 |
+ return; |
|
2178 |
+ |
|
2179 |
+ for (i = 0; i < FF_ARRAY_ELEMS(s->imdct); i++) |
|
2180 |
+ ff_celt_imdct_uninit(&s->imdct[i]); |
|
2181 |
+ |
|
2182 |
+ av_freep(ps); |
|
2183 |
+} |
|
2184 |
+ |
|
2185 |
+int ff_celt_init(AVCodecContext *avctx, CeltContext **ps, int output_channels) |
|
2186 |
+{ |
|
2187 |
+ CeltContext *s; |
|
2188 |
+ int i, ret; |
|
2189 |
+ |
|
2190 |
+ if (output_channels != 1 && output_channels != 2) { |
|
2191 |
+ av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n", |
|
2192 |
+ output_channels); |
|
2193 |
+ return AVERROR(EINVAL); |
|
2194 |
+ } |
|
2195 |
+ |
|
2196 |
+ s = av_mallocz(sizeof(*s)); |
|
2197 |
+ if (!s) |
|
2198 |
+ return AVERROR(ENOMEM); |
|
2199 |
+ |
|
2200 |
+ s->avctx = avctx; |
|
2201 |
+ s->output_channels = output_channels; |
|
2202 |
+ |
|
2203 |
+ for (i = 0; i < FF_ARRAY_ELEMS(s->imdct); i++) { |
|
2204 |
+ ret = ff_celt_imdct_init(&s->imdct[i], i + 3); |
|
2205 |
+ if (ret < 0) |
|
2206 |
+ goto fail; |
|
2207 |
+ } |
|
2208 |
+ |
|
2209 |
+ avpriv_float_dsp_init(&s->dsp, avctx->flags & CODEC_FLAG_BITEXACT); |
|
2210 |
+ |
|
2211 |
+ ff_celt_flush(s); |
|
2212 |
+ |
|
2213 |
+ *ps = s; |
|
2214 |
+ |
|
2215 |
+ return 0; |
|
2216 |
+fail: |
|
2217 |
+ ff_celt_free(&s); |
|
2218 |
+ return ret; |
|
2219 |
+} |
0 | 2220 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,268 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2013-2014 Mozilla Corporation |
|
2 |
+ * |
|
3 |
+ * This file is part of Libav. |
|
4 |
+ * |
|
5 |
+ * Libav is free software; you can redistribute it and/or |
|
6 |
+ * modify it under the terms of the GNU Lesser General Public |
|
7 |
+ * License as published by the Free Software Foundation; either |
|
8 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
9 |
+ * |
|
10 |
+ * Libav is distributed in the hope that it will be useful, |
|
11 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
+ * Lesser General Public License for more details. |
|
14 |
+ * |
|
15 |
+ * You should have received a copy of the GNU Lesser General Public |
|
16 |
+ * License along with Libav; if not, write to the Free Software |
|
17 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
+ */ |
|
19 |
+ |
|
20 |
+/** |
|
21 |
+ * @file |
|
22 |
+ * Celt non-power of 2 iMDCT |
|
23 |
+ */ |
|
24 |
+ |
|
25 |
+#include <float.h> |
|
26 |
+#include <math.h> |
|
27 |
+ |
|
28 |
+#include "libavutil/attributes.h" |
|
29 |
+#include "libavutil/common.h" |
|
30 |
+ |
|
31 |
+#include "fft.h" |
|
32 |
+#include "opus.h" |
|
33 |
+ |
|
34 |
+// complex c = a * b |
|
35 |
+#define CMUL3(cre, cim, are, aim, bre, bim) \ |
|
36 |
+do { \ |
|
37 |
+ cre = are * bre - aim * bim; \ |
|
38 |
+ cim = are * bim + aim * bre; \ |
|
39 |
+} while (0) |
|
40 |
+ |
|
41 |
+#define CMUL(c, a, b) CMUL3((c).re, (c).im, (a).re, (a).im, (b).re, (b).im) |
|
42 |
+ |
|
43 |
+// complex c = a * b |
|
44 |
+// d = a * conjugate(b) |
|
45 |
+#define CMUL2(c, d, a, b) \ |
|
46 |
+do { \ |
|
47 |
+ float are = (a).re; \ |
|
48 |
+ float aim = (a).im; \ |
|
49 |
+ float bre = (b).re; \ |
|
50 |
+ float bim = (b).im; \ |
|
51 |
+ float rr = are * bre; \ |
|
52 |
+ float ri = are * bim; \ |
|
53 |
+ float ir = aim * bre; \ |
|
54 |
+ float ii = aim * bim; \ |
|
55 |
+ (c).re = rr - ii; \ |
|
56 |
+ (c).im = ri + ir; \ |
|
57 |
+ (d).re = rr + ii; \ |
|
58 |
+ (d).im = -ri + ir; \ |
|
59 |
+} while (0) |
|
60 |
+ |
|
61 |
+struct CeltIMDCTContext { |
|
62 |
+ int fft_n; |
|
63 |
+ int len2; |
|
64 |
+ int len4; |
|
65 |
+ |
|
66 |
+ FFTComplex *tmp; |
|
67 |
+ |
|
68 |
+ FFTComplex *twiddle_exptab; |
|
69 |
+ |
|
70 |
+ FFTComplex *exptab[6]; |
|
71 |
+}; |
|
72 |
+ |
|
73 |
+av_cold void ff_celt_imdct_uninit(CeltIMDCTContext **ps) |
|
74 |
+{ |
|
75 |
+ CeltIMDCTContext *s = *ps; |
|
76 |
+ int i; |
|
77 |
+ |
|
78 |
+ if (!s) |
|
79 |
+ return; |
|
80 |
+ |
|
81 |
+ for (i = 0; i < FF_ARRAY_ELEMS(s->exptab); i++) |
|
82 |
+ av_freep(&s->exptab[i]); |
|
83 |
+ |
|
84 |
+ av_freep(&s->twiddle_exptab); |
|
85 |
+ |
|
86 |
+ av_freep(&s->tmp); |
|
87 |
+ |
|
88 |
+ av_freep(ps); |
|
89 |
+} |
|
90 |
+ |
|
91 |
+av_cold int ff_celt_imdct_init(CeltIMDCTContext **ps, int N) |
|
92 |
+{ |
|
93 |
+ CeltIMDCTContext *s; |
|
94 |
+ int len2 = 15 * (1 << N); |
|
95 |
+ int len = 2 * len2; |
|
96 |
+ int i, j; |
|
97 |
+ |
|
98 |
+ if (len2 > CELT_MAX_FRAME_SIZE) |
|
99 |
+ return AVERROR(EINVAL); |
|
100 |
+ |
|
101 |
+ s = av_mallocz(sizeof(*s)); |
|
102 |
+ if (!s) |
|
103 |
+ return AVERROR(ENOMEM); |
|
104 |
+ |
|
105 |
+ s->fft_n = N - 1; |
|
106 |
+ s->len4 = len2 / 2; |
|
107 |
+ s->len2 = len2; |
|
108 |
+ |
|
109 |
+ s->tmp = av_malloc(len * 2 * sizeof(*s->tmp)); |
|
110 |
+ if (!s->tmp) |
|
111 |
+ goto fail; |
|
112 |
+ |
|
113 |
+ s->twiddle_exptab = av_malloc(s->len4 * sizeof(*s->twiddle_exptab)); |
|
114 |
+ if (!s->twiddle_exptab) |
|
115 |
+ goto fail; |
|
116 |
+ |
|
117 |
+ for (i = 0; i < s->len4; i++) { |
|
118 |
+ s->twiddle_exptab[i].re = cos(2 * M_PI * (i + 0.125 + s->len4) / len); |
|
119 |
+ s->twiddle_exptab[i].im = sin(2 * M_PI * (i + 0.125 + s->len4) / len); |
|
120 |
+ } |
|
121 |
+ |
|
122 |
+ for (i = 0; i < FF_ARRAY_ELEMS(s->exptab); i++) { |
|
123 |
+ int N = 15 * (1 << i); |
|
124 |
+ s->exptab[i] = av_malloc(sizeof(*s->exptab[i]) * FFMAX(N, 19)); |
|
125 |
+ if (!s->exptab[i]) |
|
126 |
+ goto fail; |
|
127 |
+ |
|
128 |
+ for (j = 0; j < N; j++) { |
|
129 |
+ s->exptab[i][j].re = cos(2 * M_PI * j / N); |
|
130 |
+ s->exptab[i][j].im = sin(2 * M_PI * j / N); |
|
131 |
+ } |
|
132 |
+ } |
|
133 |
+ |
|
134 |
+ // wrap around to simplify fft15 |
|
135 |
+ for (j = 15; j < 19; j++) |
|
136 |
+ s->exptab[0][j] = s->exptab[0][j - 15]; |
|
137 |
+ |
|
138 |
+ *ps = s; |
|
139 |
+ |
|
140 |
+ return 0; |
|
141 |
+fail: |
|
142 |
+ ff_celt_imdct_uninit(&s); |
|
143 |
+ return AVERROR(ENOMEM); |
|
144 |
+} |
|
145 |
+ |
|
146 |
+static void fft5(FFTComplex *out, const FFTComplex *in, int stride) |
|
147 |
+{ |
|
148 |
+ // [0] = exp(2 * i * pi / 5), [1] = exp(2 * i * pi * 2 / 5) |
|
149 |
+ static const FFTComplex fact[] = { { 0.30901699437494745, 0.95105651629515353 }, |
|
150 |
+ { -0.80901699437494734, 0.58778525229247325 } }; |
|
151 |
+ |
|
152 |
+ FFTComplex z[4][4]; |
|
153 |
+ |
|
154 |
+ CMUL2(z[0][0], z[0][3], in[1 * stride], fact[0]); |
|
155 |
+ CMUL2(z[0][1], z[0][2], in[1 * stride], fact[1]); |
|
156 |
+ CMUL2(z[1][0], z[1][3], in[2 * stride], fact[0]); |
|
157 |
+ CMUL2(z[1][1], z[1][2], in[2 * stride], fact[1]); |
|
158 |
+ CMUL2(z[2][0], z[2][3], in[3 * stride], fact[0]); |
|
159 |
+ CMUL2(z[2][1], z[2][2], in[3 * stride], fact[1]); |
|
160 |
+ CMUL2(z[3][0], z[3][3], in[4 * stride], fact[0]); |
|
161 |
+ CMUL2(z[3][1], z[3][2], in[4 * stride], fact[1]); |
|
162 |
+ |
|
163 |
+ out[0].re = in[0].re + in[stride].re + in[2 * stride].re + in[3 * stride].re + in[4 * stride].re; |
|
164 |
+ out[0].im = in[0].im + in[stride].im + in[2 * stride].im + in[3 * stride].im + in[4 * stride].im; |
|
165 |
+ |
|
166 |
+ out[1].re = in[0].re + z[0][0].re + z[1][1].re + z[2][2].re + z[3][3].re; |
|
167 |
+ out[1].im = in[0].im + z[0][0].im + z[1][1].im + z[2][2].im + z[3][3].im; |
|
168 |
+ |
|
169 |
+ out[2].re = in[0].re + z[0][1].re + z[1][3].re + z[2][0].re + z[3][2].re; |
|
170 |
+ out[2].im = in[0].im + z[0][1].im + z[1][3].im + z[2][0].im + z[3][2].im; |
|
171 |
+ |
|
172 |
+ out[3].re = in[0].re + z[0][2].re + z[1][0].re + z[2][3].re + z[3][1].re; |
|
173 |
+ out[3].im = in[0].im + z[0][2].im + z[1][0].im + z[2][3].im + z[3][1].im; |
|
174 |
+ |
|
175 |
+ out[4].re = in[0].re + z[0][3].re + z[1][2].re + z[2][1].re + z[3][0].re; |
|
176 |
+ out[4].im = in[0].im + z[0][3].im + z[1][2].im + z[2][1].im + z[3][0].im; |
|
177 |
+} |
|
178 |
+ |
|
179 |
+static void fft15(CeltIMDCTContext *s, FFTComplex *out, const FFTComplex *in, int stride) |
|
180 |
+{ |
|
181 |
+ const FFTComplex *exptab = s->exptab[0]; |
|
182 |
+ FFTComplex tmp[5]; |
|
183 |
+ FFTComplex tmp1[5]; |
|
184 |
+ FFTComplex tmp2[5]; |
|
185 |
+ int k; |
|
186 |
+ |
|
187 |
+ fft5(tmp, in, stride * 3); |
|
188 |
+ fft5(tmp1, in + stride, stride * 3); |
|
189 |
+ fft5(tmp2, in + 2 * stride, stride * 3); |
|
190 |
+ |
|
191 |
+ for (k = 0; k < 5; k++) { |
|
192 |
+ FFTComplex t1, t2; |
|
193 |
+ |
|
194 |
+ CMUL(t1, tmp1[k], exptab[k]); |
|
195 |
+ CMUL(t2, tmp2[k], exptab[2 * k]); |
|
196 |
+ out[k].re = tmp[k].re + t1.re + t2.re; |
|
197 |
+ out[k].im = tmp[k].im + t1.im + t2.im; |
|
198 |
+ |
|
199 |
+ CMUL(t1, tmp1[k], exptab[k + 5]); |
|
200 |
+ CMUL(t2, tmp2[k], exptab[2 * (k + 5)]); |
|
201 |
+ out[k + 5].re = tmp[k].re + t1.re + t2.re; |
|
202 |
+ out[k + 5].im = tmp[k].im + t1.im + t2.im; |
|
203 |
+ |
|
204 |
+ CMUL(t1, tmp1[k], exptab[k + 10]); |
|
205 |
+ CMUL(t2, tmp2[k], exptab[2 * k + 5]); |
|
206 |
+ out[k + 10].re = tmp[k].re + t1.re + t2.re; |
|
207 |
+ out[k + 10].im = tmp[k].im + t1.im + t2.im; |
|
208 |
+ } |
|
209 |
+} |
|
210 |
+ |
|
211 |
+/* |
|
212 |
+ * FFT of the length 15 * (2^N) |
|
213 |
+ */ |
|
214 |
+static void fft_calc(CeltIMDCTContext *s, FFTComplex *out, const FFTComplex *in, int N, int stride) |
|
215 |
+{ |
|
216 |
+ if (N) { |
|
217 |
+ const FFTComplex *exptab = s->exptab[N]; |
|
218 |
+ const int len2 = 15 * (1 << (N - 1)); |
|
219 |
+ int k; |
|
220 |
+ |
|
221 |
+ fft_calc(s, out, in, N - 1, stride * 2); |
|
222 |
+ fft_calc(s, out + len2, in + stride, N - 1, stride * 2); |
|
223 |
+ |
|
224 |
+ for (k = 0; k < len2; k++) { |
|
225 |
+ FFTComplex t; |
|
226 |
+ |
|
227 |
+ CMUL(t, out[len2 + k], exptab[k]); |
|
228 |
+ |
|
229 |
+ out[len2 + k].re = out[k].re - t.re; |
|
230 |
+ out[len2 + k].im = out[k].im - t.im; |
|
231 |
+ |
|
232 |
+ out[k].re += t.re; |
|
233 |
+ out[k].im += t.im; |
|
234 |
+ } |
|
235 |
+ } else |
|
236 |
+ fft15(s, out, in, stride); |
|
237 |
+} |
|
238 |
+ |
|
239 |
+void ff_celt_imdct_half(CeltIMDCTContext *s, float *dst, const float *src, |
|
240 |
+ int stride, float scale) |
|
241 |
+{ |
|
242 |
+ FFTComplex *z = (FFTComplex *)dst; |
|
243 |
+ const int len8 = s->len4 / 2; |
|
244 |
+ const float *in1 = src; |
|
245 |
+ const float *in2 = src + (s->len2 - 1) * stride; |
|
246 |
+ int i; |
|
247 |
+ |
|
248 |
+ for (i = 0; i < s->len4; i++) { |
|
249 |
+ FFTComplex tmp = { *in2, *in1 }; |
|
250 |
+ CMUL(s->tmp[i], tmp, s->twiddle_exptab[i]); |
|
251 |
+ in1 += 2 * stride; |
|
252 |
+ in2 -= 2 * stride; |
|
253 |
+ } |
|
254 |
+ |
|
255 |
+ fft_calc(s, z, s->tmp, s->fft_n, 1); |
|
256 |
+ |
|
257 |
+ for (i = 0; i < len8; i++) { |
|
258 |
+ float r0, i0, r1, i1; |
|
259 |
+ |
|
260 |
+ CMUL3(r0, i1, z[len8 - i - 1].im, z[len8 - i - 1].re, s->twiddle_exptab[len8 - i - 1].im, s->twiddle_exptab[len8 - i - 1].re); |
|
261 |
+ CMUL3(r1, i0, z[len8 + i].im, z[len8 + i].re, s->twiddle_exptab[len8 + i].im, s->twiddle_exptab[len8 + i].re); |
|
262 |
+ z[len8 - i - 1].re = scale * r0; |
|
263 |
+ z[len8 - i - 1].im = scale * i0; |
|
264 |
+ z[len8 + i].re = scale * r1; |
|
265 |
+ z[len8 + i].im = scale * i1; |
|
266 |
+ } |
|
267 |
+} |
0 | 268 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,75 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2013-2014 Mozilla Corporation |
|
2 |
+ * |
|
3 |
+ * This file is part of Libav. |
|
4 |
+ * |
|
5 |
+ * Libav is free software; you can redistribute it and/or |
|
6 |
+ * modify it under the terms of the GNU Lesser General Public |
|
7 |
+ * License as published by the Free Software Foundation; either |
|
8 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
9 |
+ * |
|
10 |
+ * Libav is distributed in the hope that it will be useful, |
|
11 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
+ * Lesser General Public License for more details. |
|
14 |
+ * |
|
15 |
+ * You should have received a copy of the GNU Lesser General Public |
|
16 |
+ * License along with Libav; if not, write to the Free Software |
|
17 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
+ */ |
|
19 |
+ |
|
20 |
+/** |
|
21 |
+ * @file |
|
22 |
+ * Opus parser |
|
23 |
+ * |
|
24 |
+ * Determines the duration for each packet. |
|
25 |
+ */ |
|
26 |
+ |
|
27 |
+#include "avcodec.h" |
|
28 |
+#include "opus.h" |
|
29 |
+ |
|
30 |
+typedef struct OpusParseContext { |
|
31 |
+ OpusContext ctx; |
|
32 |
+ OpusPacket pkt; |
|
33 |
+ int extradata_parsed; |
|
34 |
+} OpusParseContext; |
|
35 |
+ |
|
36 |
+static int opus_parse(AVCodecParserContext *ctx, AVCodecContext *avctx, |
|
37 |
+ const uint8_t **poutbuf, int *poutbuf_size, |
|
38 |
+ const uint8_t *buf, int buf_size) |
|
39 |
+{ |
|
40 |
+ OpusParseContext *s = ctx->priv_data; |
|
41 |
+ int ret; |
|
42 |
+ |
|
43 |
+ if (!buf_size) |
|
44 |
+ return 0; |
|
45 |
+ |
|
46 |
+ if (avctx->extradata && !s->extradata_parsed) { |
|
47 |
+ ret = ff_opus_parse_extradata(avctx, &s->ctx); |
|
48 |
+ if (ret < 0) { |
|
49 |
+ av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n"); |
|
50 |
+ goto fail; |
|
51 |
+ } |
|
52 |
+ av_freep(&s->ctx.channel_maps); |
|
53 |
+ s->extradata_parsed = 1; |
|
54 |
+ } |
|
55 |
+ |
|
56 |
+ ret = ff_opus_parse_packet(&s->pkt, buf, buf_size, s->ctx.nb_streams > 1); |
|
57 |
+ if (ret < 0) { |
|
58 |
+ av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n"); |
|
59 |
+ goto fail; |
|
60 |
+ } |
|
61 |
+ |
|
62 |
+ ctx->duration = s->pkt.frame_count * s->pkt.frame_duration; |
|
63 |
+ |
|
64 |
+fail: |
|
65 |
+ *poutbuf = buf; |
|
66 |
+ *poutbuf_size = buf_size; |
|
67 |
+ return buf_size; |
|
68 |
+} |
|
69 |
+ |
|
70 |
+AVCodecParser ff_opus_parser = { |
|
71 |
+ .codec_ids = { AV_CODEC_ID_OPUS }, |
|
72 |
+ .priv_data_size = sizeof(OpusParseContext), |
|
73 |
+ .parser_parse = opus_parse, |
|
74 |
+}; |
0 | 75 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,1597 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2012 Andrew D'Addesio |
|
2 |
+ * Copyright (c) 2013-2014 Mozilla Corporation |
|
3 |
+ * |
|
4 |
+ * This file is part of Libav. |
|
5 |
+ * |
|
6 |
+ * Libav is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * Libav is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with Libav; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+/** |
|
22 |
+ * @file |
|
23 |
+ * Opus SILK decoder |
|
24 |
+ */ |
|
25 |
+ |
|
26 |
+#include <stdint.h> |
|
27 |
+ |
|
28 |
+#include "opus.h" |
|
29 |
+ |
|
30 |
+typedef struct SilkFrame { |
|
31 |
+ int coded; |
|
32 |
+ int log_gain; |
|
33 |
+ int16_t nlsf[16]; |
|
34 |
+ float lpc[16]; |
|
35 |
+ |
|
36 |
+ float output [2 * SILK_HISTORY]; |
|
37 |
+ float lpc_history[2 * SILK_HISTORY]; |
|
38 |
+ int primarylag; |
|
39 |
+ |
|
40 |
+ int prev_voiced; |
|
41 |
+} SilkFrame; |
|
42 |
+ |
|
43 |
+struct SilkContext { |
|
44 |
+ AVCodecContext *avctx; |
|
45 |
+ int output_channels; |
|
46 |
+ |
|
47 |
+ int midonly; |
|
48 |
+ int subframes; |
|
49 |
+ int sflength; |
|
50 |
+ int flength; |
|
51 |
+ int nlsf_interp_factor; |
|
52 |
+ |
|
53 |
+ enum OpusBandwidth bandwidth; |
|
54 |
+ int wb; |
|
55 |
+ |
|
56 |
+ SilkFrame frame[2]; |
|
57 |
+ float prev_stereo_weights[2]; |
|
58 |
+ float stereo_weights[2]; |
|
59 |
+ |
|
60 |
+ int prev_coded_channels; |
|
61 |
+}; |
|
62 |
+ |
|
63 |
+static const uint16_t silk_model_stereo_s1[] = { |
|
64 |
+ 256, 7, 9, 10, 11, 12, 22, 46, 54, 55, 56, 59, 82, 174, 197, 200, |
|
65 |
+ 201, 202, 210, 234, 244, 245, 246, 247, 249, 256 |
|
66 |
+}; |
|
67 |
+ |
|
68 |
+static const uint16_t silk_model_stereo_s2[] = {256, 85, 171, 256}; |
|
69 |
+ |
|
70 |
+static const uint16_t silk_model_stereo_s3[] = {256, 51, 102, 154, 205, 256}; |
|
71 |
+ |
|
72 |
+static const uint16_t silk_model_mid_only[] = {256, 192, 256}; |
|
73 |
+ |
|
74 |
+static const uint16_t silk_model_frame_type_inactive[] = {256, 26, 256}; |
|
75 |
+ |
|
76 |
+static const uint16_t silk_model_frame_type_active[] = {256, 24, 98, 246, 256}; |
|
77 |
+ |
|
78 |
+static const uint16_t silk_model_gain_highbits[3][9] = { |
|
79 |
+ {256, 32, 144, 212, 241, 253, 254, 255, 256}, |
|
80 |
+ {256, 2, 19, 64, 124, 186, 233, 252, 256}, |
|
81 |
+ {256, 1, 4, 30, 101, 195, 245, 254, 256} |
|
82 |
+}; |
|
83 |
+ |
|
84 |
+static const uint16_t silk_model_gain_lowbits[] = {256, 32, 64, 96, 128, 160, 192, 224, 256}; |
|
85 |
+ |
|
86 |
+static const uint16_t silk_model_gain_delta[] = { |
|
87 |
+ 256, 6, 11, 22, 53, 185, 206, 214, 218, 221, 223, 225, 227, 228, 229, 230, |
|
88 |
+ 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, |
|
89 |
+ 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 |
|
90 |
+}; |
|
91 |
+static const uint16_t silk_model_lsf_s1[2][2][33] = { |
|
92 |
+ { |
|
93 |
+ { // NB or MB, unvoiced |
|
94 |
+ 256, 44, 78, 108, 127, 148, 160, 171, 174, 177, 179, 195, 197, 199, 200, 205, |
|
95 |
+ 207, 208, 211, 214, 215, 216, 218, 220, 222, 225, 226, 235, 244, 246, 253, 255, 256 |
|
96 |
+ }, { // NB or MB, voiced |
|
97 |
+ 256, 1, 11, 12, 20, 23, 31, 39, 53, 66, 80, 81, 95, 107, 120, 131, |
|
98 |
+ 142, 154, 165, 175, 185, 196, 204, 213, 221, 228, 236, 237, 238, 244, 245, 251, 256 |
|
99 |
+ } |
|
100 |
+ }, { |
|
101 |
+ { // WB, unvoiced |
|
102 |
+ 256, 31, 52, 55, 72, 73, 81, 98, 102, 103, 121, 137, 141, 143, 146, 147, |
|
103 |
+ 157, 158, 161, 177, 188, 204, 206, 208, 211, 213, 224, 225, 229, 238, 246, 253, 256 |
|
104 |
+ }, { // WB, voiced |
|
105 |
+ 256, 1, 5, 21, 26, 44, 55, 60, 74, 89, 90, 93, 105, 118, 132, 146, |
|
106 |
+ 152, 166, 178, 180, 186, 187, 199, 211, 222, 232, 235, 245, 250, 251, 252, 253, 256 |
|
107 |
+ } |
|
108 |
+ } |
|
109 |
+}; |
|
110 |
+ |
|
111 |
+static const uint16_t silk_model_lsf_s2[32][10] = { |
|
112 |
+ // NB, MB |
|
113 |
+ { 256, 1, 2, 3, 18, 242, 253, 254, 255, 256 }, |
|
114 |
+ { 256, 1, 2, 4, 38, 221, 253, 254, 255, 256 }, |
|
115 |
+ { 256, 1, 2, 6, 48, 197, 252, 254, 255, 256 }, |
|
116 |
+ { 256, 1, 2, 10, 62, 185, 246, 254, 255, 256 }, |
|
117 |
+ { 256, 1, 4, 20, 73, 174, 248, 254, 255, 256 }, |
|
118 |
+ { 256, 1, 4, 21, 76, 166, 239, 254, 255, 256 }, |
|
119 |
+ { 256, 1, 8, 32, 85, 159, 226, 252, 255, 256 }, |
|
120 |
+ { 256, 1, 2, 20, 83, 161, 219, 249, 255, 256 }, |
|
121 |
+ |
|
122 |
+ // WB |
|
123 |
+ { 256, 1, 2, 3, 12, 244, 253, 254, 255, 256 }, |
|
124 |
+ { 256, 1, 2, 4, 32, 218, 253, 254, 255, 256 }, |
|
125 |
+ { 256, 1, 2, 5, 47, 199, 252, 254, 255, 256 }, |
|
126 |
+ { 256, 1, 2, 12, 61, 187, 252, 254, 255, 256 }, |
|
127 |
+ { 256, 1, 5, 24, 72, 172, 249, 254, 255, 256 }, |
|
128 |
+ { 256, 1, 2, 16, 70, 170, 242, 254, 255, 256 }, |
|
129 |
+ { 256, 1, 2, 17, 78, 165, 226, 251, 255, 256 }, |
|
130 |
+ { 256, 1, 8, 29, 79, 156, 237, 254, 255, 256 } |
|
131 |
+}; |
|
132 |
+ |
|
133 |
+static const uint16_t silk_model_lsf_s2_ext[] = { 256, 156, 216, 240, 249, 253, 255, 256 }; |
|
134 |
+ |
|
135 |
+static const uint16_t silk_model_lsf_interpolation_offset[] = { 256, 13, 35, 64, 75, 256 }; |
|
136 |
+ |
|
137 |
+static const uint16_t silk_model_pitch_highbits[] = { |
|
138 |
+ 256, 3, 6, 12, 23, 44, 74, 106, 125, 136, 146, 158, 171, 184, 196, 207, |
|
139 |
+ 216, 224, 231, 237, 241, 243, 245, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 |
|
140 |
+}; |
|
141 |
+ |
|
142 |
+static const uint16_t silk_model_pitch_lowbits_nb[]= { 256, 64, 128, 192, 256 }; |
|
143 |
+ |
|
144 |
+static const uint16_t silk_model_pitch_lowbits_mb[]= { 256, 43, 85, 128, 171, 213, 256 }; |
|
145 |
+ |
|
146 |
+static const uint16_t silk_model_pitch_lowbits_wb[]= { 256, 32, 64, 96, 128, 160, 192, 224, 256 }; |
|
147 |
+ |
|
148 |
+static const uint16_t silk_model_pitch_delta[] = { |
|
149 |
+ 256, 46, 48, 50, 53, 57, 63, 73, 88, 114, 152, 182, 204, 219, 229, 236, |
|
150 |
+ 242, 246, 250, 252, 254, 256 |
|
151 |
+}; |
|
152 |
+ |
|
153 |
+static const uint16_t silk_model_pitch_contour_nb10ms[] = { 256, 143, 193, 256 }; |
|
154 |
+ |
|
155 |
+static const uint16_t silk_model_pitch_contour_nb20ms[] = { |
|
156 |
+ 256, 68, 80, 101, 118, 137, 159, 189, 213, 230, 246, 256 |
|
157 |
+}; |
|
158 |
+ |
|
159 |
+static const uint16_t silk_model_pitch_contour_mbwb10ms[] = { |
|
160 |
+ 256, 91, 137, 176, 195, 209, 221, 229, 236, 242, 247, 252, 256 |
|
161 |
+}; |
|
162 |
+ |
|
163 |
+static const uint16_t silk_model_pitch_contour_mbwb20ms[] = { |
|
164 |
+ 256, 33, 55, 73, 89, 104, 118, 132, 145, 158, 168, 177, 186, 194, 200, 206, |
|
165 |
+ 212, 217, 221, 225, 229, 232, 235, 238, 240, 242, 244, 246, 248, 250, 252, 253, |
|
166 |
+ 254, 255, 256 |
|
167 |
+}; |
|
168 |
+ |
|
169 |
+static const uint16_t silk_model_ltp_filter[] = { 256, 77, 157, 256 }; |
|
170 |
+ |
|
171 |
+static const uint16_t silk_model_ltp_filter0_sel[] = { |
|
172 |
+ 256, 185, 200, 213, 226, 235, 244, 250, 256 |
|
173 |
+}; |
|
174 |
+ |
|
175 |
+static const uint16_t silk_model_ltp_filter1_sel[] = { |
|
176 |
+ 256, 57, 91, 112, 132, 147, 160, 172, 185, 195, 205, 214, 224, 233, 241, 248, 256 |
|
177 |
+}; |
|
178 |
+ |
|
179 |
+static const uint16_t silk_model_ltp_filter2_sel[] = { |
|
180 |
+ 256, 15, 31, 45, 57, 69, 81, 92, 103, 114, 124, 133, 142, 151, 160, 168, |
|
181 |
+ 176, 184, 192, 199, 206, 212, 218, 223, 227, 232, 236, 240, 244, 247, 251, 254, 256 |
|
182 |
+}; |
|
183 |
+ |
|
184 |
+static const uint16_t silk_model_ltp_scale_index[] = { 256, 128, 192, 256 }; |
|
185 |
+ |
|
186 |
+static const uint16_t silk_model_lcg_seed[] = { 256, 64, 128, 192, 256 }; |
|
187 |
+ |
|
188 |
+static const uint16_t silk_model_exc_rate[2][10] = { |
|
189 |
+ { 256, 15, 66, 78, 124, 169, 182, 215, 242, 256 }, // unvoiced |
|
190 |
+ { 256, 33, 63, 99, 116, 150, 199, 217, 238, 256 } // voiced |
|
191 |
+}; |
|
192 |
+ |
|
193 |
+static const uint16_t silk_model_pulse_count[11][19] = { |
|
194 |
+ { 256, 131, 205, 230, 238, 241, 244, 245, 246, |
|
195 |
+ 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 }, |
|
196 |
+ { 256, 58, 151, 211, 234, 241, 244, 245, 246, |
|
197 |
+ 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 }, |
|
198 |
+ { 256, 43, 94, 140, 173, 197, 213, 224, 232, |
|
199 |
+ 238, 241, 244, 247, 249, 250, 251, 253, 254, 256 }, |
|
200 |
+ { 256, 17, 69, 140, 197, 228, 240, 245, 246, |
|
201 |
+ 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 }, |
|
202 |
+ { 256, 6, 27, 68, 121, 170, 205, 226, 237, |
|
203 |
+ 243, 246, 248, 250, 251, 252, 253, 254, 255, 256 }, |
|
204 |
+ { 256, 7, 21, 43, 71, 100, 128, 153, 173, |
|
205 |
+ 190, 203, 214, 223, 230, 235, 239, 243, 246, 256 }, |
|
206 |
+ { 256, 2, 7, 21, 50, 92, 138, 179, 210, |
|
207 |
+ 229, 240, 246, 249, 251, 252, 253, 254, 255, 256 }, |
|
208 |
+ { 256, 1, 3, 7, 17, 36, 65, 100, 137, |
|
209 |
+ 171, 199, 219, 233, 241, 246, 250, 252, 254, 256 }, |
|
210 |
+ { 256, 1, 3, 5, 10, 19, 33, 53, 77, |
|
211 |
+ 104, 132, 158, 181, 201, 216, 227, 235, 241, 256 }, |
|
212 |
+ { 256, 1, 2, 3, 9, 36, 94, 150, 189, |
|
213 |
+ 214, 228, 238, 244, 247, 250, 252, 253, 254, 256 }, |
|
214 |
+ { 256, 2, 3, 9, 36, 94, 150, 189, 214, |
|
215 |
+ 228, 238, 244, 247, 250, 252, 253, 254, 256, 256 } |
|
216 |
+}; |
|
217 |
+ |
|
218 |
+static const uint16_t silk_model_pulse_location[4][168] = { |
|
219 |
+ { |
|
220 |
+ 256, 126, 256, |
|
221 |
+ 256, 56, 198, 256, |
|
222 |
+ 256, 25, 126, 230, 256, |
|
223 |
+ 256, 12, 72, 180, 244, 256, |
|
224 |
+ 256, 7, 42, 126, 213, 250, 256, |
|
225 |
+ 256, 4, 24, 83, 169, 232, 253, 256, |
|
226 |
+ 256, 3, 15, 53, 125, 200, 242, 254, 256, |
|
227 |
+ 256, 2, 10, 35, 89, 162, 221, 248, 255, 256, |
|
228 |
+ 256, 2, 7, 24, 63, 126, 191, 233, 251, 255, 256, |
|
229 |
+ 256, 1, 5, 17, 45, 94, 157, 211, 241, 252, 255, 256, |
|
230 |
+ 256, 1, 5, 13, 33, 70, 125, 182, 223, 245, 253, 255, 256, |
|
231 |
+ 256, 1, 4, 11, 26, 54, 98, 151, 199, 232, 248, 254, 255, 256, |
|
232 |
+ 256, 1, 3, 9, 21, 42, 77, 124, 172, 212, 237, 249, 254, 255, 256, |
|
233 |
+ 256, 1, 2, 6, 16, 33, 60, 97, 144, 187, 220, 241, 250, 254, 255, 256, |
|
234 |
+ 256, 1, 2, 3, 11, 25, 47, 80, 120, 163, 201, 229, 245, 253, 254, 255, 256, |
|
235 |
+ 256, 1, 2, 3, 4, 17, 35, 62, 98, 139, 180, 214, 238, 252, 253, 254, 255, 256 |
|
236 |
+ },{ |
|
237 |
+ 256, 127, 256, |
|
238 |
+ 256, 53, 202, 256, |
|
239 |
+ 256, 22, 127, 233, 256, |
|
240 |
+ 256, 11, 72, 183, 246, 256, |
|
241 |
+ 256, 6, 41, 127, 215, 251, 256, |
|
242 |
+ 256, 4, 24, 83, 170, 232, 253, 256, |
|
243 |
+ 256, 3, 16, 56, 127, 200, 241, 254, 256, |
|
244 |
+ 256, 3, 12, 39, 92, 162, 218, 246, 255, 256, |
|
245 |
+ 256, 3, 11, 30, 67, 124, 185, 229, 249, 255, 256, |
|
246 |
+ 256, 3, 10, 25, 53, 97, 151, 200, 233, 250, 255, 256, |
|
247 |
+ 256, 1, 8, 21, 43, 77, 123, 171, 209, 237, 251, 255, 256, |
|
248 |
+ 256, 1, 2, 13, 35, 62, 97, 139, 186, 219, 244, 254, 255, 256, |
|
249 |
+ 256, 1, 2, 8, 22, 48, 85, 128, 171, 208, 234, 248, 254, 255, 256, |
|
250 |
+ 256, 1, 2, 6, 16, 36, 67, 107, 149, 189, 220, 240, 250, 254, 255, 256, |
|
251 |
+ 256, 1, 2, 5, 13, 29, 55, 90, 128, 166, 201, 227, 243, 251, 254, 255, 256, |
|
252 |
+ 256, 1, 2, 4, 10, 22, 43, 73, 109, 147, 183, 213, 234, 246, 252, 254, 255, 256 |
|
253 |
+ },{ |
|
254 |
+ 256, 127, 256, |
|
255 |
+ 256, 49, 206, 256, |
|
256 |
+ 256, 20, 127, 236, 256, |
|
257 |
+ 256, 11, 71, 184, 246, 256, |
|
258 |
+ 256, 7, 43, 127, 214, 250, 256, |
|
259 |
+ 256, 6, 30, 87, 169, 229, 252, 256, |
|
260 |
+ 256, 5, 23, 62, 126, 194, 236, 252, 256, |
|
261 |
+ 256, 6, 20, 49, 96, 157, 209, 239, 253, 256, |
|
262 |
+ 256, 1, 16, 39, 74, 125, 175, 215, 245, 255, 256, |
|
263 |
+ 256, 1, 2, 23, 55, 97, 149, 195, 236, 254, 255, 256, |
|
264 |
+ 256, 1, 7, 23, 50, 86, 128, 170, 206, 233, 249, 255, 256, |
|
265 |
+ 256, 1, 6, 18, 39, 70, 108, 148, 186, 217, 238, 250, 255, 256, |
|
266 |
+ 256, 1, 4, 13, 30, 56, 90, 128, 166, 200, 226, 243, 252, 255, 256, |
|
267 |
+ 256, 1, 4, 11, 25, 47, 76, 110, 146, 180, 209, 231, 245, 252, 255, 256, |
|
268 |
+ 256, 1, 3, 8, 19, 37, 62, 93, 128, 163, 194, 219, 237, 248, 253, 255, 256, |
|
269 |
+ 256, 1, 2, 6, 15, 30, 51, 79, 111, 145, 177, 205, 226, 241, 250, 254, 255, 256 |
|
270 |
+ },{ |
|
271 |
+ 256, 128, 256, |
|
272 |
+ 256, 42, 214, 256, |
|
273 |
+ 256, 21, 128, 235, 256, |
|
274 |
+ 256, 12, 72, 184, 245, 256, |
|
275 |
+ 256, 8, 42, 128, 214, 249, 256, |
|
276 |
+ 256, 8, 31, 86, 176, 231, 251, 256, |
|
277 |
+ 256, 5, 20, 58, 130, 202, 238, 253, 256, |
|
278 |
+ 256, 6, 18, 45, 97, 174, 221, 241, 251, 256, |
|
279 |
+ 256, 6, 25, 53, 88, 128, 168, 203, 231, 250, 256, |
|
280 |
+ 256, 4, 18, 40, 71, 108, 148, 185, 216, 238, 252, 256, |
|
281 |
+ 256, 3, 13, 31, 57, 90, 128, 166, 199, 225, 243, 253, 256, |
|
282 |
+ 256, 2, 10, 23, 44, 73, 109, 147, 183, 212, 233, 246, 254, 256, |
|
283 |
+ 256, 1, 6, 16, 33, 58, 90, 128, 166, 198, 223, 240, 250, 255, 256, |
|
284 |
+ 256, 1, 5, 12, 25, 46, 75, 110, 146, 181, 210, 231, 244, 251, 255, 256, |
|
285 |
+ 256, 1, 3, 8, 18, 35, 60, 92, 128, 164, 196, 221, 238, 248, 253, 255, 256, |
|
286 |
+ 256, 1, 3, 7, 14, 27, 48, 76, 110, 146, 180, 208, 229, 242, 249, 253, 255, 256 |
|
287 |
+ } |
|
288 |
+}; |
|
289 |
+ |
|
290 |
+static const uint16_t silk_model_excitation_lsb[] = {256, 136, 256}; |
|
291 |
+ |
|
292 |
+static const uint16_t silk_model_excitation_sign[3][2][7][3] = { |
|
293 |
+ { // Inactive |
|
294 |
+ { // Low offset |
|
295 |
+ {256, 2, 256}, |
|
296 |
+ {256, 207, 256}, |
|
297 |
+ {256, 189, 256}, |
|
298 |
+ {256, 179, 256}, |
|
299 |
+ {256, 174, 256}, |
|
300 |
+ {256, 163, 256}, |
|
301 |
+ {256, 157, 256} |
|
302 |
+ }, { // High offset |
|
303 |
+ {256, 58, 256}, |
|
304 |
+ {256, 245, 256}, |
|
305 |
+ {256, 238, 256}, |
|
306 |
+ {256, 232, 256}, |
|
307 |
+ {256, 225, 256}, |
|
308 |
+ {256, 220, 256}, |
|
309 |
+ {256, 211, 256} |
|
310 |
+ } |
|
311 |
+ }, { // Unvoiced |
|
312 |
+ { // Low offset |
|
313 |
+ {256, 1, 256}, |
|
314 |
+ {256, 210, 256}, |
|
315 |
+ {256, 190, 256}, |
|
316 |
+ {256, 178, 256}, |
|
317 |
+ {256, 169, 256}, |
|
318 |
+ {256, 162, 256}, |
|
319 |
+ {256, 152, 256} |
|
320 |
+ }, { // High offset |
|
321 |
+ {256, 48, 256}, |
|
322 |
+ {256, 242, 256}, |
|
323 |
+ {256, 235, 256}, |
|
324 |
+ {256, 224, 256}, |
|
325 |
+ {256, 214, 256}, |
|
326 |
+ {256, 205, 256}, |
|
327 |
+ {256, 190, 256} |
|
328 |
+ } |
|
329 |
+ }, { // Voiced |
|
330 |
+ { // Low offset |
|
331 |
+ {256, 1, 256}, |
|
332 |
+ {256, 162, 256}, |
|
333 |
+ {256, 152, 256}, |
|
334 |
+ {256, 147, 256}, |
|
335 |
+ {256, 144, 256}, |
|
336 |
+ {256, 141, 256}, |
|
337 |
+ {256, 138, 256} |
|
338 |
+ }, { // High offset |
|
339 |
+ {256, 8, 256}, |
|
340 |
+ {256, 203, 256}, |
|
341 |
+ {256, 187, 256}, |
|
342 |
+ {256, 176, 256}, |
|
343 |
+ {256, 168, 256}, |
|
344 |
+ {256, 161, 256}, |
|
345 |
+ {256, 154, 256} |
|
346 |
+ } |
|
347 |
+ } |
|
348 |
+}; |
|
349 |
+ |
|
350 |
+static const int16_t silk_stereo_weights[] = { |
|
351 |
+ -13732, -10050, -8266, -7526, -6500, -5000, -2950, -820, |
|
352 |
+ 820, 2950, 5000, 6500, 7526, 8266, 10050, 13732 |
|
353 |
+}; |
|
354 |
+ |
|
355 |
+static const uint8_t silk_lsf_s2_model_sel_nbmb[32][10] = { |
|
356 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
|
357 |
+ { 1, 3, 1, 2, 2, 1, 2, 1, 1, 1 }, |
|
358 |
+ { 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, |
|
359 |
+ { 1, 2, 2, 2, 2, 1, 2, 1, 1, 1 }, |
|
360 |
+ { 2, 3, 3, 3, 3, 2, 2, 2, 2, 2 }, |
|
361 |
+ { 0, 5, 3, 3, 2, 2, 2, 2, 1, 1 }, |
|
362 |
+ { 0, 2, 2, 2, 2, 2, 2, 2, 2, 1 }, |
|
363 |
+ { 2, 3, 6, 4, 4, 4, 5, 4, 5, 5 }, |
|
364 |
+ { 2, 4, 5, 5, 4, 5, 4, 6, 4, 4 }, |
|
365 |
+ { 2, 4, 4, 7, 4, 5, 4, 5, 5, 4 }, |
|
366 |
+ { 4, 3, 3, 3, 2, 3, 2, 2, 2, 2 }, |
|
367 |
+ { 1, 5, 5, 6, 4, 5, 4, 5, 5, 5 }, |
|
368 |
+ { 2, 7, 4, 6, 5, 5, 5, 5, 5, 5 }, |
|
369 |
+ { 2, 7, 5, 5, 5, 5, 5, 6, 5, 4 }, |
|
370 |
+ { 3, 3, 5, 4, 4, 5, 4, 5, 4, 4 }, |
|
371 |
+ { 2, 3, 3, 5, 5, 4, 4, 4, 4, 4 }, |
|
372 |
+ { 2, 4, 4, 6, 4, 5, 4, 5, 5, 5 }, |
|
373 |
+ { 2, 5, 4, 6, 5, 5, 5, 4, 5, 4 }, |
|
374 |
+ { 2, 7, 4, 5, 4, 5, 4, 5, 5, 5 }, |
|
375 |
+ { 2, 5, 4, 6, 7, 6, 5, 6, 5, 4 }, |
|
376 |
+ { 3, 6, 7, 4, 6, 5, 5, 6, 4, 5 }, |
|
377 |
+ { 2, 7, 6, 4, 4, 4, 5, 4, 5, 5 }, |
|
378 |
+ { 4, 5, 5, 4, 6, 6, 5, 6, 5, 4 }, |
|
379 |
+ { 2, 5, 5, 6, 5, 6, 4, 6, 4, 4 }, |
|
380 |
+ { 4, 5, 5, 5, 3, 7, 4, 5, 5, 4 }, |
|
381 |
+ { 2, 3, 4, 5, 5, 6, 4, 5, 5, 4 }, |
|
382 |
+ { 2, 3, 2, 3, 3, 4, 2, 3, 3, 3 }, |
|
383 |
+ { 1, 1, 2, 2, 2, 2, 2, 3, 2, 2 }, |
|
384 |
+ { 4, 5, 5, 6, 6, 6, 5, 6, 4, 5 }, |
|
385 |
+ { 3, 5, 5, 4, 4, 4, 4, 3, 3, 2 }, |
|
386 |
+ { 2, 5, 3, 7, 5, 5, 4, 4, 5, 4 }, |
|
387 |
+ { 4, 4, 5, 4, 5, 6, 5, 6, 5, 4 } |
|
388 |
+}; |
|
389 |
+ |
|
390 |
+static const uint8_t silk_lsf_s2_model_sel_wb[32][16] = { |
|
391 |
+ { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }, |
|
392 |
+ { 10, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 9, 9, 9, 8, 11 }, |
|
393 |
+ { 10, 13, 13, 11, 15, 12, 12, 13, 10, 13, 12, 13, 13, 12, 11, 11 }, |
|
394 |
+ { 8, 10, 9, 10, 10, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 9 }, |
|
395 |
+ { 8, 14, 13, 12, 14, 12, 15, 13, 12, 12, 12, 13, 13, 12, 12, 11 }, |
|
396 |
+ { 8, 11, 13, 13, 12, 11, 11, 13, 11, 11, 11, 11, 11, 11, 10, 12 }, |
|
397 |
+ { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }, |
|
398 |
+ { 8, 10, 14, 11, 15, 10, 13, 11, 12, 13, 13, 12, 11, 11, 10, 11 }, |
|
399 |
+ { 8, 14, 10, 14, 14, 12, 13, 12, 14, 13, 12, 12, 13, 11, 11, 11 }, |
|
400 |
+ { 10, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }, |
|
401 |
+ { 8, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9 }, |
|
402 |
+ { 10, 10, 11, 12, 13, 11, 11, 11, 11, 11, 11, 11, 10, 10, 9, 11 }, |
|
403 |
+ { 10, 10, 11, 11, 12, 11, 11, 11, 11, 11, 11, 11, 11, 10, 9, 11 }, |
|
404 |
+ { 11, 12, 12, 12, 14, 12, 12, 13, 11, 13, 12, 12, 13, 12, 11, 12 }, |
|
405 |
+ { 8, 14, 12, 13, 12, 15, 13, 10, 14, 13, 15, 12, 12, 11, 13, 11 }, |
|
406 |
+ { 8, 9, 8, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 9, 8 }, |
|
407 |
+ { 9, 14, 13, 15, 13, 12, 13, 11, 12, 13, 12, 12, 12, 11, 11, 12 }, |
|
408 |
+ { 9, 11, 11, 12, 12, 11, 11, 13, 10, 11, 11, 13, 13, 13, 11, 12 }, |
|
409 |
+ { 10, 11, 11, 10, 10, 10, 11, 10, 9, 10, 9, 10, 9, 9, 9, 12 }, |
|
410 |
+ { 8, 10, 11, 13, 11, 11, 10, 10, 10, 9, 9, 8, 8, 8, 8, 8 }, |
|
411 |
+ { 11, 12, 11, 13, 11, 11, 10, 10, 9, 9, 9, 9, 9, 10, 10, 12 }, |
|
412 |
+ { 10, 14, 11, 15, 15, 12, 13, 12, 13, 11, 13, 11, 11, 10, 11, 11 }, |
|
413 |
+ { 10, 11, 13, 14, 14, 11, 13, 11, 12, 12, 11, 11, 11, 11, 10, 12 }, |
|
414 |
+ { 9, 11, 11, 12, 12, 12, 12, 11, 13, 13, 13, 11, 9, 9, 9, 9 }, |
|
415 |
+ { 10, 13, 11, 14, 14, 12, 15, 12, 12, 13, 11, 12, 12, 11, 11, 11 }, |
|
416 |
+ { 8, 14, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }, |
|
417 |
+ { 8, 14, 14, 11, 13, 10, 13, 13, 11, 12, 12, 15, 15, 12, 12, 12 }, |
|
418 |
+ { 11, 11, 15, 11, 13, 12, 11, 11, 11, 10, 10, 11, 11, 11, 10, 11 }, |
|
419 |
+ { 8, 8, 9, 8, 8, 8, 10, 9, 10, 9, 9, 10, 10, 10, 9, 9 }, |
|
420 |
+ { 8, 11, 10, 13, 11, 11, 10, 11, 10, 9, 8, 8, 9, 8, 8, 9 }, |
|
421 |
+ { 11, 13, 13, 12, 15, 13, 11, 11, 10, 11, 10, 10, 9, 8, 9, 8 }, |
|
422 |
+ { 10, 11, 13, 11, 12, 11, 11, 11, 10, 9, 10, 14, 12, 8, 8, 8 } |
|
423 |
+}; |
|
424 |
+ |
|
425 |
+static const uint8_t silk_lsf_pred_weights_nbmb[2][9] = { |
|
426 |
+ {179, 138, 140, 148, 151, 149, 153, 151, 163}, |
|
427 |
+ {116, 67, 82, 59, 92, 72, 100, 89, 92} |
|
428 |
+}; |
|
429 |
+ |
|
430 |
+static const uint8_t silk_lsf_pred_weights_wb[2][15] = { |
|
431 |
+ {175, 148, 160, 176, 178, 173, 174, 164, 177, 174, 196, 182, 198, 192, 182}, |
|
432 |
+ { 68, 62, 66, 60, 72, 117, 85, 90, 118, 136, 151, 142, 160, 142, 155} |
|
433 |
+}; |
|
434 |
+ |
|
435 |
+static const uint8_t silk_lsf_weight_sel_nbmb[32][9] = { |
|
436 |
+ { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, |
|
437 |
+ { 1, 0, 0, 0, 0, 0, 0, 0, 0 }, |
|
438 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
|
439 |
+ { 1, 1, 1, 0, 0, 0, 0, 1, 0 }, |
|
440 |
+ { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, |
|
441 |
+ { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, |
|
442 |
+ { 1, 0, 1, 1, 0, 0, 0, 1, 0 }, |
|
443 |
+ { 0, 1, 1, 0, 0, 1, 1, 0, 0 }, |
|
444 |
+ { 0, 0, 1, 1, 0, 1, 0, 1, 1 }, |
|
445 |
+ { 0, 0, 1, 1, 0, 0, 1, 1, 1 }, |
|
446 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
|
447 |
+ { 0, 1, 0, 1, 1, 1, 1, 1, 0 }, |
|
448 |
+ { 0, 1, 0, 1, 1, 1, 1, 1, 0 }, |
|
449 |
+ { 0, 1, 1, 1, 1, 1, 1, 1, 0 }, |
|
450 |
+ { 1, 0, 1, 1, 0, 1, 1, 1, 1 }, |
|
451 |
+ { 0, 1, 1, 1, 1, 1, 0, 1, 0 }, |
|
452 |
+ { 0, 0, 1, 1, 0, 1, 0, 1, 0 }, |
|
453 |
+ { 0, 0, 1, 1, 1, 0, 1, 1, 1 }, |
|
454 |
+ { 0, 1, 1, 0, 0, 1, 1, 1, 0 }, |
|
455 |
+ { 0, 0, 0, 1, 1, 1, 0, 1, 0 }, |
|
456 |
+ { 0, 1, 1, 0, 0, 1, 0, 1, 0 }, |
|
457 |
+ { 0, 1, 1, 0, 0, 0, 1, 1, 0 }, |
|
458 |
+ { 0, 0, 0, 0, 0, 1, 1, 1, 1 }, |
|
459 |
+ { 0, 0, 1, 1, 0, 0, 0, 1, 1 }, |
|
460 |
+ { 0, 0, 0, 1, 0, 1, 1, 1, 1 }, |
|
461 |
+ { 0, 1, 1, 1, 1, 1, 1, 1, 0 }, |
|
462 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
|
463 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
|
464 |
+ { 0, 0, 1, 0, 1, 1, 0, 1, 0 }, |
|
465 |
+ { 1, 0, 0, 1, 0, 0, 0, 0, 0 }, |
|
466 |
+ { 0, 0, 0, 1, 1, 0, 1, 0, 1 }, |
|
467 |
+ { 1, 0, 1, 1, 0, 1, 1, 1, 1 } |
|
468 |
+}; |
|
469 |
+ |
|
470 |
+static const uint8_t silk_lsf_weight_sel_wb[32][15] = { |
|
471 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, |
|
472 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
|
473 |
+ { 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0 }, |
|
474 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, |
|
475 |
+ { 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0 }, |
|
476 |
+ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
|
477 |
+ { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 }, |
|
478 |
+ { 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1 }, |
|
479 |
+ { 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1 }, |
|
480 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, |
|
481 |
+ { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
|
482 |
+ { 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0 }, |
|
483 |
+ { 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 }, |
|
484 |
+ { 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0 }, |
|
485 |
+ { 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1 }, |
|
486 |
+ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, |
|
487 |
+ { 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0 }, |
|
488 |
+ { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0 }, |
|
489 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, |
|
490 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, |
|
491 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
|
492 |
+ { 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0 }, |
|
493 |
+ { 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0 }, |
|
494 |
+ { 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0 }, |
|
495 |
+ { 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 }, |
|
496 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, |
|
497 |
+ { 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1 }, |
|
498 |
+ { 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, |
|
499 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, |
|
500 |
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, |
|
501 |
+ { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, |
|
502 |
+ { 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0 } |
|
503 |
+}; |
|
504 |
+ |
|
505 |
+static const uint8_t silk_lsf_codebook_nbmb[32][10] = { |
|
506 |
+ { 12, 35, 60, 83, 108, 132, 157, 180, 206, 228 }, |
|
507 |
+ { 15, 32, 55, 77, 101, 125, 151, 175, 201, 225 }, |
|
508 |
+ { 19, 42, 66, 89, 114, 137, 162, 184, 209, 230 }, |
|
509 |
+ { 12, 25, 50, 72, 97, 120, 147, 172, 200, 223 }, |
|
510 |
+ { 26, 44, 69, 90, 114, 135, 159, 180, 205, 225 }, |
|
511 |
+ { 13, 22, 53, 80, 106, 130, 156, 180, 205, 228 }, |
|
512 |
+ { 15, 25, 44, 64, 90, 115, 142, 168, 196, 222 }, |
|
513 |
+ { 19, 24, 62, 82, 100, 120, 145, 168, 190, 214 }, |
|
514 |
+ { 22, 31, 50, 79, 103, 120, 151, 170, 203, 227 }, |
|
515 |
+ { 21, 29, 45, 65, 106, 124, 150, 171, 196, 224 }, |
|
516 |
+ { 30, 49, 75, 97, 121, 142, 165, 186, 209, 229 }, |
|
517 |
+ { 19, 25, 52, 70, 93, 116, 143, 166, 192, 219 }, |
|
518 |
+ { 26, 34, 62, 75, 97, 118, 145, 167, 194, 217 }, |
|
519 |
+ { 25, 33, 56, 70, 91, 113, 143, 165, 196, 223 }, |
|
520 |
+ { 21, 34, 51, 72, 97, 117, 145, 171, 196, 222 }, |
|
521 |
+ { 20, 29, 50, 67, 90, 117, 144, 168, 197, 221 }, |
|
522 |
+ { 22, 31, 48, 66, 95, 117, 146, 168, 196, 222 }, |
|
523 |
+ { 24, 33, 51, 77, 116, 134, 158, 180, 200, 224 }, |
|
524 |
+ { 21, 28, 70, 87, 106, 124, 149, 170, 194, 217 }, |
|
525 |
+ { 26, 33, 53, 64, 83, 117, 152, 173, 204, 225 }, |
|
526 |
+ { 27, 34, 65, 95, 108, 129, 155, 174, 210, 225 }, |
|
527 |
+ { 20, 26, 72, 99, 113, 131, 154, 176, 200, 219 }, |
|
528 |
+ { 34, 43, 61, 78, 93, 114, 155, 177, 205, 229 }, |
|
529 |
+ { 23, 29, 54, 97, 124, 138, 163, 179, 209, 229 }, |
|
530 |
+ { 30, 38, 56, 89, 118, 129, 158, 178, 200, 231 }, |
|
531 |
+ { 21, 29, 49, 63, 85, 111, 142, 163, 193, 222 }, |
|
532 |
+ { 27, 48, 77, 103, 133, 158, 179, 196, 215, 232 }, |
|
533 |
+ { 29, 47, 74, 99, 124, 151, 176, 198, 220, 237 }, |
|
534 |
+ { 33, 42, 61, 76, 93, 121, 155, 174, 207, 225 }, |
|
535 |
+ { 29, 53, 87, 112, 136, 154, 170, 188, 208, 227 }, |
|
536 |
+ { 24, 30, 52, 84, 131, 150, 166, 186, 203, 229 }, |
|
537 |
+ { 37, 48, 64, 84, 104, 118, 156, 177, 201, 230 } |
|
538 |
+}; |
|
539 |
+ |
|
540 |
+static const uint8_t silk_lsf_codebook_wb[32][16] = { |
|
541 |
+ { 7, 23, 38, 54, 69, 85, 100, 116, 131, 147, 162, 178, 193, 208, 223, 239 }, |
|
542 |
+ { 13, 25, 41, 55, 69, 83, 98, 112, 127, 142, 157, 171, 187, 203, 220, 236 }, |
|
543 |
+ { 15, 21, 34, 51, 61, 78, 92, 106, 126, 136, 152, 167, 185, 205, 225, 240 }, |
|
544 |
+ { 10, 21, 36, 50, 63, 79, 95, 110, 126, 141, 157, 173, 189, 205, 221, 237 }, |
|
545 |
+ { 17, 20, 37, 51, 59, 78, 89, 107, 123, 134, 150, 164, 184, 205, 224, 240 }, |
|
546 |
+ { 10, 15, 32, 51, 67, 81, 96, 112, 129, 142, 158, 173, 189, 204, 220, 236 }, |
|
547 |
+ { 8, 21, 37, 51, 65, 79, 98, 113, 126, 138, 155, 168, 179, 192, 209, 218 }, |
|
548 |
+ { 12, 15, 34, 55, 63, 78, 87, 108, 118, 131, 148, 167, 185, 203, 219, 236 }, |
|
549 |
+ { 16, 19, 32, 36, 56, 79, 91, 108, 118, 136, 154, 171, 186, 204, 220, 237 }, |
|
550 |
+ { 11, 28, 43, 58, 74, 89, 105, 120, 135, 150, 165, 180, 196, 211, 226, 241 }, |
|
551 |
+ { 6, 16, 33, 46, 60, 75, 92, 107, 123, 137, 156, 169, 185, 199, 214, 225 }, |
|
552 |
+ { 11, 19, 30, 44, 57, 74, 89, 105, 121, 135, 152, 169, 186, 202, 218, 234 }, |
|
553 |
+ { 12, 19, 29, 46, 57, 71, 88, 100, 120, 132, 148, 165, 182, 199, 216, 233 }, |
|
554 |
+ { 17, 23, 35, 46, 56, 77, 92, 106, 123, 134, 152, 167, 185, 204, 222, 237 }, |
|
555 |
+ { 14, 17, 45, 53, 63, 75, 89, 107, 115, 132, 151, 171, 188, 206, 221, 240 }, |
|
556 |
+ { 9, 16, 29, 40, 56, 71, 88, 103, 119, 137, 154, 171, 189, 205, 222, 237 }, |
|
557 |
+ { 16, 19, 36, 48, 57, 76, 87, 105, 118, 132, 150, 167, 185, 202, 218, 236 }, |
|
558 |
+ { 12, 17, 29, 54, 71, 81, 94, 104, 126, 136, 149, 164, 182, 201, 221, 237 }, |
|
559 |
+ { 15, 28, 47, 62, 79, 97, 115, 129, 142, 155, 168, 180, 194, 208, 223, 238 }, |
|
560 |
+ { 8, 14, 30, 45, 62, 78, 94, 111, 127, 143, 159, 175, 192, 207, 223, 239 }, |
|
561 |
+ { 17, 30, 49, 62, 79, 92, 107, 119, 132, 145, 160, 174, 190, 204, 220, 235 }, |
|
562 |
+ { 14, 19, 36, 45, 61, 76, 91, 108, 121, 138, 154, 172, 189, 205, 222, 238 }, |
|
563 |
+ { 12, 18, 31, 45, 60, 76, 91, 107, 123, 138, 154, 171, 187, 204, 221, 236 }, |
|
564 |
+ { 13, 17, 31, 43, 53, 70, 83, 103, 114, 131, 149, 167, 185, 203, 220, 237 }, |
|
565 |
+ { 17, 22, 35, 42, 58, 78, 93, 110, 125, 139, 155, 170, 188, 206, 224, 240 }, |
|
566 |
+ { 8, 15, 34, 50, 67, 83, 99, 115, 131, 146, 162, 178, 193, 209, 224, 239 }, |
|
567 |
+ { 13, 16, 41, 66, 73, 86, 95, 111, 128, 137, 150, 163, 183, 206, 225, 241 }, |
|
568 |
+ { 17, 25, 37, 52, 63, 75, 92, 102, 119, 132, 144, 160, 175, 191, 212, 231 }, |
|
569 |
+ { 19, 31, 49, 65, 83, 100, 117, 133, 147, 161, 174, 187, 200, 213, 227, 242 }, |
|
570 |
+ { 18, 31, 52, 68, 88, 103, 117, 126, 138, 149, 163, 177, 192, 207, 223, 239 }, |
|
571 |
+ { 16, 29, 47, 61, 76, 90, 106, 119, 133, 147, 161, 176, 193, 209, 224, 240 }, |
|
572 |
+ { 15, 21, 35, 50, 61, 73, 86, 97, 110, 119, 129, 141, 175, 198, 218, 237 } |
|
573 |
+}; |
|
574 |
+ |
|
575 |
+static const uint16_t silk_lsf_min_spacing_nbmb[] = { |
|
576 |
+ 250, 3, 6, 3, 3, 3, 4, 3, 3, 3, 461 |
|
577 |
+}; |
|
578 |
+ |
|
579 |
+static const uint16_t silk_lsf_min_spacing_wb[] = { |
|
580 |
+ 100, 3, 40, 3, 3, 3, 5, 14, 14, 10, 11, 3, 8, 9, 7, 3, 347 |
|
581 |
+}; |
|
582 |
+ |
|
583 |
+static const uint8_t silk_lsf_ordering_nbmb[] = { |
|
584 |
+ 0, 9, 6, 3, 4, 5, 8, 1, 2, 7 |
|
585 |
+}; |
|
586 |
+ |
|
587 |
+static const uint8_t silk_lsf_ordering_wb[] = { |
|
588 |
+ 0, 15, 8, 7, 4, 11, 12, 3, 2, 13, 10, 5, 6, 9, 14, 1 |
|
589 |
+}; |
|
590 |
+ |
|
591 |
+static const int16_t silk_cosine[] = { /* (0.12) */ |
|
592 |
+ 4096, 4095, 4091, 4085, |
|
593 |
+ 4076, 4065, 4052, 4036, |
|
594 |
+ 4017, 3997, 3973, 3948, |
|
595 |
+ 3920, 3889, 3857, 3822, |
|
596 |
+ 3784, 3745, 3703, 3659, |
|
597 |
+ 3613, 3564, 3513, 3461, |
|
598 |
+ 3406, 3349, 3290, 3229, |
|
599 |
+ 3166, 3102, 3035, 2967, |
|
600 |
+ 2896, 2824, 2751, 2676, |
|
601 |
+ 2599, 2520, 2440, 2359, |
|
602 |
+ 2276, 2191, 2106, 2019, |
|
603 |
+ 1931, 1842, 1751, 1660, |
|
604 |
+ 1568, 1474, 1380, 1285, |
|
605 |
+ 1189, 1093, 995, 897, |
|
606 |
+ 799, 700, 601, 501, |
|
607 |
+ 401, 301, 201, 101, |
|
608 |
+ 0, -101, -201, -301, |
|
609 |
+ -401, -501, -601, -700, |
|
610 |
+ -799, -897, -995, -1093, |
|
611 |
+ -1189, -1285, -1380, -1474, |
|
612 |
+ -1568, -1660, -1751, -1842, |
|
613 |
+ -1931, -2019, -2106, -2191, |
|
614 |
+ -2276, -2359, -2440, -2520, |
|
615 |
+ -2599, -2676, -2751, -2824, |
|
616 |
+ -2896, -2967, -3035, -3102, |
|
617 |
+ -3166, -3229, -3290, -3349, |
|
618 |
+ -3406, -3461, -3513, -3564, |
|
619 |
+ -3613, -3659, -3703, -3745, |
|
620 |
+ -3784, -3822, -3857, -3889, |
|
621 |
+ -3920, -3948, -3973, -3997, |
|
622 |
+ -4017, -4036, -4052, -4065, |
|
623 |
+ -4076, -4085, -4091, -4095, |
|
624 |
+ -4096 |
|
625 |
+}; |
|
626 |
+ |
|
627 |
+static const uint16_t silk_pitch_scale[] = { 4, 6, 8}; |
|
628 |
+ |
|
629 |
+static const uint16_t silk_pitch_min_lag[] = { 16, 24, 32}; |
|
630 |
+ |
|
631 |
+static const uint16_t silk_pitch_max_lag[] = {144, 216, 288}; |
|
632 |
+ |
|
633 |
+static const int8_t silk_pitch_offset_nb10ms[3][2] = { |
|
634 |
+ { 0, 0}, |
|
635 |
+ { 1, 0}, |
|
636 |
+ { 0, 1} |
|
637 |
+}; |
|
638 |
+ |
|
639 |
+static const int8_t silk_pitch_offset_nb20ms[11][4] = { |
|
640 |
+ { 0, 0, 0, 0}, |
|
641 |
+ { 2, 1, 0, -1}, |
|
642 |
+ {-1, 0, 1, 2}, |
|
643 |
+ {-1, 0, 0, 1}, |
|
644 |
+ {-1, 0, 0, 0}, |
|
645 |
+ { 0, 0, 0, 1}, |
|
646 |
+ { 0, 0, 1, 1}, |
|
647 |
+ { 1, 1, 0, 0}, |
|
648 |
+ { 1, 0, 0, 0}, |
|
649 |
+ { 0, 0, 0, -1}, |
|
650 |
+ { 1, 0, 0, -1} |
|
651 |
+}; |
|
652 |
+ |
|
653 |
+static const int8_t silk_pitch_offset_mbwb10ms[12][2] = { |
|
654 |
+ { 0, 0}, |
|
655 |
+ { 0, 1}, |
|
656 |
+ { 1, 0}, |
|
657 |
+ {-1, 1}, |
|
658 |
+ { 1, -1}, |
|
659 |
+ {-1, 2}, |
|
660 |
+ { 2, -1}, |
|
661 |
+ {-2, 2}, |
|
662 |
+ { 2, -2}, |
|
663 |
+ {-2, 3}, |
|
664 |
+ { 3, -2}, |
|
665 |
+ {-3, 3} |
|
666 |
+}; |
|
667 |
+ |
|
668 |
+static const int8_t silk_pitch_offset_mbwb20ms[34][4] = { |
|
669 |
+ { 0, 0, 0, 0}, |
|
670 |
+ { 0, 0, 1, 1}, |
|
671 |
+ { 1, 1, 0, 0}, |
|
672 |
+ {-1, 0, 0, 0}, |
|
673 |
+ { 0, 0, 0, 1}, |
|
674 |
+ { 1, 0, 0, 0}, |
|
675 |
+ {-1, 0, 0, 1}, |
|
676 |
+ { 0, 0, 0, -1}, |
|
677 |
+ {-1, 0, 1, 2}, |
|
678 |
+ { 1, 0, 0, -1}, |
|
679 |
+ {-2, -1, 1, 2}, |
|
680 |
+ { 2, 1, 0, -1}, |
|
681 |
+ {-2, 0, 0, 2}, |
|
682 |
+ {-2, 0, 1, 3}, |
|
683 |
+ { 2, 1, -1, -2}, |
|
684 |
+ {-3, -1, 1, 3}, |
|
685 |
+ { 2, 0, 0, -2}, |
|
686 |
+ { 3, 1, 0, -2}, |
|
687 |
+ {-3, -1, 2, 4}, |
|
688 |
+ {-4, -1, 1, 4}, |
|
689 |
+ { 3, 1, -1, -3}, |
|
690 |
+ {-4, -1, 2, 5}, |
|
691 |
+ { 4, 2, -1, -3}, |
|
692 |
+ { 4, 1, -1, -4}, |
|
693 |
+ {-5, -1, 2, 6}, |
|
694 |
+ { 5, 2, -1, -4}, |
|
695 |
+ {-6, -2, 2, 6}, |
|
696 |
+ {-5, -2, 2, 5}, |
|
697 |
+ { 6, 2, -1, -5}, |
|
698 |
+ {-7, -2, 3, 8}, |
|
699 |
+ { 6, 2, -2, -6}, |
|
700 |
+ { 5, 2, -2, -5}, |
|
701 |
+ { 8, 3, -2, -7}, |
|
702 |
+ {-9, -3, 3, 9} |
|
703 |
+}; |
|
704 |
+ |
|
705 |
+static const int8_t silk_ltp_filter0_taps[8][5] = { |
|
706 |
+ { 4, 6, 24, 7, 5}, |
|
707 |
+ { 0, 0, 2, 0, 0}, |
|
708 |
+ { 12, 28, 41, 13, -4}, |
|
709 |
+ { -9, 15, 42, 25, 14}, |
|
710 |
+ { 1, -2, 62, 41, -9}, |
|
711 |
+ {-10, 37, 65, -4, 3}, |
|
712 |
+ { -6, 4, 66, 7, -8}, |
|
713 |
+ { 16, 14, 38, -3, 33} |
|
714 |
+}; |
|
715 |
+ |
|
716 |
+static const int8_t silk_ltp_filter1_taps[16][5] = { |
|
717 |
+ { 13, 22, 39, 23, 12}, |
|
718 |
+ { -1, 36, 64, 27, -6}, |
|
719 |
+ { -7, 10, 55, 43, 17}, |
|
720 |
+ { 1, 1, 8, 1, 1}, |
|
721 |
+ { 6, -11, 74, 53, -9}, |
|
722 |
+ {-12, 55, 76, -12, 8}, |
|
723 |
+ { -3, 3, 93, 27, -4}, |
|
724 |
+ { 26, 39, 59, 3, -8}, |
|
725 |
+ { 2, 0, 77, 11, 9}, |
|
726 |
+ { -8, 22, 44, -6, 7}, |
|
727 |
+ { 40, 9, 26, 3, 9}, |
|
728 |
+ { -7, 20, 101, -7, 4}, |
|
729 |
+ { 3, -8, 42, 26, 0}, |
|
730 |
+ {-15, 33, 68, 2, 23}, |
|
731 |
+ { -2, 55, 46, -2, 15}, |
|
732 |
+ { 3, -1, 21, 16, 41} |
|
733 |
+}; |
|
734 |
+ |
|
735 |
+static const int8_t silk_ltp_filter2_taps[32][5] = { |
|
736 |
+ { -6, 27, 61, 39, 5}, |
|
737 |
+ {-11, 42, 88, 4, 1}, |
|
738 |
+ { -2, 60, 65, 6, -4}, |
|
739 |
+ { -1, -5, 73, 56, 1}, |
|
740 |
+ { -9, 19, 94, 29, -9}, |
|
741 |
+ { 0, 12, 99, 6, 4}, |
|
742 |
+ { 8, -19, 102, 46, -13}, |
|
743 |
+ { 3, 2, 13, 3, 2}, |
|
744 |
+ { 9, -21, 84, 72, -18}, |
|
745 |
+ {-11, 46, 104, -22, 8}, |
|
746 |
+ { 18, 38, 48, 23, 0}, |
|
747 |
+ {-16, 70, 83, -21, 11}, |
|
748 |
+ { 5, -11, 117, 22, -8}, |
|
749 |
+ { -6, 23, 117, -12, 3}, |
|
750 |
+ { 3, -8, 95, 28, 4}, |
|
751 |
+ {-10, 15, 77, 60, -15}, |
|
752 |
+ { -1, 4, 124, 2, -4}, |
|
753 |
+ { 3, 38, 84, 24, -25}, |
|
754 |
+ { 2, 13, 42, 13, 31}, |
|
755 |
+ { 21, -4, 56, 46, -1}, |
|
756 |
+ { -1, 35, 79, -13, 19}, |
|
757 |
+ { -7, 65, 88, -9, -14}, |
|
758 |
+ { 20, 4, 81, 49, -29}, |
|
759 |
+ { 20, 0, 75, 3, -17}, |
|
760 |
+ { 5, -9, 44, 92, -8}, |
|
761 |
+ { 1, -3, 22, 69, 31}, |
|
762 |
+ { -6, 95, 41, -12, 5}, |
|
763 |
+ { 39, 67, 16, -4, 1}, |
|
764 |
+ { 0, -6, 120, 55, -36}, |
|
765 |
+ {-13, 44, 122, 4, -24}, |
|
766 |
+ { 81, 5, 11, 3, 7}, |
|
767 |
+ { 2, 0, 9, 10, 88} |
|
768 |
+}; |
|
769 |
+ |
|
770 |
+static const uint16_t silk_ltp_scale_factor[] = {15565, 12288, 8192}; |
|
771 |
+ |
|
772 |
+static const uint8_t silk_shell_blocks[3][2] = { |
|
773 |
+ { 5, 10}, // NB |
|
774 |
+ { 8, 15}, // MB |
|
775 |
+ {10, 20} // WB |
|
776 |
+}; |
|
777 |
+ |
|
778 |
+static const uint8_t silk_quant_offset[2][2] = { /* (0.23) */ |
|
779 |
+ {25, 60}, // Inactive or Unvoiced |
|
780 |
+ { 8, 25} // Voiced |
|
781 |
+}; |
|
782 |
+ |
|
783 |
+static const int silk_stereo_interp_len[3] = { |
|
784 |
+ 64, 96, 128 |
|
785 |
+}; |
|
786 |
+ |
|
787 |
+static inline void silk_stabilize_lsf(int16_t nlsf[16], int order, const uint16_t min_delta[17]) |
|
788 |
+{ |
|
789 |
+ int pass, i; |
|
790 |
+ for (pass = 0; pass < 20; pass++) { |
|
791 |
+ int k, min_diff = 0; |
|
792 |
+ for (i = 0; i < order+1; i++) { |
|
793 |
+ int low = i != 0 ? nlsf[i-1] : 0; |
|
794 |
+ int high = i != order ? nlsf[i] : 32768; |
|
795 |
+ int diff = (high - low) - (min_delta[i]); |
|
796 |
+ |
|
797 |
+ if (diff < min_diff) { |
|
798 |
+ min_diff = diff; |
|
799 |
+ k = i; |
|
800 |
+ |
|
801 |
+ if (pass == 20) |
|
802 |
+ break; |
|
803 |
+ } |
|
804 |
+ } |
|
805 |
+ if (min_diff == 0) /* no issues; stabilized */ |
|
806 |
+ return; |
|
807 |
+ |
|
808 |
+ /* wiggle one or two LSFs */ |
|
809 |
+ if (k == 0) { |
|
810 |
+ /* repel away from lower bound */ |
|
811 |
+ nlsf[0] = min_delta[0]; |
|
812 |
+ } else if (k == order) { |
|
813 |
+ /* repel away from higher bound */ |
|
814 |
+ nlsf[order-1] = 32768 - min_delta[order]; |
|
815 |
+ } else { |
|
816 |
+ /* repel away from current position */ |
|
817 |
+ int min_center = 0, max_center = 32768, center_val; |
|
818 |
+ |
|
819 |
+ /* lower extent */ |
|
820 |
+ for (i = 0; i < k; i++) |
|
821 |
+ min_center += min_delta[i]; |
|
822 |
+ min_center += min_delta[k] >> 1; |
|
823 |
+ |
|
824 |
+ /* upper extent */ |
|
825 |
+ for (i = order; i > k; i--) |
|
826 |
+ max_center -= min_delta[k]; |
|
827 |
+ max_center -= min_delta[k] >> 1; |
|
828 |
+ |
|
829 |
+ /* move apart */ |
|
830 |
+ center_val = nlsf[k - 1] + nlsf[k]; |
|
831 |
+ center_val = (center_val >> 1) + (center_val & 1); // rounded divide by 2 |
|
832 |
+ center_val = FFMIN(max_center, FFMAX(min_center, center_val)); |
|
833 |
+ |
|
834 |
+ nlsf[k - 1] = center_val - (min_delta[k] >> 1); |
|
835 |
+ nlsf[k] = nlsf[k - 1] + min_delta[k]; |
|
836 |
+ } |
|
837 |
+ } |
|
838 |
+ |
|
839 |
+ /* resort to the fall-back method, the standard method for LSF stabilization */ |
|
840 |
+ |
|
841 |
+ /* sort; as the LSFs should be nearly sorted, use insertion sort */ |
|
842 |
+ for (i = 1; i < order; i++) { |
|
843 |
+ int j, value = nlsf[i]; |
|
844 |
+ for (j = i - 1; j >= 0 && nlsf[j] > value; j--) |
|
845 |
+ nlsf[j + 1] = nlsf[j]; |
|
846 |
+ nlsf[j + 1] = value; |
|
847 |
+ } |
|
848 |
+ |
|
849 |
+ /* push forwards to increase distance */ |
|
850 |
+ if (nlsf[0] < min_delta[0]) |
|
851 |
+ nlsf[0] = min_delta[0]; |
|
852 |
+ for (i = 1; i < order; i++) |
|
853 |
+ if (nlsf[i] < nlsf[i - 1] + min_delta[i]) |
|
854 |
+ nlsf[i] = nlsf[i - 1] + min_delta[i]; |
|
855 |
+ |
|
856 |
+ /* push backwards to increase distance */ |
|
857 |
+ if (nlsf[order-1] > 32768 - min_delta[order]) |
|
858 |
+ nlsf[order-1] = 32768 - min_delta[order]; |
|
859 |
+ for (i = order-2; i >= 0; i--) |
|
860 |
+ if (nlsf[i] > nlsf[i + 1] - min_delta[i+1]) |
|
861 |
+ nlsf[i] = nlsf[i + 1] - min_delta[i+1]; |
|
862 |
+ |
|
863 |
+ return; |
|
864 |
+} |
|
865 |
+ |
|
866 |
+static inline int silk_is_lpc_stable(const int16_t lpc[16], int order) |
|
867 |
+{ |
|
868 |
+ int k, j, DC_resp = 0; |
|
869 |
+ int32_t lpc32[2][16]; // Q24 |
|
870 |
+ int totalinvgain = 1 << 30; // 1.0 in Q30 |
|
871 |
+ int32_t *row = lpc32[0], *prevrow; |
|
872 |
+ |
|
873 |
+ /* initialize the first row for the Levinson recursion */ |
|
874 |
+ for (k = 0; k < order; k++) { |
|
875 |
+ DC_resp += lpc[k]; |
|
876 |
+ row[k] = lpc[k] * 4096; |
|
877 |
+ } |
|
878 |
+ |
|
879 |
+ if (DC_resp >= 4096) |
|
880 |
+ return 0; |
|
881 |
+ |
|
882 |
+ /* check if prediction gain pushes any coefficients too far */ |
|
883 |
+ for (k = order - 1; 1; k--) { |
|
884 |
+ int rc; // Q31; reflection coefficient |
|
885 |
+ int gaindiv; // Q30; inverse of the gain (the divisor) |
|
886 |
+ int gain; // gain for this reflection coefficient |
|
887 |
+ int fbits; // fractional bits used for the gain |
|
888 |
+ int error; // Q29; estimate of the error of our partial estimate of 1/gaindiv |
|
889 |
+ |
|
890 |
+ if (FFABS(row[k]) > 16773022) |
|
891 |
+ return 0; |
|
892 |
+ |
|
893 |
+ rc = -(row[k] * 128); |
|
894 |
+ gaindiv = (1 << 30) - MULH(rc, rc); |
|
895 |
+ |
|
896 |
+ totalinvgain = MULH(totalinvgain, gaindiv) << 2; |
|
897 |
+ if (k == 0) |
|
898 |
+ return (totalinvgain >= 107374); |
|
899 |
+ |
|
900 |
+ /* approximate 1.0/gaindiv */ |
|
901 |
+ fbits = opus_ilog(gaindiv); |
|
902 |
+ gain = ((1 << 29) - 1) / (gaindiv >> (fbits + 1 - 16)); // Q<fbits-16> |
|
903 |
+ error = (1 << 29) - MULL(gaindiv << (15 + 16 - fbits), gain, 16); |
|
904 |
+ gain = ((gain << 16) + (error * gain >> 13)); |
|
905 |
+ |
|
906 |
+ /* switch to the next row of the LPC coefficients */ |
|
907 |
+ prevrow = row; |
|
908 |
+ row = lpc32[k & 1]; |
|
909 |
+ |
|
910 |
+ for (j = 0; j < k; j++) { |
|
911 |
+ int x = prevrow[j] - ROUND_MULL(prevrow[k - j - 1], rc, 31); |
|
912 |
+ row[j] = ROUND_MULL(x, gain, fbits); |
|
913 |
+ } |
|
914 |
+ } |
|
915 |
+} |
|
916 |
+ |
|
917 |
+static void silk_lsp2poly(const int32_t lsp[16], int32_t pol[16], int half_order) |
|
918 |
+{ |
|
919 |
+ int i, j; |
|
920 |
+ |
|
921 |
+ pol[0] = 65536; // 1.0 in Q16 |
|
922 |
+ pol[1] = -lsp[0]; |
|
923 |
+ |
|
924 |
+ for (i = 1; i < half_order; i++) { |
|
925 |
+ pol[i + 1] = pol[i - 1] * 2 - ROUND_MULL(lsp[2 * i], pol[i], 16); |
|
926 |
+ for (j = i; j > 1; j--) |
|
927 |
+ pol[j] += pol[j - 2] - ROUND_MULL(lsp[2 * i], pol[j - 1], 16); |
|
928 |
+ |
|
929 |
+ pol[1] -= lsp[2 * i]; |
|
930 |
+ } |
|
931 |
+} |
|
932 |
+ |
|
933 |
+static void silk_lsf2lpc(const int16_t nlsf[16], float lpcf[16], int order) |
|
934 |
+{ |
|
935 |
+ int i, k; |
|
936 |
+ int32_t lsp[16]; // Q17; 2*cos(LSF) |
|
937 |
+ int32_t p[9], q[9]; // Q16 |
|
938 |
+ int32_t lpc32[16]; // Q17 |
|
939 |
+ int16_t lpc[16]; // Q12 |
|
940 |
+ |
|
941 |
+ /* convert the LSFs to LSPs, i.e. 2*cos(LSF) */ |
|
942 |
+ for (k = 0; k < order; k++) { |
|
943 |
+ int index = nlsf[k] >> 8; |
|
944 |
+ int offset = nlsf[k] & 255; |
|
945 |
+ int k2 = (order == 10) ? silk_lsf_ordering_nbmb[k] : silk_lsf_ordering_wb[k]; |
|
946 |
+ |
|
947 |
+ /* interpolate and round */ |
|
948 |
+ lsp[k2] = silk_cosine[index] * 256; |
|
949 |
+ lsp[k2] += (silk_cosine[index + 1] - silk_cosine[index]) * offset; |
|
950 |
+ lsp[k2] = (lsp[k2] + 4) >> 3; |
|
951 |
+ } |
|
952 |
+ |
|
953 |
+ silk_lsp2poly(lsp , p, order >> 1); |
|
954 |
+ silk_lsp2poly(lsp + 1, q, order >> 1); |
|
955 |
+ |
|
956 |
+ /* reconstruct A(z) */ |
|
957 |
+ for (k = 0; k < order>>1; k++) { |
|
958 |
+ lpc32[k] = -p[k + 1] - p[k] - q[k + 1] + q[k]; |
|
959 |
+ lpc32[order-k-1] = -p[k + 1] - p[k] + q[k + 1] - q[k]; |
|
960 |
+ } |
|
961 |
+ |
|
962 |
+ /* limit the range of the LPC coefficients to each fit within an int16_t */ |
|
963 |
+ for (i = 0; i < 10; i++) { |
|
964 |
+ int j; |
|
965 |
+ unsigned int maxabs = 0; |
|
966 |
+ for (j = 0, k = 0; j < order; j++) { |
|
967 |
+ unsigned int x = FFABS(lpc32[k]); |
|
968 |
+ if (x > maxabs) { |
|
969 |
+ maxabs = x; // Q17 |
|
970 |
+ k = j; |
|
971 |
+ } |
|
972 |
+ } |
|
973 |
+ |
|
974 |
+ maxabs = (maxabs + 16) >> 5; // convert to Q12 |
|
975 |
+ |
|
976 |
+ if (maxabs > 32767) { |
|
977 |
+ /* perform bandwidth expansion */ |
|
978 |
+ unsigned int chirp, chirp_base; // Q16 |
|
979 |
+ maxabs = FFMIN(maxabs, 163838); // anything above this overflows chirp's numerator |
|
980 |
+ chirp_base = chirp = 65470 - ((maxabs - 32767) << 14) / ((maxabs * (k+1)) >> 2); |
|
981 |
+ |
|
982 |
+ for (k = 0; k < order; k++) { |
|
983 |
+ lpc32[k] = ROUND_MULL(lpc32[k], chirp, 16); |
|
984 |
+ chirp = (chirp_base * chirp + 32768) >> 16; |
|
985 |
+ } |
|
986 |
+ } else break; |
|
987 |
+ } |
|
988 |
+ |
|
989 |
+ if (i == 10) { |
|
990 |
+ /* time's up: just clamp */ |
|
991 |
+ for (k = 0; k < order; k++) { |
|
992 |
+ int x = (lpc32[k] + 16) >> 5; |
|
993 |
+ lpc[k] = av_clip_int16(x); |
|
994 |
+ lpc32[k] = lpc[k] << 5; // shortcut mandated by the spec; drops lower 5 bits |
|
995 |
+ } |
|
996 |
+ } else { |
|
997 |
+ for (k = 0; k < order; k++) |
|
998 |
+ lpc[k] = (lpc32[k] + 16) >> 5; |
|
999 |
+ } |
|
1000 |
+ |
|
1001 |
+ /* if the prediction gain causes the LPC filter to become unstable, |
|
1002 |
+ apply further bandwidth expansion on the Q17 coefficients */ |
|
1003 |
+ for (i = 1; i <= 16 && !silk_is_lpc_stable(lpc, order); i++) { |
|
1004 |
+ unsigned int chirp, chirp_base; |
|
1005 |
+ chirp_base = chirp = 65536 - (1 << i); |
|
1006 |
+ |
|
1007 |
+ for (k = 0; k < order; k++) { |
|
1008 |
+ lpc32[k] = ROUND_MULL(lpc32[k], chirp, 16); |
|
1009 |
+ lpc[k] = (lpc32[k] + 16) >> 5; |
|
1010 |
+ chirp = (chirp_base * chirp + 32768) >> 16; |
|
1011 |
+ } |
|
1012 |
+ } |
|
1013 |
+ |
|
1014 |
+ for (i = 0; i < order; i++) |
|
1015 |
+ lpcf[i] = lpc[i] / 4096.0f; |
|
1016 |
+} |
|
1017 |
+ |
|
1018 |
+static inline void silk_decode_lpc(SilkContext *s, SilkFrame *frame, |
|
1019 |
+ OpusRangeCoder *rc, |
|
1020 |
+ float lpc_leadin[16], float lpc[16], |
|
1021 |
+ int *lpc_order, int *has_lpc_leadin, int voiced) |
|
1022 |
+{ |
|
1023 |
+ int i; |
|
1024 |
+ int order; // order of the LP polynomial; 10 for NB/MB and 16 for WB |
|
1025 |
+ int8_t lsf_i1, lsf_i2[16]; // stage-1 and stage-2 codebook indices |
|
1026 |
+ int16_t lsf_res[16]; // residual as a Q10 value |
|
1027 |
+ int16_t nlsf[16]; // Q15 |
|
1028 |
+ |
|
1029 |
+ *lpc_order = order = s->wb ? 16 : 10; |
|
1030 |
+ |
|
1031 |
+ /* obtain LSF stage-1 and stage-2 indices */ |
|
1032 |
+ lsf_i1 = opus_rc_getsymbol(rc, silk_model_lsf_s1[s->wb][voiced]); |
|
1033 |
+ for (i = 0; i < order; i++) { |
|
1034 |
+ int index = s->wb ? silk_lsf_s2_model_sel_wb [lsf_i1][i] : |
|
1035 |
+ silk_lsf_s2_model_sel_nbmb[lsf_i1][i]; |
|
1036 |
+ lsf_i2[i] = opus_rc_getsymbol(rc, silk_model_lsf_s2[index]) - 4; |
|
1037 |
+ if (lsf_i2[i] == -4) |
|
1038 |
+ lsf_i2[i] -= opus_rc_getsymbol(rc, silk_model_lsf_s2_ext); |
|
1039 |
+ else if (lsf_i2[i] == 4) |
|
1040 |
+ lsf_i2[i] += opus_rc_getsymbol(rc, silk_model_lsf_s2_ext); |
|
1041 |
+ } |
|
1042 |
+ |
|
1043 |
+ /* reverse the backwards-prediction step */ |
|
1044 |
+ for (i = order - 1; i >= 0; i--) { |
|
1045 |
+ int qstep = s->wb ? 9830 : 11796; |
|
1046 |
+ |
|
1047 |
+ lsf_res[i] = lsf_i2[i] * 1024; |
|
1048 |
+ if (lsf_i2[i] < 0) lsf_res[i] += 102; |
|
1049 |
+ else if (lsf_i2[i] > 0) lsf_res[i] -= 102; |
|
1050 |
+ lsf_res[i] = (lsf_res[i] * qstep) >> 16; |
|
1051 |
+ |
|
1052 |
+ if (i + 1 < order) { |
|
1053 |
+ int weight = s->wb ? silk_lsf_pred_weights_wb [silk_lsf_weight_sel_wb [lsf_i1][i]][i] : |
|
1054 |
+ silk_lsf_pred_weights_nbmb[silk_lsf_weight_sel_nbmb[lsf_i1][i]][i]; |
|
1055 |
+ lsf_res[i] += (lsf_res[i+1] * weight) >> 8; |
|
1056 |
+ } |
|
1057 |
+ } |
|
1058 |
+ |
|
1059 |
+ /* reconstruct the NLSF coefficients from the supplied indices */ |
|
1060 |
+ for (i = 0; i < order; i++) { |
|
1061 |
+ const uint8_t * codebook = s->wb ? silk_lsf_codebook_wb [lsf_i1] : |
|
1062 |
+ silk_lsf_codebook_nbmb[lsf_i1]; |
|
1063 |
+ int cur, prev, next, weight_sq, weight, ipart, fpart, y, value; |
|
1064 |
+ |
|
1065 |
+ /* find the weight of the residual */ |
|
1066 |
+ /* TODO: precompute */ |
|
1067 |
+ cur = codebook[i]; |
|
1068 |
+ prev = i ? codebook[i - 1] : 0; |
|
1069 |
+ next = i + 1 < order ? codebook[i + 1] : 256; |
|
1070 |
+ weight_sq = (1024 / (cur - prev) + 1024 / (next - cur)) << 16; |
|
1071 |
+ |
|
1072 |
+ /* approximate square-root with mandated fixed-point arithmetic */ |
|
1073 |
+ ipart = opus_ilog(weight_sq); |
|
1074 |
+ fpart = (weight_sq >> (ipart-8)) & 127; |
|
1075 |
+ y = ((ipart & 1) ? 32768 : 46214) >> ((32 - ipart)>>1); |
|
1076 |
+ weight = y + ((213 * fpart * y) >> 16); |
|
1077 |
+ |
|
1078 |
+ value = cur * 128 + (lsf_res[i] * 16384) / weight; |
|
1079 |
+ nlsf[i] = av_clip(value, 0, 32767); |
|
1080 |
+ } |
|
1081 |
+ |
|
1082 |
+ /* stabilize the NLSF coefficients */ |
|
1083 |
+ silk_stabilize_lsf(nlsf, order, s->wb ? silk_lsf_min_spacing_wb : |
|
1084 |
+ silk_lsf_min_spacing_nbmb); |
|
1085 |
+ |
|
1086 |
+ /* produce an interpolation for the first 2 subframes, */ |
|
1087 |
+ /* and then convert both sets of NLSFs to LPC coefficients */ |
|
1088 |
+ *has_lpc_leadin = 0; |
|
1089 |
+ if (s->subframes == 4) { |
|
1090 |
+ int offset = opus_rc_getsymbol(rc, silk_model_lsf_interpolation_offset); |
|
1091 |
+ if (offset != 4 && frame->coded) { |
|
1092 |
+ *has_lpc_leadin = 1; |
|
1093 |
+ if (offset != 0) { |
|
1094 |
+ int16_t nlsf_leadin[16]; |
|
1095 |
+ for (i = 0; i < order; i++) |
|
1096 |
+ nlsf_leadin[i] = frame->nlsf[i] + |
|
1097 |
+ ((nlsf[i] - frame->nlsf[i]) * offset >> 2); |
|
1098 |
+ silk_lsf2lpc(nlsf_leadin, lpc_leadin, order); |
|
1099 |
+ } else /* avoid re-computation for a (roughly) 1-in-4 occurrence */ |
|
1100 |
+ memcpy(lpc_leadin, frame->lpc, 16 * sizeof(float)); |
|
1101 |
+ } else |
|
1102 |
+ offset = 4; |
|
1103 |
+ s->nlsf_interp_factor = offset; |
|
1104 |
+ |
|
1105 |
+ silk_lsf2lpc(nlsf, lpc, order); |
|
1106 |
+ } else { |
|
1107 |
+ s->nlsf_interp_factor = 4; |
|
1108 |
+ silk_lsf2lpc(nlsf, lpc, order); |
|
1109 |
+ } |
|
1110 |
+ |
|
1111 |
+ memcpy(frame->nlsf, nlsf, order * sizeof(nlsf[0])); |
|
1112 |
+ memcpy(frame->lpc, lpc, order * sizeof(lpc[0])); |
|
1113 |
+} |
|
1114 |
+ |
|
1115 |
+static inline void silk_count_children(OpusRangeCoder *rc, int model, int32_t total, |
|
1116 |
+ int32_t child[2]) |
|
1117 |
+{ |
|
1118 |
+ if (total != 0) { |
|
1119 |
+ child[0] = opus_rc_getsymbol(rc, |
|
1120 |
+ silk_model_pulse_location[model] + (((total - 1 + 5) * (total - 1)) >> 1)); |
|
1121 |
+ child[1] = total - child[0]; |
|
1122 |
+ } else { |
|
1123 |
+ child[0] = 0; |
|
1124 |
+ child[1] = 0; |
|
1125 |
+ } |
|
1126 |
+} |
|
1127 |
+ |
|
1128 |
+static inline void silk_decode_excitation(SilkContext *s, OpusRangeCoder *rc, |
|
1129 |
+ float* excitationf, |
|
1130 |
+ int qoffset_high, int active, int voiced) |
|
1131 |
+{ |
|
1132 |
+ int i; |
|
1133 |
+ uint32_t seed; |
|
1134 |
+ int shellblocks; |
|
1135 |
+ int ratelevel; |
|
1136 |
+ uint8_t pulsecount[20]; // total pulses in each shell block |
|
1137 |
+ uint8_t lsbcount[20] = {0}; // raw lsbits defined for each pulse in each shell block |
|
1138 |
+ int32_t excitation[320]; // Q23 |
|
1139 |
+ |
|
1140 |
+ /* excitation parameters */ |
|
1141 |
+ seed = opus_rc_getsymbol(rc, silk_model_lcg_seed); |
|
1142 |
+ shellblocks = silk_shell_blocks[s->bandwidth][s->subframes >> 2]; |
|
1143 |
+ ratelevel = opus_rc_getsymbol(rc, silk_model_exc_rate[voiced]); |
|
1144 |
+ |
|
1145 |
+ for (i = 0; i < shellblocks; i++) { |
|
1146 |
+ pulsecount[i] = opus_rc_getsymbol(rc, silk_model_pulse_count[ratelevel]); |
|
1147 |
+ if (pulsecount[i] == 17) { |
|
1148 |
+ while (pulsecount[i] == 17 && ++lsbcount[i] != 10) |
|
1149 |
+ pulsecount[i] = opus_rc_getsymbol(rc, silk_model_pulse_count[9]); |
|
1150 |
+ if (lsbcount[i] == 10) |
|
1151 |
+ pulsecount[i] = opus_rc_getsymbol(rc, silk_model_pulse_count[10]); |
|
1152 |
+ } |
|
1153 |
+ } |
|
1154 |
+ |
|
1155 |
+ /* decode pulse locations using PVQ */ |
|
1156 |
+ for (i = 0; i < shellblocks; i++) { |
|
1157 |
+ if (pulsecount[i] != 0) { |
|
1158 |
+ int a, b, c, d; |
|
1159 |
+ int32_t * location = excitation + 16*i; |
|
1160 |
+ int32_t branch[4][2]; |
|
1161 |
+ branch[0][0] = pulsecount[i]; |
|
1162 |
+ |
|
1163 |
+ /* unrolled tail recursion */ |
|
1164 |
+ for (a = 0; a < 1; a++) { |
|
1165 |
+ silk_count_children(rc, 0, branch[0][a], branch[1]); |
|
1166 |
+ for (b = 0; b < 2; b++) { |
|
1167 |
+ silk_count_children(rc, 1, branch[1][b], branch[2]); |
|
1168 |
+ for (c = 0; c < 2; c++) { |
|
1169 |
+ silk_count_children(rc, 2, branch[2][c], branch[3]); |
|
1170 |
+ for (d = 0; d < 2; d++) { |
|
1171 |
+ silk_count_children(rc, 3, branch[3][d], location); |
|
1172 |
+ location += 2; |
|
1173 |
+ } |
|
1174 |
+ } |
|
1175 |
+ } |
|
1176 |
+ } |
|
1177 |
+ } else |
|
1178 |
+ memset(excitation + 16*i, 0, 16*sizeof(int32_t)); |
|
1179 |
+ } |
|
1180 |
+ |
|
1181 |
+ /* decode least significant bits */ |
|
1182 |
+ for (i = 0; i < shellblocks << 4; i++) { |
|
1183 |
+ int bit; |
|
1184 |
+ for (bit = 0; bit < lsbcount[i >> 4]; bit++) |
|
1185 |
+ excitation[i] = (excitation[i] << 1) | |
|
1186 |
+ opus_rc_getsymbol(rc, silk_model_excitation_lsb); |
|
1187 |
+ } |
|
1188 |
+ |
|
1189 |
+ /* decode signs */ |
|
1190 |
+ for (i = 0; i < shellblocks << 4; i++) { |
|
1191 |
+ if (excitation[i] != 0) { |
|
1192 |
+ int sign = opus_rc_getsymbol(rc, silk_model_excitation_sign[active + |
|
1193 |
+ voiced][qoffset_high][FFMIN(pulsecount[i >> 4], 6)]); |
|
1194 |
+ if (sign == 0) |
|
1195 |
+ excitation[i] *= -1; |
|
1196 |
+ } |
|
1197 |
+ } |
|
1198 |
+ |
|
1199 |
+ /* assemble the excitation */ |
|
1200 |
+ for (i = 0; i < shellblocks << 4; i++) { |
|
1201 |
+ int value = excitation[i]; |
|
1202 |
+ excitation[i] = value * 256 | silk_quant_offset[voiced][qoffset_high]; |
|
1203 |
+ if (value < 0) excitation[i] += 20; |
|
1204 |
+ else if (value > 0) excitation[i] -= 20; |
|
1205 |
+ |
|
1206 |
+ /* invert samples pseudorandomly */ |
|
1207 |
+ seed = 196314165 * seed + 907633515; |
|
1208 |
+ if (seed & 0x80000000) |
|
1209 |
+ excitation[i] *= -1; |
|
1210 |
+ seed += value; |
|
1211 |
+ |
|
1212 |
+ excitationf[i] = excitation[i] / 8388608.0f; |
|
1213 |
+ } |
|
1214 |
+} |
|
1215 |
+ |
|
1216 |
+/** Maximum residual history according to 4.2.7.6.1 */ |
|
1217 |
+#define SILK_MAX_LAG (288 + LTP_ORDER / 2) |
|
1218 |
+ |
|
1219 |
+/** Order of the LTP filter */ |
|
1220 |
+#define LTP_ORDER 5 |
|
1221 |
+ |
|
1222 |
+static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc, |
|
1223 |
+ int frame_num, int channel, int coded_channels, int active, int active1) |
|
1224 |
+{ |
|
1225 |
+ /* per frame */ |
|
1226 |
+ int voiced; // combines with active to indicate inactive, active, or active+voiced |
|
1227 |
+ int qoffset_high; |
|
1228 |
+ int order; // order of the LPC coefficients |
|
1229 |
+ float lpc_leadin[16], lpc_body[16], residual[SILK_MAX_LAG + SILK_HISTORY]; |
|
1230 |
+ int has_lpc_leadin; |
|
1231 |
+ float ltpscale; |
|
1232 |
+ |
|
1233 |
+ /* per subframe */ |
|
1234 |
+ struct { |
|
1235 |
+ float gain; |
|
1236 |
+ int pitchlag; |
|
1237 |
+ float ltptaps[5]; |
|
1238 |
+ } sf[4]; |
|
1239 |
+ |
|
1240 |
+ SilkFrame * const frame = s->frame + channel; |
|
1241 |
+ |
|
1242 |
+ int i; |
|
1243 |
+ |
|
1244 |
+ /* obtain stereo weights */ |
|
1245 |
+ if (coded_channels == 2 && channel == 0) { |
|
1246 |
+ int n, wi[2], ws[2], w[2]; |
|
1247 |
+ n = opus_rc_getsymbol(rc, silk_model_stereo_s1); |
|
1248 |
+ wi[0] = opus_rc_getsymbol(rc, silk_model_stereo_s2) + 3 * (n / 5); |
|
1249 |
+ ws[0] = opus_rc_getsymbol(rc, silk_model_stereo_s3); |
|
1250 |
+ wi[1] = opus_rc_getsymbol(rc, silk_model_stereo_s2) + 3 * (n % 5); |
|
1251 |
+ ws[1] = opus_rc_getsymbol(rc, silk_model_stereo_s3); |
|
1252 |
+ |
|
1253 |
+ for (i = 0; i < 2; i++) |
|
1254 |
+ w[i] = silk_stereo_weights[wi[i]] + |
|
1255 |
+ (((silk_stereo_weights[wi[i] + 1] - silk_stereo_weights[wi[i]]) * 6554) >> 16) |
|
1256 |
+ * (ws[i]*2 + 1); |
|
1257 |
+ |
|
1258 |
+ s->stereo_weights[0] = (w[0] - w[1]) / 8192.0; |
|
1259 |
+ s->stereo_weights[1] = w[1] / 8192.0; |
|
1260 |
+ |
|
1261 |
+ /* and read the mid-only flag */ |
|
1262 |
+ s->midonly = active1 ? 0 : opus_rc_getsymbol(rc, silk_model_mid_only); |
|
1263 |
+ } |
|
1264 |
+ |
|
1265 |
+ /* obtain frame type */ |
|
1266 |
+ if (!active) { |
|
1267 |
+ qoffset_high = opus_rc_getsymbol(rc, silk_model_frame_type_inactive); |
|
1268 |
+ voiced = 0; |
|
1269 |
+ } else { |
|
1270 |
+ int type = opus_rc_getsymbol(rc, silk_model_frame_type_active); |
|
1271 |
+ qoffset_high = type & 1; |
|
1272 |
+ voiced = type >> 1; |
|
1273 |
+ } |
|
1274 |
+ |
|
1275 |
+ /* obtain subframe quantization gains */ |
|
1276 |
+ for (i = 0; i < s->subframes; i++) { |
|
1277 |
+ int log_gain; //Q7 |
|
1278 |
+ int ipart, fpart, lingain; |
|
1279 |
+ |
|
1280 |
+ if (i == 0 && (frame_num == 0 || !frame->coded)) { |
|
1281 |
+ /* gain is coded absolute */ |
|
1282 |
+ int x = opus_rc_getsymbol(rc, silk_model_gain_highbits[active + voiced]); |
|
1283 |
+ log_gain = (x<<3) | opus_rc_getsymbol(rc, silk_model_gain_lowbits); |
|
1284 |
+ |
|
1285 |
+ if (frame->coded) |
|
1286 |
+ log_gain = FFMAX(log_gain, frame->log_gain - 16); |
|
1287 |
+ } else { |
|
1288 |
+ /* gain is coded relative */ |
|
1289 |
+ int delta_gain = opus_rc_getsymbol(rc, silk_model_gain_delta); |
|
1290 |
+ log_gain = av_clip(FFMAX((delta_gain<<1) - 16, |
|
1291 |
+ frame->log_gain + delta_gain - 4), 0, 63); |
|
1292 |
+ } |
|
1293 |
+ |
|
1294 |
+ frame->log_gain = log_gain; |
|
1295 |
+ |
|
1296 |
+ /* approximate 2**(x/128) with a Q7 (i.e. non-integer) input */ |
|
1297 |
+ log_gain = (log_gain * 0x1D1C71 >> 16) + 2090; |
|
1298 |
+ ipart = log_gain >> 7; |
|
1299 |
+ fpart = log_gain & 127; |
|
1300 |
+ lingain = (1 << ipart) + ((-174 * fpart * (128-fpart) >>16) + fpart) * ((1<<ipart) >> 7); |
|
1301 |
+ sf[i].gain = lingain / 65536.0f; |
|
1302 |
+ } |
|
1303 |
+ |
|
1304 |
+ /* obtain LPC filter coefficients */ |
|
1305 |
+ silk_decode_lpc(s, frame, rc, lpc_leadin, lpc_body, &order, &has_lpc_leadin, voiced); |
|
1306 |
+ |
|
1307 |
+ /* obtain pitch lags, if this is a voiced frame */ |
|
1308 |
+ if (voiced) { |
|
1309 |
+ int lag_absolute = (!frame_num || !frame->prev_voiced); |
|
1310 |
+ int primarylag; // primary pitch lag for the entire SILK frame |
|
1311 |
+ int ltpfilter; |
|
1312 |
+ const int8_t * offsets; |
|
1313 |
+ |
|
1314 |
+ if (!lag_absolute) { |
|
1315 |
+ int delta = opus_rc_getsymbol(rc, silk_model_pitch_delta); |
|
1316 |
+ if (delta) |
|
1317 |
+ primarylag = frame->primarylag + delta - 9; |
|
1318 |
+ else |
|
1319 |
+ lag_absolute = 1; |
|
1320 |
+ } |
|
1321 |
+ |
|
1322 |
+ if (lag_absolute) { |
|
1323 |
+ /* primary lag is coded absolute */ |
|
1324 |
+ int highbits, lowbits; |
|
1325 |
+ const uint16_t *model[] = { |
|
1326 |
+ silk_model_pitch_lowbits_nb, silk_model_pitch_lowbits_mb, |
|
1327 |
+ silk_model_pitch_lowbits_wb |
|
1328 |
+ }; |
|
1329 |
+ highbits = opus_rc_getsymbol(rc, silk_model_pitch_highbits); |
|
1330 |
+ lowbits = opus_rc_getsymbol(rc, model[s->bandwidth]); |
|
1331 |
+ |
|
1332 |
+ primarylag = silk_pitch_min_lag[s->bandwidth] + |
|
1333 |
+ highbits*silk_pitch_scale[s->bandwidth] + lowbits; |
|
1334 |
+ } |
|
1335 |
+ frame->primarylag = primarylag; |
|
1336 |
+ |
|
1337 |
+ if (s->subframes == 2) |
|
1338 |
+ offsets = (s->bandwidth == OPUS_BANDWIDTH_NARROWBAND) |
|
1339 |
+ ? silk_pitch_offset_nb10ms[opus_rc_getsymbol(rc, |
|
1340 |
+ silk_model_pitch_contour_nb10ms)] |
|
1341 |
+ : silk_pitch_offset_mbwb10ms[opus_rc_getsymbol(rc, |
|
1342 |
+ silk_model_pitch_contour_mbwb10ms)]; |
|
1343 |
+ else |
|
1344 |
+ offsets = (s->bandwidth == OPUS_BANDWIDTH_NARROWBAND) |
|
1345 |
+ ? silk_pitch_offset_nb20ms[opus_rc_getsymbol(rc, |
|
1346 |
+ silk_model_pitch_contour_nb20ms)] |
|
1347 |
+ : silk_pitch_offset_mbwb20ms[opus_rc_getsymbol(rc, |
|
1348 |
+ silk_model_pitch_contour_mbwb20ms)]; |
|
1349 |
+ |
|
1350 |
+ for (i = 0; i < s->subframes; i++) |
|
1351 |
+ sf[i].pitchlag = av_clip(primarylag + offsets[i], |
|
1352 |
+ silk_pitch_min_lag[s->bandwidth], |
|
1353 |
+ silk_pitch_max_lag[s->bandwidth]); |
|
1354 |
+ |
|
1355 |
+ /* obtain LTP filter coefficients */ |
|
1356 |
+ ltpfilter = opus_rc_getsymbol(rc, silk_model_ltp_filter); |
|
1357 |
+ for (i = 0; i < s->subframes; i++) { |
|
1358 |
+ int index, j; |
|
1359 |
+ const uint16_t *filter_sel[] = { |
|
1360 |
+ silk_model_ltp_filter0_sel, silk_model_ltp_filter1_sel, |
|
1361 |
+ silk_model_ltp_filter2_sel |
|
1362 |
+ }; |
|
1363 |
+ const int8_t (*filter_taps[])[5] = { |
|
1364 |
+ silk_ltp_filter0_taps, silk_ltp_filter1_taps, silk_ltp_filter2_taps |
|
1365 |
+ }; |
|
1366 |
+ index = opus_rc_getsymbol(rc, filter_sel[ltpfilter]); |
|
1367 |
+ for (j = 0; j < 5; j++) |
|
1368 |
+ sf[i].ltptaps[j] = filter_taps[ltpfilter][index][j] / 128.0f; |
|
1369 |
+ } |
|
1370 |
+ } |
|
1371 |
+ |
|
1372 |
+ /* obtain LTP scale factor */ |
|
1373 |
+ if (voiced && frame_num == 0) |
|
1374 |
+ ltpscale = silk_ltp_scale_factor[opus_rc_getsymbol(rc, |
|
1375 |
+ silk_model_ltp_scale_index)] / 16384.0f; |
|
1376 |
+ else ltpscale = 15565.0f/16384.0f; |
|
1377 |
+ |
|
1378 |
+ /* generate the excitation signal for the entire frame */ |
|
1379 |
+ silk_decode_excitation(s, rc, residual + SILK_MAX_LAG, qoffset_high, |
|
1380 |
+ active, voiced); |
|
1381 |
+ |
|
1382 |
+ /* skip synthesising the side channel if we want mono-only */ |
|
1383 |
+ if (s->output_channels == channel) |
|
1384 |
+ return; |
|
1385 |
+ |
|
1386 |
+ /* generate the output signal */ |
|
1387 |
+ for (i = 0; i < s->subframes; i++) { |
|
1388 |
+ const float * lpc_coeff = (i < 2 && has_lpc_leadin) ? lpc_leadin : lpc_body; |
|
1389 |
+ float *dst = frame->output + SILK_HISTORY + i * s->sflength; |
|
1390 |
+ float *resptr = residual + SILK_MAX_LAG + i * s->sflength; |
|
1391 |
+ float *lpc = frame->lpc_history + SILK_HISTORY + i * s->sflength; |
|
1392 |
+ float sum; |
|
1393 |
+ int j, k; |
|
1394 |
+ |
|
1395 |
+ if (voiced) { |
|
1396 |
+ int out_end; |
|
1397 |
+ float scale; |
|
1398 |
+ |
|
1399 |
+ if (i < 2 || s->nlsf_interp_factor == 4) { |
|
1400 |
+ out_end = -i * s->sflength; |
|
1401 |
+ scale = ltpscale; |
|
1402 |
+ } else { |
|
1403 |
+ out_end = -(i - 2) * s->sflength; |
|
1404 |
+ scale = 1.0f; |
|
1405 |
+ } |
|
1406 |
+ |
|
1407 |
+ /* when the LPC coefficients change, a re-whitening filter is used */ |
|
1408 |
+ /* to produce a residual that accounts for the change */ |
|
1409 |
+ for (j = - sf[i].pitchlag - LTP_ORDER/2; j < out_end; j++) { |
|
1410 |
+ sum = dst[j]; |
|
1411 |
+ for (k = 0; k < order; k++) |
|
1412 |
+ sum -= lpc_coeff[k] * dst[j - k - 1]; |
|
1413 |
+ resptr[j] = av_clipf(sum, -1.0f, 1.0f) * scale / sf[i].gain; |
|
1414 |
+ } |
|
1415 |
+ |
|
1416 |
+ if (out_end) { |
|
1417 |
+ float rescale = sf[i-1].gain / sf[i].gain; |
|
1418 |
+ for (j = out_end; j < 0; j++) |
|
1419 |
+ resptr[j] *= rescale; |
|
1420 |
+ } |
|
1421 |
+ |
|
1422 |
+ /* LTP synthesis */ |
|
1423 |
+ for (j = 0; j < s->sflength; j++) { |
|
1424 |
+ sum = resptr[j]; |
|
1425 |
+ for (k = 0; k < LTP_ORDER; k++) |
|
1426 |
+ sum += sf[i].ltptaps[k] * resptr[j - sf[i].pitchlag + LTP_ORDER/2 - k]; |
|
1427 |
+ resptr[j] = sum; |
|
1428 |
+ } |
|
1429 |
+ } |
|
1430 |
+ |
|
1431 |
+ /* LPC synthesis */ |
|
1432 |
+ for (j = 0; j < s->sflength; j++) { |
|
1433 |
+ sum = resptr[j] * sf[i].gain; |
|
1434 |
+ for (k = 1; k <= order; k++) |
|
1435 |
+ sum += lpc_coeff[k - 1] * lpc[j - k]; |
|
1436 |
+ |
|
1437 |
+ lpc[j] = sum; |
|
1438 |
+ dst[j] = av_clipf(sum, -1.0f, 1.0f); |
|
1439 |
+ } |
|
1440 |
+ } |
|
1441 |
+ |
|
1442 |
+ frame->prev_voiced = voiced; |
|
1443 |
+ memmove(frame->lpc_history, frame->lpc_history + s->flength, SILK_HISTORY * sizeof(float)); |
|
1444 |
+ memmove(frame->output, frame->output + s->flength, SILK_HISTORY * sizeof(float)); |
|
1445 |
+ |
|
1446 |
+ frame->coded = 1; |
|
1447 |
+} |
|
1448 |
+ |
|
1449 |
+static void silk_unmix_ms(SilkContext *s, float *l, float *r) |
|
1450 |
+{ |
|
1451 |
+ float *mid = s->frame[0].output + SILK_HISTORY - s->flength; |
|
1452 |
+ float *side = s->frame[1].output + SILK_HISTORY - s->flength; |
|
1453 |
+ float w0_prev = s->prev_stereo_weights[0]; |
|
1454 |
+ float w1_prev = s->prev_stereo_weights[1]; |
|
1455 |
+ float w0 = s->stereo_weights[0]; |
|
1456 |
+ float w1 = s->stereo_weights[1]; |
|
1457 |
+ int n1 = silk_stereo_interp_len[s->bandwidth]; |
|
1458 |
+ int i; |
|
1459 |
+ |
|
1460 |
+ for (i = 0; i < n1; i++) { |
|
1461 |
+ float interp0 = w0_prev + i * (w0 - w0_prev) / n1; |
|
1462 |
+ float interp1 = w1_prev + i * (w1 - w1_prev) / n1; |
|
1463 |
+ float p0 = 0.25 * (mid[i - 2] + 2 * mid[i - 1] + mid[i]); |
|
1464 |
+ |
|
1465 |
+ l[i] = av_clipf((1 + interp1) * mid[i - 1] + side[i - 1] + interp0 * p0, -1.0, 1.0); |
|
1466 |
+ r[i] = av_clipf((1 - interp1) * mid[i - 1] - side[i - 1] - interp0 * p0, -1.0, 1.0); |
|
1467 |
+ } |
|
1468 |
+ |
|
1469 |
+ for (; i < s->flength; i++) { |
|
1470 |
+ float p0 = 0.25 * (mid[i - 2] + 2 * mid[i - 1] + mid[i]); |
|
1471 |
+ |
|
1472 |
+ l[i] = av_clipf((1 + w1) * mid[i - 1] + side[i - 1] + w0 * p0, -1.0, 1.0); |
|
1473 |
+ r[i] = av_clipf((1 - w1) * mid[i - 1] - side[i - 1] - w0 * p0, -1.0, 1.0); |
|
1474 |
+ } |
|
1475 |
+ |
|
1476 |
+ memcpy(s->prev_stereo_weights, s->stereo_weights, sizeof(s->stereo_weights)); |
|
1477 |
+} |
|
1478 |
+ |
|
1479 |
+static void silk_flush_frame(SilkFrame *frame) |
|
1480 |
+{ |
|
1481 |
+ if (!frame->coded) |
|
1482 |
+ return; |
|
1483 |
+ |
|
1484 |
+ memset(frame->output, 0, sizeof(frame->output)); |
|
1485 |
+ memset(frame->lpc_history, 0, sizeof(frame->lpc_history)); |
|
1486 |
+ |
|
1487 |
+ memset(frame->lpc, 0, sizeof(frame->lpc)); |
|
1488 |
+ memset(frame->nlsf, 0, sizeof(frame->nlsf)); |
|
1489 |
+ |
|
1490 |
+ frame->log_gain = 0; |
|
1491 |
+ |
|
1492 |
+ frame->primarylag = 0; |
|
1493 |
+ frame->prev_voiced = 0; |
|
1494 |
+ frame->coded = 0; |
|
1495 |
+} |
|
1496 |
+ |
|
1497 |
+int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, |
|
1498 |
+ float *output[2], |
|
1499 |
+ enum OpusBandwidth bandwidth, |
|
1500 |
+ int coded_channels, |
|
1501 |
+ int duration_ms) |
|
1502 |
+{ |
|
1503 |
+ int active[2][6], redundancy[2]; |
|
1504 |
+ int nb_frames, i, j; |
|
1505 |
+ |
|
1506 |
+ if (bandwidth > OPUS_BANDWIDTH_WIDEBAND || |
|
1507 |
+ coded_channels > 2 || duration_ms > 60) { |
|
1508 |
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid parameters passed " |
|
1509 |
+ "to the SILK decoder.\n"); |
|
1510 |
+ return AVERROR(EINVAL); |
|
1511 |
+ } |
|
1512 |
+ |
|
1513 |
+ nb_frames = 1 + (duration_ms > 20) + (duration_ms > 40); |
|
1514 |
+ s->subframes = duration_ms / nb_frames / 5; // 5ms subframes |
|
1515 |
+ s->sflength = 20 * (bandwidth + 2); |
|
1516 |
+ s->flength = s->sflength * s->subframes; |
|
1517 |
+ s->bandwidth = bandwidth; |
|
1518 |
+ s->wb = bandwidth == OPUS_BANDWIDTH_WIDEBAND; |
|
1519 |
+ |
|
1520 |
+ /* make sure to flush the side channel when switching from mono to stereo */ |
|
1521 |
+ if (coded_channels > s->prev_coded_channels) |
|
1522 |
+ silk_flush_frame(&s->frame[1]); |
|
1523 |
+ s->prev_coded_channels = coded_channels; |
|
1524 |
+ |
|
1525 |
+ /* read the LP-layer header bits */ |
|
1526 |
+ for (i = 0; i < coded_channels; i++) { |
|
1527 |
+ for (j = 0; j < nb_frames; j++) |
|
1528 |
+ active[i][j] = opus_rc_p2model(rc, 1); |
|
1529 |
+ |
|
1530 |
+ redundancy[i] = opus_rc_p2model(rc, 1); |
|
1531 |
+ if (redundancy[i]) { |
|
1532 |
+ av_log(s->avctx, AV_LOG_ERROR, "LBRR frames present; this is unsupported\n"); |
|
1533 |
+ return AVERROR_PATCHWELCOME; |
|
1534 |
+ } |
|
1535 |
+ } |
|
1536 |
+ |
|
1537 |
+ for (i = 0; i < nb_frames; i++) { |
|
1538 |
+ for (j = 0; j < coded_channels && !s->midonly; j++) |
|
1539 |
+ silk_decode_frame(s, rc, i, j, coded_channels, active[j][i], active[1][i]); |
|
1540 |
+ |
|
1541 |
+ /* reset the side channel if it is not coded */ |
|
1542 |
+ if (s->midonly && s->frame[1].coded) |
|
1543 |
+ silk_flush_frame(&s->frame[1]); |
|
1544 |
+ |
|
1545 |
+ if (coded_channels == 1 || s->output_channels == 1) { |
|
1546 |
+ for (j = 0; j < s->output_channels; j++) { |
|
1547 |
+ memcpy(output[j] + i * s->flength, |
|
1548 |
+ s->frame[0].output + SILK_HISTORY - s->flength - 2, |
|
1549 |
+ s->flength * sizeof(float)); |
|
1550 |
+ } |
|
1551 |
+ } else { |
|
1552 |
+ silk_unmix_ms(s, output[0] + i * s->flength, output[1] + i * s->flength); |
|
1553 |
+ } |
|
1554 |
+ |
|
1555 |
+ s->midonly = 0; |
|
1556 |
+ } |
|
1557 |
+ |
|
1558 |
+ return nb_frames * s->flength; |
|
1559 |
+} |
|
1560 |
+ |
|
1561 |
+void ff_silk_free(SilkContext **ps) |
|
1562 |
+{ |
|
1563 |
+ av_freep(ps); |
|
1564 |
+} |
|
1565 |
+ |
|
1566 |
+void ff_silk_flush(SilkContext *s) |
|
1567 |
+{ |
|
1568 |
+ silk_flush_frame(&s->frame[0]); |
|
1569 |
+ silk_flush_frame(&s->frame[1]); |
|
1570 |
+ |
|
1571 |
+ memset(s->prev_stereo_weights, 0, sizeof(s->prev_stereo_weights)); |
|
1572 |
+} |
|
1573 |
+ |
|
1574 |
+int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels) |
|
1575 |
+{ |
|
1576 |
+ SilkContext *s; |
|
1577 |
+ |
|
1578 |
+ if (output_channels != 1 && output_channels != 2) { |
|
1579 |
+ av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n", |
|
1580 |
+ output_channels); |
|
1581 |
+ return AVERROR(EINVAL); |
|
1582 |
+ } |
|
1583 |
+ |
|
1584 |
+ s = av_mallocz(sizeof(*s)); |
|
1585 |
+ if (!s) |
|
1586 |
+ return AVERROR(ENOMEM); |
|
1587 |
+ |
|
1588 |
+ s->avctx = avctx; |
|
1589 |
+ s->output_channels = output_channels; |
|
1590 |
+ |
|
1591 |
+ ff_silk_flush(s); |
|
1592 |
+ |
|
1593 |
+ *ps = s; |
|
1594 |
+ |
|
1595 |
+ return 0; |
|
1596 |
+} |
0 | 1597 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,674 @@ |
0 |
+/* |
|
1 |
+ * Opus decoder |
|
2 |
+ * Copyright (c) 2012 Andrew D'Addesio |
|
3 |
+ * Copyright (c) 2013-2014 Mozilla Corporation |
|
4 |
+ * |
|
5 |
+ * This file is part of Libav. |
|
6 |
+ * |
|
7 |
+ * Libav 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 |
+ * Libav 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 Libav; if not, write to the Free Software |
|
19 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
20 |
+ */ |
|
21 |
+ |
|
22 |
+/** |
|
23 |
+ * @file |
|
24 |
+ * Opus decoder |
|
25 |
+ * @author Andrew D'Addesio, Anton Khirnov |
|
26 |
+ * |
|
27 |
+ * Codec homepage: http://opus-codec.org/ |
|
28 |
+ * Specification: http://tools.ietf.org/html/rfc6716 |
|
29 |
+ * Ogg Opus specification: https://tools.ietf.org/html/draft-ietf-codec-oggopus-03 |
|
30 |
+ * |
|
31 |
+ * Ogg-contained .opus files can be produced with opus-tools: |
|
32 |
+ * http://git.xiph.org/?p=opus-tools.git |
|
33 |
+ */ |
|
34 |
+ |
|
35 |
+#include <stdint.h> |
|
36 |
+ |
|
37 |
+#include "libavutil/attributes.h" |
|
38 |
+#include "libavutil/audio_fifo.h" |
|
39 |
+#include "libavutil/channel_layout.h" |
|
40 |
+#include "libavutil/opt.h" |
|
41 |
+ |
|
42 |
+#include "libavresample/avresample.h" |
|
43 |
+ |
|
44 |
+#include "avcodec.h" |
|
45 |
+#include "celp_filters.h" |
|
46 |
+#include "fft.h" |
|
47 |
+#include "get_bits.h" |
|
48 |
+#include "internal.h" |
|
49 |
+#include "mathops.h" |
|
50 |
+#include "opus.h" |
|
51 |
+ |
|
52 |
+static const uint16_t silk_frame_duration_ms[16] = { |
|
53 |
+ 10, 20, 40, 60, |
|
54 |
+ 10, 20, 40, 60, |
|
55 |
+ 10, 20, 40, 60, |
|
56 |
+ 10, 20, |
|
57 |
+ 10, 20, |
|
58 |
+}; |
|
59 |
+ |
|
60 |
+/* number of samples of silence to feed to the resampler |
|
61 |
+ * at the beginning */ |
|
62 |
+static const int silk_resample_delay[] = { |
|
63 |
+ 4, 8, 11, 11, 11 |
|
64 |
+}; |
|
65 |
+ |
|
66 |
+static const uint8_t celt_band_end[] = { 13, 17, 17, 19, 21 }; |
|
67 |
+ |
|
68 |
+static int get_silk_samplerate(int config) |
|
69 |
+{ |
|
70 |
+ if (config < 4) |
|
71 |
+ return 8000; |
|
72 |
+ else if (config < 8) |
|
73 |
+ return 12000; |
|
74 |
+ return 16000; |
|
75 |
+} |
|
76 |
+ |
|
77 |
+/** |
|
78 |
+ * Range decoder |
|
79 |
+ */ |
|
80 |
+static int opus_rc_init(OpusRangeCoder *rc, const uint8_t *data, int size) |
|
81 |
+{ |
|
82 |
+ int ret = init_get_bits8(&rc->gb, data, size); |
|
83 |
+ if (ret < 0) |
|
84 |
+ return ret; |
|
85 |
+ |
|
86 |
+ rc->range = 128; |
|
87 |
+ rc->value = 127 - get_bits(&rc->gb, 7); |
|
88 |
+ rc->total_read_bits = 9; |
|
89 |
+ opus_rc_normalize(rc); |
|
90 |
+ |
|
91 |
+ return 0; |
|
92 |
+} |
|
93 |
+ |
|
94 |
+static void opus_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, |
|
95 |
+ unsigned int bytes) |
|
96 |
+{ |
|
97 |
+ rc->rb.position = rightend; |
|
98 |
+ rc->rb.bytes = bytes; |
|
99 |
+ rc->rb.cachelen = 0; |
|
100 |
+ rc->rb.cacheval = 0; |
|
101 |
+} |
|
102 |
+ |
|
103 |
+static void opus_fade(float *out, |
|
104 |
+ const float *in1, const float *in2, |
|
105 |
+ const float *window, int len) |
|
106 |
+{ |
|
107 |
+ int i; |
|
108 |
+ for (i = 0; i < len; i++) |
|
109 |
+ out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]); |
|
110 |
+} |
|
111 |
+ |
|
112 |
+static int opus_flush_resample(OpusStreamContext *s, int nb_samples) |
|
113 |
+{ |
|
114 |
+ int celt_size = av_audio_fifo_size(s->celt_delay); |
|
115 |
+ int ret, i; |
|
116 |
+ |
|
117 |
+ ret = avresample_convert(s->avr, (uint8_t**)s->out, s->out_size, nb_samples, |
|
118 |
+ NULL, 0, 0); |
|
119 |
+ if (ret < 0) |
|
120 |
+ return ret; |
|
121 |
+ else if (ret != nb_samples) { |
|
122 |
+ av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n", |
|
123 |
+ ret); |
|
124 |
+ return AVERROR_BUG; |
|
125 |
+ } |
|
126 |
+ |
|
127 |
+ if (celt_size) { |
|
128 |
+ if (celt_size != nb_samples) { |
|
129 |
+ av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n"); |
|
130 |
+ return AVERROR_BUG; |
|
131 |
+ } |
|
132 |
+ av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples); |
|
133 |
+ for (i = 0; i < s->output_channels; i++) { |
|
134 |
+ s->fdsp->vector_fmac_scalar(s->out[i], |
|
135 |
+ s->celt_output[i], 1.0, |
|
136 |
+ nb_samples); |
|
137 |
+ } |
|
138 |
+ } |
|
139 |
+ |
|
140 |
+ if (s->redundancy_idx) { |
|
141 |
+ for (i = 0; i < s->output_channels; i++) |
|
142 |
+ opus_fade(s->out[i], s->out[i], |
|
143 |
+ s->redundancy_output[i] + 120 + s->redundancy_idx, |
|
144 |
+ ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx); |
|
145 |
+ s->redundancy_idx = 0; |
|
146 |
+ } |
|
147 |
+ |
|
148 |
+ s->out[0] += nb_samples; |
|
149 |
+ s->out[1] += nb_samples; |
|
150 |
+ s->out_size -= nb_samples * sizeof(float); |
|
151 |
+ |
|
152 |
+ return 0; |
|
153 |
+} |
|
154 |
+ |
|
155 |
+static int opus_init_resample(OpusStreamContext *s) |
|
156 |
+{ |
|
157 |
+ float delay[16] = { 0.0 }; |
|
158 |
+ uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay }; |
|
159 |
+ int ret; |
|
160 |
+ |
|
161 |
+ av_opt_set_int(s->avr, "in_sample_rate", s->silk_samplerate, 0); |
|
162 |
+ ret = avresample_open(s->avr); |
|
163 |
+ if (ret < 0) { |
|
164 |
+ av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n"); |
|
165 |
+ return ret; |
|
166 |
+ } |
|
167 |
+ |
|
168 |
+ ret = avresample_convert(s->avr, NULL, 0, 0, delayptr, sizeof(delay), |
|
169 |
+ silk_resample_delay[s->packet.bandwidth]); |
|
170 |
+ if (ret < 0) { |
|
171 |
+ av_log(s->avctx, AV_LOG_ERROR, |
|
172 |
+ "Error feeding initial silence to the resampler.\n"); |
|
173 |
+ return ret; |
|
174 |
+ } |
|
175 |
+ |
|
176 |
+ return 0; |
|
177 |
+} |
|
178 |
+ |
|
179 |
+static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size) |
|
180 |
+{ |
|
181 |
+ int ret; |
|
182 |
+ enum OpusBandwidth bw = s->packet.bandwidth; |
|
183 |
+ |
|
184 |
+ if (s->packet.mode == OPUS_MODE_SILK && |
|
185 |
+ bw == OPUS_BANDWIDTH_MEDIUMBAND) |
|
186 |
+ bw = OPUS_BANDWIDTH_WIDEBAND; |
|
187 |
+ |
|
188 |
+ ret = opus_rc_init(&s->redundancy_rc, data, size); |
|
189 |
+ if (ret < 0) |
|
190 |
+ goto fail; |
|
191 |
+ opus_raw_init(&s->redundancy_rc, data + size, size); |
|
192 |
+ |
|
193 |
+ ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc, |
|
194 |
+ s->redundancy_output, |
|
195 |
+ s->packet.stereo + 1, 240, |
|
196 |
+ 0, celt_band_end[s->packet.bandwidth]); |
|
197 |
+ if (ret < 0) |
|
198 |
+ goto fail; |
|
199 |
+ |
|
200 |
+ return 0; |
|
201 |
+fail: |
|
202 |
+ av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n"); |
|
203 |
+ return ret; |
|
204 |
+} |
|
205 |
+ |
|
206 |
+static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size) |
|
207 |
+{ |
|
208 |
+ int samples = s->packet.frame_duration; |
|
209 |
+ int redundancy = 0; |
|
210 |
+ int redundancy_size, redundancy_pos; |
|
211 |
+ int ret, i, consumed; |
|
212 |
+ int delayed_samples = s->delayed_samples; |
|
213 |
+ |
|
214 |
+ ret = opus_rc_init(&s->rc, data, size); |
|
215 |
+ if (ret < 0) |
|
216 |
+ return ret; |
|
217 |
+ |
|
218 |
+ /* decode the silk frame */ |
|
219 |
+ if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) { |
|
220 |
+ if (!avresample_is_open(s->avr)) { |
|
221 |
+ ret = opus_init_resample(s); |
|
222 |
+ if (ret < 0) |
|
223 |
+ return ret; |
|
224 |
+ } |
|
225 |
+ |
|
226 |
+ samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output, |
|
227 |
+ FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND), |
|
228 |
+ s->packet.stereo + 1, |
|
229 |
+ silk_frame_duration_ms[s->packet.config]); |
|
230 |
+ if (samples < 0) { |
|
231 |
+ av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n"); |
|
232 |
+ return samples; |
|
233 |
+ } |
|
234 |
+ |
|
235 |
+ samples = avresample_convert(s->avr, (uint8_t**)s->out, s->out_size, |
|
236 |
+ s->packet.frame_duration, |
|
237 |
+ (uint8_t**)s->silk_output, |
|
238 |
+ sizeof(s->silk_buf[0]), |
|
239 |
+ samples); |
|
240 |
+ if (samples < 0) { |
|
241 |
+ av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n"); |
|
242 |
+ return samples; |
|
243 |
+ } |
|
244 |
+ s->delayed_samples += s->packet.frame_duration - samples; |
|
245 |
+ } else |
|
246 |
+ ff_silk_flush(s->silk); |
|
247 |
+ |
|
248 |
+ // decode redundancy information |
|
249 |
+ consumed = opus_rc_tell(&s->rc); |
|
250 |
+ if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8) |
|
251 |
+ redundancy = opus_rc_p2model(&s->rc, 12); |
|
252 |
+ else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8) |
|
253 |
+ redundancy = 1; |
|
254 |
+ |
|
255 |
+ if (redundancy) { |
|
256 |
+ redundancy_pos = opus_rc_p2model(&s->rc, 1); |
|
257 |
+ |
|
258 |
+ if (s->packet.mode == OPUS_MODE_HYBRID) |
|
259 |
+ redundancy_size = opus_rc_unimodel(&s->rc, 256) + 2; |
|
260 |
+ else |
|
261 |
+ redundancy_size = size - (consumed + 7) / 8; |
|
262 |
+ size -= redundancy_size; |
|
263 |
+ if (size < 0) { |
|
264 |
+ av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n"); |
|
265 |
+ return AVERROR_INVALIDDATA; |
|
266 |
+ } |
|
267 |
+ |
|
268 |
+ if (redundancy_pos) { |
|
269 |
+ ret = opus_decode_redundancy(s, data + size, redundancy_size); |
|
270 |
+ if (ret < 0) |
|
271 |
+ return ret; |
|
272 |
+ ff_celt_flush(s->celt); |
|
273 |
+ } |
|
274 |
+ } |
|
275 |
+ |
|
276 |
+ /* decode the CELT frame */ |
|
277 |
+ if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) { |
|
278 |
+ float *out_tmp[2] = { s->out[0], s->out[1] }; |
|
279 |
+ float **dst = (s->packet.mode == OPUS_MODE_CELT) ? |
|
280 |
+ out_tmp : s->celt_output; |
|
281 |
+ int celt_output_samples = samples; |
|
282 |
+ int delay_samples = av_audio_fifo_size(s->celt_delay); |
|
283 |
+ |
|
284 |
+ if (delay_samples) { |
|
285 |
+ if (s->packet.mode == OPUS_MODE_HYBRID) { |
|
286 |
+ av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples); |
|
287 |
+ |
|
288 |
+ for (i = 0; i < s->output_channels; i++) { |
|
289 |
+ s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0, |
|
290 |
+ delay_samples); |
|
291 |
+ out_tmp[i] += delay_samples; |
|
292 |
+ } |
|
293 |
+ celt_output_samples -= delay_samples; |
|
294 |
+ } else { |
|
295 |
+ av_log(s->avctx, AV_LOG_WARNING, |
|
296 |
+ "Spurious CELT delay samples present.\n"); |
|
297 |
+ av_audio_fifo_drain(s->celt_delay, delay_samples); |
|
298 |
+ if (s->avctx->err_recognition & AV_EF_EXPLODE) |
|
299 |
+ return AVERROR_BUG; |
|
300 |
+ } |
|
301 |
+ } |
|
302 |
+ |
|
303 |
+ opus_raw_init(&s->rc, data + size, size); |
|
304 |
+ |
|
305 |
+ ret = ff_celt_decode_frame(s->celt, &s->rc, dst, |
|
306 |
+ s->packet.stereo + 1, |
|
307 |
+ s->packet.frame_duration, |
|
308 |
+ (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0, |
|
309 |
+ celt_band_end[s->packet.bandwidth]); |
|
310 |
+ if (ret < 0) |
|
311 |
+ return ret; |
|
312 |
+ |
|
313 |
+ if (s->packet.mode == OPUS_MODE_HYBRID) { |
|
314 |
+ int celt_delay = s->packet.frame_duration - celt_output_samples; |
|
315 |
+ void *delaybuf[2] = { s->celt_output[0] + celt_output_samples, |
|
316 |
+ s->celt_output[1] + celt_output_samples }; |
|
317 |
+ |
|
318 |
+ for (i = 0; i < s->output_channels; i++) { |
|
319 |
+ s->fdsp->vector_fmac_scalar(out_tmp[i], |
|
320 |
+ s->celt_output[i], 1.0, |
|
321 |
+ celt_output_samples); |
|
322 |
+ } |
|
323 |
+ |
|
324 |
+ ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay); |
|
325 |
+ if (ret < 0) |
|
326 |
+ return ret; |
|
327 |
+ } |
|
328 |
+ } else |
|
329 |
+ ff_celt_flush(s->celt); |
|
330 |
+ |
|
331 |
+ if (s->redundancy_idx) { |
|
332 |
+ for (i = 0; i < s->output_channels; i++) |
|
333 |
+ opus_fade(s->out[i], s->out[i], |
|
334 |
+ s->redundancy_output[i] + 120 + s->redundancy_idx, |
|
335 |
+ ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx); |
|
336 |
+ s->redundancy_idx = 0; |
|
337 |
+ } |
|
338 |
+ if (redundancy) { |
|
339 |
+ if (!redundancy_pos) { |
|
340 |
+ ff_celt_flush(s->celt); |
|
341 |
+ ret = opus_decode_redundancy(s, data + size, redundancy_size); |
|
342 |
+ if (ret < 0) |
|
343 |
+ return ret; |
|
344 |
+ |
|
345 |
+ for (i = 0; i < s->output_channels; i++) { |
|
346 |
+ opus_fade(s->out[i] + samples - 120 + delayed_samples, |
|
347 |
+ s->out[i] + samples - 120 + delayed_samples, |
|
348 |
+ s->redundancy_output[i] + 120, |
|
349 |
+ ff_celt_window2, 120 - delayed_samples); |
|
350 |
+ if (delayed_samples) |
|
351 |
+ s->redundancy_idx = 120 - delayed_samples; |
|
352 |
+ } |
|
353 |
+ } else { |
|
354 |
+ for (i = 0; i < s->output_channels; i++) { |
|
355 |
+ memcpy(s->out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float)); |
|
356 |
+ opus_fade(s->out[i] + 120 + delayed_samples, |
|
357 |
+ s->redundancy_output[i] + 120, |
|
358 |
+ s->out[i] + 120 + delayed_samples, |
|
359 |
+ ff_celt_window2, 120); |
|
360 |
+ } |
|
361 |
+ } |
|
362 |
+ } |
|
363 |
+ |
|
364 |
+ return samples; |
|
365 |
+} |
|
366 |
+ |
|
367 |
+static int opus_decode_subpacket(OpusStreamContext *s, |
|
368 |
+ const uint8_t *buf, int buf_size, |
|
369 |
+ int nb_samples) |
|
370 |
+{ |
|
371 |
+ int output_samples = 0; |
|
372 |
+ int flush_needed = 0; |
|
373 |
+ int i, j, ret; |
|
374 |
+ |
|
375 |
+ /* check if we need to flush the resampler */ |
|
376 |
+ if (avresample_is_open(s->avr)) { |
|
377 |
+ if (buf) { |
|
378 |
+ int64_t cur_samplerate; |
|
379 |
+ av_opt_get_int(s->avr, "in_sample_rate", 0, &cur_samplerate); |
|
380 |
+ flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate); |
|
381 |
+ } else { |
|
382 |
+ flush_needed = !!s->delayed_samples; |
|
383 |
+ } |
|
384 |
+ } |
|
385 |
+ |
|
386 |
+ if (!buf && !flush_needed) |
|
387 |
+ return 0; |
|
388 |
+ |
|
389 |
+ /* use dummy output buffers if the channel is not mapped to anything */ |
|
390 |
+ if (!s->out[0] || |
|
391 |
+ (s->output_channels == 2 && !s->out[1])) { |
|
392 |
+ av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size, s->out_size); |
|
393 |
+ if (!s->out_dummy) |
|
394 |
+ return AVERROR(ENOMEM); |
|
395 |
+ if (!s->out[0]) |
|
396 |
+ s->out[0] = s->out_dummy; |
|
397 |
+ if (!s->out[1]) |
|
398 |
+ s->out[1] = s->out_dummy; |
|
399 |
+ } |
|
400 |
+ |
|
401 |
+ /* flush the resampler if necessary */ |
|
402 |
+ if (flush_needed) { |
|
403 |
+ ret = opus_flush_resample(s, s->delayed_samples); |
|
404 |
+ if (ret < 0) { |
|
405 |
+ av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n"); |
|
406 |
+ return ret; |
|
407 |
+ } |
|
408 |
+ avresample_close(s->avr); |
|
409 |
+ output_samples += s->delayed_samples; |
|
410 |
+ s->delayed_samples = 0; |
|
411 |
+ |
|
412 |
+ if (!buf) |
|
413 |
+ goto finish; |
|
414 |
+ } |
|
415 |
+ |
|
416 |
+ /* decode all the frames in the packet */ |
|
417 |
+ for (i = 0; i < s->packet.frame_count; i++) { |
|
418 |
+ int size = s->packet.frame_size[i]; |
|
419 |
+ int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size); |
|
420 |
+ |
|
421 |
+ if (samples < 0) { |
|
422 |
+ av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n"); |
|
423 |
+ if (s->avctx->err_recognition & AV_EF_EXPLODE) |
|
424 |
+ return samples; |
|
425 |
+ |
|
426 |
+ for (j = 0; j < s->output_channels; j++) |
|
427 |
+ memset(s->out[j], 0, s->packet.frame_duration * sizeof(float)); |
|
428 |
+ samples = s->packet.frame_duration; |
|
429 |
+ } |
|
430 |
+ output_samples += samples; |
|
431 |
+ |
|
432 |
+ for (j = 0; j < s->output_channels; j++) |
|
433 |
+ s->out[j] += samples; |
|
434 |
+ s->out_size -= samples * sizeof(float); |
|
435 |
+ } |
|
436 |
+ |
|
437 |
+finish: |
|
438 |
+ s->out[0] = s->out[1] = NULL; |
|
439 |
+ s->out_size = 0; |
|
440 |
+ |
|
441 |
+ return output_samples; |
|
442 |
+} |
|
443 |
+ |
|
444 |
+static int opus_decode_packet(AVCodecContext *avctx, void *data, |
|
445 |
+ int *got_frame_ptr, AVPacket *avpkt) |
|
446 |
+{ |
|
447 |
+ OpusContext *c = avctx->priv_data; |
|
448 |
+ AVFrame *frame = data; |
|
449 |
+ const uint8_t *buf = avpkt->data; |
|
450 |
+ int buf_size = avpkt->size; |
|
451 |
+ int coded_samples = 0; |
|
452 |
+ int decoded_samples = 0; |
|
453 |
+ int i, ret; |
|
454 |
+ |
|
455 |
+ /* decode the header of the first sub-packet to find out the sample count */ |
|
456 |
+ if (buf) { |
|
457 |
+ OpusPacket *pkt = &c->streams[0].packet; |
|
458 |
+ ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1); |
|
459 |
+ if (ret < 0) { |
|
460 |
+ av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n"); |
|
461 |
+ return ret; |
|
462 |
+ } |
|
463 |
+ coded_samples += pkt->frame_count * pkt->frame_duration; |
|
464 |
+ c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config); |
|
465 |
+ } |
|
466 |
+ |
|
467 |
+ frame->nb_samples = coded_samples + c->streams[0].delayed_samples; |
|
468 |
+ |
|
469 |
+ /* no input or buffered data => nothing to do */ |
|
470 |
+ if (!frame->nb_samples) { |
|
471 |
+ *got_frame_ptr = 0; |
|
472 |
+ return 0; |
|
473 |
+ } |
|
474 |
+ |
|
475 |
+ /* setup the data buffers */ |
|
476 |
+ ret = ff_get_buffer(avctx, frame, 0); |
|
477 |
+ if (ret < 0) { |
|
478 |
+ av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); |
|
479 |
+ return ret; |
|
480 |
+ } |
|
481 |
+ frame->nb_samples = 0; |
|
482 |
+ |
|
483 |
+ for (i = 0; i < avctx->channels; i++) { |
|
484 |
+ ChannelMap *map = &c->channel_maps[i]; |
|
485 |
+ if (!map->copy) |
|
486 |
+ c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i]; |
|
487 |
+ } |
|
488 |
+ |
|
489 |
+ for (i = 0; i < c->nb_streams; i++) |
|
490 |
+ c->streams[i].out_size = frame->linesize[0]; |
|
491 |
+ |
|
492 |
+ /* decode each sub-packet */ |
|
493 |
+ for (i = 0; i < c->nb_streams; i++) { |
|
494 |
+ OpusStreamContext *s = &c->streams[i]; |
|
495 |
+ |
|
496 |
+ if (i && buf) { |
|
497 |
+ ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1); |
|
498 |
+ if (ret < 0) { |
|
499 |
+ av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n"); |
|
500 |
+ return ret; |
|
501 |
+ } |
|
502 |
+ s->silk_samplerate = get_silk_samplerate(s->packet.config); |
|
503 |
+ } |
|
504 |
+ |
|
505 |
+ ret = opus_decode_subpacket(&c->streams[i], buf, |
|
506 |
+ s->packet.data_size, coded_samples); |
|
507 |
+ if (ret < 0) |
|
508 |
+ return ret; |
|
509 |
+ if (decoded_samples && ret != decoded_samples) { |
|
510 |
+ av_log(avctx, AV_LOG_ERROR, "Different numbers of decoded samples " |
|
511 |
+ "in a multi-channel stream\n"); |
|
512 |
+ return AVERROR_INVALIDDATA; |
|
513 |
+ } |
|
514 |
+ decoded_samples = ret; |
|
515 |
+ buf += s->packet.packet_size; |
|
516 |
+ buf_size -= s->packet.packet_size; |
|
517 |
+ } |
|
518 |
+ |
|
519 |
+ for (i = 0; i < avctx->channels; i++) { |
|
520 |
+ ChannelMap *map = &c->channel_maps[i]; |
|
521 |
+ |
|
522 |
+ /* handle copied channels */ |
|
523 |
+ if (map->copy) { |
|
524 |
+ memcpy(frame->extended_data[i], |
|
525 |
+ frame->extended_data[map->copy_idx], |
|
526 |
+ frame->linesize[0]); |
|
527 |
+ } else if (map->silence) { |
|
528 |
+ memset(frame->extended_data[i], 0, frame->linesize[0]); |
|
529 |
+ } |
|
530 |
+ |
|
531 |
+ if (c->gain_i) { |
|
532 |
+ c->fdsp.vector_fmul_scalar((float*)frame->extended_data[i], |
|
533 |
+ (float*)frame->extended_data[i], |
|
534 |
+ c->gain, FFALIGN(decoded_samples, 8)); |
|
535 |
+ } |
|
536 |
+ } |
|
537 |
+ |
|
538 |
+ frame->nb_samples = decoded_samples; |
|
539 |
+ *got_frame_ptr = !!decoded_samples; |
|
540 |
+ |
|
541 |
+ return avpkt->size; |
|
542 |
+} |
|
543 |
+ |
|
544 |
+static av_cold void opus_decode_flush(AVCodecContext *ctx) |
|
545 |
+{ |
|
546 |
+ OpusContext *c = ctx->priv_data; |
|
547 |
+ int i; |
|
548 |
+ |
|
549 |
+ for (i = 0; i < c->nb_streams; i++) { |
|
550 |
+ OpusStreamContext *s = &c->streams[i]; |
|
551 |
+ |
|
552 |
+ memset(&s->packet, 0, sizeof(s->packet)); |
|
553 |
+ s->delayed_samples = 0; |
|
554 |
+ |
|
555 |
+ if (s->celt_delay) |
|
556 |
+ av_audio_fifo_drain(s->celt_delay, av_audio_fifo_size(s->celt_delay)); |
|
557 |
+ avresample_close(s->avr); |
|
558 |
+ |
|
559 |
+ ff_silk_flush(s->silk); |
|
560 |
+ ff_celt_flush(s->celt); |
|
561 |
+ } |
|
562 |
+} |
|
563 |
+ |
|
564 |
+static av_cold int opus_decode_close(AVCodecContext *avctx) |
|
565 |
+{ |
|
566 |
+ OpusContext *c = avctx->priv_data; |
|
567 |
+ int i; |
|
568 |
+ |
|
569 |
+ for (i = 0; i < c->nb_streams; i++) { |
|
570 |
+ OpusStreamContext *s = &c->streams[i]; |
|
571 |
+ |
|
572 |
+ ff_silk_free(&s->silk); |
|
573 |
+ ff_celt_free(&s->celt); |
|
574 |
+ |
|
575 |
+ av_freep(&s->out_dummy); |
|
576 |
+ s->out_dummy_allocated_size = 0; |
|
577 |
+ |
|
578 |
+ av_audio_fifo_free(s->celt_delay); |
|
579 |
+ avresample_free(&s->avr); |
|
580 |
+ } |
|
581 |
+ |
|
582 |
+ av_freep(&c->streams); |
|
583 |
+ c->nb_streams = 0; |
|
584 |
+ |
|
585 |
+ av_freep(&c->channel_maps); |
|
586 |
+ |
|
587 |
+ return 0; |
|
588 |
+} |
|
589 |
+ |
|
590 |
+static av_cold int opus_decode_init(AVCodecContext *avctx) |
|
591 |
+{ |
|
592 |
+ OpusContext *c = avctx->priv_data; |
|
593 |
+ int ret, i, j; |
|
594 |
+ |
|
595 |
+ avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; |
|
596 |
+ avctx->sample_rate = 48000; |
|
597 |
+ |
|
598 |
+ avpriv_float_dsp_init(&c->fdsp, 0); |
|
599 |
+ |
|
600 |
+ /* find out the channel configuration */ |
|
601 |
+ ret = ff_opus_parse_extradata(avctx, c); |
|
602 |
+ if (ret < 0) |
|
603 |
+ return ret; |
|
604 |
+ |
|
605 |
+ /* allocate and init each independent decoder */ |
|
606 |
+ c->streams = av_mallocz_array(c->nb_streams, sizeof(*c->streams)); |
|
607 |
+ if (!c->streams) { |
|
608 |
+ c->nb_streams = 0; |
|
609 |
+ ret = AVERROR(ENOMEM); |
|
610 |
+ goto fail; |
|
611 |
+ } |
|
612 |
+ |
|
613 |
+ for (i = 0; i < c->nb_streams; i++) { |
|
614 |
+ OpusStreamContext *s = &c->streams[i]; |
|
615 |
+ uint64_t layout; |
|
616 |
+ |
|
617 |
+ s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1; |
|
618 |
+ |
|
619 |
+ s->avctx = avctx; |
|
620 |
+ |
|
621 |
+ for (j = 0; j < s->output_channels; j++) { |
|
622 |
+ s->silk_output[j] = s->silk_buf[j]; |
|
623 |
+ s->celt_output[j] = s->celt_buf[j]; |
|
624 |
+ s->redundancy_output[j] = s->redundancy_buf[j]; |
|
625 |
+ } |
|
626 |
+ |
|
627 |
+ s->fdsp = &c->fdsp; |
|
628 |
+ |
|
629 |
+ s->avr = avresample_alloc_context(); |
|
630 |
+ if (!s->avr) |
|
631 |
+ goto fail; |
|
632 |
+ |
|
633 |
+ layout = (s->output_channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO; |
|
634 |
+ av_opt_set_int(s->avr, "in_sample_fmt", avctx->sample_fmt, 0); |
|
635 |
+ av_opt_set_int(s->avr, "out_sample_fmt", avctx->sample_fmt, 0); |
|
636 |
+ av_opt_set_int(s->avr, "in_channel_layout", layout, 0); |
|
637 |
+ av_opt_set_int(s->avr, "out_channel_layout", layout, 0); |
|
638 |
+ av_opt_set_int(s->avr, "out_sample_rate", avctx->sample_rate, 0); |
|
639 |
+ |
|
640 |
+ ret = ff_silk_init(avctx, &s->silk, s->output_channels); |
|
641 |
+ if (ret < 0) |
|
642 |
+ goto fail; |
|
643 |
+ |
|
644 |
+ ret = ff_celt_init(avctx, &s->celt, s->output_channels); |
|
645 |
+ if (ret < 0) |
|
646 |
+ goto fail; |
|
647 |
+ |
|
648 |
+ s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt, |
|
649 |
+ s->output_channels, 1024); |
|
650 |
+ if (!s->celt_delay) { |
|
651 |
+ ret = AVERROR(ENOMEM); |
|
652 |
+ goto fail; |
|
653 |
+ } |
|
654 |
+ } |
|
655 |
+ |
|
656 |
+ return 0; |
|
657 |
+fail: |
|
658 |
+ opus_decode_close(avctx); |
|
659 |
+ return ret; |
|
660 |
+} |
|
661 |
+ |
|
662 |
+AVCodec ff_opus_decoder = { |
|
663 |
+ .name = "opus", |
|
664 |
+ .long_name = NULL_IF_CONFIG_SMALL("Opus"), |
|
665 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
666 |
+ .id = AV_CODEC_ID_OPUS, |
|
667 |
+ .priv_data_size = sizeof(OpusContext), |
|
668 |
+ .init = opus_decode_init, |
|
669 |
+ .close = opus_decode_close, |
|
670 |
+ .decode = opus_decode_packet, |
|
671 |
+ .flush = opus_decode_flush, |
|
672 |
+ .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY, |
|
673 |
+}; |
... | ... |
@@ -29,8 +29,8 @@ |
29 | 29 |
#include "libavutil/version.h" |
30 | 30 |
|
31 | 31 |
#define LIBAVCODEC_VERSION_MAJOR 55 |
32 |
-#define LIBAVCODEC_VERSION_MINOR 50 |
|
33 |
-#define LIBAVCODEC_VERSION_MICRO 3 |
|
32 |
+#define LIBAVCODEC_VERSION_MINOR 51 |
|
33 |
+#define LIBAVCODEC_VERSION_MICRO 0 |
|
34 | 34 |
|
35 | 35 |
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ |
36 | 36 |
LIBAVCODEC_VERSION_MINOR, \ |
... | ... |
@@ -93,6 +93,7 @@ include $(SRC_PATH)/tests/fate/microsoft.mak |
93 | 93 |
include $(SRC_PATH)/tests/fate/monkeysaudio.mak |
94 | 94 |
include $(SRC_PATH)/tests/fate/mp3.mak |
95 | 95 |
include $(SRC_PATH)/tests/fate/mpc.mak |
96 |
+include $(SRC_PATH)/tests/fate/opus.mak |
|
96 | 97 |
include $(SRC_PATH)/tests/fate/pcm.mak |
97 | 98 |
include $(SRC_PATH)/tests/fate/probe.mak |
98 | 99 |
include $(SRC_PATH)/tests/fate/prores.mak |
99 | 100 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,39 @@ |
0 |
+# The samples were produced by simply rewrapping the official test vectors from |
|
1 |
+# their custom format into Matroska. |
|
2 |
+# The reference files were created with our decoder and tested against the |
|
3 |
+# libopus output with the official opus_compare tool. We cannot use libopus |
|
4 |
+# output as reference directly, because the use of different resamplers would |
|
5 |
+# require too high fuzz values, which can hide bugs. |
|
6 |
+# Before adding new tests here, always make sure they pass opus_compare. |
|
7 |
+ |
|
8 |
+OPUS_CELT_SAMPLES = $(addprefix testvector, 01 07 11) tron.6ch.tinypkts |
|
9 |
+OPUS_HYBRID_SAMPLES = $(addprefix testvector, 05 06) |
|
10 |
+OPUS_SILK_SAMPLES = $(addprefix testvector, 02 03 04) |
|
11 |
+OPUS_SAMPLES = $(addprefix testvector, 08 09 10 12) |
|
12 |
+ |
|
13 |
+define FATE_OPUS_TEST |
|
14 |
+FATE_OPUS += fate-opus-$(1) |
|
15 |
+FATE_OPUS$(2) += fate-opus-$(1) |
|
16 |
+fate-opus-$(1): CMD = avconv -i $(TARGET_SAMPLES)/opus/$(1).mka -f f32le - |
|
17 |
+fate-opus-$(1): REF = $(TARGET_SAMPLES)/opus/$(1).f32 |
|
18 |
+endef |
|
19 |
+ |
|
20 |
+$(foreach N,$(OPUS_CELT_SAMPLES), $(eval $(call FATE_OPUS_TEST,$(N),_CELT))) |
|
21 |
+$(foreach N,$(OPUS_HYBRID_SAMPLES),$(eval $(call FATE_OPUS_TEST,$(N),_HYBRID))) |
|
22 |
+$(foreach N,$(OPUS_SILK_SAMPLES), $(eval $(call FATE_OPUS_TEST,$(N),_SILK))) |
|
23 |
+$(foreach N,$(OPUS_SAMPLES), $(eval $(call FATE_OPUS_TEST,$(N),))) |
|
24 |
+ |
|
25 |
+FATE_OPUS := $(sort $(FATE_OPUS)) |
|
26 |
+ |
|
27 |
+$(FATE_OPUS): CMP = stddev |
|
28 |
+$(FATE_OPUS): CMP_UNIT = f32 |
|
29 |
+$(FATE_OPUS): FUZZ = 3 |
|
30 |
+ |
|
31 |
+$(FATE_OPUS_CELT): CMP = oneoff |
|
32 |
+$(FATE_OPUS_CELT): FUZZ = 5 |
|
33 |
+ |
|
34 |
+FATE_SAMPLES_AVCONV-$(call DEMDEC, MATROSKA, OPUS) += $(FATE_OPUS) |
|
35 |
+fate-opus-celt: $(FATE_OPUS_CELT) |
|
36 |
+fate-opus-hybrid: $(FATE_OPUS_HYBRID) |
|
37 |
+fate-opus-silk: $(FATE_OPUS_SILK) |
|
38 |
+fate-opus: $(FATE_OPUS) |